config.go 8.8 KB

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