github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/eth/ethconfig/config.go (about)

     1  // Copyright 2021 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/tirogen/go-ethereum/common"
    29  	"github.com/tirogen/go-ethereum/consensus"
    30  	"github.com/tirogen/go-ethereum/consensus/beacon"
    31  	"github.com/tirogen/go-ethereum/consensus/clique"
    32  	"github.com/tirogen/go-ethereum/consensus/ethash"
    33  	"github.com/tirogen/go-ethereum/core"
    34  	"github.com/tirogen/go-ethereum/core/txpool"
    35  	"github.com/tirogen/go-ethereum/eth/downloader"
    36  	"github.com/tirogen/go-ethereum/eth/gasprice"
    37  	"github.com/tirogen/go-ethereum/ethdb"
    38  	"github.com/tirogen/go-ethereum/log"
    39  	"github.com/tirogen/go-ethereum/miner"
    40  	"github.com/tirogen/go-ethereum/node"
    41  	"github.com/tirogen/go-ethereum/params"
    42  )
    43  
    44  // FullNodeGPO contains default gasprice oracle settings for full node.
    45  var FullNodeGPO = gasprice.Config{
    46  	Blocks:           20,
    47  	Percentile:       60,
    48  	MaxHeaderHistory: 1024,
    49  	MaxBlockHistory:  1024,
    50  	MaxPrice:         gasprice.DefaultMaxPrice,
    51  	IgnorePrice:      gasprice.DefaultIgnorePrice,
    52  }
    53  
    54  // LightClientGPO contains default gasprice oracle settings for light client.
    55  var LightClientGPO = gasprice.Config{
    56  	Blocks:           2,
    57  	Percentile:       60,
    58  	MaxHeaderHistory: 300,
    59  	MaxBlockHistory:  5,
    60  	MaxPrice:         gasprice.DefaultMaxPrice,
    61  	IgnorePrice:      gasprice.DefaultIgnorePrice,
    62  }
    63  
    64  // Defaults contains default settings for use on the Ethereum main net.
    65  var Defaults = Config{
    66  	SyncMode: downloader.SnapSync,
    67  	Ethash: ethash.Config{
    68  		CacheDir:         "ethash",
    69  		CachesInMem:      2,
    70  		CachesOnDisk:     3,
    71  		CachesLockMmap:   false,
    72  		DatasetsInMem:    1,
    73  		DatasetsOnDisk:   2,
    74  		DatasetsLockMmap: false,
    75  	},
    76  	NetworkId:               1,
    77  	TxLookupLimit:           2350000,
    78  	LightPeers:              100,
    79  	UltraLightFraction:      75,
    80  	DatabaseCache:           512,
    81  	TrieCleanCache:          154,
    82  	TrieCleanCacheJournal:   "triecache",
    83  	TrieCleanCacheRejournal: 60 * time.Minute,
    84  	TrieDirtyCache:          256,
    85  	TrieTimeout:             60 * time.Minute,
    86  	SnapshotCache:           102,
    87  	FilterLogCacheSize:      32,
    88  	Miner:                   miner.DefaultConfig,
    89  	TxPool:                  txpool.DefaultConfig,
    90  	RPCGasCap:               50000000,
    91  	RPCEVMTimeout:           5 * time.Second,
    92  	GPO:                     FullNodeGPO,
    93  	RPCTxFeeCap:             1, // 1 ether
    94  }
    95  
    96  func init() {
    97  	home := os.Getenv("HOME")
    98  	if home == "" {
    99  		if user, err := user.Current(); err == nil {
   100  			home = user.HomeDir
   101  		}
   102  	}
   103  	if runtime.GOOS == "darwin" {
   104  		Defaults.Ethash.DatasetDir = filepath.Join(home, "Library", "Ethash")
   105  	} else if runtime.GOOS == "windows" {
   106  		localappdata := os.Getenv("LOCALAPPDATA")
   107  		if localappdata != "" {
   108  			Defaults.Ethash.DatasetDir = filepath.Join(localappdata, "Ethash")
   109  		} else {
   110  			Defaults.Ethash.DatasetDir = filepath.Join(home, "AppData", "Local", "Ethash")
   111  		}
   112  	} else {
   113  		Defaults.Ethash.DatasetDir = filepath.Join(home, ".ethash")
   114  	}
   115  }
   116  
   117  //go:generate go run github.com/fjl/gencodec -type Config -formats toml -out gen_config.go
   118  
   119  // Config contains configuration options for of the ETH and LES protocols.
   120  type Config struct {
   121  	// The genesis block, which is inserted if the database is empty.
   122  	// If nil, the Ethereum main net block is used.
   123  	Genesis *core.Genesis `toml:",omitempty"`
   124  
   125  	// Protocol options
   126  	NetworkId uint64 // Network ID to use for selecting peers to connect to
   127  	SyncMode  downloader.SyncMode
   128  
   129  	// This can be set to list of enrtree:// URLs which will be queried for
   130  	// for nodes to connect to.
   131  	EthDiscoveryURLs  []string
   132  	SnapDiscoveryURLs []string
   133  
   134  	NoPruning  bool // Whether to disable pruning and flush everything to disk
   135  	NoPrefetch bool // Whether to disable prefetching and only load state on demand
   136  
   137  	TxLookupLimit uint64 `toml:",omitempty"` // The maximum number of blocks from head whose tx indices are reserved.
   138  
   139  	// RequiredBlocks is a set of block number -> hash mappings which must be in the
   140  	// canonical chain of all remote peers. Setting the option makes geth verify the
   141  	// presence of these blocks for every new peer connection.
   142  	RequiredBlocks 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  	// This is the number of blocks for which logs will be cached in the filter system.
   173  	FilterLogCacheSize int
   174  
   175  	// Mining options
   176  	Miner miner.Config
   177  
   178  	// Ethash options
   179  	Ethash ethash.Config
   180  
   181  	// Transaction pool options
   182  	TxPool txpool.Config
   183  
   184  	// Gas Price Oracle options
   185  	GPO gasprice.Config
   186  
   187  	// Enables tracking of SHA3 preimages in the VM
   188  	EnablePreimageRecording bool
   189  
   190  	// Miscellaneous options
   191  	DocRoot string `toml:"-"`
   192  
   193  	// RPCGasCap is the global gas cap for eth-call variants.
   194  	RPCGasCap uint64
   195  
   196  	// RPCEVMTimeout is the global timeout for eth-call.
   197  	RPCEVMTimeout time.Duration
   198  
   199  	// RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
   200  	// send-transaction variants. The unit is ether.
   201  	RPCTxFeeCap float64
   202  
   203  	// Checkpoint is a hardcoded checkpoint which can be nil.
   204  	Checkpoint *params.TrustedCheckpoint `toml:",omitempty"`
   205  
   206  	// CheckpointOracle is the configuration for checkpoint oracle.
   207  	CheckpointOracle *params.CheckpointOracleConfig `toml:",omitempty"`
   208  
   209  	// OverrideTerminalTotalDifficulty (TODO: remove after the fork)
   210  	OverrideTerminalTotalDifficulty *big.Int `toml:",omitempty"`
   211  
   212  	// OverrideTerminalTotalDifficultyPassed (TODO: remove after the fork)
   213  	OverrideTerminalTotalDifficultyPassed *bool `toml:",omitempty"`
   214  }
   215  
   216  // CreateConsensusEngine creates a consensus engine for the given chain configuration.
   217  func CreateConsensusEngine(stack *node.Node, ethashConfig *ethash.Config, cliqueConfig *params.CliqueConfig, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
   218  	// If proof-of-authority is requested, set it up
   219  	var engine consensus.Engine
   220  	if cliqueConfig != nil {
   221  		engine = clique.New(cliqueConfig, db)
   222  	} else {
   223  		switch ethashConfig.PowMode {
   224  		case ethash.ModeFake:
   225  			log.Warn("Ethash used in fake mode")
   226  		case ethash.ModeTest:
   227  			log.Warn("Ethash used in test mode")
   228  		case ethash.ModeShared:
   229  			log.Warn("Ethash used in shared mode")
   230  		}
   231  		engine = ethash.New(ethash.Config{
   232  			PowMode:          ethashConfig.PowMode,
   233  			CacheDir:         stack.ResolvePath(ethashConfig.CacheDir),
   234  			CachesInMem:      ethashConfig.CachesInMem,
   235  			CachesOnDisk:     ethashConfig.CachesOnDisk,
   236  			CachesLockMmap:   ethashConfig.CachesLockMmap,
   237  			DatasetDir:       ethashConfig.DatasetDir,
   238  			DatasetsInMem:    ethashConfig.DatasetsInMem,
   239  			DatasetsOnDisk:   ethashConfig.DatasetsOnDisk,
   240  			DatasetsLockMmap: ethashConfig.DatasetsLockMmap,
   241  			NotifyFull:       ethashConfig.NotifyFull,
   242  		}, notify, noverify)
   243  		engine.(*ethash.Ethash).SetThreads(-1) // Disable CPU mining
   244  	}
   245  	return beacon.New(engine)
   246  }