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