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