config.go 8.0 KB

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