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