config.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. "strings"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/consensus"
  28. "github.com/ethereum/go-ethereum/consensus/clique"
  29. "github.com/ethereum/go-ethereum/consensus/ethash"
  30. "github.com/ethereum/go-ethereum/consensus/istanbul"
  31. istanbulBackend "github.com/ethereum/go-ethereum/consensus/istanbul/backend"
  32. "github.com/ethereum/go-ethereum/core"
  33. "github.com/ethereum/go-ethereum/eth/downloader"
  34. "github.com/ethereum/go-ethereum/eth/gasprice"
  35. "github.com/ethereum/go-ethereum/ethdb"
  36. "github.com/ethereum/go-ethereum/log"
  37. "github.com/ethereum/go-ethereum/miner"
  38. "github.com/ethereum/go-ethereum/node"
  39. "github.com/ethereum/go-ethereum/params"
  40. )
  41. // FullNodeGPO contains default gasprice oracle settings for full node.
  42. var FullNodeGPO = gasprice.Config{
  43. Blocks: 20,
  44. Percentile: 60,
  45. MaxPrice: gasprice.DefaultMaxPrice,
  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. // Quorum - make full sync the default sync mode in quorum (as opposed to upstream geth)
  56. SyncMode: downloader.FullSync,
  57. // End Quorum
  58. Ethash: ethash.Config{
  59. CacheDir: "ethash",
  60. CachesInMem: 2,
  61. CachesOnDisk: 3,
  62. CachesLockMmap: false,
  63. DatasetsInMem: 1,
  64. DatasetsOnDisk: 2,
  65. DatasetsLockMmap: false,
  66. },
  67. NetworkId: 1337,
  68. TxLookupLimit: 2350000,
  69. LightPeers: 100,
  70. UltraLightFraction: 75,
  71. DatabaseCache: 768,
  72. TrieCleanCache: 154,
  73. TrieCleanCacheJournal: "triecache",
  74. TrieCleanCacheRejournal: 60 * time.Minute,
  75. TrieDirtyCache: 256,
  76. TrieTimeout: 60 * time.Minute,
  77. SnapshotCache: 102,
  78. Miner: miner.Config{
  79. GasFloor: params.DefaultMinGasLimit,
  80. GasCeil: params.GenesisGasLimit,
  81. GasPrice: big.NewInt(params.GWei),
  82. Recommit: 3 * time.Second,
  83. },
  84. TxPool: core.DefaultTxPoolConfig,
  85. RPCGasCap: 25000000,
  86. GPO: FullNodeGPO,
  87. RPCTxFeeCap: 1, // 1 ether
  88. // Quorum
  89. Istanbul: *istanbul.DefaultConfig, // Quorum
  90. }
  91. func init() {
  92. home := os.Getenv("HOME")
  93. if home == "" {
  94. if user, err := user.Current(); err == nil {
  95. home = user.HomeDir
  96. }
  97. }
  98. if runtime.GOOS == "darwin" {
  99. Defaults.Ethash.DatasetDir = filepath.Join(home, "Library", "Ethash")
  100. } else if runtime.GOOS == "windows" {
  101. localappdata := os.Getenv("LOCALAPPDATA")
  102. if localappdata != "" {
  103. Defaults.Ethash.DatasetDir = filepath.Join(localappdata, "Ethash")
  104. } else {
  105. Defaults.Ethash.DatasetDir = filepath.Join(home, "AppData", "Local", "Ethash")
  106. }
  107. } else {
  108. Defaults.Ethash.DatasetDir = filepath.Join(home, ".ethash")
  109. }
  110. }
  111. //go:generate gencodec -type Config -formats toml -out gen_config.go
  112. // Config contains configuration options for of the ETH and LES protocols.
  113. type Config struct {
  114. // The genesis block, which is inserted if the database is empty.
  115. // If nil, the Ethereum main net block is used.
  116. Genesis *core.Genesis `toml:",omitempty"`
  117. // Protocol options
  118. NetworkId uint64 // Network ID to use for selecting peers to connect to
  119. SyncMode downloader.SyncMode
  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. NoPrefetch bool // Whether to disable prefetching and only load state on demand
  126. TxLookupLimit uint64 `toml:",omitempty"` // The maximum number of blocks from head whose tx indices are reserved.
  127. // AuthorizationList of required block number -> hash values to accept
  128. AuthorizationList map[uint64]common.Hash `toml:"-"` // not in the TOML configuration
  129. // Light client options
  130. LightServ int `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests
  131. LightIngress int `toml:",omitempty"` // Incoming bandwidth limit for light servers
  132. LightEgress int `toml:",omitempty"` // Outgoing bandwidth limit for light servers
  133. LightPeers int `toml:",omitempty"` // Maximum number of LES client peers
  134. LightNoPrune bool `toml:",omitempty"` // Whether to disable light chain pruning
  135. LightNoSyncServe bool `toml:",omitempty"` // Whether to serve light clients before syncing
  136. SyncFromCheckpoint bool `toml:",omitempty"` // Whether to sync the header chain from the configured checkpoint
  137. // Ultra Light client options
  138. UltraLightServers []string `toml:",omitempty"` // List of trusted ultra light servers
  139. UltraLightFraction int `toml:",omitempty"` // Percentage of trusted servers to accept an announcement
  140. UltraLightOnlyAnnounce bool `toml:",omitempty"` // Whether to only announce headers, or also serve them
  141. // Database options
  142. SkipBcVersionCheck bool `toml:"-"`
  143. DatabaseHandles int `toml:"-"`
  144. DatabaseCache int
  145. DatabaseFreezer string
  146. TrieCleanCache int
  147. TrieCleanCacheJournal string `toml:",omitempty"` // Disk journal directory for trie cache to survive node restarts
  148. TrieCleanCacheRejournal time.Duration `toml:",omitempty"` // Time interval to regenerate the journal for clean cache
  149. TrieDirtyCache int
  150. TrieTimeout time.Duration `toml:",omitempty"` // Cumulative Time interval spent on gc, after which to flush trie cache to disk
  151. SnapshotCache int
  152. Preimages bool
  153. // Mining options
  154. Miner miner.Config
  155. // Ethash options
  156. Ethash ethash.Config
  157. // Transaction pool options
  158. TxPool core.TxPoolConfig
  159. // Gas Price Oracle options
  160. GPO gasprice.Config
  161. // Enables tracking of SHA3 preimages in the VM
  162. EnablePreimageRecording bool
  163. // Miscellaneous options
  164. DocRoot string `toml:"-"`
  165. // Type of the EWASM interpreter ("" for default)
  166. EWASMInterpreter string
  167. // Type of the EVM interpreter ("" for default)
  168. EVMInterpreter string
  169. // RPCGasCap is the global gas cap for eth-call variants.
  170. RPCGasCap uint64
  171. // RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
  172. // send-transction variants. The unit is ether.
  173. RPCTxFeeCap float64
  174. // Checkpoint is a hardcoded checkpoint which can be nil.
  175. Checkpoint *params.TrustedCheckpoint `toml:",omitempty"`
  176. // CheckpointOracle is the configuration for checkpoint oracle.
  177. CheckpointOracle *params.CheckpointOracleConfig `toml:",omitempty"`
  178. // Berlin block override (TODO: remove after the fork)
  179. OverrideBerlin *big.Int `toml:",omitempty"`
  180. // Quorum
  181. RaftMode bool
  182. EnableNodePermission bool
  183. // Istanbul options
  184. Istanbul istanbul.Config
  185. // timeout value for call
  186. EVMCallTimeOut time.Duration
  187. // Quorum
  188. core.QuorumChainConfig `toml:"-"`
  189. // QuorumLight
  190. QuorumLightServer bool `toml:",omitempty"`
  191. QuorumLightClient *QuorumLightClient `toml:",omitempty"`
  192. }
  193. // CreateConsensusEngine creates a consensus engine for the given chain configuration.
  194. func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, config *Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
  195. // If proof-of-authority is requested, set it up
  196. if chainConfig.Clique != nil {
  197. chainConfig.Clique.AllowedFutureBlockTime = config.Miner.AllowedFutureBlockTime //Quorum
  198. return clique.New(chainConfig.Clique, db)
  199. }
  200. if len(chainConfig.Transitions) > 0 {
  201. config.Istanbul.Transitions = chainConfig.Transitions
  202. }
  203. // If Istanbul is requested, set it up
  204. if chainConfig.Istanbul != nil {
  205. log.Warn("WARNING: The attribute config.istanbul is deprecated and will be removed in the future, please use config.ibft on genesis file")
  206. if chainConfig.Istanbul.Epoch != 0 {
  207. config.Istanbul.Epoch = chainConfig.Istanbul.Epoch
  208. }
  209. config.Istanbul.ProposerPolicy = istanbul.NewProposerPolicy(istanbul.ProposerPolicyId(chainConfig.Istanbul.ProposerPolicy))
  210. config.Istanbul.Ceil2Nby3Block = chainConfig.Istanbul.Ceil2Nby3Block
  211. config.Istanbul.AllowedFutureBlockTime = config.Miner.AllowedFutureBlockTime //Quorum
  212. config.Istanbul.TestQBFTBlock = chainConfig.Istanbul.TestQBFTBlock
  213. return istanbulBackend.New(&config.Istanbul, stack.GetNodeKey(), db)
  214. }
  215. if chainConfig.IBFT == nil && len(chainConfig.Transitions) > 0 {
  216. chainConfig.GetTransitionValue(big.NewInt(0), func(t params.Transition) {
  217. if strings.EqualFold(t.Algorithm, params.IBFT) {
  218. chainConfig.IBFT = &params.IBFTConfig{
  219. BFTConfig: &params.BFTConfig{
  220. EpochLength: t.EpochLength,
  221. BlockPeriodSeconds: t.BlockPeriodSeconds,
  222. EmptyBlockPeriodSeconds: t.EmptyBlockPeriodSeconds,
  223. ValidatorContractAddress: t.ValidatorContractAddress,
  224. RequestTimeoutSeconds: t.RequestTimeoutSeconds,
  225. },
  226. }
  227. log.Info("new chain config with", "ibft", *chainConfig.IBFT)
  228. }
  229. })
  230. }
  231. if chainConfig.IBFT != nil {
  232. setBFTConfig(&config.Istanbul, chainConfig.IBFT.BFTConfig)
  233. config.Istanbul.TestQBFTBlock = nil
  234. if chainConfig.IBFT.ValidatorContractAddress != (common.Address{}) {
  235. config.Istanbul.ValidatorContract = chainConfig.IBFT.ValidatorContractAddress
  236. }
  237. return istanbulBackend.New(&config.Istanbul, stack.GetNodeKey(), db)
  238. }
  239. if chainConfig.QBFT == nil && len(chainConfig.Transitions) > 0 {
  240. chainConfig.GetTransitionValue(big.NewInt(0), func(t params.Transition) {
  241. if strings.EqualFold(t.Algorithm, params.QBFT) {
  242. chainConfig.QBFT = &params.QBFTConfig{
  243. BFTConfig: &params.BFTConfig{
  244. EpochLength: t.EpochLength,
  245. BlockPeriodSeconds: t.BlockPeriodSeconds,
  246. EmptyBlockPeriodSeconds: t.EmptyBlockPeriodSeconds,
  247. ValidatorContractAddress: t.ValidatorContractAddress,
  248. RequestTimeoutSeconds: t.RequestTimeoutSeconds,
  249. },
  250. }
  251. log.Info("new chain config with", "qbft", *chainConfig.QBFT)
  252. }
  253. })
  254. }
  255. if chainConfig.QBFT != nil {
  256. setBFTConfig(&config.Istanbul, chainConfig.QBFT.BFTConfig)
  257. config.Istanbul.TestQBFTBlock = big.NewInt(0)
  258. if chainConfig.QBFT.ValidatorContractAddress != (common.Address{}) {
  259. config.Istanbul.ValidatorContract = chainConfig.QBFT.ValidatorContractAddress
  260. }
  261. config.Istanbul.BlockReward = chainConfig.QBFT.BlockReward
  262. config.Istanbul.BeneficiaryMode = chainConfig.QBFT.BeneficiaryMode
  263. config.Istanbul.MiningBeneficiary = chainConfig.QBFT.MiningBeneficiary
  264. config.Istanbul.ValidatorSelectionMode = chainConfig.QBFT.ValidatorSelectionMode
  265. config.Istanbul.Validators = chainConfig.QBFT.Validators
  266. return istanbulBackend.New(&config.Istanbul, stack.GetNodeKey(), db)
  267. }
  268. // For Quorum, Raft run as a separate service, so
  269. // the Ethereum service still needs a consensus engine,
  270. // use the consensus with the lightest overhead
  271. engine := ethash.NewFullFaker()
  272. engine.SetThreads(-1) // Disable CPU Mining
  273. return engine
  274. }
  275. // Quorum
  276. type QuorumLightClient struct {
  277. Use bool `toml:",omitempty"`
  278. PSI string `toml:",omitempty"`
  279. TokenEnabled bool `toml:",omitempty"`
  280. TokenValue string `toml:",omitempty"`
  281. TokenManagement string `toml:",omitempty"`
  282. RPCTLS bool `toml:",omitempty"`
  283. RPCTLSInsecureSkipVerify bool `toml:",omitempty"`
  284. RPCTLSCACert string `toml:",omitempty"`
  285. RPCTLSCert string `toml:",omitempty"`
  286. RPCTLSKey string `toml:",omitempty"`
  287. ServerNode string `toml:",omitempty"`
  288. ServerNodeRPC string `toml:",omitempty"`
  289. }
  290. func (q *QuorumLightClient) Enabled() bool {
  291. return q != nil && q.Use
  292. }
  293. func setBFTConfig(istanbulConfig *istanbul.Config, bftConfig *params.BFTConfig) {
  294. if bftConfig.BlockPeriodSeconds != 0 {
  295. istanbulConfig.BlockPeriod = bftConfig.BlockPeriodSeconds
  296. }
  297. if bftConfig.EmptyBlockPeriodSeconds != nil {
  298. istanbulConfig.EmptyBlockPeriod = *bftConfig.EmptyBlockPeriodSeconds
  299. }
  300. if bftConfig.RequestTimeoutSeconds != 0 {
  301. istanbulConfig.RequestTimeout = bftConfig.RequestTimeoutSeconds * 1000
  302. }
  303. if bftConfig.EpochLength != 0 {
  304. istanbulConfig.Epoch = bftConfig.EpochLength
  305. }
  306. if bftConfig.ProposerPolicy != 0 {
  307. istanbulConfig.ProposerPolicy = istanbul.NewProposerPolicy(istanbul.ProposerPolicyId(bftConfig.ProposerPolicy))
  308. }
  309. if bftConfig.Ceil2Nby3Block != nil {
  310. istanbulConfig.Ceil2Nby3Block = bftConfig.Ceil2Nby3Block
  311. }
  312. }