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