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