github.com/farazdagi/go-ethereum@v1.8.1/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  
    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  
    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  
    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), 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, &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), 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  
   120  	// Various consensus engines
   121  	Ethash *EthashConfig `json:"ethash,omitempty"`
   122  	Clique *CliqueConfig `json:"clique,omitempty"`
   123  }
   124  
   125  // EthashConfig is the consensus engine configs for proof-of-work based sealing.
   126  type EthashConfig struct{}
   127  
   128  // String implements the stringer interface, returning the consensus engine details.
   129  func (c *EthashConfig) String() string {
   130  	return "ethash"
   131  }
   132  
   133  // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
   134  type CliqueConfig struct {
   135  	Period uint64 `json:"period"` // Number of seconds between blocks to enforce
   136  	Epoch  uint64 `json:"epoch"`  // Epoch length to reset votes and checkpoint
   137  }
   138  
   139  // String implements the stringer interface, returning the consensus engine details.
   140  func (c *CliqueConfig) String() string {
   141  	return "clique"
   142  }
   143  
   144  // String implements the fmt.Stringer interface.
   145  func (c *ChainConfig) String() string {
   146  	var engine interface{}
   147  	switch {
   148  	case c.Ethash != nil:
   149  		engine = c.Ethash
   150  	case c.Clique != nil:
   151  		engine = c.Clique
   152  	default:
   153  		engine = "unknown"
   154  	}
   155  	return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Engine: %v}",
   156  		c.ChainId,
   157  		c.HomesteadBlock,
   158  		c.DAOForkBlock,
   159  		c.DAOForkSupport,
   160  		c.EIP150Block,
   161  		c.EIP155Block,
   162  		c.EIP158Block,
   163  		c.ByzantiumBlock,
   164  		engine,
   165  	)
   166  }
   167  
   168  // IsHomestead returns whether num is either equal to the homestead block or greater.
   169  func (c *ChainConfig) IsHomestead(num *big.Int) bool {
   170  	return isForked(c.HomesteadBlock, num)
   171  }
   172  
   173  // IsDAO returns whether num is either equal to the DAO fork block or greater.
   174  func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
   175  	return isForked(c.DAOForkBlock, num)
   176  }
   177  
   178  func (c *ChainConfig) IsEIP150(num *big.Int) bool {
   179  	return isForked(c.EIP150Block, num)
   180  }
   181  
   182  func (c *ChainConfig) IsEIP155(num *big.Int) bool {
   183  	return isForked(c.EIP155Block, num)
   184  }
   185  
   186  func (c *ChainConfig) IsEIP158(num *big.Int) bool {
   187  	return isForked(c.EIP158Block, num)
   188  }
   189  
   190  func (c *ChainConfig) IsByzantium(num *big.Int) bool {
   191  	return isForked(c.ByzantiumBlock, num)
   192  }
   193  
   194  // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice).
   195  //
   196  // The returned GasTable's fields shouldn't, under any circumstances, be changed.
   197  func (c *ChainConfig) GasTable(num *big.Int) GasTable {
   198  	if num == nil {
   199  		return GasTableHomestead
   200  	}
   201  	switch {
   202  	case c.IsEIP158(num):
   203  		return GasTableEIP158
   204  	case c.IsEIP150(num):
   205  		return GasTableEIP150
   206  	default:
   207  		return GasTableHomestead
   208  	}
   209  }
   210  
   211  // CheckCompatible checks whether scheduled fork transitions have been imported
   212  // with a mismatching chain configuration.
   213  func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
   214  	bhead := new(big.Int).SetUint64(height)
   215  
   216  	// Iterate checkCompatible to find the lowest conflict.
   217  	var lasterr *ConfigCompatError
   218  	for {
   219  		err := c.checkCompatible(newcfg, bhead)
   220  		if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
   221  			break
   222  		}
   223  		lasterr = err
   224  		bhead.SetUint64(err.RewindTo)
   225  	}
   226  	return lasterr
   227  }
   228  
   229  func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
   230  	if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
   231  		return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
   232  	}
   233  	if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
   234  		return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
   235  	}
   236  	if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
   237  		return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
   238  	}
   239  	if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
   240  		return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
   241  	}
   242  	if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
   243  		return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
   244  	}
   245  	if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
   246  		return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
   247  	}
   248  	if c.IsEIP158(head) && !configNumEqual(c.ChainId, newcfg.ChainId) {
   249  		return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
   250  	}
   251  	if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
   252  		return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
   253  	}
   254  	return nil
   255  }
   256  
   257  // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
   258  // block s2 because head is already past the fork.
   259  func isForkIncompatible(s1, s2, head *big.Int) bool {
   260  	return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
   261  }
   262  
   263  // isForked returns whether a fork scheduled at block s is active at the given head block.
   264  func isForked(s, head *big.Int) bool {
   265  	if s == nil || head == nil {
   266  		return false
   267  	}
   268  	return s.Cmp(head) <= 0
   269  }
   270  
   271  func configNumEqual(x, y *big.Int) bool {
   272  	if x == nil {
   273  		return y == nil
   274  	}
   275  	if y == nil {
   276  		return x == nil
   277  	}
   278  	return x.Cmp(y) == 0
   279  }
   280  
   281  // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
   282  // ChainConfig that would alter the past.
   283  type ConfigCompatError struct {
   284  	What string
   285  	// block numbers of the stored and new configurations
   286  	StoredConfig, NewConfig *big.Int
   287  	// the block number to which the local chain must be rewound to correct the error
   288  	RewindTo uint64
   289  }
   290  
   291  func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
   292  	var rew *big.Int
   293  	switch {
   294  	case storedblock == nil:
   295  		rew = newblock
   296  	case newblock == nil || storedblock.Cmp(newblock) < 0:
   297  		rew = storedblock
   298  	default:
   299  		rew = newblock
   300  	}
   301  	err := &ConfigCompatError{what, storedblock, newblock, 0}
   302  	if rew != nil && rew.Sign() > 0 {
   303  		err.RewindTo = rew.Uint64() - 1
   304  	}
   305  	return err
   306  }
   307  
   308  func (err *ConfigCompatError) Error() string {
   309  	return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
   310  }
   311  
   312  // Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions
   313  // that do not have or require information about the block.
   314  //
   315  // Rules is a one time interface meaning that it shouldn't be used in between transition
   316  // phases.
   317  type Rules struct {
   318  	ChainId                                   *big.Int
   319  	IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
   320  	IsByzantium                               bool
   321  }
   322  
   323  func (c *ChainConfig) Rules(num *big.Int) Rules {
   324  	chainId := c.ChainId
   325  	if chainId == nil {
   326  		chainId = new(big.Int)
   327  	}
   328  	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)}
   329  }