github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/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 // MainnetChainConfig is the chain parameters to run a node on the main network. 27 var MainnetChainConfig = &ChainConfig{ 28 ChainId: MainNetChainID, 29 HomesteadBlock: MainNetHomesteadBlock, 30 DAOForkBlock: MainNetDAOForkBlock, 31 DAOForkSupport: true, 32 EIP150Block: MainNetHomesteadGasRepriceBlock, 33 EIP150Hash: MainNetHomesteadGasRepriceHash, 34 EIP155Block: MainNetSpuriousDragon, 35 EIP158Block: MainNetSpuriousDragon, 36 } 37 38 // TestnetChainConfig is the chain parameters to run a node on the test network. 39 var TestnetChainConfig = &ChainConfig{ 40 ChainId: big.NewInt(3), 41 HomesteadBlock: big.NewInt(0), 42 DAOForkBlock: nil, 43 DAOForkSupport: true, 44 EIP150Block: big.NewInt(0), 45 EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"), 46 EIP155Block: big.NewInt(10), 47 EIP158Block: big.NewInt(10), 48 } 49 50 // AllProtocolChanges contains every protocol change (EIPs) 51 // introduced and accepted by the Ethereum core developers. 52 // 53 // This configuration is intentionally not using keyed fields. 54 // This configuration must *always* have all forks enabled, which 55 // means that all fields must be set at all times. This forces 56 // anyone adding flags to the config to also have to set these 57 // fields. 58 var AllProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0)} 59 60 // ChainConfig is the core config which determines the blockchain settings. 61 // 62 // ChainConfig is stored in the database on a per block basis. This means 63 // that any network, identified by its genesis block, can have its own 64 // set of configuration options. 65 type ChainConfig struct { 66 ChainId *big.Int `json:"chainId"` // Chain id identifies the current chain and is used for replay protection 67 68 HomesteadBlock *big.Int `json:"homesteadBlock"` // Homestead switch block (nil = no fork, 0 = already homestead) 69 DAOForkBlock *big.Int `json:"daoForkBlock"` // TheDAO hard-fork switch block (nil = no fork) 70 DAOForkSupport bool `json:"daoForkSupport"` // Whether the nodes supports or opposes the DAO hard-fork 71 72 // EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150) 73 EIP150Block *big.Int `json:"eip150Block"` // EIP150 HF block (nil = no fork) 74 EIP150Hash common.Hash `json:"eip150Hash"` // EIP150 HF hash (fast sync aid) 75 76 EIP155Block *big.Int `json:"eip155Block"` // EIP155 HF block 77 EIP158Block *big.Int `json:"eip158Block"` // EIP158 HF block 78 } 79 80 // String implements the Stringer interface. 81 func (c *ChainConfig) String() string { 82 return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v}", 83 c.ChainId, 84 c.HomesteadBlock, 85 c.DAOForkBlock, 86 c.DAOForkSupport, 87 c.EIP150Block, 88 c.EIP155Block, 89 c.EIP158Block, 90 ) 91 } 92 93 var ( 94 TestChainConfig = &ChainConfig{big.NewInt(1), new(big.Int), new(big.Int), true, new(big.Int), common.Hash{}, new(big.Int), new(big.Int)} 95 TestRules = TestChainConfig.Rules(new(big.Int)) 96 ) 97 98 // IsHomestead returns whether num is either equal to the homestead block or greater. 99 func (c *ChainConfig) IsHomestead(num *big.Int) bool { 100 if c.HomesteadBlock == nil || num == nil { 101 return false 102 } 103 return num.Cmp(c.HomesteadBlock) >= 0 104 } 105 106 // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice). 107 // 108 // The returned GasTable's fields shouldn't, under any circumstances, be changed. 109 func (c *ChainConfig) GasTable(num *big.Int) GasTable { 110 if num == nil { 111 return GasTableHomestead 112 } 113 114 switch { 115 case c.EIP158Block != nil && num.Cmp(c.EIP158Block) >= 0: 116 return GasTableEIP158 117 case c.EIP150Block != nil && num.Cmp(c.EIP150Block) >= 0: 118 return GasTableHomesteadGasRepriceFork 119 default: 120 return GasTableHomestead 121 } 122 } 123 124 func (c *ChainConfig) IsEIP150(num *big.Int) bool { 125 if c.EIP150Block == nil || num == nil { 126 return false 127 } 128 return num.Cmp(c.EIP150Block) >= 0 129 130 } 131 132 func (c *ChainConfig) IsEIP155(num *big.Int) bool { 133 if c.EIP155Block == nil || num == nil { 134 return false 135 } 136 return num.Cmp(c.EIP155Block) >= 0 137 138 } 139 140 func (c *ChainConfig) IsEIP158(num *big.Int) bool { 141 if c.EIP158Block == nil || num == nil { 142 return false 143 } 144 return num.Cmp(c.EIP158Block) >= 0 145 146 } 147 148 // Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions 149 // that do not have or require information about the block. 150 // 151 // Rules is a one time interface meaning that it shouldn't be used in between transition 152 // phases. 153 type Rules struct { 154 ChainId *big.Int 155 IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool 156 } 157 158 func (c *ChainConfig) Rules(num *big.Int) Rules { 159 return Rules{ChainId: new(big.Int).Set(c.ChainId), IsHomestead: c.IsHomestead(num), IsEIP150: c.IsEIP150(num), IsEIP155: c.IsEIP155(num), IsEIP158: c.IsEIP158(num)} 160 }