config.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. "fmt"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. )
  24. // Genesis hashes to enforce below configs on.
  25. var (
  26. MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
  27. RopstenGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")
  28. RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177")
  29. GoerliGenesisHash = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a")
  30. // TODO: update with yolov2 values
  31. YoloV2GenesisHash = common.HexToHash("0x498a7239036dd2cd09e2bb8a80922b78632017958c332b42044c250d603a8a3e")
  32. )
  33. // TrustedCheckpoints associates each known checkpoint with the genesis hash of
  34. // the chain it belongs to.
  35. var TrustedCheckpoints = map[common.Hash]*TrustedCheckpoint{
  36. MainnetGenesisHash: MainnetTrustedCheckpoint,
  37. RopstenGenesisHash: RopstenTrustedCheckpoint,
  38. RinkebyGenesisHash: RinkebyTrustedCheckpoint,
  39. GoerliGenesisHash: GoerliTrustedCheckpoint,
  40. }
  41. // CheckpointOracles associates each known checkpoint oracles with the genesis hash of
  42. // the chain it belongs to.
  43. var CheckpointOracles = map[common.Hash]*CheckpointOracleConfig{
  44. MainnetGenesisHash: MainnetCheckpointOracle,
  45. RopstenGenesisHash: RopstenCheckpointOracle,
  46. RinkebyGenesisHash: RinkebyCheckpointOracle,
  47. GoerliGenesisHash: GoerliCheckpointOracle,
  48. }
  49. var (
  50. // MainnetChainConfig is the chain parameters to run a node on the main network.
  51. MainnetChainConfig = &ChainConfig{
  52. ChainID: big.NewInt(1),
  53. HomesteadBlock: big.NewInt(1150000),
  54. DAOForkBlock: big.NewInt(1920000),
  55. DAOForkSupport: true,
  56. EIP150Block: big.NewInt(2463000),
  57. EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
  58. EIP155Block: big.NewInt(2675000),
  59. EIP158Block: big.NewInt(2675000),
  60. ByzantiumBlock: big.NewInt(4370000),
  61. ConstantinopleBlock: big.NewInt(7280000),
  62. PetersburgBlock: big.NewInt(7280000),
  63. IstanbulBlock: big.NewInt(9069000),
  64. MuirGlacierBlock: big.NewInt(9200000),
  65. Ethash: new(EthashConfig),
  66. }
  67. // MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network.
  68. MainnetTrustedCheckpoint = &TrustedCheckpoint{
  69. SectionIndex: 345,
  70. SectionHead: common.HexToHash("0x5453bab878704adebc934b41fd214a07ea7a72b8572ff088dca7f7956cd0ef28"),
  71. CHTRoot: common.HexToHash("0x7693d432595846c094f47cb37f5c868b0b7b1968fc6b0fc411ded1345fdaffab"),
  72. BloomRoot: common.HexToHash("0x8b0e7895bc39840d8dac857e26bdf3d0a07684b0b962b252546659e0337a9f70"),
  73. }
  74. // MainnetCheckpointOracle contains a set of configs for the main network oracle.
  75. MainnetCheckpointOracle = &CheckpointOracleConfig{
  76. Address: common.HexToAddress("0x9a9070028361F7AAbeB3f2F2Dc07F82C4a98A02a"),
  77. Signers: []common.Address{
  78. common.HexToAddress("0x1b2C260efc720BE89101890E4Db589b44E950527"), // Peter
  79. common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
  80. common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
  81. common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
  82. common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
  83. },
  84. Threshold: 2,
  85. }
  86. // RopstenChainConfig contains the chain parameters to run a node on the Ropsten test network.
  87. RopstenChainConfig = &ChainConfig{
  88. ChainID: big.NewInt(3),
  89. HomesteadBlock: big.NewInt(0),
  90. DAOForkBlock: nil,
  91. DAOForkSupport: true,
  92. EIP150Block: big.NewInt(0),
  93. EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
  94. EIP155Block: big.NewInt(10),
  95. EIP158Block: big.NewInt(10),
  96. ByzantiumBlock: big.NewInt(1700000),
  97. ConstantinopleBlock: big.NewInt(4230000),
  98. PetersburgBlock: big.NewInt(4939394),
  99. IstanbulBlock: big.NewInt(6485846),
  100. MuirGlacierBlock: big.NewInt(7117117),
  101. Ethash: new(EthashConfig),
  102. }
  103. // RopstenTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network.
  104. RopstenTrustedCheckpoint = &TrustedCheckpoint{
  105. SectionIndex: 279,
  106. SectionHead: common.HexToHash("0x4a4912848d4c06090097073357c10015d11c6f4544a0f93cbdd584701c3b7d58"),
  107. CHTRoot: common.HexToHash("0x9053b7867ae921e80a4e2f5a4b15212e4af3d691ca712fb33dc150e9c6ea221c"),
  108. BloomRoot: common.HexToHash("0x3dc04cb1be7ddc271f3f83469b47b76184a79d7209ef51d85b1539ea6d25a645"),
  109. }
  110. // RopstenCheckpointOracle contains a set of configs for the Ropsten test network oracle.
  111. RopstenCheckpointOracle = &CheckpointOracleConfig{
  112. Address: common.HexToAddress("0xEF79475013f154E6A65b54cB2742867791bf0B84"),
  113. Signers: []common.Address{
  114. common.HexToAddress("0x32162F3581E88a5f62e8A61892B42C46E2c18f7b"), // Peter
  115. common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
  116. common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
  117. common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
  118. common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
  119. },
  120. Threshold: 2,
  121. }
  122. // RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
  123. RinkebyChainConfig = &ChainConfig{
  124. ChainID: big.NewInt(4),
  125. HomesteadBlock: big.NewInt(1),
  126. DAOForkBlock: nil,
  127. DAOForkSupport: true,
  128. EIP150Block: big.NewInt(2),
  129. EIP150Hash: common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
  130. EIP155Block: big.NewInt(3),
  131. EIP158Block: big.NewInt(3),
  132. ByzantiumBlock: big.NewInt(1035301),
  133. ConstantinopleBlock: big.NewInt(3660663),
  134. PetersburgBlock: big.NewInt(4321234),
  135. IstanbulBlock: big.NewInt(5435345),
  136. MuirGlacierBlock: nil,
  137. Clique: &CliqueConfig{
  138. Period: 15,
  139. Epoch: 30000,
  140. },
  141. }
  142. // RinkebyTrustedCheckpoint contains the light client trusted checkpoint for the Rinkeby test network.
  143. RinkebyTrustedCheckpoint = &TrustedCheckpoint{
  144. SectionIndex: 232,
  145. SectionHead: common.HexToHash("0x8170fca4039b11a008c11f9996ff112151cbb17411437bb2f86288e11158b2f0"),
  146. CHTRoot: common.HexToHash("0x4526560d92ae1b3a6d3ee780c3ad289ba2bbf1b5da58d9ea107f2f26412b631f"),
  147. BloomRoot: common.HexToHash("0x82a889098a35d6a21ea8894d35a1db69b94bad61b988bbe5ae4601437320e331"),
  148. }
  149. // RinkebyCheckpointOracle contains a set of configs for the Rinkeby test network oracle.
  150. RinkebyCheckpointOracle = &CheckpointOracleConfig{
  151. Address: common.HexToAddress("0xebe8eFA441B9302A0d7eaECc277c09d20D684540"),
  152. Signers: []common.Address{
  153. common.HexToAddress("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3"), // Peter
  154. common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
  155. common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
  156. common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
  157. },
  158. Threshold: 2,
  159. }
  160. // GoerliChainConfig contains the chain parameters to run a node on the Görli test network.
  161. GoerliChainConfig = &ChainConfig{
  162. ChainID: big.NewInt(5),
  163. HomesteadBlock: big.NewInt(0),
  164. DAOForkBlock: nil,
  165. DAOForkSupport: true,
  166. EIP150Block: big.NewInt(0),
  167. EIP155Block: big.NewInt(0),
  168. EIP158Block: big.NewInt(0),
  169. ByzantiumBlock: big.NewInt(0),
  170. ConstantinopleBlock: big.NewInt(0),
  171. PetersburgBlock: big.NewInt(0),
  172. IstanbulBlock: big.NewInt(1561651),
  173. MuirGlacierBlock: nil,
  174. Clique: &CliqueConfig{
  175. Period: 15,
  176. Epoch: 30000,
  177. },
  178. }
  179. // GoerliTrustedCheckpoint contains the light client trusted checkpoint for the Görli test network.
  180. GoerliTrustedCheckpoint = &TrustedCheckpoint{
  181. SectionIndex: 116,
  182. SectionHead: common.HexToHash("0xf2d200f636f213c9c7bb4e747ff564813da7708253037103aef3d8be5203c5e1"),
  183. CHTRoot: common.HexToHash("0xb0ac83e2ccf6c2776945e099c4e3df50fe6200499c8b2045c34cafdf57d15087"),
  184. BloomRoot: common.HexToHash("0xfb580ad1c611230a4bfc56534f58bcb156d028bc6ce70e35403dc019c7c02d90"),
  185. }
  186. // GoerliCheckpointOracle contains a set of configs for the Goerli test network oracle.
  187. GoerliCheckpointOracle = &CheckpointOracleConfig{
  188. Address: common.HexToAddress("0x18CA0E045F0D772a851BC7e48357Bcaab0a0795D"),
  189. Signers: []common.Address{
  190. common.HexToAddress("0x4769bcaD07e3b938B7f43EB7D278Bc7Cb9efFb38"), // Peter
  191. common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
  192. common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
  193. common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
  194. common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
  195. },
  196. Threshold: 2,
  197. }
  198. // YoloV2ChainConfig contains the chain parameters to run a node on the YOLOv2 test network.
  199. YoloV2ChainConfig = &ChainConfig{
  200. ChainID: big.NewInt(133519467574834),
  201. HomesteadBlock: big.NewInt(0),
  202. DAOForkBlock: nil,
  203. DAOForkSupport: true,
  204. EIP150Block: big.NewInt(0),
  205. EIP155Block: big.NewInt(0),
  206. EIP158Block: big.NewInt(0),
  207. ByzantiumBlock: big.NewInt(0),
  208. ConstantinopleBlock: big.NewInt(0),
  209. PetersburgBlock: big.NewInt(0),
  210. IstanbulBlock: big.NewInt(0),
  211. MuirGlacierBlock: nil,
  212. YoloV2Block: big.NewInt(0),
  213. Clique: &CliqueConfig{
  214. Period: 15,
  215. Epoch: 30000,
  216. },
  217. }
  218. // AllEthashProtocolChanges contains every protocol change (EIPs) introduced
  219. // and accepted by the Ethereum core developers into the Ethash consensus.
  220. //
  221. // This configuration is intentionally not using keyed fields to force anyone
  222. // adding flags to the config to also have to set these fields.
  223. 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), nil, nil, nil, new(EthashConfig), nil}
  224. // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
  225. // and accepted by the Ethereum core developers into the Clique consensus.
  226. //
  227. // This configuration is intentionally not using keyed fields to force anyone
  228. // adding flags to the config to also have to set these fields.
  229. AllCliqueProtocolChanges = &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), nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}}
  230. TestChainConfig = &ChainConfig{big.NewInt(1), 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, new(EthashConfig), nil}
  231. TestRules = TestChainConfig.Rules(new(big.Int))
  232. )
  233. // TrustedCheckpoint represents a set of post-processed trie roots (CHT and
  234. // BloomTrie) associated with the appropriate section index and head hash. It is
  235. // used to start light syncing from this checkpoint and avoid downloading the
  236. // entire header chain while still being able to securely access old headers/logs.
  237. type TrustedCheckpoint struct {
  238. SectionIndex uint64 `json:"sectionIndex"`
  239. SectionHead common.Hash `json:"sectionHead"`
  240. CHTRoot common.Hash `json:"chtRoot"`
  241. BloomRoot common.Hash `json:"bloomRoot"`
  242. }
  243. // HashEqual returns an indicator comparing the itself hash with given one.
  244. func (c *TrustedCheckpoint) HashEqual(hash common.Hash) bool {
  245. if c.Empty() {
  246. return hash == common.Hash{}
  247. }
  248. return c.Hash() == hash
  249. }
  250. // Hash returns the hash of checkpoint's four key fields(index, sectionHead, chtRoot and bloomTrieRoot).
  251. func (c *TrustedCheckpoint) Hash() common.Hash {
  252. buf := make([]byte, 8+3*common.HashLength)
  253. binary.BigEndian.PutUint64(buf, c.SectionIndex)
  254. copy(buf[8:], c.SectionHead.Bytes())
  255. copy(buf[8+common.HashLength:], c.CHTRoot.Bytes())
  256. copy(buf[8+2*common.HashLength:], c.BloomRoot.Bytes())
  257. return crypto.Keccak256Hash(buf)
  258. }
  259. // Empty returns an indicator whether the checkpoint is regarded as empty.
  260. func (c *TrustedCheckpoint) Empty() bool {
  261. return c.SectionHead == (common.Hash{}) || c.CHTRoot == (common.Hash{}) || c.BloomRoot == (common.Hash{})
  262. }
  263. // CheckpointOracleConfig represents a set of checkpoint contract(which acts as an oracle)
  264. // config which used for light client checkpoint syncing.
  265. type CheckpointOracleConfig struct {
  266. Address common.Address `json:"address"`
  267. Signers []common.Address `json:"signers"`
  268. Threshold uint64 `json:"threshold"`
  269. }
  270. // ChainConfig is the core config which determines the blockchain settings.
  271. //
  272. // ChainConfig is stored in the database on a per block basis. This means
  273. // that any network, identified by its genesis block, can have its own
  274. // set of configuration options.
  275. type ChainConfig struct {
  276. ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
  277. HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)
  278. DAOForkBlock *big.Int `json:"daoForkBlock,omitempty"` // TheDAO hard-fork switch block (nil = no fork)
  279. DAOForkSupport bool `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
  280. // EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
  281. EIP150Block *big.Int `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork)
  282. EIP150Hash common.Hash `json:"eip150Hash,omitempty"` // EIP150 HF hash (needed for header only clients as only gas pricing changed)
  283. EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
  284. EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block
  285. ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium)
  286. ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
  287. PetersburgBlock *big.Int `json:"petersburgBlock,omitempty"` // Petersburg switch block (nil = same as Constantinople)
  288. IstanbulBlock *big.Int `json:"istanbulBlock,omitempty"` // Istanbul switch block (nil = no fork, 0 = already on istanbul)
  289. MuirGlacierBlock *big.Int `json:"muirGlacierBlock,omitempty"` // Eip-2384 (bomb delay) switch block (nil = no fork, 0 = already activated)
  290. YoloV2Block *big.Int `json:"yoloV2Block,omitempty"` // YOLO v2: Gas repricings TODO @holiman add EIP references
  291. EWASMBlock *big.Int `json:"ewasmBlock,omitempty"` // EWASM switch block (nil = no fork, 0 = already activated)
  292. // Various consensus engines
  293. Ethash *EthashConfig `json:"ethash,omitempty"`
  294. Clique *CliqueConfig `json:"clique,omitempty"`
  295. }
  296. // EthashConfig is the consensus engine configs for proof-of-work based sealing.
  297. type EthashConfig struct{}
  298. // String implements the stringer interface, returning the consensus engine details.
  299. func (c *EthashConfig) String() string {
  300. return "ethash"
  301. }
  302. // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
  303. type CliqueConfig struct {
  304. Period uint64 `json:"period"` // Number of seconds between blocks to enforce
  305. Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
  306. }
  307. // String implements the stringer interface, returning the consensus engine details.
  308. func (c *CliqueConfig) String() string {
  309. return "clique"
  310. }
  311. // String implements the fmt.Stringer interface.
  312. func (c *ChainConfig) String() string {
  313. var engine interface{}
  314. switch {
  315. case c.Ethash != nil:
  316. engine = c.Ethash
  317. case c.Clique != nil:
  318. engine = c.Clique
  319. default:
  320. engine = "unknown"
  321. }
  322. return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Petersburg: %v Istanbul: %v, Muir Glacier: %v, YOLO v2: %v, Engine: %v}",
  323. c.ChainID,
  324. c.HomesteadBlock,
  325. c.DAOForkBlock,
  326. c.DAOForkSupport,
  327. c.EIP150Block,
  328. c.EIP155Block,
  329. c.EIP158Block,
  330. c.ByzantiumBlock,
  331. c.ConstantinopleBlock,
  332. c.PetersburgBlock,
  333. c.IstanbulBlock,
  334. c.MuirGlacierBlock,
  335. c.YoloV2Block,
  336. engine,
  337. )
  338. }
  339. // IsHomestead returns whether num is either equal to the homestead block or greater.
  340. func (c *ChainConfig) IsHomestead(num *big.Int) bool {
  341. return isForked(c.HomesteadBlock, num)
  342. }
  343. // IsDAOFork returns whether num is either equal to the DAO fork block or greater.
  344. func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
  345. return isForked(c.DAOForkBlock, num)
  346. }
  347. // IsEIP150 returns whether num is either equal to the EIP150 fork block or greater.
  348. func (c *ChainConfig) IsEIP150(num *big.Int) bool {
  349. return isForked(c.EIP150Block, num)
  350. }
  351. // IsEIP155 returns whether num is either equal to the EIP155 fork block or greater.
  352. func (c *ChainConfig) IsEIP155(num *big.Int) bool {
  353. return isForked(c.EIP155Block, num)
  354. }
  355. // IsEIP158 returns whether num is either equal to the EIP158 fork block or greater.
  356. func (c *ChainConfig) IsEIP158(num *big.Int) bool {
  357. return isForked(c.EIP158Block, num)
  358. }
  359. // IsByzantium returns whether num is either equal to the Byzantium fork block or greater.
  360. func (c *ChainConfig) IsByzantium(num *big.Int) bool {
  361. return isForked(c.ByzantiumBlock, num)
  362. }
  363. // IsConstantinople returns whether num is either equal to the Constantinople fork block or greater.
  364. func (c *ChainConfig) IsConstantinople(num *big.Int) bool {
  365. return isForked(c.ConstantinopleBlock, num)
  366. }
  367. // IsMuirGlacier returns whether num is either equal to the Muir Glacier (EIP-2384) fork block or greater.
  368. func (c *ChainConfig) IsMuirGlacier(num *big.Int) bool {
  369. return isForked(c.MuirGlacierBlock, num)
  370. }
  371. // IsPetersburg returns whether num is either
  372. // - equal to or greater than the PetersburgBlock fork block,
  373. // - OR is nil, and Constantinople is active
  374. func (c *ChainConfig) IsPetersburg(num *big.Int) bool {
  375. return isForked(c.PetersburgBlock, num) || c.PetersburgBlock == nil && isForked(c.ConstantinopleBlock, num)
  376. }
  377. // IsIstanbul returns whether num is either equal to the Istanbul fork block or greater.
  378. func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
  379. return isForked(c.IstanbulBlock, num)
  380. }
  381. // IsYoloV2 returns whether num is either equal to the YoloV1 fork block or greater.
  382. func (c *ChainConfig) IsYoloV2(num *big.Int) bool {
  383. return isForked(c.YoloV2Block, num)
  384. }
  385. // IsEWASM returns whether num represents a block number after the EWASM fork
  386. func (c *ChainConfig) IsEWASM(num *big.Int) bool {
  387. return isForked(c.EWASMBlock, num)
  388. }
  389. // CheckCompatible checks whether scheduled fork transitions have been imported
  390. // with a mismatching chain configuration.
  391. func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
  392. bhead := new(big.Int).SetUint64(height)
  393. // Iterate checkCompatible to find the lowest conflict.
  394. var lasterr *ConfigCompatError
  395. for {
  396. err := c.checkCompatible(newcfg, bhead)
  397. if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
  398. break
  399. }
  400. lasterr = err
  401. bhead.SetUint64(err.RewindTo)
  402. }
  403. return lasterr
  404. }
  405. // CheckConfigForkOrder checks that we don't "skip" any forks, geth isn't pluggable enough
  406. // to guarantee that forks can be implemented in a different order than on official networks
  407. func (c *ChainConfig) CheckConfigForkOrder() error {
  408. type fork struct {
  409. name string
  410. block *big.Int
  411. optional bool // if true, the fork may be nil and next fork is still allowed
  412. }
  413. var lastFork fork
  414. for _, cur := range []fork{
  415. {name: "homesteadBlock", block: c.HomesteadBlock},
  416. {name: "daoForkBlock", block: c.DAOForkBlock, optional: true},
  417. {name: "eip150Block", block: c.EIP150Block},
  418. {name: "eip155Block", block: c.EIP155Block},
  419. {name: "eip158Block", block: c.EIP158Block},
  420. {name: "byzantiumBlock", block: c.ByzantiumBlock},
  421. {name: "constantinopleBlock", block: c.ConstantinopleBlock},
  422. {name: "petersburgBlock", block: c.PetersburgBlock},
  423. {name: "istanbulBlock", block: c.IstanbulBlock},
  424. {name: "muirGlacierBlock", block: c.MuirGlacierBlock, optional: true},
  425. {name: "yoloV2Block", block: c.YoloV2Block},
  426. } {
  427. if lastFork.name != "" {
  428. // Next one must be higher number
  429. if lastFork.block == nil && cur.block != nil {
  430. return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at %v",
  431. lastFork.name, cur.name, cur.block)
  432. }
  433. if lastFork.block != nil && cur.block != nil {
  434. if lastFork.block.Cmp(cur.block) > 0 {
  435. return fmt.Errorf("unsupported fork ordering: %v enabled at %v, but %v enabled at %v",
  436. lastFork.name, lastFork.block, cur.name, cur.block)
  437. }
  438. }
  439. }
  440. // If it was optional and not set, then ignore it
  441. if !cur.optional || cur.block != nil {
  442. lastFork = cur
  443. }
  444. }
  445. return nil
  446. }
  447. func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
  448. if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
  449. return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
  450. }
  451. if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
  452. return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
  453. }
  454. if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
  455. return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
  456. }
  457. if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
  458. return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
  459. }
  460. if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
  461. return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
  462. }
  463. if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
  464. return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
  465. }
  466. if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
  467. return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
  468. }
  469. if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
  470. return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
  471. }
  472. if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) {
  473. return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock)
  474. }
  475. if isForkIncompatible(c.PetersburgBlock, newcfg.PetersburgBlock, head) {
  476. // the only case where we allow Petersburg to be set in the past is if it is equal to Constantinople
  477. // mainly to satisfy fork ordering requirements which state that Petersburg fork be set if Constantinople fork is set
  478. if isForkIncompatible(c.ConstantinopleBlock, newcfg.PetersburgBlock, head) {
  479. return newCompatError("Petersburg fork block", c.PetersburgBlock, newcfg.PetersburgBlock)
  480. }
  481. }
  482. if isForkIncompatible(c.IstanbulBlock, newcfg.IstanbulBlock, head) {
  483. return newCompatError("Istanbul fork block", c.IstanbulBlock, newcfg.IstanbulBlock)
  484. }
  485. if isForkIncompatible(c.MuirGlacierBlock, newcfg.MuirGlacierBlock, head) {
  486. return newCompatError("Muir Glacier fork block", c.MuirGlacierBlock, newcfg.MuirGlacierBlock)
  487. }
  488. if isForkIncompatible(c.YoloV2Block, newcfg.YoloV2Block, head) {
  489. return newCompatError("YOLOv2 fork block", c.YoloV2Block, newcfg.YoloV2Block)
  490. }
  491. if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) {
  492. return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock)
  493. }
  494. return nil
  495. }
  496. // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
  497. // block s2 because head is already past the fork.
  498. func isForkIncompatible(s1, s2, head *big.Int) bool {
  499. return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
  500. }
  501. // isForked returns whether a fork scheduled at block s is active at the given head block.
  502. func isForked(s, head *big.Int) bool {
  503. if s == nil || head == nil {
  504. return false
  505. }
  506. return s.Cmp(head) <= 0
  507. }
  508. func configNumEqual(x, y *big.Int) bool {
  509. if x == nil {
  510. return y == nil
  511. }
  512. if y == nil {
  513. return x == nil
  514. }
  515. return x.Cmp(y) == 0
  516. }
  517. // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
  518. // ChainConfig that would alter the past.
  519. type ConfigCompatError struct {
  520. What string
  521. // block numbers of the stored and new configurations
  522. StoredConfig, NewConfig *big.Int
  523. // the block number to which the local chain must be rewound to correct the error
  524. RewindTo uint64
  525. }
  526. func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
  527. var rew *big.Int
  528. switch {
  529. case storedblock == nil:
  530. rew = newblock
  531. case newblock == nil || storedblock.Cmp(newblock) < 0:
  532. rew = storedblock
  533. default:
  534. rew = newblock
  535. }
  536. err := &ConfigCompatError{what, storedblock, newblock, 0}
  537. if rew != nil && rew.Sign() > 0 {
  538. err.RewindTo = rew.Uint64() - 1
  539. }
  540. return err
  541. }
  542. func (err *ConfigCompatError) Error() string {
  543. return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
  544. }
  545. // Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
  546. // that do not have or require information about the block.
  547. //
  548. // Rules is a one time interface meaning that it shouldn't be used in between transition
  549. // phases.
  550. type Rules struct {
  551. ChainID *big.Int
  552. IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
  553. IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
  554. IsYoloV2 bool
  555. }
  556. // Rules ensures c's ChainID is not nil.
  557. func (c *ChainConfig) Rules(num *big.Int) Rules {
  558. chainID := c.ChainID
  559. if chainID == nil {
  560. chainID = new(big.Int)
  561. }
  562. return Rules{
  563. ChainID: new(big.Int).Set(chainID),
  564. IsHomestead: c.IsHomestead(num),
  565. IsEIP150: c.IsEIP150(num),
  566. IsEIP155: c.IsEIP155(num),
  567. IsEIP158: c.IsEIP158(num),
  568. IsByzantium: c.IsByzantium(num),
  569. IsConstantinople: c.IsConstantinople(num),
  570. IsPetersburg: c.IsPetersburg(num),
  571. IsIstanbul: c.IsIstanbul(num),
  572. IsYoloV2: c.IsYoloV2(num),
  573. }
  574. }