config.go 24 KB

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