github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/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/ethereum/go-ethereum/common" 29 "github.com/ethereum/go-ethereum/consensus" 30 "github.com/ethereum/go-ethereum/consensus/beacon" 31 "github.com/ethereum/go-ethereum/consensus/clique" 32 "github.com/ethereum/go-ethereum/consensus/ethash" 33 "github.com/ethereum/go-ethereum/core" 34 "github.com/ethereum/go-ethereum/eth/downloader" 35 "github.com/ethereum/go-ethereum/eth/gasprice" 36 "github.com/ethereum/go-ethereum/ethdb" 37 "github.com/ethereum/go-ethereum/log" 38 "github.com/ethereum/go-ethereum/miner" 39 "github.com/ethereum/go-ethereum/node" 40 "github.com/ethereum/go-ethereum/params" 41 ) 42 43 // FullNodeGPO contains default gasprice oracle settings for full node. 44 var FullNodeGPO = gasprice.Config{ 45 Blocks: 20, 46 Percentile: 60, 47 MaxHeaderHistory: 1024, 48 MaxBlockHistory: 1024, 49 MaxPrice: gasprice.DefaultMaxPrice, 50 IgnorePrice: gasprice.DefaultIgnorePrice, 51 } 52 53 // LightClientGPO contains default gasprice oracle settings for light client. 54 var LightClientGPO = gasprice.Config{ 55 Blocks: 2, 56 Percentile: 60, 57 MaxHeaderHistory: 300, 58 MaxBlockHistory: 5, 59 MaxPrice: gasprice.DefaultMaxPrice, 60 IgnorePrice: gasprice.DefaultIgnorePrice, 61 } 62 63 // Defaults contains default settings for use on the Ethereum main net. 64 var Defaults = Config{ 65 SyncMode: downloader.SnapSync, 66 Ethash: ethash.Config{ 67 CacheDir: "ethash", 68 CachesInMem: 2, 69 CachesOnDisk: 3, 70 CachesLockMmap: false, 71 DatasetsInMem: 1, 72 DatasetsOnDisk: 2, 73 DatasetsLockMmap: false, 74 }, 75 NetworkId: 1, 76 TxLookupLimit: 2350000, 77 LightPeers: 100, 78 UltraLightFraction: 75, 79 DatabaseCache: 512, 80 TrieCleanCache: 154, 81 TrieCleanCacheJournal: "triecache", 82 TrieCleanCacheRejournal: 60 * time.Minute, 83 TrieDirtyCache: 256, 84 TrieTimeout: 60 * time.Minute, 85 SnapshotCache: 102, 86 Miner: miner.Config{ 87 GasCeil: 30000000, 88 GasPrice: big.NewInt(params.GWei), 89 Recommit: 3 * time.Second, 90 }, 91 TxPool: core.DefaultTxPoolConfig, 92 RPCGasCap: 50000000, 93 RPCEVMTimeout: 5 * time.Second, 94 GPO: FullNodeGPO, 95 RPCTxFeeCap: 1, // 1 ether 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 go run github.com/fjl/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 // RequiredBlocks is a set of block number -> hash mappings which must be in the 142 // canonical chain of all remote peers. Setting the option makes geth verify the 143 // presence of these blocks for every new peer connection. 144 RequiredBlocks map[uint64]common.Hash `toml:"-"` 145 146 // Light client options 147 LightServ int `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests 148 LightIngress int `toml:",omitempty"` // Incoming bandwidth limit for light servers 149 LightEgress int `toml:",omitempty"` // Outgoing bandwidth limit for light servers 150 LightPeers int `toml:",omitempty"` // Maximum number of LES client peers 151 LightNoPrune bool `toml:",omitempty"` // Whether to disable light chain pruning 152 LightNoSyncServe bool `toml:",omitempty"` // Whether to serve light clients before syncing 153 SyncFromCheckpoint bool `toml:",omitempty"` // Whether to sync the header chain from the configured checkpoint 154 155 // Ultra Light client options 156 UltraLightServers []string `toml:",omitempty"` // List of trusted ultra light servers 157 UltraLightFraction int `toml:",omitempty"` // Percentage of trusted servers to accept an announcement 158 UltraLightOnlyAnnounce bool `toml:",omitempty"` // Whether to only announce headers, or also serve them 159 160 // Database options 161 SkipBcVersionCheck bool `toml:"-"` 162 DatabaseHandles int `toml:"-"` 163 DatabaseCache int 164 DatabaseFreezer string 165 166 TrieCleanCache int 167 TrieCleanCacheJournal string `toml:",omitempty"` // Disk journal directory for trie cache to survive node restarts 168 TrieCleanCacheRejournal time.Duration `toml:",omitempty"` // Time interval to regenerate the journal for clean cache 169 TrieDirtyCache int 170 TrieTimeout time.Duration 171 SnapshotCache int 172 Preimages bool 173 174 // Mining options 175 Miner miner.Config 176 177 // Ethash options 178 Ethash ethash.Config 179 180 // Transaction pool options 181 TxPool core.TxPoolConfig 182 183 // Gas Price Oracle options 184 GPO gasprice.Config 185 186 // Enables tracking of SHA3 preimages in the VM 187 EnablePreimageRecording bool 188 189 // Miscellaneous options 190 DocRoot string `toml:"-"` 191 192 // RPCGasCap is the global gas cap for eth-call variants. 193 RPCGasCap uint64 194 195 // RPCEVMTimeout is the global timeout for eth-call. 196 RPCEVMTimeout time.Duration 197 198 // RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for 199 // send-transction variants. The unit is ether. 200 RPCTxFeeCap float64 201 202 // Checkpoint is a hardcoded checkpoint which can be nil. 203 Checkpoint *params.TrustedCheckpoint `toml:",omitempty"` 204 205 // CheckpointOracle is the configuration for checkpoint oracle. 206 CheckpointOracle *params.CheckpointOracleConfig `toml:",omitempty"` 207 208 // Gray Glacier block override (TODO: remove after the fork) 209 OverrideGrayGlacier *big.Int `toml:",omitempty"` 210 211 // OverrideTerminalTotalDifficulty (TODO: remove after the fork) 212 OverrideTerminalTotalDifficulty *big.Int `toml:",omitempty"` 213 } 214 215 // CreateConsensusEngine creates a consensus engine for the given chain configuration. 216 func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine { 217 // If proof-of-authority is requested, set it up 218 var engine consensus.Engine 219 if chainConfig.Clique != nil { 220 engine = clique.New(chainConfig.Clique, db) 221 } else { 222 switch config.PowMode { 223 case ethash.ModeFake: 224 log.Warn("Ethash used in fake mode") 225 case ethash.ModeTest: 226 log.Warn("Ethash used in test mode") 227 case ethash.ModeShared: 228 log.Warn("Ethash used in shared mode") 229 } 230 engine = ethash.New(ethash.Config{ 231 PowMode: config.PowMode, 232 CacheDir: stack.ResolvePath(config.CacheDir), 233 CachesInMem: config.CachesInMem, 234 CachesOnDisk: config.CachesOnDisk, 235 CachesLockMmap: config.CachesLockMmap, 236 DatasetDir: config.DatasetDir, 237 DatasetsInMem: config.DatasetsInMem, 238 DatasetsOnDisk: config.DatasetsOnDisk, 239 DatasetsLockMmap: config.DatasetsLockMmap, 240 NotifyFull: config.NotifyFull, 241 }, notify, noverify) 242 engine.(*ethash.Ethash).SetThreads(-1) // Disable CPU mining 243 } 244 return beacon.New(engine) 245 }