github.com/nikandfor/go-ethereum@v1.8.27/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 "fmt" 21 "math/big" 22 23 "github.com/ethereum/go-ethereum/common" 24 ) 25 26 // Genesis hashes to enforce below configs on. 27 var ( 28 MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3") 29 TestnetGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d") 30 RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177") 31 GoerliGenesisHash = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a") 32 ) 33 34 // TrustedCheckpoints associates each known checkpoint with the genesis hash of 35 // the chain it belongs to. 36 var TrustedCheckpoints = map[common.Hash]*TrustedCheckpoint{ 37 MainnetGenesisHash: MainnetTrustedCheckpoint, 38 TestnetGenesisHash: TestnetTrustedCheckpoint, 39 RinkebyGenesisHash: RinkebyTrustedCheckpoint, 40 GoerliGenesisHash: GoerliTrustedCheckpoint, 41 } 42 43 var ( 44 // MainnetChainConfig is the chain parameters to run a node on the main network. 45 MainnetChainConfig = &ChainConfig{ 46 ChainID: big.NewInt(1), 47 HomesteadBlock: big.NewInt(1150000), 48 DAOForkBlock: big.NewInt(1920000), 49 DAOForkSupport: true, 50 EIP150Block: big.NewInt(2463000), 51 EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"), 52 EIP155Block: big.NewInt(2675000), 53 EIP158Block: big.NewInt(2675000), 54 ByzantiumBlock: big.NewInt(4370000), 55 ConstantinopleBlock: big.NewInt(7280000), 56 PetersburgBlock: big.NewInt(7280000), 57 Ethash: new(EthashConfig), 58 } 59 60 // MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network. 61 MainnetTrustedCheckpoint = &TrustedCheckpoint{ 62 Name: "mainnet", 63 SectionIndex: 227, 64 SectionHead: common.HexToHash("0xa2e0b25d72c2fc6e35a7f853cdacb193b4b4f95c606accf7f8fa8415283582c7"), 65 CHTRoot: common.HexToHash("0xf69bdd4053b95b61a27b106a0e86103d791edd8574950dc96aa351ab9b9f1aa0"), 66 BloomRoot: common.HexToHash("0xec1b454d4c6322c78ccedf76ac922a8698c3cac4d98748a84af4995b7bd3d744"), 67 } 68 69 // TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network. 70 TestnetChainConfig = &ChainConfig{ 71 ChainID: big.NewInt(3), 72 HomesteadBlock: big.NewInt(0), 73 DAOForkBlock: nil, 74 DAOForkSupport: true, 75 EIP150Block: big.NewInt(0), 76 EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"), 77 EIP155Block: big.NewInt(10), 78 EIP158Block: big.NewInt(10), 79 ByzantiumBlock: big.NewInt(1700000), 80 ConstantinopleBlock: big.NewInt(4230000), 81 PetersburgBlock: big.NewInt(4939394), 82 Ethash: new(EthashConfig), 83 } 84 85 // TestnetTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network. 86 TestnetTrustedCheckpoint = &TrustedCheckpoint{ 87 Name: "testnet", 88 SectionIndex: 161, 89 SectionHead: common.HexToHash("0x5378afa734e1feafb34bcca1534c4d96952b754579b96a4afb23d5301ecececc"), 90 CHTRoot: common.HexToHash("0x1cf2b071e7443a62914362486b613ff30f60cea0d9c268ed8c545f876a3ee60c"), 91 BloomRoot: common.HexToHash("0x5ac25c84bd18a9cbe878d4609a80220f57f85037a112644532412ba0d498a31b"), 92 } 93 94 // RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network. 95 RinkebyChainConfig = &ChainConfig{ 96 ChainID: big.NewInt(4), 97 HomesteadBlock: big.NewInt(1), 98 DAOForkBlock: nil, 99 DAOForkSupport: true, 100 EIP150Block: big.NewInt(2), 101 EIP150Hash: common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"), 102 EIP155Block: big.NewInt(3), 103 EIP158Block: big.NewInt(3), 104 ByzantiumBlock: big.NewInt(1035301), 105 ConstantinopleBlock: big.NewInt(3660663), 106 PetersburgBlock: big.NewInt(4321234), 107 Clique: &CliqueConfig{ 108 Period: 15, 109 Epoch: 30000, 110 }, 111 } 112 113 // RinkebyTrustedCheckpoint contains the light client trusted checkpoint for the Rinkeby test network. 114 RinkebyTrustedCheckpoint = &TrustedCheckpoint{ 115 Name: "rinkeby", 116 SectionIndex: 125, 117 SectionHead: common.HexToHash("0x8a738386f6bb34add15846f8f49c4c519a2f32519096e792b9f43bcb407c831c"), 118 CHTRoot: common.HexToHash("0xa1e5720a9bad4dce794f129e4ac6744398197b652868011486a6f89c8ec84a75"), 119 BloomRoot: common.HexToHash("0xa3048fe8b7e30f77f11bc755a88478363d7d3e71c2bdfe4e8ab9e269cd804ba2"), 120 } 121 122 // GoerliChainConfig contains the chain parameters to run a node on the Görli test network. 123 GoerliChainConfig = &ChainConfig{ 124 ChainID: big.NewInt(5), 125 HomesteadBlock: big.NewInt(0), 126 DAOForkBlock: nil, 127 DAOForkSupport: true, 128 EIP150Block: big.NewInt(0), 129 EIP155Block: big.NewInt(0), 130 EIP158Block: big.NewInt(0), 131 ByzantiumBlock: big.NewInt(0), 132 ConstantinopleBlock: big.NewInt(0), 133 PetersburgBlock: big.NewInt(0), 134 Clique: &CliqueConfig{ 135 Period: 15, 136 Epoch: 30000, 137 }, 138 } 139 140 // GoerliTrustedCheckpoint contains the light client trusted checkpoint for the Görli test network. 141 GoerliTrustedCheckpoint = &TrustedCheckpoint{ 142 Name: "goerli", 143 SectionIndex: 9, 144 SectionHead: common.HexToHash("0x8e223d827391eee53b07cb8ee057dbfa11c93e0b45352188c783affd7840a921"), 145 CHTRoot: common.HexToHash("0xe0a817ac69b36c1e437c5b0cff9e764853f5115702b5f66d451b665d6afb7e78"), 146 BloomRoot: common.HexToHash("0x50d672aeb655b723284969c7c1201fb6ca003c23ed144bcb9f2d1b30e2971c1b"), 147 } 148 149 // AllEthashProtocolChanges contains every protocol change (EIPs) introduced 150 // and accepted by the Ethereum core developers into the Ethash consensus. 151 // 152 // This configuration is intentionally not using keyed fields to force anyone 153 // adding flags to the config to also have to set these fields. 154 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), nil, new(EthashConfig), nil} 155 156 // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced 157 // and accepted by the Ethereum core developers into the Clique consensus. 158 // 159 // This configuration is intentionally not using keyed fields to force anyone 160 // adding flags to the config to also have to set these fields. 161 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), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}} 162 163 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), nil, new(EthashConfig), nil} 164 TestRules = TestChainConfig.Rules(new(big.Int)) 165 ) 166 167 // TrustedCheckpoint represents a set of post-processed trie roots (CHT and 168 // BloomTrie) associated with the appropriate section index and head hash. It is 169 // used to start light syncing from this checkpoint and avoid downloading the 170 // entire header chain while still being able to securely access old headers/logs. 171 type TrustedCheckpoint struct { 172 Name string `json:"-"` 173 SectionIndex uint64 `json:"sectionIndex"` 174 SectionHead common.Hash `json:"sectionHead"` 175 CHTRoot common.Hash `json:"chtRoot"` 176 BloomRoot common.Hash `json:"bloomRoot"` 177 } 178 179 // ChainConfig is the core config which determines the blockchain settings. 180 // 181 // ChainConfig is stored in the database on a per block basis. This means 182 // that any network, identified by its genesis block, can have its own 183 // set of configuration options. 184 type ChainConfig struct { 185 ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection 186 187 HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead) 188 189 DAOForkBlock *big.Int `json:"daoForkBlock,omitempty"` // TheDAO hard-fork switch block (nil = no fork) 190 DAOForkSupport bool `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork 191 192 // EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150) 193 EIP150Block *big.Int `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork) 194 EIP150Hash common.Hash `json:"eip150Hash,omitempty"` // EIP150 HF hash (needed for header only clients as only gas pricing changed) 195 196 EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block 197 EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block 198 199 ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium) 200 ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated) 201 PetersburgBlock *big.Int `json:"petersburgBlock,omitempty"` // Petersburg switch block (nil = same as Constantinople) 202 EWASMBlock *big.Int `json:"ewasmBlock,omitempty"` // EWASM switch block (nil = no fork, 0 = already activated) 203 204 // Various consensus engines 205 Ethash *EthashConfig `json:"ethash,omitempty"` 206 Clique *CliqueConfig `json:"clique,omitempty"` 207 } 208 209 // EthashConfig is the consensus engine configs for proof-of-work based sealing. 210 type EthashConfig struct{} 211 212 // String implements the stringer interface, returning the consensus engine details. 213 func (c *EthashConfig) String() string { 214 return "ethash" 215 } 216 217 // CliqueConfig is the consensus engine configs for proof-of-authority based sealing. 218 type CliqueConfig struct { 219 Period uint64 `json:"period"` // Number of seconds between blocks to enforce 220 Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint 221 } 222 223 // String implements the stringer interface, returning the consensus engine details. 224 func (c *CliqueConfig) String() string { 225 return "clique" 226 } 227 228 // String implements the fmt.Stringer interface. 229 func (c *ChainConfig) String() string { 230 var engine interface{} 231 switch { 232 case c.Ethash != nil: 233 engine = c.Ethash 234 case c.Clique != nil: 235 engine = c.Clique 236 default: 237 engine = "unknown" 238 } 239 return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v ConstantinopleFix: %v Engine: %v}", 240 c.ChainID, 241 c.HomesteadBlock, 242 c.DAOForkBlock, 243 c.DAOForkSupport, 244 c.EIP150Block, 245 c.EIP155Block, 246 c.EIP158Block, 247 c.ByzantiumBlock, 248 c.ConstantinopleBlock, 249 c.PetersburgBlock, 250 engine, 251 ) 252 } 253 254 // IsHomestead returns whether num is either equal to the homestead block or greater. 255 func (c *ChainConfig) IsHomestead(num *big.Int) bool { 256 return isForked(c.HomesteadBlock, num) 257 } 258 259 // IsDAOFork returns whether num is either equal to the DAO fork block or greater. 260 func (c *ChainConfig) IsDAOFork(num *big.Int) bool { 261 return isForked(c.DAOForkBlock, num) 262 } 263 264 // IsEIP150 returns whether num is either equal to the EIP150 fork block or greater. 265 func (c *ChainConfig) IsEIP150(num *big.Int) bool { 266 return isForked(c.EIP150Block, num) 267 } 268 269 // IsEIP155 returns whether num is either equal to the EIP155 fork block or greater. 270 func (c *ChainConfig) IsEIP155(num *big.Int) bool { 271 return isForked(c.EIP155Block, num) 272 } 273 274 // IsEIP158 returns whether num is either equal to the EIP158 fork block or greater. 275 func (c *ChainConfig) IsEIP158(num *big.Int) bool { 276 return isForked(c.EIP158Block, num) 277 } 278 279 // IsByzantium returns whether num is either equal to the Byzantium fork block or greater. 280 func (c *ChainConfig) IsByzantium(num *big.Int) bool { 281 return isForked(c.ByzantiumBlock, num) 282 } 283 284 // IsConstantinople returns whether num is either equal to the Constantinople fork block or greater. 285 func (c *ChainConfig) IsConstantinople(num *big.Int) bool { 286 return isForked(c.ConstantinopleBlock, num) 287 } 288 289 // IsPetersburg returns whether num is either 290 // - equal to or greater than the PetersburgBlock fork block, 291 // - OR is nil, and Constantinople is active 292 func (c *ChainConfig) IsPetersburg(num *big.Int) bool { 293 return isForked(c.PetersburgBlock, num) || c.PetersburgBlock == nil && isForked(c.ConstantinopleBlock, num) 294 } 295 296 // IsEWASM returns whether num represents a block number after the EWASM fork 297 func (c *ChainConfig) IsEWASM(num *big.Int) bool { 298 return isForked(c.EWASMBlock, num) 299 } 300 301 // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice). 302 // 303 // The returned GasTable's fields shouldn't, under any circumstances, be changed. 304 func (c *ChainConfig) GasTable(num *big.Int) GasTable { 305 if num == nil { 306 return GasTableHomestead 307 } 308 switch { 309 case c.IsConstantinople(num): 310 return GasTableConstantinople 311 case c.IsEIP158(num): 312 return GasTableEIP158 313 case c.IsEIP150(num): 314 return GasTableEIP150 315 default: 316 return GasTableHomestead 317 } 318 } 319 320 // CheckCompatible checks whether scheduled fork transitions have been imported 321 // with a mismatching chain configuration. 322 func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError { 323 bhead := new(big.Int).SetUint64(height) 324 325 // Iterate checkCompatible to find the lowest conflict. 326 var lasterr *ConfigCompatError 327 for { 328 err := c.checkCompatible(newcfg, bhead) 329 if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) { 330 break 331 } 332 lasterr = err 333 bhead.SetUint64(err.RewindTo) 334 } 335 return lasterr 336 } 337 338 func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError { 339 if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) { 340 return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock) 341 } 342 if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) { 343 return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock) 344 } 345 if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport { 346 return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock) 347 } 348 if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) { 349 return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block) 350 } 351 if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) { 352 return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block) 353 } 354 if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) { 355 return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block) 356 } 357 if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) { 358 return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block) 359 } 360 if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) { 361 return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock) 362 } 363 if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) { 364 return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock) 365 } 366 if isForkIncompatible(c.PetersburgBlock, newcfg.PetersburgBlock, head) { 367 return newCompatError("ConstantinopleFix fork block", c.PetersburgBlock, newcfg.PetersburgBlock) 368 } 369 if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) { 370 return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock) 371 } 372 return nil 373 } 374 375 // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to 376 // block s2 because head is already past the fork. 377 func isForkIncompatible(s1, s2, head *big.Int) bool { 378 return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2) 379 } 380 381 // isForked returns whether a fork scheduled at block s is active at the given head block. 382 func isForked(s, head *big.Int) bool { 383 if s == nil || head == nil { 384 return false 385 } 386 return s.Cmp(head) <= 0 387 } 388 389 func configNumEqual(x, y *big.Int) bool { 390 if x == nil { 391 return y == nil 392 } 393 if y == nil { 394 return x == nil 395 } 396 return x.Cmp(y) == 0 397 } 398 399 // ConfigCompatError is raised if the locally-stored blockchain is initialised with a 400 // ChainConfig that would alter the past. 401 type ConfigCompatError struct { 402 What string 403 // block numbers of the stored and new configurations 404 StoredConfig, NewConfig *big.Int 405 // the block number to which the local chain must be rewound to correct the error 406 RewindTo uint64 407 } 408 409 func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError { 410 var rew *big.Int 411 switch { 412 case storedblock == nil: 413 rew = newblock 414 case newblock == nil || storedblock.Cmp(newblock) < 0: 415 rew = storedblock 416 default: 417 rew = newblock 418 } 419 err := &ConfigCompatError{what, storedblock, newblock, 0} 420 if rew != nil && rew.Sign() > 0 { 421 err.RewindTo = rew.Uint64() - 1 422 } 423 return err 424 } 425 426 func (err *ConfigCompatError) Error() string { 427 return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo) 428 } 429 430 // Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions 431 // that do not have or require information about the block. 432 // 433 // Rules is a one time interface meaning that it shouldn't be used in between transition 434 // phases. 435 type Rules struct { 436 ChainID *big.Int 437 IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool 438 IsByzantium, IsConstantinople, IsPetersburg bool 439 } 440 441 // Rules ensures c's ChainID is not nil. 442 func (c *ChainConfig) Rules(num *big.Int) Rules { 443 chainID := c.ChainID 444 if chainID == nil { 445 chainID = new(big.Int) 446 } 447 return Rules{ 448 ChainID: new(big.Int).Set(chainID), 449 IsHomestead: c.IsHomestead(num), 450 IsEIP150: c.IsEIP150(num), 451 IsEIP155: c.IsEIP155(num), 452 IsEIP158: c.IsEIP158(num), 453 IsByzantium: c.IsByzantium(num), 454 IsConstantinople: c.IsConstantinople(num), 455 IsPetersburg: c.IsPetersburg(num), 456 } 457 }