github.com/holiman/go-ethereum@v1.5.5-0.20190823064119-c35259dd265e/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  	"encoding/binary"
    21  	"fmt"
    22  	"math/big"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/crypto"
    26  )
    27  
    28  // Genesis hashes to enforce below configs on.
    29  var (
    30  	MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
    31  	TestnetGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")
    32  	RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177")
    33  	GoerliGenesisHash  = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a")
    34  )
    35  
    36  // TrustedCheckpoints associates each known checkpoint with the genesis hash of
    37  // the chain it belongs to.
    38  var TrustedCheckpoints = map[common.Hash]*TrustedCheckpoint{
    39  	MainnetGenesisHash: MainnetTrustedCheckpoint,
    40  	TestnetGenesisHash: TestnetTrustedCheckpoint,
    41  	RinkebyGenesisHash: RinkebyTrustedCheckpoint,
    42  	GoerliGenesisHash:  GoerliTrustedCheckpoint,
    43  }
    44  
    45  // CheckpointOracles associates each known checkpoint oracles with the genesis hash of
    46  // the chain it belongs to.
    47  var CheckpointOracles = map[common.Hash]*CheckpointOracleConfig{
    48  	MainnetGenesisHash: MainnetCheckpointOracle,
    49  	TestnetGenesisHash: TestnetCheckpointOracle,
    50  	RinkebyGenesisHash: RinkebyCheckpointOracle,
    51  	GoerliGenesisHash:  GoerliCheckpointOracle,
    52  }
    53  
    54  var (
    55  	// MainnetChainConfig is the chain parameters to run a node on the main network.
    56  	MainnetChainConfig = &ChainConfig{
    57  		ChainID:             big.NewInt(1),
    58  		HomesteadBlock:      big.NewInt(1150000),
    59  		DAOForkBlock:        big.NewInt(1920000),
    60  		DAOForkSupport:      true,
    61  		EIP150Block:         big.NewInt(2463000),
    62  		EIP150Hash:          common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
    63  		EIP155Block:         big.NewInt(2675000),
    64  		EIP158Block:         big.NewInt(2675000),
    65  		ByzantiumBlock:      big.NewInt(4370000),
    66  		ConstantinopleBlock: big.NewInt(7280000),
    67  		PetersburgBlock:     big.NewInt(7280000),
    68  		IstanbulBlock:       nil,
    69  		Ethash:              new(EthashConfig),
    70  	}
    71  
    72  	// MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network.
    73  	MainnetTrustedCheckpoint = &TrustedCheckpoint{
    74  		SectionIndex: 253,
    75  		SectionHead:  common.HexToHash("0xf35fabd036e2030196183bb70ae194f6ce1ea7b58559e3825c168f1df9c0a258"),
    76  		CHTRoot:      common.HexToHash("0x8992849e2be3390696eaf66312626e484045501cd3ec207922c27a6a80a7bb07"),
    77  		BloomRoot:    common.HexToHash("0xcc510b51ca4d73fb3fdf43208d73286f8f23817cdc31b8ea9f4de8d645f07df4"),
    78  	}
    79  
    80  	// MainnetCheckpointOracle contains a set of configs for the main network oracle.
    81  	MainnetCheckpointOracle = &CheckpointOracleConfig{
    82  		Address: common.HexToAddress("0x9a9070028361F7AAbeB3f2F2Dc07F82C4a98A02a"),
    83  		Signers: []common.Address{
    84  			common.HexToAddress("0x1b2C260efc720BE89101890E4Db589b44E950527"), // Peter
    85  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
    86  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
    87  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
    88  			common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
    89  		},
    90  		Threshold: 2,
    91  	}
    92  
    93  	// TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network.
    94  	TestnetChainConfig = &ChainConfig{
    95  		ChainID:             big.NewInt(3),
    96  		HomesteadBlock:      big.NewInt(0),
    97  		DAOForkBlock:        nil,
    98  		DAOForkSupport:      true,
    99  		EIP150Block:         big.NewInt(0),
   100  		EIP150Hash:          common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
   101  		EIP155Block:         big.NewInt(10),
   102  		EIP158Block:         big.NewInt(10),
   103  		ByzantiumBlock:      big.NewInt(1700000),
   104  		ConstantinopleBlock: big.NewInt(4230000),
   105  		PetersburgBlock:     big.NewInt(4939394),
   106  		IstanbulBlock:       nil,
   107  		Ethash:              new(EthashConfig),
   108  	}
   109  
   110  	// TestnetTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network.
   111  	TestnetTrustedCheckpoint = &TrustedCheckpoint{
   112  		SectionIndex: 187,
   113  		SectionHead:  common.HexToHash("0x7d6db64d8ec43303e4392fb726d2346f7231b246decca3d8140dd7e2c0d0b07d"),
   114  		CHTRoot:      common.HexToHash("0xa5095e1a004a8642fb93ca682eb91e8f20ef5bce151e47404fbb68772d17705b"),
   115  		BloomRoot:    common.HexToHash("0x90b28050f948ec6fb35b23a91d9aed38ce0c92d3cdd6e1d383c1bddf8b4071cf"),
   116  	}
   117  
   118  	// TestnetCheckpointOracle contains a set of configs for the Ropsten test network oracle.
   119  	TestnetCheckpointOracle = &CheckpointOracleConfig{
   120  		Address: common.HexToAddress("0xEF79475013f154E6A65b54cB2742867791bf0B84"),
   121  		Signers: []common.Address{
   122  			common.HexToAddress("0x32162F3581E88a5f62e8A61892B42C46E2c18f7b"), // Peter
   123  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
   124  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
   125  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
   126  			common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
   127  		},
   128  		Threshold: 2,
   129  	}
   130  
   131  	// RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
   132  	RinkebyChainConfig = &ChainConfig{
   133  		ChainID:             big.NewInt(4),
   134  		HomesteadBlock:      big.NewInt(1),
   135  		DAOForkBlock:        nil,
   136  		DAOForkSupport:      true,
   137  		EIP150Block:         big.NewInt(2),
   138  		EIP150Hash:          common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
   139  		EIP155Block:         big.NewInt(3),
   140  		EIP158Block:         big.NewInt(3),
   141  		ByzantiumBlock:      big.NewInt(1035301),
   142  		ConstantinopleBlock: big.NewInt(3660663),
   143  		PetersburgBlock:     big.NewInt(4321234),
   144  		IstanbulBlock:       nil,
   145  		Clique: &CliqueConfig{
   146  			Period: 15,
   147  			Epoch:  30000,
   148  		},
   149  	}
   150  
   151  	// RinkebyTrustedCheckpoint contains the light client trusted checkpoint for the Rinkeby test network.
   152  	RinkebyTrustedCheckpoint = &TrustedCheckpoint{
   153  		SectionIndex: 148,
   154  		SectionHead:  common.HexToHash("0x45918f4686732c2a3e80827e1bc39cdb6a27fa362ddfe1fdfb61c69a7f1df1a9"),
   155  		CHTRoot:      common.HexToHash("0x8ac7046391fec14834a2a0183513937c0b5f696666545991477d24b067008961"),
   156  		BloomRoot:    common.HexToHash("0xfe4b852517612d7da54bf7e9fc18861a83171a93c72583bb6a61893b74422168"),
   157  	}
   158  
   159  	// RinkebyCheckpointOracle contains a set of configs for the Rinkeby test network oracle.
   160  	RinkebyCheckpointOracle = &CheckpointOracleConfig{
   161  		Address: common.HexToAddress("0xebe8eFA441B9302A0d7eaECc277c09d20D684540"),
   162  		Signers: []common.Address{
   163  			common.HexToAddress("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3"), // Peter
   164  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
   165  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
   166  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
   167  		},
   168  		Threshold: 2,
   169  	}
   170  
   171  	// GoerliChainConfig contains the chain parameters to run a node on the Görli test network.
   172  	GoerliChainConfig = &ChainConfig{
   173  		ChainID:             big.NewInt(5),
   174  		HomesteadBlock:      big.NewInt(0),
   175  		DAOForkBlock:        nil,
   176  		DAOForkSupport:      true,
   177  		EIP150Block:         big.NewInt(0),
   178  		EIP155Block:         big.NewInt(0),
   179  		EIP158Block:         big.NewInt(0),
   180  		ByzantiumBlock:      big.NewInt(0),
   181  		ConstantinopleBlock: big.NewInt(0),
   182  		PetersburgBlock:     big.NewInt(0),
   183  		IstanbulBlock:       nil,
   184  		Clique: &CliqueConfig{
   185  			Period: 15,
   186  			Epoch:  30000,
   187  		},
   188  	}
   189  
   190  	// GoerliTrustedCheckpoint contains the light client trusted checkpoint for the Görli test network.
   191  	GoerliTrustedCheckpoint = &TrustedCheckpoint{
   192  		SectionIndex: 32,
   193  		SectionHead:  common.HexToHash("0x50eaedd8361fa9edd0ac2dec410310b9bdf67b963b60f3b1dce47f84b30670f9"),
   194  		CHTRoot:      common.HexToHash("0x6504db73139f75ffa9102ae980e41b361cf3d5b66cea06c79cde9f457368820c"),
   195  		BloomRoot:    common.HexToHash("0x7551ae027bb776252a20ded51ee2ff0cbfbd1d8d57261b9161cc1f2f80237001"),
   196  	}
   197  
   198  	// GoerliCheckpointOracle contains a set of configs for the Goerli test network oracle.
   199  	GoerliCheckpointOracle = &CheckpointOracleConfig{
   200  		Address: common.HexToAddress("0x18CA0E045F0D772a851BC7e48357Bcaab0a0795D"),
   201  		Signers: []common.Address{
   202  			common.HexToAddress("0x4769bcaD07e3b938B7f43EB7D278Bc7Cb9efFb38"), // Peter
   203  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
   204  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
   205  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
   206  			common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
   207  		},
   208  		Threshold: 2,
   209  	}
   210  
   211  	// AllEthashProtocolChanges contains every protocol change (EIPs) introduced
   212  	// and accepted by the Ethereum core developers into the Ethash consensus.
   213  	//
   214  	// This configuration is intentionally not using keyed fields to force anyone
   215  	// adding flags to the config to also have to set these fields.
   216  	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, nil, new(EthashConfig), nil}
   217  
   218  	// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
   219  	// and accepted by the Ethereum core developers into the Clique consensus.
   220  	//
   221  	// This configuration is intentionally not using keyed fields to force anyone
   222  	// adding flags to the config to also have to set these fields.
   223  	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, nil, &CliqueConfig{Period: 0, Epoch: 30000}}
   224  
   225  	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, nil, new(EthashConfig), nil}
   226  	TestRules       = TestChainConfig.Rules(new(big.Int))
   227  )
   228  
   229  // TrustedCheckpoint represents a set of post-processed trie roots (CHT and
   230  // BloomTrie) associated with the appropriate section index and head hash. It is
   231  // used to start light syncing from this checkpoint and avoid downloading the
   232  // entire header chain while still being able to securely access old headers/logs.
   233  type TrustedCheckpoint struct {
   234  	SectionIndex uint64      `json:"sectionIndex"`
   235  	SectionHead  common.Hash `json:"sectionHead"`
   236  	CHTRoot      common.Hash `json:"chtRoot"`
   237  	BloomRoot    common.Hash `json:"bloomRoot"`
   238  }
   239  
   240  // HashEqual returns an indicator comparing the itself hash with given one.
   241  func (c *TrustedCheckpoint) HashEqual(hash common.Hash) bool {
   242  	if c.Empty() {
   243  		return hash == common.Hash{}
   244  	}
   245  	return c.Hash() == hash
   246  }
   247  
   248  // Hash returns the hash of checkpoint's four key fields(index, sectionHead, chtRoot and bloomTrieRoot).
   249  func (c *TrustedCheckpoint) Hash() common.Hash {
   250  	buf := make([]byte, 8+3*common.HashLength)
   251  	binary.BigEndian.PutUint64(buf, c.SectionIndex)
   252  	copy(buf[8:], c.SectionHead.Bytes())
   253  	copy(buf[8+common.HashLength:], c.CHTRoot.Bytes())
   254  	copy(buf[8+2*common.HashLength:], c.BloomRoot.Bytes())
   255  	return crypto.Keccak256Hash(buf)
   256  }
   257  
   258  // Empty returns an indicator whether the checkpoint is regarded as empty.
   259  func (c *TrustedCheckpoint) Empty() bool {
   260  	return c.SectionHead == (common.Hash{}) || c.CHTRoot == (common.Hash{}) || c.BloomRoot == (common.Hash{})
   261  }
   262  
   263  // CheckpointOracleConfig represents a set of checkpoint contract(which acts as an oracle)
   264  // config which used for light client checkpoint syncing.
   265  type CheckpointOracleConfig struct {
   266  	Address   common.Address   `json:"address"`
   267  	Signers   []common.Address `json:"signers"`
   268  	Threshold uint64           `json:"threshold"`
   269  }
   270  
   271  // ChainConfig is the core config which determines the blockchain settings.
   272  //
   273  // ChainConfig is stored in the database on a per block basis. This means
   274  // that any network, identified by its genesis block, can have its own
   275  // set of configuration options.
   276  type ChainConfig struct {
   277  	ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
   278  
   279  	HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)
   280  
   281  	DAOForkBlock   *big.Int `json:"daoForkBlock,omitempty"`   // TheDAO hard-fork switch block (nil = no fork)
   282  	DAOForkSupport bool     `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
   283  
   284  	// EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
   285  	EIP150Block *big.Int    `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork)
   286  	EIP150Hash  common.Hash `json:"eip150Hash,omitempty"`  // EIP150 HF hash (needed for header only clients as only gas pricing changed)
   287  
   288  	EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
   289  	EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block
   290  
   291  	ByzantiumBlock      *big.Int `json:"byzantiumBlock,omitempty"`      // Byzantium switch block (nil = no fork, 0 = already on byzantium)
   292  	ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
   293  	PetersburgBlock     *big.Int `json:"petersburgBlock,omitempty"`     // Petersburg switch block (nil = same as Constantinople)
   294  	IstanbulBlock       *big.Int `json:"istanbulBlock,omitempty"`       // Istanbul switch block (nil = no fork, 0 = already on istanbul)
   295  	EWASMBlock          *big.Int `json:"ewasmBlock,omitempty"`          // EWASM switch block (nil = no fork, 0 = already activated)
   296  
   297  	// Various consensus engines
   298  	Ethash *EthashConfig `json:"ethash,omitempty"`
   299  	Clique *CliqueConfig `json:"clique,omitempty"`
   300  }
   301  
   302  // EthashConfig is the consensus engine configs for proof-of-work based sealing.
   303  type EthashConfig struct{}
   304  
   305  // String implements the stringer interface, returning the consensus engine details.
   306  func (c *EthashConfig) String() string {
   307  	return "ethash"
   308  }
   309  
   310  // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
   311  type CliqueConfig struct {
   312  	Period uint64 `json:"period"` // Number of seconds between blocks to enforce
   313  	Epoch  uint64 `json:"epoch"`  // Epoch length to reset votes and checkpoint
   314  }
   315  
   316  // String implements the stringer interface, returning the consensus engine details.
   317  func (c *CliqueConfig) String() string {
   318  	return "clique"
   319  }
   320  
   321  // String implements the fmt.Stringer interface.
   322  func (c *ChainConfig) String() string {
   323  	var engine interface{}
   324  	switch {
   325  	case c.Ethash != nil:
   326  		engine = c.Ethash
   327  	case c.Clique != nil:
   328  		engine = c.Clique
   329  	default:
   330  		engine = "unknown"
   331  	}
   332  	return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Petersburg: %v Istanbul: %v Engine: %v}",
   333  		c.ChainID,
   334  		c.HomesteadBlock,
   335  		c.DAOForkBlock,
   336  		c.DAOForkSupport,
   337  		c.EIP150Block,
   338  		c.EIP155Block,
   339  		c.EIP158Block,
   340  		c.ByzantiumBlock,
   341  		c.ConstantinopleBlock,
   342  		c.PetersburgBlock,
   343  		c.IstanbulBlock,
   344  		engine,
   345  	)
   346  }
   347  
   348  // IsHomestead returns whether num is either equal to the homestead block or greater.
   349  func (c *ChainConfig) IsHomestead(num *big.Int) bool {
   350  	return isForked(c.HomesteadBlock, num)
   351  }
   352  
   353  // IsDAOFork returns whether num is either equal to the DAO fork block or greater.
   354  func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
   355  	return isForked(c.DAOForkBlock, num)
   356  }
   357  
   358  // IsEIP150 returns whether num is either equal to the EIP150 fork block or greater.
   359  func (c *ChainConfig) IsEIP150(num *big.Int) bool {
   360  	return isForked(c.EIP150Block, num)
   361  }
   362  
   363  // IsEIP155 returns whether num is either equal to the EIP155 fork block or greater.
   364  func (c *ChainConfig) IsEIP155(num *big.Int) bool {
   365  	return isForked(c.EIP155Block, num)
   366  }
   367  
   368  // IsEIP158 returns whether num is either equal to the EIP158 fork block or greater.
   369  func (c *ChainConfig) IsEIP158(num *big.Int) bool {
   370  	return isForked(c.EIP158Block, num)
   371  }
   372  
   373  // IsByzantium returns whether num is either equal to the Byzantium fork block or greater.
   374  func (c *ChainConfig) IsByzantium(num *big.Int) bool {
   375  	return isForked(c.ByzantiumBlock, num)
   376  }
   377  
   378  // IsConstantinople returns whether num is either equal to the Constantinople fork block or greater.
   379  func (c *ChainConfig) IsConstantinople(num *big.Int) bool {
   380  	return isForked(c.ConstantinopleBlock, num)
   381  }
   382  
   383  // IsPetersburg returns whether num is either
   384  // - equal to or greater than the PetersburgBlock fork block,
   385  // - OR is nil, and Constantinople is active
   386  func (c *ChainConfig) IsPetersburg(num *big.Int) bool {
   387  	return isForked(c.PetersburgBlock, num) || c.PetersburgBlock == nil && isForked(c.ConstantinopleBlock, num)
   388  }
   389  
   390  // IsIstanbul returns whether num is either equal to the Istanbul fork block or greater.
   391  func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
   392  	return isForked(c.IstanbulBlock, num)
   393  }
   394  
   395  // IsEWASM returns whether num represents a block number after the EWASM fork
   396  func (c *ChainConfig) IsEWASM(num *big.Int) bool {
   397  	return isForked(c.EWASMBlock, num)
   398  }
   399  
   400  // CheckCompatible checks whether scheduled fork transitions have been imported
   401  // with a mismatching chain configuration.
   402  func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
   403  	bhead := new(big.Int).SetUint64(height)
   404  
   405  	// Iterate checkCompatible to find the lowest conflict.
   406  	var lasterr *ConfigCompatError
   407  	for {
   408  		err := c.checkCompatible(newcfg, bhead)
   409  		if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
   410  			break
   411  		}
   412  		lasterr = err
   413  		bhead.SetUint64(err.RewindTo)
   414  	}
   415  	return lasterr
   416  }
   417  
   418  func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
   419  	if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
   420  		return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
   421  	}
   422  	if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
   423  		return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
   424  	}
   425  	if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
   426  		return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
   427  	}
   428  	if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
   429  		return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
   430  	}
   431  	if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
   432  		return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
   433  	}
   434  	if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
   435  		return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
   436  	}
   437  	if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
   438  		return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
   439  	}
   440  	if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
   441  		return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
   442  	}
   443  	if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) {
   444  		return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock)
   445  	}
   446  	if isForkIncompatible(c.PetersburgBlock, newcfg.PetersburgBlock, head) {
   447  		return newCompatError("Petersburg fork block", c.PetersburgBlock, newcfg.PetersburgBlock)
   448  	}
   449  	if isForkIncompatible(c.IstanbulBlock, newcfg.IstanbulBlock, head) {
   450  		return newCompatError("Istanbul fork block", c.IstanbulBlock, newcfg.IstanbulBlock)
   451  	}
   452  	if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) {
   453  		return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock)
   454  	}
   455  	return nil
   456  }
   457  
   458  // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
   459  // block s2 because head is already past the fork.
   460  func isForkIncompatible(s1, s2, head *big.Int) bool {
   461  	return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
   462  }
   463  
   464  // isForked returns whether a fork scheduled at block s is active at the given head block.
   465  func isForked(s, head *big.Int) bool {
   466  	if s == nil || head == nil {
   467  		return false
   468  	}
   469  	return s.Cmp(head) <= 0
   470  }
   471  
   472  func configNumEqual(x, y *big.Int) bool {
   473  	if x == nil {
   474  		return y == nil
   475  	}
   476  	if y == nil {
   477  		return x == nil
   478  	}
   479  	return x.Cmp(y) == 0
   480  }
   481  
   482  // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
   483  // ChainConfig that would alter the past.
   484  type ConfigCompatError struct {
   485  	What string
   486  	// block numbers of the stored and new configurations
   487  	StoredConfig, NewConfig *big.Int
   488  	// the block number to which the local chain must be rewound to correct the error
   489  	RewindTo uint64
   490  }
   491  
   492  func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
   493  	var rew *big.Int
   494  	switch {
   495  	case storedblock == nil:
   496  		rew = newblock
   497  	case newblock == nil || storedblock.Cmp(newblock) < 0:
   498  		rew = storedblock
   499  	default:
   500  		rew = newblock
   501  	}
   502  	err := &ConfigCompatError{what, storedblock, newblock, 0}
   503  	if rew != nil && rew.Sign() > 0 {
   504  		err.RewindTo = rew.Uint64() - 1
   505  	}
   506  	return err
   507  }
   508  
   509  func (err *ConfigCompatError) Error() string {
   510  	return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
   511  }
   512  
   513  // Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
   514  // that do not have or require information about the block.
   515  //
   516  // Rules is a one time interface meaning that it shouldn't be used in between transition
   517  // phases.
   518  type Rules struct {
   519  	ChainID                                                 *big.Int
   520  	IsHomestead, IsEIP150, IsEIP155, IsEIP158               bool
   521  	IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
   522  }
   523  
   524  // Rules ensures c's ChainID is not nil.
   525  func (c *ChainConfig) Rules(num *big.Int) Rules {
   526  	chainID := c.ChainID
   527  	if chainID == nil {
   528  		chainID = new(big.Int)
   529  	}
   530  	return Rules{
   531  		ChainID:          new(big.Int).Set(chainID),
   532  		IsHomestead:      c.IsHomestead(num),
   533  		IsEIP150:         c.IsEIP150(num),
   534  		IsEIP155:         c.IsEIP155(num),
   535  		IsEIP158:         c.IsEIP158(num),
   536  		IsByzantium:      c.IsByzantium(num),
   537  		IsConstantinople: c.IsConstantinople(num),
   538  		IsPetersburg:     c.IsPetersburg(num),
   539  		IsIstanbul:       c.IsIstanbul(num),
   540  	}
   541  }