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