github.com/bps1980/go-ethereum@v1.9.7/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:       big.NewInt(9069000),
    69  		Ethash:              new(EthashConfig),
    70  	}
    71  
    72  	// MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network.
    73  	MainnetTrustedCheckpoint = &TrustedCheckpoint{
    74  		SectionIndex: 270,
    75  		SectionHead:  common.HexToHash("0xb67c33d838a60c282c2fb49b188fbbac1ef8565ffb4a1c4909b0a05885e72e40"),
    76  		CHTRoot:      common.HexToHash("0x781daa4607782300da85d440df3813ba38a1262585231e35e9480726de81dbfc"),
    77  		BloomRoot:    common.HexToHash("0xfd8951fa6d779cbc981df40dc31056ed1a549db529349d7dfae016f9d96cae72"),
    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:       big.NewInt(6485846),
   107  		Ethash:              new(EthashConfig),
   108  	}
   109  
   110  	// TestnetTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network.
   111  	TestnetTrustedCheckpoint = &TrustedCheckpoint{
   112  		SectionIndex: 204,
   113  		SectionHead:  common.HexToHash("0xa39168b51c3205456f30ce6a91f3590a43295b15a1c8c2ab86bb8c06b8ad1808"),
   114  		CHTRoot:      common.HexToHash("0x9a3654147b79882bfc4e16fbd3421512aa7e4dfadc6c511923980e0877bdf3b4"),
   115  		BloomRoot:    common.HexToHash("0xe72b979522d94fa45c1331639316da234a9bb85062d64d72e13afe1d3f5c17d5"),
   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:       big.NewInt(5435345),
   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: 163,
   154  		SectionHead:  common.HexToHash("0x36e5deaa46f258bece94b05d8e10f1ef68f422fb62ed47a2b6e616aa26e84997"),
   155  		CHTRoot:      common.HexToHash("0x829b9feca1c2cdf5a4cf3efac554889e438ee4df8718c2ce3e02555a02d9e9e5"),
   156  		BloomRoot:    common.HexToHash("0x58c01de24fdae7c082ebbe7665f189d0aa4d90ee10e72086bf56651c63269e54"),
   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:       big.NewInt(1561651),
   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: 47,
   193  		SectionHead:  common.HexToHash("0x00c5b54c6c9a73660501fd9273ccdb4c5bbdbe5d7b8b650e28f881ec9d2337f6"),
   194  		CHTRoot:      common.HexToHash("0xef35caa155fd659f57167e7d507de2f8132cbb31f771526481211d8a977d704c"),
   195  		BloomRoot:    common.HexToHash("0xbda330402f66008d52e7adc748da28535b1212a7912a21244acd2ba77ff0ff06"),
   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), big.NewInt(0), 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), big.NewInt(0), 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), big.NewInt(0), 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  // CheckConfigForkOrder checks that we don't "skip" any forks, geth isn't pluggable enough
   419  // to guarantee that forks
   420  func (c *ChainConfig) CheckConfigForkOrder() error {
   421  	type fork struct {
   422  		name  string
   423  		block *big.Int
   424  	}
   425  	var lastFork fork
   426  	for _, cur := range []fork{
   427  		{"homesteadBlock", c.HomesteadBlock},
   428  		{"eip150Block", c.EIP150Block},
   429  		{"eip155Block", c.EIP155Block},
   430  		{"eip158Block", c.EIP158Block},
   431  		{"byzantiumBlock", c.ByzantiumBlock},
   432  		{"constantinopleBlock", c.ConstantinopleBlock},
   433  		{"petersburgBlock", c.PetersburgBlock},
   434  		{"istanbulBlock", c.IstanbulBlock},
   435  	} {
   436  		if lastFork.name != "" {
   437  			// Next one must be higher number
   438  			if lastFork.block == nil && cur.block != nil {
   439  				return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at %v",
   440  					lastFork.name, cur.name, cur.block)
   441  			}
   442  			if lastFork.block != nil && cur.block != nil {
   443  				if lastFork.block.Cmp(cur.block) > 0 {
   444  					return fmt.Errorf("unsupported fork ordering: %v enabled at %v, but %v enabled at %v",
   445  						lastFork.name, lastFork.block, cur.name, cur.block)
   446  				}
   447  			}
   448  		}
   449  		lastFork = cur
   450  	}
   451  	return nil
   452  }
   453  
   454  func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
   455  	if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
   456  		return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
   457  	}
   458  	if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
   459  		return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
   460  	}
   461  	if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
   462  		return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
   463  	}
   464  	if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
   465  		return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
   466  	}
   467  	if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
   468  		return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
   469  	}
   470  	if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
   471  		return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
   472  	}
   473  	if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
   474  		return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
   475  	}
   476  	if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
   477  		return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
   478  	}
   479  	if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) {
   480  		return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock)
   481  	}
   482  	if isForkIncompatible(c.PetersburgBlock, newcfg.PetersburgBlock, head) {
   483  		return newCompatError("Petersburg fork block", c.PetersburgBlock, newcfg.PetersburgBlock)
   484  	}
   485  	if isForkIncompatible(c.IstanbulBlock, newcfg.IstanbulBlock, head) {
   486  		return newCompatError("Istanbul fork block", c.IstanbulBlock, newcfg.IstanbulBlock)
   487  	}
   488  	if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) {
   489  		return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock)
   490  	}
   491  	return nil
   492  }
   493  
   494  // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
   495  // block s2 because head is already past the fork.
   496  func isForkIncompatible(s1, s2, head *big.Int) bool {
   497  	return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
   498  }
   499  
   500  // isForked returns whether a fork scheduled at block s is active at the given head block.
   501  func isForked(s, head *big.Int) bool {
   502  	if s == nil || head == nil {
   503  		return false
   504  	}
   505  	return s.Cmp(head) <= 0
   506  }
   507  
   508  func configNumEqual(x, y *big.Int) bool {
   509  	if x == nil {
   510  		return y == nil
   511  	}
   512  	if y == nil {
   513  		return x == nil
   514  	}
   515  	return x.Cmp(y) == 0
   516  }
   517  
   518  // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
   519  // ChainConfig that would alter the past.
   520  type ConfigCompatError struct {
   521  	What string
   522  	// block numbers of the stored and new configurations
   523  	StoredConfig, NewConfig *big.Int
   524  	// the block number to which the local chain must be rewound to correct the error
   525  	RewindTo uint64
   526  }
   527  
   528  func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
   529  	var rew *big.Int
   530  	switch {
   531  	case storedblock == nil:
   532  		rew = newblock
   533  	case newblock == nil || storedblock.Cmp(newblock) < 0:
   534  		rew = storedblock
   535  	default:
   536  		rew = newblock
   537  	}
   538  	err := &ConfigCompatError{what, storedblock, newblock, 0}
   539  	if rew != nil && rew.Sign() > 0 {
   540  		err.RewindTo = rew.Uint64() - 1
   541  	}
   542  	return err
   543  }
   544  
   545  func (err *ConfigCompatError) Error() string {
   546  	return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
   547  }
   548  
   549  // Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
   550  // that do not have or require information about the block.
   551  //
   552  // Rules is a one time interface meaning that it shouldn't be used in between transition
   553  // phases.
   554  type Rules struct {
   555  	ChainID                                                 *big.Int
   556  	IsHomestead, IsEIP150, IsEIP155, IsEIP158               bool
   557  	IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
   558  }
   559  
   560  // Rules ensures c's ChainID is not nil.
   561  func (c *ChainConfig) Rules(num *big.Int) Rules {
   562  	chainID := c.ChainID
   563  	if chainID == nil {
   564  		chainID = new(big.Int)
   565  	}
   566  	return Rules{
   567  		ChainID:          new(big.Int).Set(chainID),
   568  		IsHomestead:      c.IsHomestead(num),
   569  		IsEIP150:         c.IsEIP150(num),
   570  		IsEIP155:         c.IsEIP155(num),
   571  		IsEIP158:         c.IsEIP158(num),
   572  		IsByzantium:      c.IsByzantium(num),
   573  		IsConstantinople: c.IsConstantinople(num),
   574  		IsPetersburg:     c.IsPetersburg(num),
   575  		IsIstanbul:       c.IsIstanbul(num),
   576  	}
   577  }