config.go 27 KB

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