config.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // Package ethconfig contains the configuration of the ETH and LES protocols.
  17. package ethconfig
  18. import (
  19. "math/big"
  20. "os"
  21. "os/user"
  22. "path/filepath"
  23. "runtime"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/consensus"
  27. "github.com/ethereum/go-ethereum/consensus/beacon"
  28. "github.com/ethereum/go-ethereum/consensus/clique"
  29. "github.com/ethereum/go-ethereum/consensus/ethash"
  30. "github.com/ethereum/go-ethereum/core"
  31. "github.com/ethereum/go-ethereum/eth/downloader"
  32. "github.com/ethereum/go-ethereum/eth/gasprice"
  33. "github.com/ethereum/go-ethereum/ethdb"
  34. "github.com/ethereum/go-ethereum/log"
  35. "github.com/ethereum/go-ethereum/miner"
  36. "github.com/ethereum/go-ethereum/node"
  37. "github.com/ethereum/go-ethereum/params"
  38. )
  39. // FullNodeGPO contains default gasprice oracle settings for full node.
  40. var FullNodeGPO = gasprice.Config{
  41. Blocks: 20,
  42. Percentile: 60,
  43. MaxHeaderHistory: 1024,
  44. MaxBlockHistory: 1024,
  45. MaxPrice: gasprice.DefaultMaxPrice,
  46. IgnorePrice: gasprice.DefaultIgnorePrice,
  47. }
  48. // LightClientGPO contains default gasprice oracle settings for light client.
  49. var LightClientGPO = gasprice.Config{
  50. Blocks: 2,
  51. Percentile: 60,
  52. MaxHeaderHistory: 300,
  53. MaxBlockHistory: 5,
  54. MaxPrice: gasprice.DefaultMaxPrice,
  55. IgnorePrice: gasprice.DefaultIgnorePrice,
  56. }
  57. // Defaults contains default settings for use on the Ethereum main net.
  58. var Defaults = Config{
  59. SyncMode: downloader.SnapSync,
  60. Ethash: ethash.Config{
  61. CacheDir: "ethash",
  62. CachesInMem: 2,
  63. CachesOnDisk: 3,
  64. CachesLockMmap: false,
  65. DatasetsInMem: 1,
  66. DatasetsOnDisk: 2,
  67. DatasetsLockMmap: false,
  68. },
  69. NetworkId: 1,
  70. TxLookupLimit: 2350000,
  71. LightPeers: 100,
  72. UltraLightFraction: 75,
  73. DatabaseCache: 512,
  74. TrieCleanCache: 154,
  75. TrieCleanCacheJournal: "triecache",
  76. TrieCleanCacheRejournal: 60 * time.Minute,
  77. TrieDirtyCache: 256,
  78. TrieTimeout: 60 * time.Minute,
  79. SnapshotCache: 102,
  80. FilterLogCacheSize: 32,
  81. Miner: miner.Config{
  82. GasCeil: 30000000,
  83. GasPrice: big.NewInt(params.GWei),
  84. Recommit: 3 * time.Second,
  85. },
  86. TxPool: core.DefaultTxPoolConfig,
  87. RPCGasCap: 50000000,
  88. RPCEVMTimeout: 5 * time.Second,
  89. GPO: FullNodeGPO,
  90. RPCTxFeeCap: 1, // 1 ether
  91. }
  92. func init() {
  93. home := os.Getenv("HOME")
  94. if home == "" {
  95. if user, err := user.Current(); err == nil {
  96. home = user.HomeDir
  97. }
  98. }
  99. if runtime.GOOS == "darwin" {
  100. Defaults.Ethash.DatasetDir = filepath.Join(home, "Library", "Ethash")
  101. } else if runtime.GOOS == "windows" {
  102. localappdata := os.Getenv("LOCALAPPDATA")
  103. if localappdata != "" {
  104. Defaults.Ethash.DatasetDir = filepath.Join(localappdata, "Ethash")
  105. } else {
  106. Defaults.Ethash.DatasetDir = filepath.Join(home, "AppData", "Local", "Ethash")
  107. }
  108. } else {
  109. Defaults.Ethash.DatasetDir = filepath.Join(home, ".ethash")
  110. }
  111. }
  112. //go:generate go run github.com/fjl/gencodec -type Config -formats toml -out gen_config.go
  113. // Config contains configuration options for of the ETH and LES protocols.
  114. type Config struct {
  115. // The genesis block, which is inserted if the database is empty.
  116. // If nil, the Ethereum main net block is used.
  117. Genesis *core.Genesis `toml:",omitempty"`
  118. // Protocol options
  119. NetworkId uint64 // Network ID to use for selecting peers to connect to
  120. SyncMode downloader.SyncMode
  121. // This can be set to list of enrtree:// URLs which will be queried for
  122. // for nodes to connect to.
  123. EthDiscoveryURLs []string
  124. SnapDiscoveryURLs []string
  125. NoPruning bool // Whether to disable pruning and flush everything to disk
  126. NoPrefetch bool // Whether to disable prefetching and only load state on demand
  127. TxLookupLimit uint64 `toml:",omitempty"` // The maximum number of blocks from head whose tx indices are reserved.
  128. // RequiredBlocks is a set of block number -> hash mappings which must be in the
  129. // canonical chain of all remote peers. Setting the option makes geth verify the
  130. // presence of these blocks for every new peer connection.
  131. RequiredBlocks map[uint64]common.Hash `toml:"-"`
  132. // Light client options
  133. LightServ int `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests
  134. LightIngress int `toml:",omitempty"` // Incoming bandwidth limit for light servers
  135. LightEgress int `toml:",omitempty"` // Outgoing bandwidth limit for light servers
  136. LightPeers int `toml:",omitempty"` // Maximum number of LES client peers
  137. LightNoPrune bool `toml:",omitempty"` // Whether to disable light chain pruning
  138. LightNoSyncServe bool `toml:",omitempty"` // Whether to serve light clients before syncing
  139. SyncFromCheckpoint bool `toml:",omitempty"` // Whether to sync the header chain from the configured checkpoint
  140. // Ultra Light client options
  141. UltraLightServers []string `toml:",omitempty"` // List of trusted ultra light servers
  142. UltraLightFraction int `toml:",omitempty"` // Percentage of trusted servers to accept an announcement
  143. UltraLightOnlyAnnounce bool `toml:",omitempty"` // Whether to only announce headers, or also serve them
  144. // Database options
  145. SkipBcVersionCheck bool `toml:"-"`
  146. DatabaseHandles int `toml:"-"`
  147. DatabaseCache int
  148. DatabaseFreezer string
  149. TrieCleanCache int
  150. TrieCleanCacheJournal string `toml:",omitempty"` // Disk journal directory for trie cache to survive node restarts
  151. TrieCleanCacheRejournal time.Duration `toml:",omitempty"` // Time interval to regenerate the journal for clean cache
  152. TrieDirtyCache int
  153. TrieTimeout time.Duration
  154. SnapshotCache int
  155. Preimages bool
  156. // This is the number of blocks for which logs will be cached in the filter system.
  157. FilterLogCacheSize int
  158. // Mining options
  159. Miner miner.Config
  160. // Ethash options
  161. Ethash ethash.Config
  162. // Transaction pool options
  163. TxPool core.TxPoolConfig
  164. // Gas Price Oracle options
  165. GPO gasprice.Config
  166. // Enables tracking of SHA3 preimages in the VM
  167. EnablePreimageRecording bool
  168. // Miscellaneous options
  169. DocRoot string `toml:"-"`
  170. // RPCGasCap is the global gas cap for eth-call variants.
  171. RPCGasCap uint64
  172. // RPCEVMTimeout is the global timeout for eth-call.
  173. RPCEVMTimeout time.Duration
  174. // RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
  175. // send-transaction variants. The unit is ether.
  176. RPCTxFeeCap float64
  177. // Checkpoint is a hardcoded checkpoint which can be nil.
  178. Checkpoint *params.TrustedCheckpoint `toml:",omitempty"`
  179. // CheckpointOracle is the configuration for checkpoint oracle.
  180. CheckpointOracle *params.CheckpointOracleConfig `toml:",omitempty"`
  181. // OverrideTerminalTotalDifficulty (TODO: remove after the fork)
  182. OverrideTerminalTotalDifficulty *big.Int `toml:",omitempty"`
  183. // OverrideTerminalTotalDifficultyPassed (TODO: remove after the fork)
  184. OverrideTerminalTotalDifficultyPassed *bool `toml:",omitempty"`
  185. //is networkid set
  186. IsNetworkIdSet bool
  187. }
  188. // CreateConsensusEngine creates a consensus engine for the given chain configuration.
  189. func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
  190. // If proof-of-authority is requested, set it up
  191. var engine consensus.Engine
  192. if chainConfig.Clique != nil {
  193. engine = clique.New(chainConfig.Clique, db)
  194. } else {
  195. switch config.PowMode {
  196. case ethash.ModeFake:
  197. log.Warn("Ethash used in fake mode")
  198. case ethash.ModeTest:
  199. log.Warn("Ethash used in test mode")
  200. case ethash.ModeShared:
  201. log.Warn("Ethash used in shared mode")
  202. }
  203. engine = ethash.New(ethash.Config{
  204. PowMode: config.PowMode,
  205. CacheDir: stack.ResolvePath(config.CacheDir),
  206. CachesInMem: config.CachesInMem,
  207. CachesOnDisk: config.CachesOnDisk,
  208. CachesLockMmap: config.CachesLockMmap,
  209. DatasetDir: config.DatasetDir,
  210. DatasetsInMem: config.DatasetsInMem,
  211. DatasetsOnDisk: config.DatasetsOnDisk,
  212. DatasetsLockMmap: config.DatasetsLockMmap,
  213. NotifyFull: config.NotifyFull,
  214. }, notify, noverify)
  215. engine.(*ethash.Ethash).SetThreads(-1) // Disable CPU mining
  216. }
  217. return beacon.New(engine)
  218. }