github.com/zjj1991/quorum@v0.0.0-20190524123704-ae4b0a1e1a19/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  	"errors"
    21  	"fmt"
    22  	"math"
    23  	"math/big"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  )
    27  
    28  // Genesis hashes to enforce below configs on.
    29  var (
    30  	MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
    31  	TestnetGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")
    32  	RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177")
    33  )
    34  
    35  var (
    36  	// MainnetChainConfig is the chain parameters to run a node on the main network.
    37  	MainnetChainConfig = &ChainConfig{
    38  		ChainID:             big.NewInt(1),
    39  		HomesteadBlock:      big.NewInt(1150000),
    40  		DAOForkBlock:        big.NewInt(1920000),
    41  		DAOForkSupport:      true,
    42  		EIP150Block:         big.NewInt(2463000),
    43  		EIP150Hash:          common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
    44  		EIP155Block:         big.NewInt(2675000),
    45  		EIP158Block:         big.NewInt(2675000),
    46  		ByzantiumBlock:      big.NewInt(4370000),
    47  		ConstantinopleBlock: nil,
    48  		Ethash:              new(EthashConfig),
    49  	}
    50  
    51  	// MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network.
    52  	MainnetTrustedCheckpoint = &TrustedCheckpoint{
    53  		Name:         "mainnet",
    54  		SectionIndex: 203,
    55  		SectionHead:  common.HexToHash("0xc9e05fc67c6a9815adc8072eb18805b53da53a9a6a273e05541e1b7542cf937a"),
    56  		CHTRoot:      common.HexToHash("0xb85f42447d59f7c3e6679b9a37ed983593fd52efd6251b883592662e95769d5b"),
    57  		BloomRoot:    common.HexToHash("0xf93d50cb4c49b403c6fd33cd60896d3b36184275be0a51bae4df5e8844ac624c"),
    58  	}
    59  
    60  	// TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network.
    61  	TestnetChainConfig = &ChainConfig{
    62  		ChainID:             big.NewInt(3),
    63  		HomesteadBlock:      big.NewInt(0),
    64  		DAOForkBlock:        nil,
    65  		DAOForkSupport:      true,
    66  		EIP150Block:         big.NewInt(0),
    67  		EIP150Hash:          common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
    68  		EIP155Block:         big.NewInt(10),
    69  		EIP158Block:         big.NewInt(10),
    70  		ByzantiumBlock:      big.NewInt(1700000),
    71  		ConstantinopleBlock: big.NewInt(4230000),
    72  		Ethash:              new(EthashConfig),
    73  	}
    74  
    75  	// TestnetTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network.
    76  	TestnetTrustedCheckpoint = &TrustedCheckpoint{
    77  		Name:         "testnet",
    78  		SectionIndex: 134,
    79  		SectionHead:  common.HexToHash("0x17053ecbe045bebefaa01e7716cc85a4e22647e181416cc1098ccbb73a088931"),
    80  		CHTRoot:      common.HexToHash("0x4d2b86422e46ed76f0e3f50f06632c409f809c8375e53c8bc0f782bcb93dd49a"),
    81  		BloomRoot:    common.HexToHash("0xccba62232ee56c2967afc58f136a47ba7dc545ae586e6be666430d94516306c7"),
    82  	}
    83  
    84  	// RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
    85  	RinkebyChainConfig = &ChainConfig{
    86  		ChainID:             big.NewInt(4),
    87  		HomesteadBlock:      big.NewInt(1),
    88  		DAOForkBlock:        nil,
    89  		DAOForkSupport:      true,
    90  		EIP150Block:         big.NewInt(2),
    91  		EIP150Hash:          common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
    92  		EIP155Block:         big.NewInt(3),
    93  		EIP158Block:         big.NewInt(3),
    94  		ByzantiumBlock:      big.NewInt(1035301),
    95  		ConstantinopleBlock: nil,
    96  		Clique: &CliqueConfig{
    97  			Period: 15,
    98  			Epoch:  30000,
    99  		},
   100  	}
   101  
   102  	// RinkebyTrustedCheckpoint contains the light client trusted checkpoint for the Rinkeby test network.
   103  	RinkebyTrustedCheckpoint = &TrustedCheckpoint{
   104  		Name:         "rinkeby",
   105  		SectionIndex: 100,
   106  		SectionHead:  common.HexToHash("0xf18f9b43e16f37b12e68818536ffe455ff18d676274ffdd856a8520ed61bb514"),
   107  		CHTRoot:      common.HexToHash("0x473f5d603b1fedad75d97fd58692130b9ac9ade1aca01eb9363d79bd1c43c791"),
   108  		BloomRoot:    common.HexToHash("0xa39ced3ddbb87e909c7531df2afb6414bea9c9a60ab94da9c6b467535f05326e"),
   109  	}
   110  
   111  	// OttomanChainConfig contains the chain parameters to run a node on the Ottoman test network.
   112  	OttomanChainConfig = &ChainConfig{
   113  		ChainID:             big.NewInt(5),
   114  		HomesteadBlock:      big.NewInt(1),
   115  		DAOForkBlock:        nil,
   116  		DAOForkSupport:      true,
   117  		EIP150Block:         big.NewInt(2),
   118  		EIP150Hash:          common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
   119  		EIP155Block:         big.NewInt(3),
   120  		EIP158Block:         big.NewInt(3),
   121  		ByzantiumBlock:      big.NewInt(math.MaxInt64), // Don't enable yet
   122  		ConstantinopleBlock: nil,
   123  
   124  		Istanbul: &IstanbulConfig{
   125  			Epoch:          30000,
   126  			ProposerPolicy: 0,
   127  		},
   128  	}
   129  
   130  	// AllEthashProtocolChanges contains every protocol change (EIPs) introduced
   131  	// and accepted by the Ethereum core developers into the Ethash consensus.
   132  	//
   133  	// This configuration is intentionally not using keyed fields to force anyone
   134  	// adding flags to the config to also have to set these fields.
   135  	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), nil, new(EthashConfig), nil, nil, false, 32}
   136  
   137  	// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
   138  	// and accepted by the Ethereum core developers into the Clique consensus.
   139  	//
   140  	// This configuration is intentionally not using keyed fields to force anyone
   141  	// adding flags to the config to also have to set these fields.
   142  	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), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil, false, 32}
   143  
   144  	TestChainConfig = &ChainConfig{big.NewInt(10), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil, false, 32}
   145  	TestRules       = TestChainConfig.Rules(new(big.Int))
   146  
   147  	QuorumTestChainConfig = &ChainConfig{big.NewInt(10), big.NewInt(0), nil, false, nil, common.Hash{}, nil, nil, nil, nil, nil, new(EthashConfig), nil, nil, true, 64}
   148  )
   149  
   150  // TrustedCheckpoint represents a set of post-processed trie roots (CHT and
   151  // BloomTrie) associated with the appropriate section index and head hash. It is
   152  // used to start light syncing from this checkpoint and avoid downloading the
   153  // entire header chain while still being able to securely access old headers/logs.
   154  type TrustedCheckpoint struct {
   155  	Name         string      `json:"-"`
   156  	SectionIndex uint64      `json:"sectionIndex"`
   157  	SectionHead  common.Hash `json:"sectionHead"`
   158  	CHTRoot      common.Hash `json:"chtRoot"`
   159  	BloomRoot    common.Hash `json:"bloomRoot"`
   160  }
   161  
   162  // ChainConfig is the core config which determines the blockchain settings.
   163  //
   164  // ChainConfig is stored in the database on a per block basis. This means
   165  // that any network, identified by its genesis block, can have its own
   166  // set of configuration options.
   167  type ChainConfig struct {
   168  	ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
   169  
   170  	HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)
   171  
   172  	DAOForkBlock   *big.Int `json:"daoForkBlock,omitempty"`   // TheDAO hard-fork switch block (nil = no fork)
   173  	DAOForkSupport bool     `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
   174  
   175  	// EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
   176  	EIP150Block *big.Int    `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork)
   177  	EIP150Hash  common.Hash `json:"eip150Hash,omitempty"`  // EIP150 HF hash (needed for header only clients as only gas pricing changed)
   178  
   179  	EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
   180  	EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block
   181  
   182  	ByzantiumBlock      *big.Int `json:"byzantiumBlock,omitempty"`      // Byzantium switch block (nil = no fork, 0 = already on byzantium)
   183  	ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
   184  	EWASMBlock          *big.Int `json:"ewasmBlock,omitempty"`          // EWASM switch block (nil = no fork, 0 = already activated)
   185  
   186  	// Various consensus engines
   187  	Ethash   *EthashConfig   `json:"ethash,omitempty"`
   188  	Clique   *CliqueConfig   `json:"clique,omitempty"`
   189  	Istanbul *IstanbulConfig `json:"istanbul,omitempty"`
   190  
   191  	IsQuorum             bool   `json:"isQuorum"`
   192  	TransactionSizeLimit uint64 `json:"txnSizeLimit"`
   193  }
   194  
   195  // EthashConfig is the consensus engine configs for proof-of-work based sealing.
   196  type EthashConfig struct{}
   197  
   198  // String implements the stringer interface, returning the consensus engine details.
   199  func (c *EthashConfig) String() string {
   200  	return "ethash"
   201  }
   202  
   203  // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
   204  type CliqueConfig struct {
   205  	Period uint64 `json:"period"` // Number of seconds between blocks to enforce
   206  	Epoch  uint64 `json:"epoch"`  // Epoch length to reset votes and checkpoint
   207  }
   208  
   209  // String implements the stringer interface, returning the consensus engine details.
   210  func (c *CliqueConfig) String() string {
   211  	return "clique"
   212  }
   213  
   214  // IstanbulConfig is the consensus engine configs for Istanbul based sealing.
   215  type IstanbulConfig struct {
   216  	Epoch          uint64 `json:"epoch"`  // Epoch length to reset votes and checkpoint
   217  	ProposerPolicy uint64 `json:"policy"` // The policy for proposer selection
   218  }
   219  
   220  // String implements the stringer interface, returning the consensus engine details.
   221  func (c *IstanbulConfig) String() string {
   222  	return "istanbul"
   223  }
   224  
   225  // String implements the fmt.Stringer interface.
   226  func (c *ChainConfig) String() string {
   227  	var engine interface{}
   228  	switch {
   229  	case c.Ethash != nil:
   230  		engine = c.Ethash
   231  	case c.Clique != nil:
   232  		engine = c.Clique
   233  	case c.Istanbul != nil:
   234  		engine = c.Istanbul
   235  	default:
   236  		engine = "unknown"
   237  	}
   238  	return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v IsQuorum: %v Constantinople: %v Engine: %v}",
   239  		c.ChainID,
   240  		c.HomesteadBlock,
   241  		c.DAOForkBlock,
   242  		c.DAOForkSupport,
   243  		c.EIP150Block,
   244  		c.EIP155Block,
   245  		c.EIP158Block,
   246  		c.ByzantiumBlock,
   247  		c.IsQuorum,
   248  		c.ConstantinopleBlock,
   249  		engine,
   250  	)
   251  }
   252  
   253  func (c *ChainConfig) IsValid() error {
   254  	if c.TransactionSizeLimit < 32 || c.TransactionSizeLimit > 128 {
   255  		return errors.New("Genesis transaction size limit must be between 32 and 128")
   256  	}
   257  
   258  	return nil
   259  }
   260  
   261  // IsHomestead returns whether num is either equal to the homestead block or greater.
   262  func (c *ChainConfig) IsHomestead(num *big.Int) bool {
   263  	return isForked(c.HomesteadBlock, num)
   264  }
   265  
   266  // IsDAOFork returns whether num is either equal to the DAO fork block or greater.
   267  func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
   268  	return isForked(c.DAOForkBlock, num)
   269  }
   270  
   271  // IsEIP150 returns whether num is either equal to the EIP150 fork block or greater.
   272  func (c *ChainConfig) IsEIP150(num *big.Int) bool {
   273  	return isForked(c.EIP150Block, num)
   274  }
   275  
   276  // IsEIP155 returns whether num is either equal to the EIP155 fork block or greater.
   277  func (c *ChainConfig) IsEIP155(num *big.Int) bool {
   278  	return isForked(c.EIP155Block, num)
   279  }
   280  
   281  // IsEIP158 returns whether num is either equal to the EIP158 fork block or greater.
   282  func (c *ChainConfig) IsEIP158(num *big.Int) bool {
   283  	return isForked(c.EIP158Block, num)
   284  }
   285  
   286  // IsByzantium returns whether num is either equal to the Byzantium fork block or greater.
   287  func (c *ChainConfig) IsByzantium(num *big.Int) bool {
   288  	return isForked(c.ByzantiumBlock, num)
   289  }
   290  
   291  // IsConstantinople returns whether num is either equal to the Constantinople fork block or greater.
   292  func (c *ChainConfig) IsConstantinople(num *big.Int) bool {
   293  	return isForked(c.ConstantinopleBlock, num)
   294  }
   295  
   296  // IsEWASM returns whether num represents a block number after the EWASM fork
   297  func (c *ChainConfig) IsEWASM(num *big.Int) bool {
   298  	return isForked(c.EWASMBlock, num)
   299  }
   300  
   301  // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice).
   302  //
   303  // The returned GasTable's fields shouldn't, under any circumstances, be changed.
   304  func (c *ChainConfig) GasTable(num *big.Int) GasTable {
   305  	if num == nil {
   306  		return GasTableHomestead
   307  	}
   308  	switch {
   309  	case c.IsConstantinople(num):
   310  		return GasTableConstantinople
   311  	case c.IsEIP158(num):
   312  		return GasTableEIP158
   313  	case c.IsEIP150(num):
   314  		return GasTableEIP150
   315  	default:
   316  		return GasTableHomestead
   317  	}
   318  }
   319  
   320  // CheckCompatible checks whether scheduled fork transitions have been imported
   321  // with a mismatching chain configuration.
   322  func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64, isQuorumEIP155Activated bool) *ConfigCompatError {
   323  	bhead := new(big.Int).SetUint64(height)
   324  
   325  	// Iterate checkCompatible to find the lowest conflict.
   326  	var lasterr *ConfigCompatError
   327  	for {
   328  		err := c.checkCompatible(newcfg, bhead, isQuorumEIP155Activated)
   329  		if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
   330  			break
   331  		}
   332  		lasterr = err
   333  		bhead.SetUint64(err.RewindTo)
   334  	}
   335  	return lasterr
   336  }
   337  
   338  func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int, isQuorumEIP155Activated bool) *ConfigCompatError {
   339  	if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
   340  		return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
   341  	}
   342  	if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
   343  		return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
   344  	}
   345  	if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
   346  		return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
   347  	}
   348  	if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
   349  		return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
   350  	}
   351  	if isQuorumEIP155Activated && c.ChainID != nil && isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
   352  		return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
   353  	}
   354  	if isQuorumEIP155Activated && c.ChainID != nil && c.IsEIP155(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
   355  		return newCompatError("EIP155 chain ID", c.ChainID, newcfg.ChainID)
   356  	}
   357  	if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
   358  		return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
   359  	}
   360  	if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
   361  		return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
   362  	}
   363  	if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
   364  		return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
   365  	}
   366  	if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) {
   367  		return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock)
   368  	}
   369  	if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) {
   370  		return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock)
   371  	}
   372  	return nil
   373  }
   374  
   375  // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
   376  // block s2 because head is already past the fork.
   377  func isForkIncompatible(s1, s2, head *big.Int) bool {
   378  	return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
   379  }
   380  
   381  // isForked returns whether a fork scheduled at block s is active at the given head block.
   382  func isForked(s, head *big.Int) bool {
   383  	if s == nil || head == nil {
   384  		return false
   385  	}
   386  	return s.Cmp(head) <= 0
   387  }
   388  
   389  func configNumEqual(x, y *big.Int) bool {
   390  	if x == nil {
   391  		return y == nil
   392  	}
   393  	if y == nil {
   394  		return x == nil
   395  	}
   396  	return x.Cmp(y) == 0
   397  }
   398  
   399  // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
   400  // ChainConfig that would alter the past.
   401  type ConfigCompatError struct {
   402  	What string
   403  	// block numbers of the stored and new configurations
   404  	StoredConfig, NewConfig *big.Int
   405  	// the block number to which the local chain must be rewound to correct the error
   406  	RewindTo uint64
   407  }
   408  
   409  func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
   410  	var rew *big.Int
   411  	switch {
   412  	case storedblock == nil:
   413  		rew = newblock
   414  	case newblock == nil || storedblock.Cmp(newblock) < 0:
   415  		rew = storedblock
   416  	default:
   417  		rew = newblock
   418  	}
   419  	err := &ConfigCompatError{what, storedblock, newblock, 0}
   420  	if rew != nil && rew.Sign() > 0 {
   421  		err.RewindTo = rew.Uint64() - 1
   422  	}
   423  	return err
   424  }
   425  
   426  func (err *ConfigCompatError) Error() string {
   427  	return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
   428  }
   429  
   430  // Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
   431  // that do not have or require information about the block.
   432  //
   433  // Rules is a one time interface meaning that it shouldn't be used in between transition
   434  // phases.
   435  type Rules struct {
   436  	ChainID                                   *big.Int
   437  	IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
   438  	IsByzantium, IsConstantinople             bool
   439  }
   440  
   441  // Rules ensures c's ChainID is not nil.
   442  func (c *ChainConfig) Rules(num *big.Int) Rules {
   443  	chainID := c.ChainID
   444  	if chainID == nil {
   445  		chainID = new(big.Int)
   446  	}
   447  	return Rules{
   448  		ChainID:          new(big.Int).Set(chainID),
   449  		IsHomestead:      c.IsHomestead(num),
   450  		IsEIP150:         c.IsEIP150(num),
   451  		IsEIP155:         c.IsEIP155(num),
   452  		IsEIP158:         c.IsEIP158(num),
   453  		IsByzantium:      c.IsByzantium(num),
   454  		IsConstantinople: c.IsConstantinople(num),
   455  	}
   456  }