github.com/chapsuk/go-ethereum@v1.8.12-0.20180615081455-574378edb50c/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.IsEIP158(num): 215 return GasTableEIP158 216 case c.IsEIP150(num): 217 return GasTableEIP150 218 default: 219 return GasTableHomestead 220 } 221 } 222 223 // CheckCompatible checks whether scheduled fork transitions have been imported 224 // with a mismatching chain configuration. 225 func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError { 226 bhead := new(big.Int).SetUint64(height) 227 228 // Iterate checkCompatible to find the lowest conflict. 229 var lasterr *ConfigCompatError 230 for { 231 err := c.checkCompatible(newcfg, bhead) 232 if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) { 233 break 234 } 235 lasterr = err 236 bhead.SetUint64(err.RewindTo) 237 } 238 return lasterr 239 } 240 241 func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError { 242 if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) { 243 return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock) 244 } 245 if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) { 246 return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock) 247 } 248 if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport { 249 return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock) 250 } 251 if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) { 252 return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block) 253 } 254 if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) { 255 return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block) 256 } 257 if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) { 258 return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block) 259 } 260 if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) { 261 return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block) 262 } 263 if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) { 264 return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock) 265 } 266 if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) { 267 return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock) 268 } 269 return nil 270 } 271 272 // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to 273 // block s2 because head is already past the fork. 274 func isForkIncompatible(s1, s2, head *big.Int) bool { 275 return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2) 276 } 277 278 // isForked returns whether a fork scheduled at block s is active at the given head block. 279 func isForked(s, head *big.Int) bool { 280 if s == nil || head == nil { 281 return false 282 } 283 return s.Cmp(head) <= 0 284 } 285 286 func configNumEqual(x, y *big.Int) bool { 287 if x == nil { 288 return y == nil 289 } 290 if y == nil { 291 return x == nil 292 } 293 return x.Cmp(y) == 0 294 } 295 296 // ConfigCompatError is raised if the locally-stored blockchain is initialised with a 297 // ChainConfig that would alter the past. 298 type ConfigCompatError struct { 299 What string 300 // block numbers of the stored and new configurations 301 StoredConfig, NewConfig *big.Int 302 // the block number to which the local chain must be rewound to correct the error 303 RewindTo uint64 304 } 305 306 func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError { 307 var rew *big.Int 308 switch { 309 case storedblock == nil: 310 rew = newblock 311 case newblock == nil || storedblock.Cmp(newblock) < 0: 312 rew = storedblock 313 default: 314 rew = newblock 315 } 316 err := &ConfigCompatError{what, storedblock, newblock, 0} 317 if rew != nil && rew.Sign() > 0 { 318 err.RewindTo = rew.Uint64() - 1 319 } 320 return err 321 } 322 323 func (err *ConfigCompatError) Error() string { 324 return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo) 325 } 326 327 // Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions 328 // that do not have or require information about the block. 329 // 330 // Rules is a one time interface meaning that it shouldn't be used in between transition 331 // phases. 332 type Rules struct { 333 ChainID *big.Int 334 IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool 335 IsByzantium bool 336 } 337 338 // Rules ensures c's ChainID is not nil. 339 func (c *ChainConfig) Rules(num *big.Int) Rules { 340 chainID := c.ChainID 341 if chainID == nil { 342 chainID = new(big.Int) 343 } 344 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)} 345 }