github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/eth/ethconfig/config.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package ethconfig contains the configuration of the ETH and LES protocols.
    18  package ethconfig
    19  
    20  import (
    21  	"math/big"
    22  	"os"
    23  	"os/user"
    24  	"path/filepath"
    25  	"runtime"
    26  	"time"
    27  
    28  	"github.com/scroll-tech/go-ethereum/common"
    29  	"github.com/scroll-tech/go-ethereum/consensus"
    30  	"github.com/scroll-tech/go-ethereum/consensus/clique"
    31  	"github.com/scroll-tech/go-ethereum/consensus/ethash"
    32  	"github.com/scroll-tech/go-ethereum/core"
    33  	"github.com/scroll-tech/go-ethereum/eth/downloader"
    34  	"github.com/scroll-tech/go-ethereum/eth/gasprice"
    35  	"github.com/scroll-tech/go-ethereum/ethdb"
    36  	"github.com/scroll-tech/go-ethereum/log"
    37  	"github.com/scroll-tech/go-ethereum/miner"
    38  	"github.com/scroll-tech/go-ethereum/node"
    39  	"github.com/scroll-tech/go-ethereum/params"
    40  )
    41  
    42  // FullNodeGPO contains default gasprice oracle settings for full node.
    43  var FullNodeGPO = gasprice.Config{
    44  	Blocks:           20,
    45  	Percentile:       60,
    46  	MaxHeaderHistory: 1024,
    47  	MaxBlockHistory:  1024,
    48  	MaxPrice:         gasprice.DefaultMaxPrice,
    49  	IgnorePrice:      gasprice.DefaultIgnorePrice,
    50  }
    51  
    52  // LightClientGPO contains default gasprice oracle settings for light client.
    53  var LightClientGPO = gasprice.Config{
    54  	Blocks:           2,
    55  	Percentile:       60,
    56  	MaxHeaderHistory: 300,
    57  	MaxBlockHistory:  5,
    58  	MaxPrice:         gasprice.DefaultMaxPrice,
    59  	IgnorePrice:      gasprice.DefaultIgnorePrice,
    60  }
    61  
    62  // Defaults contains default settings for use on the Ethereum main net.
    63  var Defaults = Config{
    64  	SyncMode: downloader.FullSync,
    65  	Ethash: ethash.Config{
    66  		CacheDir:         "ethash",
    67  		CachesInMem:      2,
    68  		CachesOnDisk:     3,
    69  		CachesLockMmap:   false,
    70  		DatasetsInMem:    1,
    71  		DatasetsOnDisk:   2,
    72  		DatasetsLockMmap: false,
    73  	},
    74  	NetworkId:               1,
    75  	TxLookupLimit:           2350000,
    76  	LightPeers:              100,
    77  	UltraLightFraction:      75,
    78  	DatabaseCache:           512,
    79  	TrieCleanCache:          154,
    80  	TrieCleanCacheJournal:   "triecache",
    81  	TrieCleanCacheRejournal: 60 * time.Minute,
    82  	TrieDirtyCache:          256,
    83  	TrieTimeout:             60 * time.Minute,
    84  	SnapshotCache:           102,
    85  	Miner: miner.Config{
    86  		GasCeil:  8000000,
    87  		GasPrice: big.NewInt(params.GWei),
    88  		Recommit: 3 * time.Second,
    89  	},
    90  	TxPool:        core.DefaultTxPoolConfig,
    91  	RPCGasCap:     50000000,
    92  	RPCEVMTimeout: 5 * time.Second,
    93  	GPO:           FullNodeGPO,
    94  	RPCTxFeeCap:   1,  // 1 ether
    95  	MaxBlockRange: -1, // Default unconfigured value: no block range limit for backward compatibility
    96  }
    97  
    98  func init() {
    99  	home := os.Getenv("HOME")
   100  	if home == "" {
   101  		if user, err := user.Current(); err == nil {
   102  			home = user.HomeDir
   103  		}
   104  	}
   105  	if runtime.GOOS == "darwin" {
   106  		Defaults.Ethash.DatasetDir = filepath.Join(home, "Library", "Ethash")
   107  	} else if runtime.GOOS == "windows" {
   108  		localappdata := os.Getenv("LOCALAPPDATA")
   109  		if localappdata != "" {
   110  			Defaults.Ethash.DatasetDir = filepath.Join(localappdata, "Ethash")
   111  		} else {
   112  			Defaults.Ethash.DatasetDir = filepath.Join(home, "AppData", "Local", "Ethash")
   113  		}
   114  	} else {
   115  		Defaults.Ethash.DatasetDir = filepath.Join(home, ".ethash")
   116  	}
   117  }
   118  
   119  //go:generate gencodec -type Config -formats toml -out gen_config.go
   120  
   121  // Config contains configuration options for of the ETH and LES protocols.
   122  type Config struct {
   123  	// The genesis block, which is inserted if the database is empty.
   124  	// If nil, the Ethereum main net block is used.
   125  	Genesis *core.Genesis `toml:",omitempty"`
   126  
   127  	// Protocol options
   128  	NetworkId uint64 // Network ID to use for selecting peers to connect to
   129  	SyncMode  downloader.SyncMode
   130  
   131  	// This can be set to list of enrtree:// URLs which will be queried for
   132  	// for nodes to connect to.
   133  	EthDiscoveryURLs  []string
   134  	SnapDiscoveryURLs []string
   135  
   136  	NoPruning  bool // Whether to disable pruning and flush everything to disk
   137  	NoPrefetch bool // Whether to disable prefetching and only load state on demand
   138  
   139  	TxLookupLimit uint64 `toml:",omitempty"` // The maximum number of blocks from head whose tx indices are reserved.
   140  
   141  	// Whitelist of required block number -> hash values to accept
   142  	Whitelist map[uint64]common.Hash `toml:"-"`
   143  
   144  	// Light client options
   145  	LightServ          int  `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests
   146  	LightIngress       int  `toml:",omitempty"` // Incoming bandwidth limit for light servers
   147  	LightEgress        int  `toml:",omitempty"` // Outgoing bandwidth limit for light servers
   148  	LightPeers         int  `toml:",omitempty"` // Maximum number of LES client peers
   149  	LightNoPrune       bool `toml:",omitempty"` // Whether to disable light chain pruning
   150  	LightNoSyncServe   bool `toml:",omitempty"` // Whether to serve light clients before syncing
   151  	SyncFromCheckpoint bool `toml:",omitempty"` // Whether to sync the header chain from the configured checkpoint
   152  
   153  	// Ultra Light client options
   154  	UltraLightServers      []string `toml:",omitempty"` // List of trusted ultra light servers
   155  	UltraLightFraction     int      `toml:",omitempty"` // Percentage of trusted servers to accept an announcement
   156  	UltraLightOnlyAnnounce bool     `toml:",omitempty"` // Whether to only announce headers, or also serve them
   157  
   158  	// Database options
   159  	SkipBcVersionCheck bool `toml:"-"`
   160  	DatabaseHandles    int  `toml:"-"`
   161  	DatabaseCache      int
   162  	DatabaseFreezer    string
   163  
   164  	TrieCleanCache          int
   165  	TrieCleanCacheJournal   string        `toml:",omitempty"` // Disk journal directory for trie cache to survive node restarts
   166  	TrieCleanCacheRejournal time.Duration `toml:",omitempty"` // Time interval to regenerate the journal for clean cache
   167  	TrieDirtyCache          int
   168  	TrieTimeout             time.Duration
   169  	SnapshotCache           int
   170  	Preimages               bool
   171  
   172  	// Mining options
   173  	Miner miner.Config
   174  
   175  	// Ethash options
   176  	Ethash ethash.Config
   177  
   178  	// Transaction pool options
   179  	TxPool core.TxPoolConfig
   180  
   181  	// Gas Price Oracle options
   182  	GPO gasprice.Config
   183  
   184  	// Enables tracking of SHA3 preimages in the VM
   185  	EnablePreimageRecording bool
   186  
   187  	// Miscellaneous options
   188  	DocRoot string `toml:"-"`
   189  
   190  	// RPCGasCap is the global gas cap for eth-call variants.
   191  	RPCGasCap uint64
   192  
   193  	// RPCEVMTimeout is the global timeout for eth-call.
   194  	RPCEVMTimeout time.Duration
   195  
   196  	// RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
   197  	// send-transction variants. The unit is ether.
   198  	RPCTxFeeCap float64
   199  
   200  	// Checkpoint is a hardcoded checkpoint which can be nil.
   201  	Checkpoint *params.TrustedCheckpoint `toml:",omitempty"`
   202  
   203  	// CheckpointOracle is the configuration for checkpoint oracle.
   204  	CheckpointOracle *params.CheckpointOracleConfig `toml:",omitempty"`
   205  
   206  	// Arrow Glacier block override (TODO: remove after the fork)
   207  	OverrideArrowGlacier *big.Int `toml:",omitempty"`
   208  
   209  	// Trace option
   210  	MPTWitness int
   211  
   212  	// Check circuit capacity in block validator
   213  	CheckCircuitCapacity bool
   214  
   215  	// Enable verification of batch consistency between L1 and L2 in rollup
   216  	EnableRollupVerify bool
   217  
   218  	// Max block range for eth_getLogs api method
   219  	MaxBlockRange int64
   220  }
   221  
   222  // CreateConsensusEngine creates a consensus engine for the given chain configuration.
   223  func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
   224  	// If proof-of-authority is requested, set it up
   225  	if chainConfig.Clique != nil {
   226  		return clique.New(chainConfig.Clique, db)
   227  	}
   228  	// Otherwise assume proof-of-work
   229  	switch config.PowMode {
   230  	case ethash.ModeFake:
   231  		log.Warn("Ethash used in fake mode")
   232  	case ethash.ModeTest:
   233  		log.Warn("Ethash used in test mode")
   234  	case ethash.ModeShared:
   235  		log.Warn("Ethash used in shared mode")
   236  	}
   237  	engine := ethash.New(ethash.Config{
   238  		PowMode:          config.PowMode,
   239  		CacheDir:         stack.ResolvePath(config.CacheDir),
   240  		CachesInMem:      config.CachesInMem,
   241  		CachesOnDisk:     config.CachesOnDisk,
   242  		CachesLockMmap:   config.CachesLockMmap,
   243  		DatasetDir:       config.DatasetDir,
   244  		DatasetsInMem:    config.DatasetsInMem,
   245  		DatasetsOnDisk:   config.DatasetsOnDisk,
   246  		DatasetsLockMmap: config.DatasetsLockMmap,
   247  		NotifyFull:       config.NotifyFull,
   248  	}, notify, noverify)
   249  	engine.SetThreads(-1) // Disable CPU mining
   250  	return engine
   251  }