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