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