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