config.go 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. // Copyright 2016 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 params
  17. import (
  18. "encoding/binary"
  19. "errors"
  20. "fmt"
  21. "math/big"
  22. "strings"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/common/math"
  25. "github.com/ethereum/go-ethereum/log"
  26. "golang.org/x/crypto/sha3"
  27. )
  28. // Genesis hashes to enforce below configs on.
  29. var (
  30. MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
  31. RopstenGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")
  32. RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177")
  33. GoerliGenesisHash = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a")
  34. YoloV3GenesisHash = common.HexToHash("0xf1f2876e8500c77afcc03228757b39477eceffccf645b734967fe3c7e16967b7")
  35. )
  36. // TrustedCheckpoints associates each known checkpoint with the genesis hash of
  37. // the chain it belongs to.
  38. var TrustedCheckpoints = map[common.Hash]*TrustedCheckpoint{
  39. MainnetGenesisHash: MainnetTrustedCheckpoint,
  40. RopstenGenesisHash: RopstenTrustedCheckpoint,
  41. RinkebyGenesisHash: RinkebyTrustedCheckpoint,
  42. GoerliGenesisHash: GoerliTrustedCheckpoint,
  43. }
  44. // CheckpointOracles associates each known checkpoint oracles with the genesis hash of
  45. // the chain it belongs to.
  46. var CheckpointOracles = map[common.Hash]*CheckpointOracleConfig{
  47. MainnetGenesisHash: MainnetCheckpointOracle,
  48. RopstenGenesisHash: RopstenCheckpointOracle,
  49. RinkebyGenesisHash: RinkebyCheckpointOracle,
  50. GoerliGenesisHash: GoerliCheckpointOracle,
  51. }
  52. var (
  53. // MainnetChainConfig is the chain parameters to run a node on the main network.
  54. MainnetChainConfig = &ChainConfig{
  55. ChainID: big.NewInt(1),
  56. HomesteadBlock: big.NewInt(1_150_000),
  57. DAOForkBlock: big.NewInt(1_920_000),
  58. DAOForkSupport: true,
  59. EIP150Block: big.NewInt(2_463_000),
  60. EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
  61. EIP155Block: big.NewInt(2_675_000),
  62. EIP158Block: big.NewInt(2_675_000),
  63. ByzantiumBlock: big.NewInt(4_370_000),
  64. ConstantinopleBlock: big.NewInt(7_280_000),
  65. PetersburgBlock: big.NewInt(7_280_000),
  66. IstanbulBlock: big.NewInt(9_069_000),
  67. MuirGlacierBlock: big.NewInt(9_200_000),
  68. BerlinBlock: big.NewInt(12_244_000),
  69. Ethash: new(EthashConfig),
  70. }
  71. // MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network.
  72. MainnetTrustedCheckpoint = &TrustedCheckpoint{
  73. SectionIndex: 371,
  74. SectionHead: common.HexToHash("0x50fd3cec5376ede90ef9129772022690cd1467f22c18abb7faa11e793c51e9c9"),
  75. CHTRoot: common.HexToHash("0xb57b4b22a77b5930847b1ca9f62daa11eae6578948cb7b18997f2c0fe5757025"),
  76. BloomRoot: common.HexToHash("0xa338f8a868a194fa90327d0f5877f656a9f3640c618d2a01a01f2e76ef9ef954"),
  77. }
  78. // MainnetCheckpointOracle contains a set of configs for the main network oracle.
  79. MainnetCheckpointOracle = &CheckpointOracleConfig{
  80. Address: common.HexToAddress("0x9a9070028361F7AAbeB3f2F2Dc07F82C4a98A02a"),
  81. Signers: []common.Address{
  82. common.HexToAddress("0x1b2C260efc720BE89101890E4Db589b44E950527"), // Peter
  83. common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
  84. common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
  85. common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
  86. common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
  87. },
  88. Threshold: 2,
  89. }
  90. // RopstenChainConfig contains the chain parameters to run a node on the Ropsten test network.
  91. RopstenChainConfig = &ChainConfig{
  92. ChainID: big.NewInt(3),
  93. HomesteadBlock: big.NewInt(0),
  94. DAOForkBlock: nil,
  95. DAOForkSupport: true,
  96. EIP150Block: big.NewInt(0),
  97. EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
  98. EIP155Block: big.NewInt(10),
  99. EIP158Block: big.NewInt(10),
  100. ByzantiumBlock: big.NewInt(1_700_000),
  101. ConstantinopleBlock: big.NewInt(4_230_000),
  102. PetersburgBlock: big.NewInt(4_939_394),
  103. IstanbulBlock: big.NewInt(6_485_846),
  104. MuirGlacierBlock: big.NewInt(7_117_117),
  105. BerlinBlock: big.NewInt(9_812_189),
  106. Ethash: new(EthashConfig),
  107. }
  108. // RopstenTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network.
  109. RopstenTrustedCheckpoint = &TrustedCheckpoint{
  110. SectionIndex: 279,
  111. SectionHead: common.HexToHash("0x4a4912848d4c06090097073357c10015d11c6f4544a0f93cbdd584701c3b7d58"),
  112. CHTRoot: common.HexToHash("0x9053b7867ae921e80a4e2f5a4b15212e4af3d691ca712fb33dc150e9c6ea221c"),
  113. BloomRoot: common.HexToHash("0x3dc04cb1be7ddc271f3f83469b47b76184a79d7209ef51d85b1539ea6d25a645"),
  114. }
  115. // RopstenCheckpointOracle contains a set of configs for the Ropsten test network oracle.
  116. RopstenCheckpointOracle = &CheckpointOracleConfig{
  117. Address: common.HexToAddress("0xEF79475013f154E6A65b54cB2742867791bf0B84"),
  118. Signers: []common.Address{
  119. common.HexToAddress("0x32162F3581E88a5f62e8A61892B42C46E2c18f7b"), // Peter
  120. common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
  121. common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
  122. common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
  123. common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
  124. },
  125. Threshold: 2,
  126. }
  127. // RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
  128. RinkebyChainConfig = &ChainConfig{
  129. ChainID: big.NewInt(4),
  130. HomesteadBlock: big.NewInt(1),
  131. DAOForkBlock: nil,
  132. DAOForkSupport: true,
  133. EIP150Block: big.NewInt(2),
  134. EIP150Hash: common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
  135. EIP155Block: big.NewInt(3),
  136. EIP158Block: big.NewInt(3),
  137. ByzantiumBlock: big.NewInt(1_035_301),
  138. ConstantinopleBlock: big.NewInt(3_660_663),
  139. PetersburgBlock: big.NewInt(4_321_234),
  140. IstanbulBlock: big.NewInt(5_435_345),
  141. MuirGlacierBlock: nil,
  142. BerlinBlock: big.NewInt(8_290_928),
  143. Clique: &CliqueConfig{
  144. Period: 15,
  145. Epoch: 30000,
  146. },
  147. }
  148. // RinkebyTrustedCheckpoint contains the light client trusted checkpoint for the Rinkeby test network.
  149. RinkebyTrustedCheckpoint = &TrustedCheckpoint{
  150. SectionIndex: 254,
  151. SectionHead: common.HexToHash("0x0cba01dd71baa22ac8fa0b105bc908e94f9ecfbc79b4eb97427fe07b5851dd10"),
  152. CHTRoot: common.HexToHash("0x5673d8fc49c9c7d8729068640e4b392d46952a5a38798973bac1cf1d0d27ad7d"),
  153. BloomRoot: common.HexToHash("0x70e01232b66df9a7778ae3291c9217afb9a2d9f799f32d7b912bd37e7bce83a8"),
  154. }
  155. // RinkebyCheckpointOracle contains a set of configs for the Rinkeby test network oracle.
  156. RinkebyCheckpointOracle = &CheckpointOracleConfig{
  157. Address: common.HexToAddress("0xebe8eFA441B9302A0d7eaECc277c09d20D684540"),
  158. Signers: []common.Address{
  159. common.HexToAddress("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3"), // Peter
  160. common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
  161. common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
  162. common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
  163. },
  164. Threshold: 2,
  165. }
  166. // GoerliChainConfig contains the chain parameters to run a node on the Görli test network.
  167. GoerliChainConfig = &ChainConfig{
  168. ChainID: big.NewInt(5),
  169. HomesteadBlock: big.NewInt(0),
  170. DAOForkBlock: nil,
  171. DAOForkSupport: true,
  172. EIP150Block: big.NewInt(0),
  173. EIP155Block: big.NewInt(0),
  174. EIP158Block: big.NewInt(0),
  175. ByzantiumBlock: big.NewInt(0),
  176. ConstantinopleBlock: big.NewInt(0),
  177. PetersburgBlock: big.NewInt(0),
  178. IstanbulBlock: big.NewInt(1_561_651),
  179. MuirGlacierBlock: nil,
  180. BerlinBlock: big.NewInt(4_460_644),
  181. Clique: &CliqueConfig{
  182. Period: 15,
  183. Epoch: 30000,
  184. },
  185. }
  186. // GoerliTrustedCheckpoint contains the light client trusted checkpoint for the Görli test network.
  187. GoerliTrustedCheckpoint = &TrustedCheckpoint{
  188. SectionIndex: 138,
  189. SectionHead: common.HexToHash("0xb7ea0566abd7d0def5b3c9afa3431debb7bb30b65af35f106ca93a59e6c859a7"),
  190. CHTRoot: common.HexToHash("0x378c7ea9081242beb982e2e39567ba12f2ed3e59e5aba3f9db1d595646d7c9f4"),
  191. BloomRoot: common.HexToHash("0x523c169286cfca52e8a6579d8c35dc8bf093412d8a7478163bfa81ae91c2492d"),
  192. }
  193. // GoerliCheckpointOracle contains a set of configs for the Goerli test network oracle.
  194. GoerliCheckpointOracle = &CheckpointOracleConfig{
  195. Address: common.HexToAddress("0x18CA0E045F0D772a851BC7e48357Bcaab0a0795D"),
  196. Signers: []common.Address{
  197. common.HexToAddress("0x4769bcaD07e3b938B7f43EB7D278Bc7Cb9efFb38"), // Peter
  198. common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
  199. common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
  200. common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
  201. common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
  202. },
  203. Threshold: 2,
  204. }
  205. // YoloV3ChainConfig contains the chain parameters to run a node on the YOLOv3 test network.
  206. YoloV3ChainConfig = &ChainConfig{
  207. ChainID: new(big.Int).SetBytes([]byte("yolov3x")),
  208. HomesteadBlock: big.NewInt(0),
  209. DAOForkBlock: nil,
  210. DAOForkSupport: true,
  211. EIP150Block: big.NewInt(0),
  212. EIP155Block: big.NewInt(0),
  213. EIP158Block: big.NewInt(0),
  214. ByzantiumBlock: big.NewInt(0),
  215. ConstantinopleBlock: big.NewInt(0),
  216. PetersburgBlock: big.NewInt(0),
  217. IstanbulBlock: big.NewInt(0),
  218. MuirGlacierBlock: nil,
  219. BerlinBlock: nil, // Don't enable Berlin directly, we're YOLOing it
  220. YoloV3Block: big.NewInt(0),
  221. Clique: &CliqueConfig{
  222. Period: 15,
  223. Epoch: 30000,
  224. },
  225. }
  226. // AllEthashProtocolChanges contains every protocol change (EIPs) introduced
  227. // and accepted by the Ethereum core developers into the Ethash consensus.
  228. //
  229. // This configuration is intentionally not using keyed fields to force anyone
  230. // adding flags to the config to also have to set these fields.
  231. AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil, nil, nil, nil, nil, false, 32, 35, big.NewInt(0), big.NewInt(0), nil, nil, false, nil, nil}
  232. // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
  233. // and accepted by the Ethereum core developers into the Clique consensus.
  234. //
  235. // This configuration is intentionally not using keyed fields to force anyone
  236. // adding flags to the config to also have to set these fields.
  237. AllCliqueProtocolChanges = &ChainConfig{big.NewInt(10), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil, nil, nil, nil, false, 32, 32, big.NewInt(0), big.NewInt(0), nil, nil, false, nil, nil}
  238. // Quorum chainID should 10
  239. TestChainConfig = &ChainConfig{big.NewInt(10), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil, nil, nil, nil, nil, false, 32, 32, big.NewInt(0), big.NewInt(0), nil, nil, false, nil, nil}
  240. TestRules = TestChainConfig.Rules(new(big.Int))
  241. QuorumTestChainConfig = &ChainConfig{big.NewInt(10), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, new(EthashConfig), nil, nil, nil, nil, nil, true, 64, 32, big.NewInt(0), big.NewInt(0), nil, big.NewInt(0), false, nil, nil}
  242. QuorumMPSTestChainConfig = &ChainConfig{big.NewInt(10), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, new(EthashConfig), nil, nil, nil, nil, nil, true, 64, 32, big.NewInt(0), big.NewInt(0), nil, big.NewInt(0), true, nil, nil}
  243. )
  244. // TrustedCheckpoint represents a set of post-processed trie roots (CHT and
  245. // BloomTrie) associated with the appropriate section index and head hash. It is
  246. // used to start light syncing from this checkpoint and avoid downloading the
  247. // entire header chain while still being able to securely access old headers/logs.
  248. type TrustedCheckpoint struct {
  249. SectionIndex uint64 `json:"sectionIndex"`
  250. SectionHead common.Hash `json:"sectionHead"`
  251. CHTRoot common.Hash `json:"chtRoot"`
  252. BloomRoot common.Hash `json:"bloomRoot"`
  253. }
  254. // HashEqual returns an indicator comparing the itself hash with given one.
  255. func (c *TrustedCheckpoint) HashEqual(hash common.Hash) bool {
  256. if c.Empty() {
  257. return hash == common.Hash{}
  258. }
  259. return c.Hash() == hash
  260. }
  261. // Hash returns the hash of checkpoint's four key fields(index, sectionHead, chtRoot and bloomTrieRoot).
  262. func (c *TrustedCheckpoint) Hash() common.Hash {
  263. var sectionIndex [8]byte
  264. binary.BigEndian.PutUint64(sectionIndex[:], c.SectionIndex)
  265. w := sha3.NewLegacyKeccak256()
  266. w.Write(sectionIndex[:])
  267. w.Write(c.SectionHead[:])
  268. w.Write(c.CHTRoot[:])
  269. w.Write(c.BloomRoot[:])
  270. var h common.Hash
  271. w.Sum(h[:0])
  272. return h
  273. }
  274. // Empty returns an indicator whether the checkpoint is regarded as empty.
  275. func (c *TrustedCheckpoint) Empty() bool {
  276. return c.SectionHead == (common.Hash{}) || c.CHTRoot == (common.Hash{}) || c.BloomRoot == (common.Hash{})
  277. }
  278. // CheckpointOracleConfig represents a set of checkpoint contract(which acts as an oracle)
  279. // config which used for light client checkpoint syncing.
  280. type CheckpointOracleConfig struct {
  281. Address common.Address `json:"address"`
  282. Signers []common.Address `json:"signers"`
  283. Threshold uint64 `json:"threshold"`
  284. }
  285. type MaxCodeConfigStruct struct {
  286. Block *big.Int `json:"block,omitempty"`
  287. Size uint64 `json:"size,omitempty"`
  288. }
  289. // ChainConfig is the core config which determines the blockchain settings.
  290. //
  291. // ChainConfig is stored in the database on a per block basis. This means
  292. // that any network, identified by its genesis block, can have its own
  293. // set of configuration options.
  294. type ChainConfig struct {
  295. ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
  296. HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)
  297. DAOForkBlock *big.Int `json:"daoForkBlock,omitempty"` // TheDAO hard-fork switch block (nil = no fork)
  298. DAOForkSupport bool `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
  299. // EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
  300. EIP150Block *big.Int `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork)
  301. EIP150Hash common.Hash `json:"eip150Hash,omitempty"` // EIP150 HF hash (needed for header only clients as only gas pricing changed)
  302. EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
  303. EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block
  304. ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium)
  305. ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
  306. PetersburgBlock *big.Int `json:"petersburgBlock,omitempty"` // Petersburg switch block (nil = same as Constantinople)
  307. IstanbulBlock *big.Int `json:"istanbulBlock,omitempty"` // Istanbul switch block (nil = no fork, 0 = already on istanbul)
  308. MuirGlacierBlock *big.Int `json:"muirGlacierBlock,omitempty"` // Eip-2384 (bomb delay) switch block (nil = no fork, 0 = already activated)
  309. BerlinBlock *big.Int `json:"berlinBlock,omitempty"` // Berlin switch block (nil = no fork, 0 = already on berlin)
  310. YoloV3Block *big.Int `json:"yoloV3Block,omitempty"` // YOLO v3: Gas repricings TODO @holiman add EIP references
  311. EWASMBlock *big.Int `json:"ewasmBlock,omitempty"` // EWASM switch block (nil = no fork, 0 = already activated)
  312. CatalystBlock *big.Int `json:"catalystBlock,omitempty"` // Catalyst switch block (nil = no fork, 0 = already on catalyst)
  313. // Various consensus engines
  314. Ethash *EthashConfig `json:"ethash,omitempty"`
  315. Clique *CliqueConfig `json:"clique,omitempty"`
  316. Istanbul *IstanbulConfig `json:"istanbul,omitempty"` // Quorum
  317. IBFT *IBFTConfig `json:"ibft,omitempty"` // Quorum
  318. QBFT *QBFTConfig `json:"qbft,omitempty"` // Quorum
  319. // Start of Quorum specific configs
  320. Transitions []Transition `json:"transitions,omitempty"` // Quorum - transition config based on the block number
  321. IsQuorum bool `json:"isQuorum"` // Quorum flag
  322. TransactionSizeLimit uint64 `json:"txnSizeLimit"` // Quorum - transaction size limit
  323. MaxCodeSize uint64 `json:"maxCodeSize"` // Quorum - maximum CodeSize of contract
  324. // QIP714Block implements the permissions related changes
  325. QIP714Block *big.Int `json:"qip714Block,omitempty"`
  326. MaxCodeSizeChangeBlock *big.Int `json:"maxCodeSizeChangeBlock,omitempty"`
  327. // to track multiple changes to maxCodeSize
  328. MaxCodeSizeConfig []MaxCodeConfigStruct `json:"maxCodeSizeConfig,omitempty"`
  329. PrivacyEnhancementsBlock *big.Int `json:"privacyEnhancementsBlock,omitempty"`
  330. IsMPS bool `json:"isMPS"` // multiple private states flag
  331. PrivacyPrecompileBlock *big.Int `json:"privacyPrecompileBlock,omitempty"` // Switch block to enable privacy precompiled contract to process privacy marker transactions
  332. EnableGasPriceBlock *big.Int `json:"enableGasPriceBlock,omitempty"` // Switch block to enable usage of gas price
  333. // End of Quorum specific configs
  334. }
  335. // EthashConfig is the consensus engine configs for proof-of-work based sealing.
  336. type EthashConfig struct{}
  337. // String implements the stringer interface, returning the consensus engine details.
  338. func (c *EthashConfig) String() string {
  339. return "ethash"
  340. }
  341. // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
  342. type CliqueConfig struct {
  343. Period uint64 `json:"period"` // Number of seconds between blocks to enforce
  344. Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
  345. AllowedFutureBlockTime uint64 `json:"allowedFutureBlockTime"` // Max time (in seconds) from current time allowed for blocks, before they're considered future blocks
  346. }
  347. // String implements the stringer interface, returning the consensus engine details.
  348. func (c *CliqueConfig) String() string {
  349. return "clique"
  350. }
  351. // IstanbulConfig is the consensus engine configs for Istanbul based sealing.
  352. type IstanbulConfig struct {
  353. Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
  354. ProposerPolicy uint64 `json:"policy"` // The policy for proposer selection
  355. Ceil2Nby3Block *big.Int `json:"ceil2Nby3Block,omitempty"` // Number of confirmations required to move from one state to next [2F + 1 to Ceil(2N/3)]
  356. TestQBFTBlock *big.Int `json:"testQBFTBlock,omitempty"` // Fork block at which block confirmations are done using qbft consensus instead of ibft
  357. }
  358. // String implements the stringer interface, returning the consensus engine details.
  359. func (c *IstanbulConfig) String() string {
  360. return "istanbul"
  361. }
  362. type BFTConfig struct {
  363. EpochLength uint64 `json:"epochlength"` // Number of blocks that should pass before pending validator votes are reset
  364. BlockPeriodSeconds uint64 `json:"blockperiodseconds"` // Minimum time between two consecutive IBFT or QBFT blocks’ timestamps in seconds
  365. EmptyBlockPeriodSeconds *uint64 `json:"emptyblockperiodseconds,omitempty"` // Minimum time between two consecutive IBFT or QBFT a block and empty block’ timestamps in seconds
  366. RequestTimeoutSeconds uint64 `json:"requesttimeoutseconds"` // Minimum request timeout for each IBFT or QBFT round in milliseconds
  367. ProposerPolicy uint64 `json:"policy"` // The policy for proposer selection
  368. Ceil2Nby3Block *big.Int `json:"ceil2Nby3Block,omitempty"` // Number of confirmations required to move from one state to next [2F + 1 to Ceil(2N/3)]
  369. ValidatorContractAddress common.Address `json:"validatorcontractaddress"` // Smart contract address for list of validators
  370. }
  371. type IBFTConfig struct {
  372. *BFTConfig
  373. }
  374. func (c IBFTConfig) String() string {
  375. return "istanbul"
  376. }
  377. type QBFTConfig struct {
  378. *BFTConfig
  379. BlockReward *math.HexOrDecimal256 `json:"blockReward,omitempty"` // Reward from start, works only on QBFT consensus protocol
  380. BeneficiaryMode *string `json:"beneficiaryMode,omitempty"` // Mode for setting the beneficiary, either: list, besu, validators (beneficiary list is the list of validators)
  381. MiningBeneficiary *common.Address `json:"miningBeneficiary,omitempty"` // Wallet address that benefits at every new block (besu mode)
  382. ValidatorSelectionMode *string `json:"validatorselectionmode,omitempty"` // Select model for validators
  383. Validators []common.Address `json:"validators"` // Validators list
  384. }
  385. func (c QBFTConfig) String() string {
  386. return QBFT
  387. }
  388. const (
  389. IBFT = "ibft"
  390. QBFT = "qbft"
  391. ContractMode = "contract"
  392. BlockHeaderMode = "blockheader"
  393. )
  394. type Transition struct {
  395. Block *big.Int `json:"block"`
  396. Algorithm string `json:"algorithm,omitempty"`
  397. EpochLength uint64 `json:"epochlength,omitempty"` // Number of blocks that should pass before pending validator votes are reset
  398. BlockPeriodSeconds uint64 `json:"blockperiodseconds,omitempty"` // Minimum time between two consecutive IBFT or QBFT blocks’ timestamps in seconds
  399. EmptyBlockPeriodSeconds *uint64 `json:"emptyblockperiodseconds,omitempty"` // Minimum time between two consecutive IBFT or QBFT a block and empty block’ timestamps in seconds
  400. RequestTimeoutSeconds uint64 `json:"requesttimeoutseconds,omitempty"` // Minimum request timeout for each IBFT or QBFT round in milliseconds
  401. ContractSizeLimit uint64 `json:"contractsizelimit,omitempty"` // Maximum smart contract code size
  402. ValidatorContractAddress common.Address `json:"validatorcontractaddress"` // Smart contract address for list of validators
  403. Validators []common.Address `json:"validators"` // List of validators
  404. ValidatorSelectionMode string `json:"validatorselectionmode,omitempty"` // Validator selection mode to switch to
  405. EnhancedPermissioningEnabled *bool `json:"enhancedPermissioningEnabled,omitempty"` // aka QIP714Block
  406. PrivacyEnhancementsEnabled *bool `json:"privacyEnhancementsEnabled,omitempty"` // privacy enhancements (mandatory party, private state validation)
  407. PrivacyPrecompileEnabled *bool `json:"privacyPrecompileEnabled,omitempty"` // enable marker transactions support
  408. GasPriceEnabled *bool `json:"gasPriceEnabled,omitempty"` // enable gas price
  409. MinerGasLimit uint64 `json:"miner.gaslimit,omitempty"` // Gas Limit
  410. TwoFPlusOneEnabled *bool `json:"2FPlus1Enabled,omitempty"` // Ceil(2N/3) is the default you need to explicitly use 2F + 1
  411. TransactionSizeLimit uint64 `json:"transactionSizeLimit,omitempty"` // Modify TransactionSizeLimit
  412. BlockReward *math.HexOrDecimal256 `json:"blockReward,omitempty"` // validation rewards
  413. BeneficiaryMode *string `json:"beneficiaryMode,omitempty"` // Mode for setting the beneficiary, either: list, besu, validators (beneficiary list is the list of validators)
  414. MiningBeneficiary *common.Address `json:"miningBeneficiary,omitempty"` // Wallet address that benefits at every new block (besu mode)
  415. }
  416. // String implements the fmt.Stringer interface.
  417. func (c *ChainConfig) String() string {
  418. var engine interface{}
  419. switch {
  420. case c.Ethash != nil:
  421. engine = c.Ethash
  422. case c.Clique != nil:
  423. engine = c.Clique
  424. case c.Istanbul != nil:
  425. engine = c.Istanbul
  426. default:
  427. engine = "unknown"
  428. }
  429. return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v IsQuorum: %v Constantinople: %v TransactionSizeLimit: %v MaxCodeSize: %v Petersburg: %v Istanbul: %v, Muir Glacier: %v, Berlin: %v Catalyst: %v YOLO v3: %v PrivacyEnhancements: %v PrivacyPrecompile: %v EnableGasPriceBlock: %v Engine: %v}",
  430. c.ChainID,
  431. c.HomesteadBlock,
  432. c.DAOForkBlock,
  433. c.DAOForkSupport,
  434. c.EIP150Block,
  435. c.EIP155Block,
  436. c.EIP158Block,
  437. c.ByzantiumBlock,
  438. c.IsQuorum,
  439. c.ConstantinopleBlock,
  440. c.TransactionSizeLimit,
  441. c.MaxCodeSize,
  442. c.PetersburgBlock,
  443. c.IstanbulBlock,
  444. c.MuirGlacierBlock,
  445. c.BerlinBlock,
  446. c.CatalystBlock,
  447. c.YoloV3Block,
  448. c.PrivacyEnhancementsBlock, //Quorum
  449. c.PrivacyPrecompileBlock, //Quorum
  450. c.EnableGasPriceBlock, //Quorum
  451. engine,
  452. )
  453. }
  454. // Quorum - validate code size and transaction size limit
  455. func (c *ChainConfig) IsValid() error {
  456. if c.TransactionSizeLimit < 32 || c.TransactionSizeLimit > 128 {
  457. return errors.New("Genesis transaction size limit must be between 32 and 128")
  458. }
  459. if c.MaxCodeSize != 0 && (c.MaxCodeSize < 24 || c.MaxCodeSize > 128) {
  460. return errors.New("Genesis max code size must be between 24 and 128")
  461. }
  462. return nil
  463. }
  464. // IsHomestead returns whether num is either equal to the homestead block or greater.
  465. func (c *ChainConfig) IsHomestead(num *big.Int) bool {
  466. return isForked(c.HomesteadBlock, num)
  467. }
  468. // IsDAOFork returns whether num is either equal to the DAO fork block or greater.
  469. func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
  470. return isForked(c.DAOForkBlock, num)
  471. }
  472. // IsEIP150 returns whether num is either equal to the EIP150 fork block or greater.
  473. func (c *ChainConfig) IsEIP150(num *big.Int) bool {
  474. return isForked(c.EIP150Block, num)
  475. }
  476. // IsEIP155 returns whether num is either equal to the EIP155 fork block or greater.
  477. func (c *ChainConfig) IsEIP155(num *big.Int) bool {
  478. return isForked(c.EIP155Block, num)
  479. }
  480. // IsEIP158 returns whether num is either equal to the EIP158 fork block or greater.
  481. func (c *ChainConfig) IsEIP158(num *big.Int) bool {
  482. return isForked(c.EIP158Block, num)
  483. }
  484. // IsByzantium returns whether num is either equal to the Byzantium fork block or greater.
  485. func (c *ChainConfig) IsByzantium(num *big.Int) bool {
  486. return isForked(c.ByzantiumBlock, num)
  487. }
  488. // IsConstantinople returns whether num is either equal to the Constantinople fork block or greater.
  489. func (c *ChainConfig) IsConstantinople(num *big.Int) bool {
  490. return isForked(c.ConstantinopleBlock, num)
  491. }
  492. // IsMuirGlacier returns whether num is either equal to the Muir Glacier (EIP-2384) fork block or greater.
  493. func (c *ChainConfig) IsMuirGlacier(num *big.Int) bool {
  494. return isForked(c.MuirGlacierBlock, num)
  495. }
  496. // IsPetersburg returns whether num is either
  497. // - equal to or greater than the PetersburgBlock fork block,
  498. // - OR is nil, and Constantinople is active
  499. func (c *ChainConfig) IsPetersburg(num *big.Int) bool {
  500. return isForked(c.PetersburgBlock, num) || c.PetersburgBlock == nil && isForked(c.ConstantinopleBlock, num)
  501. }
  502. // IsIstanbul returns whether num is either equal to the Istanbul fork block or greater.
  503. func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
  504. return isForked(c.IstanbulBlock, num)
  505. }
  506. // IsBerlin returns whether num is either equal to the Berlin fork block or greater.
  507. func (c *ChainConfig) IsBerlin(num *big.Int) bool {
  508. return isForked(c.BerlinBlock, num) || isForked(c.YoloV3Block, num)
  509. }
  510. // IsCatalyst returns whether num is either equal to the Merge fork block or greater.
  511. func (c *ChainConfig) IsCatalyst(num *big.Int) bool {
  512. return isForked(c.CatalystBlock, num)
  513. }
  514. // IsEWASM returns whether num represents a block number after the EWASM fork
  515. func (c *ChainConfig) IsEWASM(num *big.Int) bool {
  516. return isForked(c.EWASMBlock, num)
  517. }
  518. // Quorum
  519. //
  520. // IsQIP714 returns whether num represents a block number where permissions is enabled
  521. func (c *ChainConfig) IsQIP714(num *big.Int) bool {
  522. enableEnhancedPermissioning := false
  523. c.GetTransitionValue(num, func(transition Transition) {
  524. if transition.EnhancedPermissioningEnabled != nil {
  525. enableEnhancedPermissioning = *transition.EnhancedPermissioningEnabled
  526. }
  527. })
  528. return isForked(c.QIP714Block, num) || enableEnhancedPermissioning
  529. }
  530. // Quorum
  531. //
  532. // GetMaxCodeSize returns maxCodeSize for the given block number
  533. func (c *ChainConfig) GetMaxCodeSize(num *big.Int) int {
  534. maxCodeSize := MaxCodeSize
  535. if len(c.MaxCodeSizeConfig) > 0 {
  536. log.Warn("WARNING: The attribute config.maxCodeSizeConfig is deprecated and will be removed in the future, please use config.transitions.contractsizelimit on genesis file")
  537. for _, data := range c.MaxCodeSizeConfig {
  538. if data.Block.Cmp(num) > 0 {
  539. break
  540. }
  541. maxCodeSize = int(data.Size) * 1024
  542. }
  543. } else if c.MaxCodeSize > 0 {
  544. if c.MaxCodeSizeChangeBlock != nil && c.MaxCodeSizeChangeBlock.Cmp(big.NewInt(0)) >= 0 {
  545. if isForked(c.MaxCodeSizeChangeBlock, num) {
  546. maxCodeSize = int(c.MaxCodeSize) * 1024
  547. }
  548. } else {
  549. maxCodeSize = int(c.MaxCodeSize) * 1024
  550. }
  551. }
  552. c.GetTransitionValue(num, func(transition Transition) {
  553. if transition.ContractSizeLimit != 0 {
  554. maxCodeSize = int(transition.ContractSizeLimit) * 1024
  555. }
  556. })
  557. return maxCodeSize
  558. }
  559. func (c *ChainConfig) GetRewardAccount(num *big.Int, coinbase common.Address) (common.Address, error) {
  560. beneficiaryMode := "validator"
  561. miningBeneficiary := common.Address{}
  562. if c.QBFT != nil && c.QBFT.MiningBeneficiary != nil {
  563. miningBeneficiary = *c.QBFT.MiningBeneficiary
  564. beneficiaryMode = "fixed"
  565. }
  566. if c.QBFT != nil && c.QBFT.BeneficiaryMode != nil {
  567. beneficiaryMode = *c.QBFT.BeneficiaryMode
  568. }
  569. c.GetTransitionValue(num, func(transition Transition) {
  570. if transition.BeneficiaryMode != nil && (*transition.BeneficiaryMode == "validators" || *transition.BeneficiaryMode == "validator") {
  571. beneficiaryMode = "validator"
  572. }
  573. if transition.MiningBeneficiary != nil && (transition.BeneficiaryMode == nil || *transition.BeneficiaryMode == "fixed") {
  574. miningBeneficiary = *transition.MiningBeneficiary
  575. beneficiaryMode = "fixed"
  576. }
  577. })
  578. switch strings.ToLower(beneficiaryMode) {
  579. case "fixed":
  580. log.Trace("fixed beneficiary mode", "miningBeneficiary", miningBeneficiary)
  581. return miningBeneficiary, nil
  582. case "validator":
  583. log.Trace("validator beneficiary mode", "coinbase", coinbase)
  584. return coinbase, nil
  585. }
  586. return common.Address{}, errors.New("BeneficiaryMode must be coinbase|fixed")
  587. }
  588. func (c *ChainConfig) GetBlockReward(num *big.Int) big.Int {
  589. blockReward := *math.NewHexOrDecimal256(0)
  590. if c.QBFT != nil && c.QBFT.BlockReward != nil {
  591. blockReward = *c.QBFT.BlockReward
  592. }
  593. c.GetTransitionValue(num, func(transition Transition) {
  594. if transition.BlockReward != nil {
  595. blockReward = *transition.BlockReward
  596. }
  597. })
  598. return big.Int(blockReward)
  599. }
  600. // Quorum
  601. // gets value at or after a transition
  602. func (c *ChainConfig) GetTransitionValue(num *big.Int, callback func(transition Transition)) {
  603. if c != nil && num != nil && c.Transitions != nil {
  604. for i := 0; i < len(c.Transitions) && c.Transitions[i].Block.Cmp(num) <= 0; i++ {
  605. callback(c.Transitions[i])
  606. }
  607. }
  608. }
  609. // Quorum
  610. //
  611. // GetMinerMinGasLimit returns the miners minGasLimit for the given block number
  612. func (c *ChainConfig) GetMinerMinGasLimit(num *big.Int, defaultValue uint64) uint64 {
  613. minGasLimit := defaultValue
  614. if c != nil && num != nil && len(c.Transitions) > 0 {
  615. for i := 0; i < len(c.Transitions) && c.Transitions[i].Block.Cmp(num) <= 0; i++ {
  616. if c.Transitions[i].MinerGasLimit != 0 {
  617. minGasLimit = c.Transitions[i].MinerGasLimit
  618. }
  619. }
  620. }
  621. return minGasLimit
  622. }
  623. // Quorum
  624. //
  625. // validates the maxCodeSizeConfig data passed in config
  626. func (c *ChainConfig) CheckMaxCodeConfigData() error {
  627. if c.MaxCodeSize != 0 || (c.MaxCodeSizeChangeBlock != nil && c.MaxCodeSizeChangeBlock.Cmp(big.NewInt(0)) >= 0) {
  628. return errors.New("maxCodeSize & maxCodeSizeChangeBlock deprecated. Consider using maxCodeSizeConfig")
  629. }
  630. // validate max code size data
  631. // 1. Code size should not be less than 24 and greater than 128
  632. // 2. block entries are in ascending order
  633. prevBlock := big.NewInt(0)
  634. for _, data := range c.MaxCodeSizeConfig {
  635. if data.Size < 24 || data.Size > 128 {
  636. return errors.New("Genesis max code size must be between 24 and 128")
  637. }
  638. if data.Block == nil {
  639. return errors.New("Block number not given in maxCodeSizeConfig data")
  640. }
  641. if data.Block.Cmp(prevBlock) < 0 {
  642. return errors.New("invalid maxCodeSize detail, block order has to be ascending")
  643. }
  644. prevBlock = data.Block
  645. }
  646. return nil
  647. }
  648. func (c *ChainConfig) CheckTransitionsData() error {
  649. isQBFT := false
  650. if c.QBFT != nil {
  651. isQBFT = true
  652. }
  653. prevBlock := big.NewInt(0)
  654. for _, transition := range c.Transitions {
  655. if transition.Algorithm != "" && !strings.EqualFold(transition.Algorithm, IBFT) && !strings.EqualFold(transition.Algorithm, QBFT) {
  656. return ErrTransitionAlgorithm
  657. }
  658. if transition.ValidatorSelectionMode != "" && transition.ValidatorSelectionMode != ContractMode && transition.ValidatorSelectionMode != BlockHeaderMode {
  659. return ErrValidatorSelectionMode
  660. }
  661. if c.Istanbul != nil && c.Istanbul.TestQBFTBlock != nil && (strings.EqualFold(transition.Algorithm, IBFT) || strings.EqualFold(transition.Algorithm, QBFT)) {
  662. return ErrTestQBFTBlockAndTransitions
  663. }
  664. if len(c.MaxCodeSizeConfig) > 0 && transition.ContractSizeLimit != 0 {
  665. return ErrMaxCodeSizeConfigAndTransitions
  666. }
  667. if strings.EqualFold(transition.Algorithm, QBFT) {
  668. isQBFT = true
  669. }
  670. if transition.Block == nil {
  671. return ErrBlockNumberMissing
  672. }
  673. if transition.Block.Cmp(prevBlock) < 0 {
  674. return ErrBlockOrder
  675. }
  676. if isQBFT && strings.EqualFold(transition.Algorithm, IBFT) {
  677. return ErrTransition
  678. }
  679. if transition.ContractSizeLimit != 0 && (transition.ContractSizeLimit < 24 || transition.ContractSizeLimit > 128) {
  680. return ErrContractSizeLimit
  681. }
  682. if transition.ValidatorContractAddress != (common.Address{}) && transition.ValidatorSelectionMode != ContractMode {
  683. return ErrMissingValidatorSelectionMode
  684. }
  685. if transition.TransactionSizeLimit != 0 && transition.TransactionSizeLimit < 32 || transition.TransactionSizeLimit > 128 {
  686. return ErrTransactionSizeLimit
  687. }
  688. if transition.BeneficiaryMode != nil && *transition.BeneficiaryMode != "fixed" && *transition.BeneficiaryMode != "validators" && *transition.BeneficiaryMode != "" && *transition.BeneficiaryMode != "list" {
  689. return ErrBeneficiaryMode
  690. }
  691. prevBlock = transition.Block
  692. }
  693. return nil
  694. }
  695. // Quorum
  696. //
  697. // checks if changes to maxCodeSizeConfig proposed are compatible
  698. // with already existing genesis data
  699. func isMaxCodeSizeConfigCompatible(c1, c2 *ChainConfig, head *big.Int) (error, *big.Int, *big.Int) {
  700. if len(c1.MaxCodeSizeConfig) == 0 && len(c2.MaxCodeSizeConfig) == 0 {
  701. // maxCodeSizeConfig not used. return
  702. return nil, big.NewInt(0), big.NewInt(0)
  703. }
  704. // existing config had maxCodeSizeConfig and new one does not have the same return error
  705. if len(c1.MaxCodeSizeConfig) > 0 && len(c2.MaxCodeSizeConfig) == 0 {
  706. return fmt.Errorf("genesis file missing max code size information"), head, head
  707. }
  708. if len(c2.MaxCodeSizeConfig) > 0 && len(c1.MaxCodeSizeConfig) == 0 {
  709. return nil, big.NewInt(0), big.NewInt(0)
  710. }
  711. // check the number of records below current head in both configs
  712. // if they do not match throw an error
  713. c1RecsBelowHead := 0
  714. for _, data := range c1.MaxCodeSizeConfig {
  715. if data.Block.Cmp(head) <= 0 {
  716. c1RecsBelowHead++
  717. } else {
  718. break
  719. }
  720. }
  721. c2RecsBelowHead := 0
  722. for _, data := range c2.MaxCodeSizeConfig {
  723. if data.Block.Cmp(head) <= 0 {
  724. c2RecsBelowHead++
  725. } else {
  726. break
  727. }
  728. }
  729. // if the count of past records is not matching return error
  730. if c1RecsBelowHead != c2RecsBelowHead {
  731. return errors.New("maxCodeSizeConfig data incompatible. updating maxCodeSize for past"), head, head
  732. }
  733. // validate that each past record is matching exactly. if not return error
  734. for i := 0; i < c1RecsBelowHead; i++ {
  735. if c1.MaxCodeSizeConfig[i].Block.Cmp(c2.MaxCodeSizeConfig[i].Block) != 0 ||
  736. c1.MaxCodeSizeConfig[i].Size != c2.MaxCodeSizeConfig[i].Size {
  737. return errors.New("maxCodeSizeConfig data incompatible. maxCodeSize historical data does not match"), head, head
  738. }
  739. }
  740. return nil, big.NewInt(0), big.NewInt(0)
  741. }
  742. // Quorum
  743. //
  744. // checks if changes to transitions proposed are compatible
  745. // with already existing genesis data
  746. func isTransitionsConfigCompatible(c1, c2 *ChainConfig, head *big.Int) (error, *big.Int, *big.Int) {
  747. if len(c1.Transitions) == 0 && len(c2.Transitions) == 0 {
  748. // maxCodeSizeConfig not used. return
  749. return nil, big.NewInt(0), big.NewInt(0)
  750. }
  751. // existing config had Transitions and new one does not have the same return error
  752. if len(c1.Transitions) > 0 && len(c2.Transitions) == 0 {
  753. return fmt.Errorf("genesis file missing transitions information"), head, head
  754. }
  755. if len(c2.Transitions) > 0 && len(c1.Transitions) == 0 {
  756. return nil, big.NewInt(0), big.NewInt(0)
  757. }
  758. // check the number of records below current head in both configs
  759. // if they do not match throw an error
  760. c1RecsBelowHead := 0
  761. for _, data := range c1.Transitions {
  762. if data.Block.Cmp(head) <= 0 {
  763. c1RecsBelowHead++
  764. } else {
  765. break
  766. }
  767. }
  768. c2RecsBelowHead := 0
  769. for _, data := range c2.Transitions {
  770. if data.Block.Cmp(head) <= 0 {
  771. c2RecsBelowHead++
  772. } else {
  773. break
  774. }
  775. }
  776. // if the count of past records is not matching return error
  777. if c1RecsBelowHead != c2RecsBelowHead {
  778. return errors.New("transitions data incompatible. updating transitions for past"), head, head
  779. }
  780. // validate that each past record is matching exactly. if not return error
  781. for i := 0; i < c1RecsBelowHead; i++ {
  782. isSameBlock := c1.Transitions[i].Block.Cmp(c2.Transitions[i].Block) != 0
  783. if isSameBlock || c1.Transitions[i].Algorithm != c2.Transitions[i].Algorithm {
  784. return ErrTransitionIncompatible("Algorithm"), head, head
  785. }
  786. if isSameBlock || c1.Transitions[i].BlockPeriodSeconds != c2.Transitions[i].BlockPeriodSeconds {
  787. return ErrTransitionIncompatible("BlockPeriodSeconds"), head, head
  788. }
  789. if isSameBlock || c1.Transitions[i].RequestTimeoutSeconds != c2.Transitions[i].RequestTimeoutSeconds {
  790. return ErrTransitionIncompatible("RequestTimeoutSeconds"), head, head
  791. }
  792. if isSameBlock || c1.Transitions[i].EpochLength != c2.Transitions[i].EpochLength {
  793. return ErrTransitionIncompatible("EpochLength"), head, head
  794. }
  795. if isSameBlock || c1.Transitions[i].ContractSizeLimit != c2.Transitions[i].ContractSizeLimit {
  796. return ErrTransitionIncompatible("ContractSizeLimit"), head, head
  797. }
  798. if isSameBlock || c1.Transitions[i].ValidatorContractAddress != c2.Transitions[i].ValidatorContractAddress {
  799. return ErrTransitionIncompatible("ValidatorContractAddress"), head, head
  800. }
  801. if isSameBlock || c1.Transitions[i].ValidatorSelectionMode != c2.Transitions[i].ValidatorSelectionMode {
  802. return ErrTransitionIncompatible("ValidatorSelectionMode"), head, head
  803. }
  804. if isSameBlock || c1.Transitions[i].MinerGasLimit != c2.Transitions[i].MinerGasLimit {
  805. return ErrTransitionIncompatible("Miner GasLimit"), head, head
  806. }
  807. if isSameBlock || c1.Transitions[i].TwoFPlusOneEnabled != c2.Transitions[i].TwoFPlusOneEnabled {
  808. return ErrTransitionIncompatible("2FPlus1Enabled"), head, head
  809. }
  810. if isSameBlock || c1.Transitions[i].MinerGasLimit != c2.Transitions[i].MinerGasLimit {
  811. return ErrTransitionIncompatible("TransactionSizeLimit"), head, head
  812. }
  813. }
  814. return nil, big.NewInt(0), big.NewInt(0)
  815. }
  816. // Quorum
  817. //
  818. // IsPrivacyEnhancementsEnabled returns whether num represents a block number after the PrivacyEnhancementsEnabled fork
  819. func (c *ChainConfig) IsPrivacyEnhancementsEnabled(num *big.Int) bool {
  820. isPrivacyEnhancementsEnabled := false
  821. c.GetTransitionValue(num, func(transition Transition) {
  822. if transition.PrivacyEnhancementsEnabled != nil {
  823. isPrivacyEnhancementsEnabled = *transition.PrivacyEnhancementsEnabled
  824. }
  825. })
  826. return isForked(c.PrivacyEnhancementsBlock, num) || isPrivacyEnhancementsEnabled
  827. }
  828. // Quorum
  829. //
  830. // Check whether num represents a block number after the PrivacyPrecompileBlock
  831. func (c *ChainConfig) IsPrivacyPrecompileEnabled(num *big.Int) bool {
  832. isPrivacyPrecompileEnabled := false
  833. c.GetTransitionValue(num, func(transition Transition) {
  834. if transition.PrivacyPrecompileEnabled != nil {
  835. isPrivacyPrecompileEnabled = *transition.PrivacyPrecompileEnabled
  836. }
  837. })
  838. return isForked(c.PrivacyPrecompileBlock, num) || isPrivacyPrecompileEnabled
  839. }
  840. // Quorum
  841. func (c *ChainConfig) GetTransactionSizeLimit(num *big.Int) uint64 {
  842. transactionSizeLimit := uint64(0)
  843. c.GetTransitionValue(num, func(transition Transition) {
  844. transactionSizeLimit = transition.TransactionSizeLimit
  845. })
  846. if transactionSizeLimit == 0 {
  847. transactionSizeLimit = c.TransactionSizeLimit
  848. }
  849. if transactionSizeLimit == 0 {
  850. transactionSizeLimit = 64
  851. }
  852. return transactionSizeLimit
  853. }
  854. // Quorum
  855. //
  856. // Check whether num represents a block number after the EnableGasPriceBlock
  857. func (c *ChainConfig) IsGasPriceEnabled(num *big.Int) bool {
  858. isGasEnabled := false
  859. c.GetTransitionValue(num, func(transition Transition) {
  860. if transition.GasPriceEnabled != nil {
  861. isGasEnabled = *transition.GasPriceEnabled
  862. }
  863. })
  864. return isForked(c.EnableGasPriceBlock, num) || isGasEnabled
  865. }
  866. // CheckCompatible checks whether scheduled fork transitions have been imported
  867. // with a mismatching chain configuration.
  868. func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64, isQuorumEIP155Activated bool) *ConfigCompatError {
  869. bhead := new(big.Int).SetUint64(height)
  870. // check if the maxCodesize data passed is compatible 1st
  871. // this is being handled separately as it can have breaks
  872. // at multiple block heights and cannot be handled with in
  873. // checkCompatible
  874. // compare the maxCodeSize data between the old and new config
  875. err, cBlock, newCfgBlock := isMaxCodeSizeConfigCompatible(c, newcfg, bhead)
  876. if err != nil {
  877. return newCompatError(err.Error(), cBlock, newCfgBlock)
  878. }
  879. // compare the transitions data between the old and new config
  880. err, cBlock, newCfgBlock = isTransitionsConfigCompatible(c, newcfg, bhead)
  881. if err != nil {
  882. return newCompatError(err.Error(), cBlock, newCfgBlock)
  883. }
  884. // Iterate checkCompatible to find the lowest conflict.
  885. var lasterr *ConfigCompatError
  886. for {
  887. err := c.checkCompatible(newcfg, bhead, isQuorumEIP155Activated)
  888. if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
  889. break
  890. }
  891. lasterr = err
  892. bhead.SetUint64(err.RewindTo)
  893. }
  894. return lasterr
  895. }
  896. // CheckConfigForkOrder checks that we don't "skip" any forks, geth isn't pluggable enough
  897. // to guarantee that forks
  898. func (c *ChainConfig) CheckConfigForkOrder() error {
  899. type fork struct {
  900. name string
  901. block *big.Int
  902. optional bool // if true, the fork may be nil and next fork is still allowed
  903. }
  904. var lastFork fork
  905. for _, cur := range []fork{
  906. {name: "homesteadBlock", block: c.HomesteadBlock},
  907. {name: "daoForkBlock", block: c.DAOForkBlock, optional: true},
  908. {name: "eip150Block", block: c.EIP150Block},
  909. {name: "eip155Block", block: c.EIP155Block},
  910. {name: "eip158Block", block: c.EIP158Block},
  911. {name: "byzantiumBlock", block: c.ByzantiumBlock},
  912. {name: "constantinopleBlock", block: c.ConstantinopleBlock},
  913. {name: "petersburgBlock", block: c.PetersburgBlock},
  914. {name: "istanbulBlock", block: c.IstanbulBlock},
  915. {name: "muirGlacierBlock", block: c.MuirGlacierBlock, optional: true},
  916. {name: "berlinBlock", block: c.BerlinBlock},
  917. } {
  918. if lastFork.name != "" {
  919. // Next one must be higher number
  920. if lastFork.block == nil && cur.block != nil {
  921. return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at %v",
  922. lastFork.name, cur.name, cur.block)
  923. }
  924. if lastFork.block != nil && cur.block != nil {
  925. if lastFork.block.Cmp(cur.block) > 0 {
  926. return fmt.Errorf("unsupported fork ordering: %v enabled at %v, but %v enabled at %v",
  927. lastFork.name, lastFork.block, cur.name, cur.block)
  928. }
  929. }
  930. }
  931. // If it was optional and not set, then ignore it
  932. if !cur.optional || cur.block != nil {
  933. lastFork = cur
  934. }
  935. }
  936. return nil
  937. }
  938. func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int, isQuorumEIP155Activated bool) *ConfigCompatError {
  939. if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
  940. return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
  941. }
  942. if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
  943. return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
  944. }
  945. if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
  946. return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
  947. }
  948. if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
  949. return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
  950. }
  951. if isQuorumEIP155Activated && c.ChainID != nil && isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
  952. return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
  953. }
  954. if isQuorumEIP155Activated && c.ChainID != nil && c.IsEIP155(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
  955. return newCompatError("EIP155 chain ID", c.ChainID, newcfg.ChainID)
  956. }
  957. if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
  958. return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
  959. }
  960. if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
  961. return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
  962. }
  963. if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
  964. return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
  965. }
  966. if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) {
  967. return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock)
  968. }
  969. if isForkIncompatible(c.PetersburgBlock, newcfg.PetersburgBlock, head) {
  970. // the only case where we allow Petersburg to be set in the past is if it is equal to Constantinople
  971. // mainly to satisfy fork ordering requirements which state that Petersburg fork be set if Constantinople fork is set
  972. if isForkIncompatible(c.ConstantinopleBlock, newcfg.PetersburgBlock, head) {
  973. return newCompatError("Petersburg fork block", c.PetersburgBlock, newcfg.PetersburgBlock)
  974. }
  975. }
  976. if isForkIncompatible(c.IstanbulBlock, newcfg.IstanbulBlock, head) {
  977. return newCompatError("Istanbul fork block", c.IstanbulBlock, newcfg.IstanbulBlock)
  978. }
  979. if isForkIncompatible(c.MuirGlacierBlock, newcfg.MuirGlacierBlock, head) {
  980. return newCompatError("Muir Glacier fork block", c.MuirGlacierBlock, newcfg.MuirGlacierBlock)
  981. }
  982. if isForkIncompatible(c.BerlinBlock, newcfg.BerlinBlock, head) {
  983. return newCompatError("Berlin fork block", c.BerlinBlock, newcfg.BerlinBlock)
  984. }
  985. if isForkIncompatible(c.YoloV3Block, newcfg.YoloV3Block, head) {
  986. return newCompatError("YOLOv3 fork block", c.YoloV3Block, newcfg.YoloV3Block)
  987. }
  988. if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) {
  989. return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock)
  990. }
  991. if c.Istanbul != nil && newcfg.Istanbul != nil && isForkIncompatible(c.Istanbul.Ceil2Nby3Block, newcfg.Istanbul.Ceil2Nby3Block, head) {
  992. return newCompatError("Ceil 2N/3 fork block", c.Istanbul.Ceil2Nby3Block, newcfg.Istanbul.Ceil2Nby3Block)
  993. }
  994. if c.Istanbul != nil && newcfg.Istanbul != nil && isForkIncompatible(c.Istanbul.TestQBFTBlock, newcfg.Istanbul.TestQBFTBlock, head) {
  995. return newCompatError("Test QBFT fork block", c.Istanbul.TestQBFTBlock, newcfg.Istanbul.TestQBFTBlock)
  996. }
  997. if isForkIncompatible(c.QIP714Block, newcfg.QIP714Block, head) {
  998. return newCompatError("permissions fork block", c.QIP714Block, newcfg.QIP714Block)
  999. }
  1000. if newcfg.MaxCodeSizeChangeBlock != nil && isForkIncompatible(c.MaxCodeSizeChangeBlock, newcfg.MaxCodeSizeChangeBlock, head) {
  1001. return newCompatError("max code size change fork block", c.MaxCodeSizeChangeBlock, newcfg.MaxCodeSizeChangeBlock)
  1002. }
  1003. if isForkIncompatible(c.PrivacyEnhancementsBlock, newcfg.PrivacyEnhancementsBlock, head) {
  1004. return newCompatError("Privacy Enhancements fork block", c.PrivacyEnhancementsBlock, newcfg.PrivacyEnhancementsBlock)
  1005. }
  1006. if isForkIncompatible(c.PrivacyPrecompileBlock, newcfg.PrivacyPrecompileBlock, head) {
  1007. return newCompatError("Privacy Precompile fork block", c.PrivacyPrecompileBlock, newcfg.PrivacyPrecompileBlock)
  1008. }
  1009. return nil
  1010. }
  1011. // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
  1012. // block s2 because head is already past the fork.
  1013. func isForkIncompatible(s1, s2, head *big.Int) bool {
  1014. return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
  1015. }
  1016. // isForked returns whether a fork scheduled at block s is active at the given head block.
  1017. func isForked(s, head *big.Int) bool {
  1018. if s == nil || head == nil {
  1019. return false
  1020. }
  1021. return s.Cmp(head) <= 0
  1022. }
  1023. func configNumEqual(x, y *big.Int) bool {
  1024. if x == nil {
  1025. return y == nil
  1026. }
  1027. if y == nil {
  1028. return x == nil
  1029. }
  1030. return x.Cmp(y) == 0
  1031. }
  1032. // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
  1033. // ChainConfig that would alter the past.
  1034. type ConfigCompatError struct {
  1035. What string
  1036. // block numbers of the stored and new configurations
  1037. StoredConfig, NewConfig *big.Int
  1038. // the block number to which the local chain must be rewound to correct the error
  1039. RewindTo uint64
  1040. }
  1041. func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
  1042. var rew *big.Int
  1043. switch {
  1044. case storedblock == nil:
  1045. rew = newblock
  1046. case newblock == nil || storedblock.Cmp(newblock) < 0:
  1047. rew = storedblock
  1048. default:
  1049. rew = newblock
  1050. }
  1051. err := &ConfigCompatError{what, storedblock, newblock, 0}
  1052. if rew != nil && rew.Sign() > 0 {
  1053. err.RewindTo = rew.Uint64() - 1
  1054. }
  1055. return err
  1056. }
  1057. func (err *ConfigCompatError) Error() string {
  1058. return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
  1059. }
  1060. // Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
  1061. // that do not have or require information about the block.
  1062. //
  1063. // Rules is a one time interface meaning that it shouldn't be used in between transition
  1064. // phases.
  1065. type Rules struct {
  1066. ChainID *big.Int
  1067. IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
  1068. IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
  1069. IsBerlin, IsCatalyst bool
  1070. // Quorum
  1071. IsPrivacyEnhancementsEnabled bool
  1072. IsPrivacyPrecompile bool
  1073. IsGasPriceEnabled bool
  1074. }
  1075. // Rules ensures c's ChainID is not nil.
  1076. func (c *ChainConfig) Rules(num *big.Int) Rules {
  1077. chainID := c.ChainID
  1078. if chainID == nil {
  1079. chainID = new(big.Int)
  1080. }
  1081. return Rules{
  1082. ChainID: new(big.Int).Set(chainID),
  1083. IsHomestead: c.IsHomestead(num),
  1084. IsEIP150: c.IsEIP150(num),
  1085. IsEIP155: c.IsEIP155(num),
  1086. IsEIP158: c.IsEIP158(num),
  1087. IsByzantium: c.IsByzantium(num),
  1088. IsConstantinople: c.IsConstantinople(num),
  1089. IsPetersburg: c.IsPetersburg(num),
  1090. IsIstanbul: c.IsIstanbul(num),
  1091. IsBerlin: c.IsBerlin(num),
  1092. IsCatalyst: c.IsCatalyst(num),
  1093. // Quorum
  1094. IsPrivacyEnhancementsEnabled: c.IsPrivacyEnhancementsEnabled(num),
  1095. IsPrivacyPrecompile: c.IsPrivacyPrecompileEnabled(num),
  1096. IsGasPriceEnabled: c.IsGasPriceEnabled(num),
  1097. }
  1098. }