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