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