github.com/daragao/go-ethereum@v1.8.14-0.20180809141559-45eaef243198/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 ) 31 32 var ( 33 // MainnetChainConfig is the chain parameters to run a node on the main network. 34 MainnetChainConfig = &ChainConfig{ 35 ChainID: big.NewInt(1), 36 HomesteadBlock: big.NewInt(1150000), 37 DAOForkBlock: big.NewInt(1920000), 38 DAOForkSupport: true, 39 EIP150Block: big.NewInt(2463000), 40 EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"), 41 EIP155Block: big.NewInt(2675000), 42 EIP158Block: big.NewInt(2675000), 43 ByzantiumBlock: big.NewInt(4370000), 44 ConstantinopleBlock: nil, 45 Ethash: new(EthashConfig), 46 } 47 48 // TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network. 49 TestnetChainConfig = &ChainConfig{ 50 ChainID: big.NewInt(3), 51 HomesteadBlock: big.NewInt(0), 52 DAOForkBlock: nil, 53 DAOForkSupport: true, 54 EIP150Block: big.NewInt(0), 55 EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"), 56 EIP155Block: big.NewInt(10), 57 EIP158Block: big.NewInt(10), 58 ByzantiumBlock: big.NewInt(1700000), 59 ConstantinopleBlock: nil, 60 Ethash: new(EthashConfig), 61 } 62 63 // RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network. 64 RinkebyChainConfig = &ChainConfig{ 65 ChainID: big.NewInt(4), 66 HomesteadBlock: big.NewInt(1), 67 DAOForkBlock: nil, 68 DAOForkSupport: true, 69 EIP150Block: big.NewInt(2), 70 EIP150Hash: common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"), 71 EIP155Block: big.NewInt(3), 72 EIP158Block: big.NewInt(3), 73 ByzantiumBlock: big.NewInt(1035301), 74 ConstantinopleBlock: nil, 75 Clique: &CliqueConfig{ 76 Period: 15, 77 Epoch: 30000, 78 }, 79 } 80 81 // AllEthashProtocolChanges contains every protocol change (EIPs) introduced 82 // and accepted by the Ethereum core developers into the Ethash consensus. 83 // 84 // This configuration is intentionally not using keyed fields to force anyone 85 // adding flags to the config to also have to set these fields. 86 AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil} 87 88 // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced 89 // and accepted by the Ethereum core developers into the Clique consensus. 90 // 91 // This configuration is intentionally not using keyed fields to force anyone 92 // adding flags to the config to also have to set these fields. 93 AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}} 94 95 TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil} 96 TestRules = TestChainConfig.Rules(new(big.Int)) 97 ) 98 99 // ChainConfig is the core config which determines the blockchain settings. 100 // 101 // ChainConfig is stored in the database on a per block basis. This means 102 // that any network, identified by its genesis block, can have its own 103 // set of configuration options. 104 type ChainConfig struct { 105 ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection 106 107 HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead) 108 109 DAOForkBlock *big.Int `json:"daoForkBlock,omitempty"` // TheDAO hard-fork switch block (nil = no fork) 110 DAOForkSupport bool `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork 111 112 // EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150) 113 EIP150Block *big.Int `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork) 114 EIP150Hash common.Hash `json:"eip150Hash,omitempty"` // EIP150 HF hash (needed for header only clients as only gas pricing changed) 115 116 EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block 117 EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block 118 119 ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium) 120 ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated) 121 122 // Various consensus engines 123 Ethash *EthashConfig `json:"ethash,omitempty"` 124 Clique *CliqueConfig `json:"clique,omitempty"` 125 } 126 127 // EthashConfig is the consensus engine configs for proof-of-work based sealing. 128 type EthashConfig struct{} 129 130 // String implements the stringer interface, returning the consensus engine details. 131 func (c *EthashConfig) String() string { 132 return "ethash" 133 } 134 135 // CliqueConfig is the consensus engine configs for proof-of-authority based sealing. 136 type CliqueConfig struct { 137 Period uint64 `json:"period"` // Number of seconds between blocks to enforce 138 Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint 139 } 140 141 // String implements the stringer interface, returning the consensus engine details. 142 func (c *CliqueConfig) String() string { 143 return "clique" 144 } 145 146 // String implements the fmt.Stringer interface. 147 func (c *ChainConfig) String() string { 148 var engine interface{} 149 switch { 150 case c.Ethash != nil: 151 engine = c.Ethash 152 case c.Clique != nil: 153 engine = c.Clique 154 default: 155 engine = "unknown" 156 } 157 return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Engine: %v}", 158 c.ChainID, 159 c.HomesteadBlock, 160 c.DAOForkBlock, 161 c.DAOForkSupport, 162 c.EIP150Block, 163 c.EIP155Block, 164 c.EIP158Block, 165 c.ByzantiumBlock, 166 c.ConstantinopleBlock, 167 engine, 168 ) 169 } 170 171 // IsHomestead returns whether num is either equal to the homestead block or greater. 172 func (c *ChainConfig) IsHomestead(num *big.Int) bool { 173 return isForked(c.HomesteadBlock, num) 174 } 175 176 // IsDAOFork returns whether num is either equal to the DAO fork block or greater. 177 func (c *ChainConfig) IsDAOFork(num *big.Int) bool { 178 return isForked(c.DAOForkBlock, num) 179 } 180 181 // IsEIP150 returns whether num is either equal to the EIP150 fork block or greater. 182 func (c *ChainConfig) IsEIP150(num *big.Int) bool { 183 return isForked(c.EIP150Block, num) 184 } 185 186 // IsEIP155 returns whether num is either equal to the EIP155 fork block or greater. 187 func (c *ChainConfig) IsEIP155(num *big.Int) bool { 188 return isForked(c.EIP155Block, num) 189 } 190 191 // IsEIP158 returns whether num is either equal to the EIP158 fork block or greater. 192 func (c *ChainConfig) IsEIP158(num *big.Int) bool { 193 return isForked(c.EIP158Block, num) 194 } 195 196 // IsByzantium returns whether num is either equal to the Byzantium fork block or greater. 197 func (c *ChainConfig) IsByzantium(num *big.Int) bool { 198 return isForked(c.ByzantiumBlock, num) 199 } 200 201 // IsConstantinople returns whether num is either equal to the Constantinople fork block or greater. 202 func (c *ChainConfig) IsConstantinople(num *big.Int) bool { 203 return isForked(c.ConstantinopleBlock, num) 204 } 205 206 // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice). 207 // 208 // The returned GasTable's fields shouldn't, under any circumstances, be changed. 209 func (c *ChainConfig) GasTable(num *big.Int) GasTable { 210 if num == nil { 211 return GasTableHomestead 212 } 213 switch { 214 case c.IsConstantinople(num): 215 return GasTableConstantinople 216 case c.IsEIP158(num): 217 return GasTableEIP158 218 case c.IsEIP150(num): 219 return GasTableEIP150 220 default: 221 return GasTableHomestead 222 } 223 } 224 225 // CheckCompatible checks whether scheduled fork transitions have been imported 226 // with a mismatching chain configuration. 227 func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError { 228 bhead := new(big.Int).SetUint64(height) 229 230 // Iterate checkCompatible to find the lowest conflict. 231 var lasterr *ConfigCompatError 232 for { 233 err := c.checkCompatible(newcfg, bhead) 234 if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) { 235 break 236 } 237 lasterr = err 238 bhead.SetUint64(err.RewindTo) 239 } 240 return lasterr 241 } 242 243 func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError { 244 if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) { 245 return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock) 246 } 247 if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) { 248 return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock) 249 } 250 if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport { 251 return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock) 252 } 253 if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) { 254 return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block) 255 } 256 if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) { 257 return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block) 258 } 259 if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) { 260 return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block) 261 } 262 if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) { 263 return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block) 264 } 265 if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) { 266 return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock) 267 } 268 if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) { 269 return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock) 270 } 271 return nil 272 } 273 274 // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to 275 // block s2 because head is already past the fork. 276 func isForkIncompatible(s1, s2, head *big.Int) bool { 277 return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2) 278 } 279 280 // isForked returns whether a fork scheduled at block s is active at the given head block. 281 func isForked(s, head *big.Int) bool { 282 if s == nil || head == nil { 283 return false 284 } 285 return s.Cmp(head) <= 0 286 } 287 288 func configNumEqual(x, y *big.Int) bool { 289 if x == nil { 290 return y == nil 291 } 292 if y == nil { 293 return x == nil 294 } 295 return x.Cmp(y) == 0 296 } 297 298 // ConfigCompatError is raised if the locally-stored blockchain is initialised with a 299 // ChainConfig that would alter the past. 300 type ConfigCompatError struct { 301 What string 302 // block numbers of the stored and new configurations 303 StoredConfig, NewConfig *big.Int 304 // the block number to which the local chain must be rewound to correct the error 305 RewindTo uint64 306 } 307 308 func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError { 309 var rew *big.Int 310 switch { 311 case storedblock == nil: 312 rew = newblock 313 case newblock == nil || storedblock.Cmp(newblock) < 0: 314 rew = storedblock 315 default: 316 rew = newblock 317 } 318 err := &ConfigCompatError{what, storedblock, newblock, 0} 319 if rew != nil && rew.Sign() > 0 { 320 err.RewindTo = rew.Uint64() - 1 321 } 322 return err 323 } 324 325 func (err *ConfigCompatError) Error() string { 326 return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo) 327 } 328 329 // Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions 330 // that do not have or require information about the block. 331 // 332 // Rules is a one time interface meaning that it shouldn't be used in between transition 333 // phases. 334 type Rules struct { 335 ChainID *big.Int 336 IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool 337 IsByzantium bool 338 } 339 340 // Rules ensures c's ChainID is not nil. 341 func (c *ChainConfig) Rules(num *big.Int) Rules { 342 chainID := c.ChainID 343 if chainID == nil { 344 chainID = new(big.Int) 345 } 346 return Rules{ChainID: new(big.Int).Set(chainID), IsHomestead: c.IsHomestead(num), IsEIP150: c.IsEIP150(num), IsEIP155: c.IsEIP155(num), IsEIP158: c.IsEIP158(num), IsByzantium: c.IsByzantium(num)} 347 }