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