gitlab.com/yannislg/go-pulse@v0.0.0-20210722055913-a3e24e95638d/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  
    35  	BSCGenesisHash    = common.HexToHash("0x0d21840abff46b96c84b2ac9e10e4f5cdaeb5693cb665db62a2f3b02d2d57b5b")
    36  	ChapelGenesisHash = common.HexToHash("0x6d3c66c5357ec91d5c43af47e234a939b22557cbb552dc45bebbceeed90fbe34")
    37  	RialtoGenesisHash = common.HexToHash("0x005dc005bddd1967de6187c1c23be801eb7abdd80cebcc24f341b727b70311d6")
    38  )
    39  
    40  // TrustedCheckpoints associates each known checkpoint with the genesis hash of
    41  // the chain it belongs to.
    42  var TrustedCheckpoints = map[common.Hash]*TrustedCheckpoint{
    43  	MainnetGenesisHash: MainnetTrustedCheckpoint,
    44  	RopstenGenesisHash: RopstenTrustedCheckpoint,
    45  	RinkebyGenesisHash: RinkebyTrustedCheckpoint,
    46  	GoerliGenesisHash:  GoerliTrustedCheckpoint,
    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  }
    57  
    58  var (
    59  	// MainnetChainConfig is the chain parameters to run a node on the main network.
    60  	MainnetChainConfig = &ChainConfig{
    61  		ChainID:             big.NewInt(1),
    62  		HomesteadBlock:      big.NewInt(1150000),
    63  		DAOForkBlock:        big.NewInt(1920000),
    64  		DAOForkSupport:      true,
    65  		EIP150Block:         big.NewInt(2463000),
    66  		EIP150Hash:          common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
    67  		EIP155Block:         big.NewInt(2675000),
    68  		EIP158Block:         big.NewInt(2675000),
    69  		ByzantiumBlock:      big.NewInt(4370000),
    70  		ConstantinopleBlock: big.NewInt(7280000),
    71  		PetersburgBlock:     big.NewInt(7280000),
    72  		IstanbulBlock:       big.NewInt(9069000),
    73  		MuirGlacierBlock:    big.NewInt(9200000),
    74  		Ethash:              new(EthashConfig),
    75  	}
    76  
    77  	// MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network.
    78  	MainnetTrustedCheckpoint = &TrustedCheckpoint{
    79  		SectionIndex: 300,
    80  		SectionHead:  common.HexToHash("0x022d252ffcd289444eed5a4b8c58018aecb2afc9ab0da5fe059a69a7fb618702"),
    81  		CHTRoot:      common.HexToHash("0xe7044c70ae068969573c7f5abe58ef23d9d82d4ee9152ec88b7c6d0cc8ee2714"),
    82  		BloomRoot:    common.HexToHash("0xe22600caa25653abaef00d0c112b07b90f4e3395ce0c1f5f7f791cdd6d30a408"),
    83  	}
    84  
    85  	// MainnetCheckpointOracle contains a set of configs for the main network oracle.
    86  	MainnetCheckpointOracle = &CheckpointOracleConfig{
    87  		Address: common.HexToAddress("0x9a9070028361F7AAbeB3f2F2Dc07F82C4a98A02a"),
    88  		Signers: []common.Address{
    89  			common.HexToAddress("0x1b2C260efc720BE89101890E4Db589b44E950527"), // Peter
    90  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
    91  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
    92  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
    93  			common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
    94  		},
    95  		Threshold: 2,
    96  	}
    97  
    98  	// RopstenChainConfig contains the chain parameters to run a node on the Ropsten test network.
    99  	RopstenChainConfig = &ChainConfig{
   100  		ChainID:             big.NewInt(3),
   101  		HomesteadBlock:      big.NewInt(0),
   102  		DAOForkBlock:        nil,
   103  		DAOForkSupport:      true,
   104  		EIP150Block:         big.NewInt(0),
   105  		EIP150Hash:          common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
   106  		EIP155Block:         big.NewInt(10),
   107  		EIP158Block:         big.NewInt(10),
   108  		ByzantiumBlock:      big.NewInt(1700000),
   109  		ConstantinopleBlock: big.NewInt(4230000),
   110  		PetersburgBlock:     big.NewInt(4939394),
   111  		IstanbulBlock:       big.NewInt(6485846),
   112  		MuirGlacierBlock:    big.NewInt(7117117),
   113  		Ethash:              new(EthashConfig),
   114  	}
   115  
   116  	// RopstenTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network.
   117  	RopstenTrustedCheckpoint = &TrustedCheckpoint{
   118  		SectionIndex: 234,
   119  		SectionHead:  common.HexToHash("0x34659b817e99e6de868b0d4c5321bcff7e36c2cf79307386a2f5053361794d95"),
   120  		CHTRoot:      common.HexToHash("0x249401cd2b07e3f64892729d3f6198514cd11001231a1c001c2e7245659b26e0"),
   121  		BloomRoot:    common.HexToHash("0x37657aa58a07ac3fa13f421c3e5500a944a76def5a11c6d57f17a85f5b33c129"),
   122  	}
   123  
   124  	// RopstenCheckpointOracle contains a set of configs for the Ropsten test network oracle.
   125  	RopstenCheckpointOracle = &CheckpointOracleConfig{
   126  		Address: common.HexToAddress("0xEF79475013f154E6A65b54cB2742867791bf0B84"),
   127  		Signers: []common.Address{
   128  			common.HexToAddress("0x32162F3581E88a5f62e8A61892B42C46E2c18f7b"), // Peter
   129  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
   130  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
   131  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
   132  			common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
   133  		},
   134  		Threshold: 2,
   135  	}
   136  
   137  	// RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
   138  	RinkebyChainConfig = &ChainConfig{
   139  		ChainID:             big.NewInt(4),
   140  		HomesteadBlock:      big.NewInt(1),
   141  		DAOForkBlock:        nil,
   142  		DAOForkSupport:      true,
   143  		EIP150Block:         big.NewInt(2),
   144  		EIP150Hash:          common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
   145  		EIP155Block:         big.NewInt(3),
   146  		EIP158Block:         big.NewInt(3),
   147  		ByzantiumBlock:      big.NewInt(1035301),
   148  		ConstantinopleBlock: big.NewInt(3660663),
   149  		PetersburgBlock:     big.NewInt(4321234),
   150  		IstanbulBlock:       big.NewInt(5435345),
   151  		Clique: &CliqueConfig{
   152  			Period: 15,
   153  			Epoch:  30000,
   154  		},
   155  	}
   156  
   157  	// RinkebyTrustedCheckpoint contains the light client trusted checkpoint for the Rinkeby test network.
   158  	RinkebyTrustedCheckpoint = &TrustedCheckpoint{
   159  		SectionIndex: 191,
   160  		SectionHead:  common.HexToHash("0xfdf3085848b4126048caf176634fd96a208d8a3b055c643e9e32690420df36d5"),
   161  		CHTRoot:      common.HexToHash("0x48059ceb7e0bd25708cc736e5603d28a6f173a3bb904e6e1b3511a97fa30ca97"),
   162  		BloomRoot:    common.HexToHash("0x3566c2b173c0591d5bb4f3ef7e341d82da7577c125fca94e9b51fb7134a676d7"),
   163  	}
   164  
   165  	// RinkebyCheckpointOracle contains a set of configs for the Rinkeby test network oracle.
   166  	RinkebyCheckpointOracle = &CheckpointOracleConfig{
   167  		Address: common.HexToAddress("0xebe8eFA441B9302A0d7eaECc277c09d20D684540"),
   168  		Signers: []common.Address{
   169  			common.HexToAddress("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3"), // Peter
   170  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
   171  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
   172  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
   173  		},
   174  		Threshold: 2,
   175  	}
   176  
   177  	// GoerliChainConfig contains the chain parameters to run a node on the Görli test network.
   178  	GoerliChainConfig = &ChainConfig{
   179  		ChainID:             big.NewInt(5),
   180  		HomesteadBlock:      big.NewInt(0),
   181  		DAOForkBlock:        nil,
   182  		DAOForkSupport:      true,
   183  		EIP150Block:         big.NewInt(0),
   184  		EIP155Block:         big.NewInt(0),
   185  		EIP158Block:         big.NewInt(0),
   186  		ByzantiumBlock:      big.NewInt(0),
   187  		ConstantinopleBlock: big.NewInt(0),
   188  		PetersburgBlock:     big.NewInt(0),
   189  		IstanbulBlock:       big.NewInt(1561651),
   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: 76,
   199  		SectionHead:  common.HexToHash("0xf56ca390d1131767b924d85ee8e039c8a4c4a498cfaf017c1a9abf63ef01ff17"),
   200  		CHTRoot:      common.HexToHash("0x78ffc5eecf514eed42f61e6f6df1bdcd79f9296c462faf6f33bd600f70a2e8b9"),
   201  		BloomRoot:    common.HexToHash("0x5186111a2d6c459cc341319398f7d14fa2c973b1ba846b7f2ec678129c7115fd"),
   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  	BSCChainConfig = &ChainConfig{
   218  		ChainID:             big.NewInt(56),
   219  		HomesteadBlock:      big.NewInt(0),
   220  		EIP150Block:         big.NewInt(0),
   221  		EIP155Block:         big.NewInt(0),
   222  		EIP158Block:         big.NewInt(0),
   223  		ByzantiumBlock:      big.NewInt(0),
   224  		ConstantinopleBlock: big.NewInt(0),
   225  		PetersburgBlock:     big.NewInt(0),
   226  		IstanbulBlock:       big.NewInt(0),
   227  		MuirGlacierBlock:    big.NewInt(0),
   228  		RamanujanBlock:      big.NewInt(0),
   229  		NielsBlock:          big.NewInt(0),
   230  		MirrorSyncBlock:     big.NewInt(5184000),
   231  		Parlia: &ParliaConfig{
   232  			Period: 3,
   233  			Epoch:  200,
   234  			Era:    144,
   235  		},
   236  	}
   237  
   238  	ChapelChainConfig = &ChainConfig{
   239  		ChainID:             big.NewInt(97),
   240  		HomesteadBlock:      big.NewInt(0),
   241  		EIP150Block:         big.NewInt(0),
   242  		EIP155Block:         big.NewInt(0),
   243  		EIP158Block:         big.NewInt(0),
   244  		ByzantiumBlock:      big.NewInt(0),
   245  		ConstantinopleBlock: big.NewInt(0),
   246  		PetersburgBlock:     big.NewInt(0),
   247  		IstanbulBlock:       big.NewInt(0),
   248  		MuirGlacierBlock:    big.NewInt(0),
   249  		RamanujanBlock:      big.NewInt(1010000),
   250  		NielsBlock:          big.NewInt(1014369),
   251  		MirrorSyncBlock:     big.NewInt(5582500),
   252  		Parlia: &ParliaConfig{
   253  			Period: 3,
   254  			Epoch:  200,
   255  			Era:    144,
   256  		},
   257  	}
   258  
   259  	RialtoChainConfig = &ChainConfig{
   260  		ChainID:             big.NewInt(1417),
   261  		HomesteadBlock:      big.NewInt(0),
   262  		EIP150Block:         big.NewInt(0),
   263  		EIP155Block:         big.NewInt(0),
   264  		EIP158Block:         big.NewInt(0),
   265  		ByzantiumBlock:      big.NewInt(0),
   266  		ConstantinopleBlock: big.NewInt(0),
   267  		PetersburgBlock:     big.NewInt(0),
   268  		IstanbulBlock:       big.NewInt(0),
   269  		MuirGlacierBlock:    big.NewInt(0),
   270  		RamanujanBlock:      big.NewInt(400),
   271  		NielsBlock:          big.NewInt(0),
   272  		MirrorSyncBlock:     big.NewInt(400),
   273  		Parlia: &ParliaConfig{
   274  			Period: 3,
   275  			Epoch:  200,
   276  			Era:    144,
   277  		},
   278  	}
   279  
   280  	PulseChainConfig = &ChainConfig{
   281  		ChainID:              big.NewInt(933),
   282  		HomesteadBlock:       big.NewInt(0),
   283  		EIP150Block:          big.NewInt(0),
   284  		EIP155Block:          big.NewInt(0),
   285  		EIP158Block:          big.NewInt(0),
   286  		ByzantiumBlock:       big.NewInt(0),
   287  		ConstantinopleBlock:  big.NewInt(0),
   288  		PetersburgBlock:      big.NewInt(0),
   289  		IstanbulBlock:        big.NewInt(0),
   290  		MuirGlacierBlock:     big.NewInt(0),
   291  		RamanujanBlock:       big.NewInt(0),
   292  		NielsBlock:           big.NewInt(0),
   293  		MirrorSyncBlock:      big.NewInt(0),
   294  		PrimordialPulseBlock: big.NewInt(0),
   295  		Parlia: &ParliaConfig{
   296  			Period: 3,
   297  			Epoch:  200,
   298  			Era:    144,
   299  		},
   300  	}
   301  
   302  	// AllEthashProtocolChanges contains every protocol change (EIPs) introduced
   303  	// and accepted by the Ethereum core developers into the Ethash consensus.
   304  	//
   305  	// This configuration is intentionally not using keyed fields to force anyone
   306  	// adding flags to the config to also have to set these fields.
   307  	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, nil, nil, nil, new(EthashConfig), nil, nil}
   308  
   309  	// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
   310  	// and accepted by the Ethereum core developers into the Clique consensus.
   311  	//
   312  	// This configuration is intentionally not using keyed fields to force anyone
   313  	// adding flags to the config to also have to set these fields.
   314  	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, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil}
   315  
   316  	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, nil, nil, nil, new(EthashConfig), nil, nil}
   317  	TestRules       = TestChainConfig.Rules(new(big.Int))
   318  )
   319  
   320  // TrustedCheckpoint represents a set of post-processed trie roots (CHT and
   321  // BloomTrie) associated with the appropriate section index and head hash. It is
   322  // used to start light syncing from this checkpoint and avoid downloading the
   323  // entire header chain while still being able to securely access old headers/logs.
   324  type TrustedCheckpoint struct {
   325  	SectionIndex uint64      `json:"sectionIndex"`
   326  	SectionHead  common.Hash `json:"sectionHead"`
   327  	CHTRoot      common.Hash `json:"chtRoot"`
   328  	BloomRoot    common.Hash `json:"bloomRoot"`
   329  }
   330  
   331  // HashEqual returns an indicator comparing the itself hash with given one.
   332  func (c *TrustedCheckpoint) HashEqual(hash common.Hash) bool {
   333  	if c.Empty() {
   334  		return hash == common.Hash{}
   335  	}
   336  	return c.Hash() == hash
   337  }
   338  
   339  // Hash returns the hash of checkpoint's four key fields(index, sectionHead, chtRoot and bloomTrieRoot).
   340  func (c *TrustedCheckpoint) Hash() common.Hash {
   341  	buf := make([]byte, 8+3*common.HashLength)
   342  	binary.BigEndian.PutUint64(buf, c.SectionIndex)
   343  	copy(buf[8:], c.SectionHead.Bytes())
   344  	copy(buf[8+common.HashLength:], c.CHTRoot.Bytes())
   345  	copy(buf[8+2*common.HashLength:], c.BloomRoot.Bytes())
   346  	return crypto.Keccak256Hash(buf)
   347  }
   348  
   349  // Empty returns an indicator whether the checkpoint is regarded as empty.
   350  func (c *TrustedCheckpoint) Empty() bool {
   351  	return c.SectionHead == (common.Hash{}) || c.CHTRoot == (common.Hash{}) || c.BloomRoot == (common.Hash{})
   352  }
   353  
   354  // CheckpointOracleConfig represents a set of checkpoint contract(which acts as an oracle)
   355  // config which used for light client checkpoint syncing.
   356  type CheckpointOracleConfig struct {
   357  	Address   common.Address   `json:"address"`
   358  	Signers   []common.Address `json:"signers"`
   359  	Threshold uint64           `json:"threshold"`
   360  }
   361  
   362  // ChainConfig is the core config which determines the blockchain settings.
   363  //
   364  // ChainConfig is stored in the database on a per block basis. This means
   365  // that any network, identified by its genesis block, can have its own
   366  // set of configuration options.
   367  type ChainConfig struct {
   368  	ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
   369  
   370  	HomesteadBlock *big.Int `json:"homesteadBlock,omitempty" toml:",omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)
   371  
   372  	DAOForkBlock   *big.Int `json:"daoForkBlock,omitempty" toml:",omitempty"`   // TheDAO hard-fork switch block (nil = no fork)
   373  	DAOForkSupport bool     `json:"daoForkSupport,omitempty" toml:",omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
   374  
   375  	// EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
   376  	EIP150Block *big.Int    `json:"eip150Block,omitempty" toml:",omitempty"` // EIP150 HF block (nil = no fork)
   377  	EIP150Hash  common.Hash `json:"eip150Hash,omitempty" toml:",omitempty"`  // EIP150 HF hash (needed for header only clients as only gas pricing changed)
   378  
   379  	EIP155Block *big.Int `json:"eip155Block,omitempty" toml:",omitempty"` // EIP155 HF block
   380  	EIP158Block *big.Int `json:"eip158Block,omitempty" toml:",omitempty"` // EIP158 HF block
   381  
   382  	ByzantiumBlock       *big.Int `json:"byzantiumBlock,omitempty" toml:",omitempty"`       // Byzantium switch block (nil = no fork, 0 = already on byzantium)
   383  	ConstantinopleBlock  *big.Int `json:"constantinopleBlock,omitempty" toml:",omitempty"`  // Constantinople switch block (nil = no fork, 0 = already activated)
   384  	PetersburgBlock      *big.Int `json:"petersburgBlock,omitempty" toml:",omitempty"`      // Petersburg switch block (nil = same as Constantinople)
   385  	IstanbulBlock        *big.Int `json:"istanbulBlock,omitempty" toml:",omitempty"`        // Istanbul switch block (nil = no fork, 0 = already on istanbul)
   386  	MuirGlacierBlock     *big.Int `json:"muirGlacierBlock,omitempty" toml:",omitempty"`     // Eip-2384 (bomb delay) switch block (nil = no fork, 0 = already activated)
   387  	EWASMBlock           *big.Int `json:"ewasmBlock,omitempty" toml:",omitempty"`           // EWASM switch block (nil = no fork, 0 = already activated)
   388  	RamanujanBlock       *big.Int `json:"ramanujanBlock,omitempty" toml:",omitempty"`       // ramanujanBlock switch block (nil = no fork, 0 = already activated)
   389  	NielsBlock           *big.Int `json:"nielsBlock,omitempty" toml:",omitempty"`           // nielsBlock switch block (nil = no fork, 0 = already activated)
   390  	MirrorSyncBlock      *big.Int `json:"mirrorSyncBlock,omitempty" toml:",omitempty"`      // mirrorSyncBlock switch block (nil = no fork, 0 = already activated)
   391  	PrimordialPulseBlock *big.Int `json:"primordialPulseBlock,omitempty" toml:",omitempty"` // primordialPulseBlock switch block (nil = no fork, 0 = already activated)
   392  
   393  	// Various consensus engines
   394  	Ethash *EthashConfig `json:"ethash,omitempty" toml:",omitempty"`
   395  	Clique *CliqueConfig `json:"clique,omitempty" toml:",omitempty"`
   396  	Parlia *ParliaConfig `json:"parlia,omitempty" toml:",omitempty"`
   397  }
   398  
   399  // EthashConfig is the consensus engine configs for proof-of-work based sealing.
   400  type EthashConfig struct{}
   401  
   402  // String implements the stringer interface, returning the consensus engine details.
   403  func (c *EthashConfig) String() string {
   404  	return "ethash"
   405  }
   406  
   407  // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
   408  type CliqueConfig struct {
   409  	Period uint64 `json:"period"` // Number of seconds between blocks to enforce
   410  	Epoch  uint64 `json:"epoch"`  // Epoch length to reset votes and checkpoint
   411  }
   412  
   413  // String implements the stringer interface, returning the consensus engine details.
   414  func (c *CliqueConfig) String() string {
   415  	return "clique"
   416  }
   417  
   418  // ParliaConfig is the consensus engine configs for proof-of-staked-authority based sealing.
   419  type ParliaConfig struct {
   420  	Period          uint64            `json:"period"`                                      // Number of seconds between blocks to enforce
   421  	Epoch           uint64            `json:"epoch"`                                       // Number of blocks between validator set updates and checkpoint
   422  	Era             uint64            `json:"era"`                                         // Number of epochs between staked validator rotations
   423  	BurnRate        uint64            `json:"burnRate,omitempty" toml:",omitempty"`        // The portion of transaction fees to burn on every block, where burn = TOTAL_FEES/BurnRate; (0 = no burn)
   424  	InitValidators  *[]string         `json:"initValidators,omitempty" toml:",omitempty"`  // The list of consensus addresses for the initial validatorSet, used for the PrimordialPulseBlock only
   425  	SystemContracts *[]SystemContract `json:"systemContracts,omitempty" toml:",omitempty"` // The list of system contracts to deploy during, used for the PrimordialPulseBlock only
   426  }
   427  
   428  type SystemContract struct {
   429  	Addr string `json:"addr"`
   430  	Code string `json:"code"`
   431  }
   432  
   433  // String implements the stringer interface, returning the consensus engine details.
   434  func (b *ParliaConfig) String() string {
   435  	return "parlia"
   436  }
   437  
   438  // String implements the fmt.Stringer interface.
   439  func (c *ChainConfig) String() string {
   440  	var engine interface{}
   441  	switch {
   442  	case c.Ethash != nil:
   443  		engine = c.Ethash
   444  	case c.Clique != nil:
   445  		engine = c.Clique
   446  	case c.Parlia != nil:
   447  		engine = c.Parlia
   448  	default:
   449  		engine = "unknown"
   450  	}
   451  	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, Ramanujan: %v, Niels: %v, MirrorSync: %v, PrimordialPulse: %v, Engine: %v}",
   452  		c.ChainID,
   453  		c.HomesteadBlock,
   454  		c.DAOForkBlock,
   455  		c.DAOForkSupport,
   456  		c.EIP150Block,
   457  		c.EIP155Block,
   458  		c.EIP158Block,
   459  		c.ByzantiumBlock,
   460  		c.ConstantinopleBlock,
   461  		c.PetersburgBlock,
   462  		c.IstanbulBlock,
   463  		c.MuirGlacierBlock,
   464  		c.RamanujanBlock,
   465  		c.NielsBlock,
   466  		c.MirrorSyncBlock,
   467  		c.PrimordialPulseBlock,
   468  		engine,
   469  	)
   470  }
   471  
   472  // IsHomestead returns whether num is either equal to the homestead block or greater.
   473  func (c *ChainConfig) IsHomestead(num *big.Int) bool {
   474  	return isForked(c.HomesteadBlock, num)
   475  }
   476  
   477  // IsDAOFork returns whether num is either equal to the DAO fork block or greater.
   478  func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
   479  	return isForked(c.DAOForkBlock, num)
   480  }
   481  
   482  // IsEIP150 returns whether num is either equal to the EIP150 fork block or greater.
   483  func (c *ChainConfig) IsEIP150(num *big.Int) bool {
   484  	return isForked(c.EIP150Block, num)
   485  }
   486  
   487  // IsEIP155 returns whether num is either equal to the EIP155 fork block or greater.
   488  func (c *ChainConfig) IsEIP155(num *big.Int) bool {
   489  	return isForked(c.EIP155Block, num)
   490  }
   491  
   492  // IsEIP158 returns whether num is either equal to the EIP158 fork block or greater.
   493  func (c *ChainConfig) IsEIP158(num *big.Int) bool {
   494  	return isForked(c.EIP158Block, num)
   495  }
   496  
   497  // IsByzantium returns whether num is either equal to the Byzantium fork block or greater.
   498  func (c *ChainConfig) IsByzantium(num *big.Int) bool {
   499  	return isForked(c.ByzantiumBlock, num)
   500  }
   501  
   502  // IsConstantinople returns whether num is either equal to the Constantinople fork block or greater.
   503  func (c *ChainConfig) IsConstantinople(num *big.Int) bool {
   504  	return isForked(c.ConstantinopleBlock, num)
   505  }
   506  
   507  // IsMuirGlacier returns whether num is either equal to the Muir Glacier (EIP-2384) fork block or greater.
   508  func (c *ChainConfig) IsMuirGlacier(num *big.Int) bool {
   509  	return isForked(c.MuirGlacierBlock, num)
   510  }
   511  
   512  // IsPetersburg returns whether num is either
   513  // - equal to or greater than the PetersburgBlock fork block,
   514  // - OR is nil, and Constantinople is active
   515  func (c *ChainConfig) IsPetersburg(num *big.Int) bool {
   516  	return isForked(c.PetersburgBlock, num) || c.PetersburgBlock == nil && isForked(c.ConstantinopleBlock, num)
   517  }
   518  
   519  // IsIstanbul returns whether num is either equal to the Istanbul fork block or greater.
   520  func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
   521  	return isForked(c.IstanbulBlock, num)
   522  }
   523  
   524  // IsEWASM returns whether num represents a block number after the EWASM fork
   525  func (c *ChainConfig) IsEWASM(num *big.Int) bool {
   526  	return isForked(c.EWASMBlock, num)
   527  }
   528  
   529  // IsPrimordialPulseBlock returns whether or not the given block is the primordial pulse block.
   530  func (c *ChainConfig) IsPrimordialPulseBlock(number uint64) bool {
   531  	// Returns whether or not the given block is the PrimordialPulseBlock.
   532  	return c.PrimordialPulseBlock != nil && number == c.PrimordialPulseBlock.Uint64()
   533  }
   534  
   535  // Returns true if there is a PrimordialPulse block in the future, indicating this chain
   536  // should still be evaluated using the ethash consensus engine.
   537  func (c *ChainConfig) PrimordialPulseAhead(number *big.Int) bool {
   538  	return c.PrimordialPulseBlock != nil && c.PrimordialPulseBlock.Cmp(number) > 0
   539  }
   540  
   541  // CheckCompatible checks whether scheduled fork transitions have been imported
   542  // with a mismatching chain configuration.
   543  func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
   544  	bhead := new(big.Int).SetUint64(height)
   545  
   546  	// Iterate checkCompatible to find the lowest conflict.
   547  	var lasterr *ConfigCompatError
   548  	for {
   549  		err := c.checkCompatible(newcfg, bhead)
   550  		if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
   551  			break
   552  		}
   553  		lasterr = err
   554  		bhead.SetUint64(err.RewindTo)
   555  	}
   556  	return lasterr
   557  }
   558  
   559  // CheckConfigForkOrder checks that we don't "skip" any forks, geth isn't pluggable enough
   560  // to guarantee that forks can be implemented in a different order than on official networks
   561  func (c *ChainConfig) CheckConfigForkOrder() error {
   562  	type fork struct {
   563  		name  string
   564  		block *big.Int
   565  	}
   566  	var lastFork fork
   567  	for _, cur := range []fork{
   568  		{"homesteadBlock", c.HomesteadBlock},
   569  		{"eip150Block", c.EIP150Block},
   570  		{"eip155Block", c.EIP155Block},
   571  		{"eip158Block", c.EIP158Block},
   572  		{"byzantiumBlock", c.ByzantiumBlock},
   573  		{"constantinopleBlock", c.ConstantinopleBlock},
   574  		{"petersburgBlock", c.PetersburgBlock},
   575  		{"istanbulBlock", c.IstanbulBlock},
   576  		{"muirGlacierBlock", c.MuirGlacierBlock},
   577  		{"ramanujanBlock", c.RamanujanBlock},
   578  		{"mirrorSyncBlock", c.MirrorSyncBlock},
   579  		{"primordialPulseBlock", c.PrimordialPulseBlock},
   580  	} {
   581  		if lastFork.name != "" {
   582  			// Next one must be higher number
   583  			if lastFork.block == nil && cur.block != nil {
   584  				return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at %v",
   585  					lastFork.name, cur.name, cur.block)
   586  			}
   587  			if lastFork.block != nil && cur.block != nil {
   588  				if lastFork.block.Cmp(cur.block) > 0 {
   589  					return fmt.Errorf("unsupported fork ordering: %v enabled at %v, but %v enabled at %v",
   590  						lastFork.name, lastFork.block, cur.name, cur.block)
   591  				}
   592  			}
   593  		}
   594  		lastFork = cur
   595  	}
   596  	return nil
   597  }
   598  
   599  func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
   600  	if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
   601  		return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
   602  	}
   603  	if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
   604  		return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
   605  	}
   606  	if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
   607  		return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
   608  	}
   609  	if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
   610  		return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
   611  	}
   612  	if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
   613  		return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
   614  	}
   615  	if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
   616  		return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
   617  	}
   618  	if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
   619  		return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
   620  	}
   621  	if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
   622  		return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
   623  	}
   624  	if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) {
   625  		return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock)
   626  	}
   627  	if isForkIncompatible(c.PetersburgBlock, newcfg.PetersburgBlock, head) {
   628  		return newCompatError("Petersburg fork block", c.PetersburgBlock, newcfg.PetersburgBlock)
   629  	}
   630  	if isForkIncompatible(c.IstanbulBlock, newcfg.IstanbulBlock, head) {
   631  		return newCompatError("Istanbul fork block", c.IstanbulBlock, newcfg.IstanbulBlock)
   632  	}
   633  	if isForkIncompatible(c.MuirGlacierBlock, newcfg.MuirGlacierBlock, head) {
   634  		return newCompatError("Muir Glacier fork block", c.MuirGlacierBlock, newcfg.MuirGlacierBlock)
   635  	}
   636  	if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) {
   637  		return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock)
   638  	}
   639  	if isForkIncompatible(c.RamanujanBlock, newcfg.RamanujanBlock, head) {
   640  		return newCompatError("ramanujan fork block", c.RamanujanBlock, newcfg.RamanujanBlock)
   641  	}
   642  	if isForkIncompatible(c.MirrorSyncBlock, newcfg.MirrorSyncBlock, head) {
   643  		return newCompatError("mirrorSync fork block", c.MirrorSyncBlock, newcfg.MirrorSyncBlock)
   644  	}
   645  	if isForkIncompatible(c.PrimordialPulseBlock, newcfg.PrimordialPulseBlock, head) {
   646  		return newCompatError("primordialPulse fork block", c.PrimordialPulseBlock, newcfg.PrimordialPulseBlock)
   647  	}
   648  	return nil
   649  }
   650  
   651  // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
   652  // block s2 because head is already past the fork.
   653  func isForkIncompatible(s1, s2, head *big.Int) bool {
   654  	return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
   655  }
   656  
   657  // isForked returns whether a fork scheduled at block s is active at the given head block.
   658  func isForked(s, head *big.Int) bool {
   659  	if s == nil || head == nil {
   660  		return false
   661  	}
   662  	return s.Cmp(head) <= 0
   663  }
   664  
   665  func configNumEqual(x, y *big.Int) bool {
   666  	if x == nil {
   667  		return y == nil
   668  	}
   669  	if y == nil {
   670  		return x == nil
   671  	}
   672  	return x.Cmp(y) == 0
   673  }
   674  
   675  // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
   676  // ChainConfig that would alter the past.
   677  type ConfigCompatError struct {
   678  	What string
   679  	// block numbers of the stored and new configurations
   680  	StoredConfig, NewConfig *big.Int
   681  	// the block number to which the local chain must be rewound to correct the error
   682  	RewindTo uint64
   683  }
   684  
   685  func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
   686  	var rew *big.Int
   687  	switch {
   688  	case storedblock == nil:
   689  		rew = newblock
   690  	case newblock == nil || storedblock.Cmp(newblock) < 0:
   691  		rew = storedblock
   692  	default:
   693  		rew = newblock
   694  	}
   695  	err := &ConfigCompatError{what, storedblock, newblock, 0}
   696  	if rew != nil && rew.Sign() > 0 {
   697  		err.RewindTo = rew.Uint64() - 1
   698  	}
   699  	return err
   700  }
   701  
   702  func (err *ConfigCompatError) Error() string {
   703  	return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
   704  }
   705  
   706  // Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
   707  // that do not have or require information about the block.
   708  //
   709  // Rules is a one time interface meaning that it shouldn't be used in between transition
   710  // phases.
   711  type Rules struct {
   712  	ChainID                                                 *big.Int
   713  	IsHomestead, IsEIP150, IsEIP155, IsEIP158               bool
   714  	IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
   715  }
   716  
   717  // Rules ensures c's ChainID is not nil.
   718  func (c *ChainConfig) Rules(num *big.Int) Rules {
   719  	chainID := c.ChainID
   720  	if chainID == nil {
   721  		chainID = new(big.Int)
   722  	}
   723  	return Rules{
   724  		ChainID:          new(big.Int).Set(chainID),
   725  		IsHomestead:      c.IsHomestead(num),
   726  		IsEIP150:         c.IsEIP150(num),
   727  		IsEIP155:         c.IsEIP155(num),
   728  		IsEIP158:         c.IsEIP158(num),
   729  		IsByzantium:      c.IsByzantium(num),
   730  		IsConstantinople: c.IsConstantinople(num),
   731  		IsPetersburg:     c.IsPetersburg(num),
   732  		IsIstanbul:       c.IsIstanbul(num),
   733  	}
   734  }