github.com/ledgerwatch/erigon-lib@v1.0.0/txpool/txpooluitl/all_components.go (about)

     1  /*
     2     Copyright 2021 The Erigon contributors
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package txpooluitl
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/c2h5oh/datasize"
    25  	mdbx2 "github.com/erigontech/mdbx-go/mdbx"
    26  	"github.com/holiman/uint256"
    27  	"github.com/ledgerwatch/log/v3"
    28  
    29  	"github.com/ledgerwatch/erigon-lib/chain"
    30  	"github.com/ledgerwatch/erigon-lib/direct"
    31  	"github.com/ledgerwatch/erigon-lib/kv"
    32  	"github.com/ledgerwatch/erigon-lib/kv/kvcache"
    33  	"github.com/ledgerwatch/erigon-lib/kv/mdbx"
    34  	"github.com/ledgerwatch/erigon-lib/txpool"
    35  	"github.com/ledgerwatch/erigon-lib/txpool/txpoolcfg"
    36  	"github.com/ledgerwatch/erigon-lib/types"
    37  )
    38  
    39  func SaveChainConfigIfNeed(ctx context.Context, coreDB kv.RoDB, txPoolDB kv.RwDB, force bool, logger log.Logger) (cc *chain.Config, blockNum uint64, err error) {
    40  	if err = txPoolDB.View(ctx, func(tx kv.Tx) error {
    41  		cc, err = txpool.ChainConfig(tx)
    42  		if err != nil {
    43  			return err
    44  		}
    45  		blockNum, err = txpool.LastSeenBlock(tx)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		return nil
    50  	}); err != nil {
    51  		return nil, 0, err
    52  	}
    53  	if cc != nil && !force {
    54  		if cc.ChainID.Uint64() == 0 {
    55  			return nil, 0, fmt.Errorf("wrong chain config")
    56  		}
    57  		return cc, blockNum, nil
    58  	}
    59  
    60  	for {
    61  		if err = coreDB.View(ctx, func(tx kv.Tx) error {
    62  			cc, err = chain.GetConfig(tx, nil)
    63  			if err != nil {
    64  				return err
    65  			}
    66  			n, err := chain.CurrentBlockNumber(tx)
    67  			if err != nil {
    68  				return err
    69  			}
    70  			if n != nil {
    71  				blockNum = *n
    72  			}
    73  			return nil
    74  		}); err != nil {
    75  			logger.Error("cant read chain config from core db", "err", err)
    76  			time.Sleep(5 * time.Second)
    77  			continue
    78  		} else if cc == nil {
    79  			logger.Error("cant read chain config from core db")
    80  			time.Sleep(5 * time.Second)
    81  			continue
    82  		}
    83  		break
    84  	}
    85  
    86  	if err = txPoolDB.Update(ctx, func(tx kv.RwTx) error {
    87  		if err = txpool.PutChainConfig(tx, cc, nil); err != nil {
    88  			return err
    89  		}
    90  		if err = txpool.PutLastSeenBlock(tx, blockNum, nil); err != nil {
    91  			return err
    92  		}
    93  		return nil
    94  	}); err != nil {
    95  		return nil, 0, err
    96  	}
    97  	if cc.ChainID.Uint64() == 0 {
    98  		return nil, 0, fmt.Errorf("wrong chain config")
    99  	}
   100  	return cc, blockNum, nil
   101  }
   102  
   103  func AllComponents(ctx context.Context, cfg txpoolcfg.Config, cache kvcache.Cache, newTxs chan types.Announcements, chainDB kv.RoDB,
   104  	sentryClients []direct.SentryClient, stateChangesClient txpool.StateChangesClient, logger log.Logger) (kv.RwDB, *txpool.TxPool, *txpool.Fetch, *txpool.Send, *txpool.GrpcServer, error) {
   105  	opts := mdbx.NewMDBX(log.New()).Label(kv.TxPoolDB).Path(cfg.DBDir).
   106  		WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg { return kv.TxpoolTablesCfg }).
   107  		Flags(func(f uint) uint { return f ^ mdbx2.Durable }).
   108  		WriteMergeThreshold(3 * 8192).
   109  		PageSize(uint64(16 * datasize.KB)).
   110  		GrowthStep(16 * datasize.MB).
   111  		MapSize(1 * datasize.TB)
   112  
   113  	if cfg.MdbxPageSize.Bytes() > 0 {
   114  		opts = opts.PageSize(cfg.MdbxPageSize.Bytes())
   115  	}
   116  	if cfg.MdbxDBSizeLimit > 0 {
   117  		opts = opts.MapSize(cfg.MdbxDBSizeLimit)
   118  	}
   119  	if cfg.MdbxGrowthStep > 0 {
   120  		opts = opts.GrowthStep(cfg.MdbxGrowthStep)
   121  	}
   122  
   123  	txPoolDB, err := opts.Open()
   124  
   125  	if err != nil {
   126  		return nil, nil, nil, nil, nil, err
   127  	}
   128  
   129  	chainConfig, _, err := SaveChainConfigIfNeed(ctx, chainDB, txPoolDB, true, logger)
   130  	if err != nil {
   131  		return nil, nil, nil, nil, nil, err
   132  	}
   133  
   134  	chainID, _ := uint256.FromBig(chainConfig.ChainID)
   135  
   136  	shanghaiTime := chainConfig.ShanghaiTime
   137  	cancunTime := chainConfig.CancunTime
   138  	if cfg.OverrideCancunTime != nil {
   139  		cancunTime = cfg.OverrideCancunTime
   140  	}
   141  
   142  	txPool, err := txpool.New(newTxs, chainDB, cfg, cache, *chainID, shanghaiTime, cancunTime, logger)
   143  	if err != nil {
   144  		return nil, nil, nil, nil, nil, err
   145  	}
   146  
   147  	fetch := txpool.NewFetch(ctx, sentryClients, txPool, stateChangesClient, chainDB, txPoolDB, *chainID, logger)
   148  	//fetch.ConnectCore()
   149  	//fetch.ConnectSentries()
   150  
   151  	send := txpool.NewSend(ctx, sentryClients, txPool, logger)
   152  	txpoolGrpcServer := txpool.NewGrpcServer(ctx, txPool, txPoolDB, *chainID, logger)
   153  	return txPoolDB, txPool, fetch, send, txpoolGrpcServer, nil
   154  }