config.go 24 KB

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