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