github.com/jpmorganchase/quorum@v21.1.0+incompatible/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  	"errors"
    22  	"fmt"
    23  	"math/big"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/crypto"
    27  )
    28  
    29  // Genesis hashes to enforce below configs on.
    30  var (
    31  	MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
    32  	TestnetGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")
    33  	RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177")
    34  	GoerliGenesisHash  = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a")
    35  )
    36  
    37  // TrustedCheckpoints associates each known checkpoint with the genesis hash of
    38  // the chain it belongs to.
    39  var TrustedCheckpoints = map[common.Hash]*TrustedCheckpoint{
    40  	MainnetGenesisHash: MainnetTrustedCheckpoint,
    41  	TestnetGenesisHash: TestnetTrustedCheckpoint,
    42  	RinkebyGenesisHash: RinkebyTrustedCheckpoint,
    43  	GoerliGenesisHash:  GoerliTrustedCheckpoint,
    44  }
    45  
    46  // CheckpointOracles associates each known checkpoint oracles with the genesis hash of
    47  // the chain it belongs to.
    48  var CheckpointOracles = map[common.Hash]*CheckpointOracleConfig{
    49  	MainnetGenesisHash: MainnetCheckpointOracle,
    50  	TestnetGenesisHash: TestnetCheckpointOracle,
    51  	RinkebyGenesisHash: RinkebyCheckpointOracle,
    52  	GoerliGenesisHash:  GoerliCheckpointOracle,
    53  }
    54  
    55  var (
    56  	// MainnetChainConfig is the chain parameters to run a node on the main network.
    57  	MainnetChainConfig = &ChainConfig{
    58  		ChainID:             big.NewInt(1),
    59  		HomesteadBlock:      big.NewInt(1150000),
    60  		DAOForkBlock:        big.NewInt(1920000),
    61  		DAOForkSupport:      true,
    62  		EIP150Block:         big.NewInt(2463000),
    63  		EIP150Hash:          common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
    64  		EIP155Block:         big.NewInt(2675000),
    65  		EIP158Block:         big.NewInt(2675000),
    66  		ByzantiumBlock:      big.NewInt(4370000),
    67  		ConstantinopleBlock: big.NewInt(7280000),
    68  		PetersburgBlock:     big.NewInt(7280000),
    69  		IstanbulBlock:       big.NewInt(9069000),
    70  		Ethash:              new(EthashConfig),
    71  	}
    72  
    73  	// MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network.
    74  	MainnetTrustedCheckpoint = &TrustedCheckpoint{
    75  		SectionIndex: 270,
    76  		SectionHead:  common.HexToHash("0xb67c33d838a60c282c2fb49b188fbbac1ef8565ffb4a1c4909b0a05885e72e40"),
    77  		CHTRoot:      common.HexToHash("0x781daa4607782300da85d440df3813ba38a1262585231e35e9480726de81dbfc"),
    78  		BloomRoot:    common.HexToHash("0xfd8951fa6d779cbc981df40dc31056ed1a549db529349d7dfae016f9d96cae72"),
    79  	}
    80  
    81  	// MainnetCheckpointOracle contains a set of configs for the main network oracle.
    82  	MainnetCheckpointOracle = &CheckpointOracleConfig{
    83  		Address: common.HexToAddress("0x9a9070028361F7AAbeB3f2F2Dc07F82C4a98A02a"),
    84  		Signers: []common.Address{
    85  			common.HexToAddress("0x1b2C260efc720BE89101890E4Db589b44E950527"), // Peter
    86  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
    87  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
    88  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
    89  			common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
    90  		},
    91  		Threshold: 2,
    92  	}
    93  
    94  	// TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network.
    95  	TestnetChainConfig = &ChainConfig{
    96  		ChainID:             big.NewInt(3),
    97  		HomesteadBlock:      big.NewInt(0),
    98  		DAOForkBlock:        nil,
    99  		DAOForkSupport:      true,
   100  		EIP150Block:         big.NewInt(0),
   101  		EIP150Hash:          common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
   102  		EIP155Block:         big.NewInt(10),
   103  		EIP158Block:         big.NewInt(10),
   104  		ByzantiumBlock:      big.NewInt(1700000),
   105  		ConstantinopleBlock: big.NewInt(4230000),
   106  		PetersburgBlock:     big.NewInt(4939394),
   107  		IstanbulBlock:       big.NewInt(6485846),
   108  		Ethash:              new(EthashConfig),
   109  	}
   110  
   111  	// TestnetTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network.
   112  	TestnetTrustedCheckpoint = &TrustedCheckpoint{
   113  		SectionIndex: 204,
   114  		SectionHead:  common.HexToHash("0xa39168b51c3205456f30ce6a91f3590a43295b15a1c8c2ab86bb8c06b8ad1808"),
   115  		CHTRoot:      common.HexToHash("0x9a3654147b79882bfc4e16fbd3421512aa7e4dfadc6c511923980e0877bdf3b4"),
   116  		BloomRoot:    common.HexToHash("0xe72b979522d94fa45c1331639316da234a9bb85062d64d72e13afe1d3f5c17d5"),
   117  	}
   118  
   119  	// TestnetCheckpointOracle contains a set of configs for the Ropsten test network oracle.
   120  	TestnetCheckpointOracle = &CheckpointOracleConfig{
   121  		Address: common.HexToAddress("0xEF79475013f154E6A65b54cB2742867791bf0B84"),
   122  		Signers: []common.Address{
   123  			common.HexToAddress("0x32162F3581E88a5f62e8A61892B42C46E2c18f7b"), // Peter
   124  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
   125  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
   126  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
   127  			common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
   128  		},
   129  		Threshold: 2,
   130  	}
   131  
   132  	// RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
   133  	RinkebyChainConfig = &ChainConfig{
   134  		ChainID:             big.NewInt(4),
   135  		HomesteadBlock:      big.NewInt(1),
   136  		DAOForkBlock:        nil,
   137  		DAOForkSupport:      true,
   138  		EIP150Block:         big.NewInt(2),
   139  		EIP150Hash:          common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
   140  		EIP155Block:         big.NewInt(3),
   141  		EIP158Block:         big.NewInt(3),
   142  		ByzantiumBlock:      big.NewInt(1035301),
   143  		ConstantinopleBlock: big.NewInt(3660663),
   144  		PetersburgBlock:     big.NewInt(4321234),
   145  		IstanbulBlock:       big.NewInt(5435345),
   146  		Clique: &CliqueConfig{
   147  			Period: 15,
   148  			Epoch:  30000,
   149  		},
   150  	}
   151  
   152  	// RinkebyTrustedCheckpoint contains the light client trusted checkpoint for the Rinkeby test network.
   153  	RinkebyTrustedCheckpoint = &TrustedCheckpoint{
   154  		SectionIndex: 163,
   155  		SectionHead:  common.HexToHash("0x36e5deaa46f258bece94b05d8e10f1ef68f422fb62ed47a2b6e616aa26e84997"),
   156  		CHTRoot:      common.HexToHash("0x829b9feca1c2cdf5a4cf3efac554889e438ee4df8718c2ce3e02555a02d9e9e5"),
   157  		BloomRoot:    common.HexToHash("0x58c01de24fdae7c082ebbe7665f189d0aa4d90ee10e72086bf56651c63269e54"),
   158  	}
   159  
   160  	// RinkebyCheckpointOracle contains a set of configs for the Rinkeby test network oracle.
   161  	RinkebyCheckpointOracle = &CheckpointOracleConfig{
   162  		Address: common.HexToAddress("0xebe8eFA441B9302A0d7eaECc277c09d20D684540"),
   163  		Signers: []common.Address{
   164  			common.HexToAddress("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3"), // Peter
   165  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
   166  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
   167  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
   168  		},
   169  		Threshold: 2,
   170  	}
   171  
   172  	// GoerliChainConfig contains the chain parameters to run a node on the Görli test network.
   173  	GoerliChainConfig = &ChainConfig{
   174  		ChainID:             big.NewInt(5),
   175  		HomesteadBlock:      big.NewInt(0),
   176  		DAOForkBlock:        nil,
   177  		DAOForkSupport:      true,
   178  		EIP150Block:         big.NewInt(0),
   179  		EIP155Block:         big.NewInt(0),
   180  		EIP158Block:         big.NewInt(0),
   181  		ByzantiumBlock:      big.NewInt(0),
   182  		ConstantinopleBlock: big.NewInt(0),
   183  		PetersburgBlock:     big.NewInt(0),
   184  		IstanbulBlock:       big.NewInt(1561651),
   185  		Clique: &CliqueConfig{
   186  			Period: 15,
   187  			Epoch:  30000,
   188  		},
   189  	}
   190  
   191  	// GoerliTrustedCheckpoint contains the light client trusted checkpoint for the Görli test network.
   192  	GoerliTrustedCheckpoint = &TrustedCheckpoint{
   193  		SectionIndex: 47,
   194  		SectionHead:  common.HexToHash("0x00c5b54c6c9a73660501fd9273ccdb4c5bbdbe5d7b8b650e28f881ec9d2337f6"),
   195  		CHTRoot:      common.HexToHash("0xef35caa155fd659f57167e7d507de2f8132cbb31f771526481211d8a977d704c"),
   196  		BloomRoot:    common.HexToHash("0xbda330402f66008d52e7adc748da28535b1212a7912a21244acd2ba77ff0ff06"),
   197  	}
   198  
   199  	// GoerliCheckpointOracle contains a set of configs for the Goerli test network oracle.
   200  	GoerliCheckpointOracle = &CheckpointOracleConfig{
   201  		Address: common.HexToAddress("0x18CA0E045F0D772a851BC7e48357Bcaab0a0795D"),
   202  		Signers: []common.Address{
   203  			common.HexToAddress("0x4769bcaD07e3b938B7f43EB7D278Bc7Cb9efFb38"), // Peter
   204  			common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
   205  			common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
   206  			common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
   207  			common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
   208  		},
   209  		Threshold: 2,
   210  	}
   211  
   212  	// AllEthashProtocolChanges contains every protocol change (EIPs) introduced
   213  	// and accepted by the Ethereum core developers into the Ethash consensus.
   214  	//
   215  	// This configuration is intentionally not using keyed fields to force anyone
   216  	// adding flags to the config to also have to set these fields.
   217  	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, new(EthashConfig), nil, nil, false, 32, 35, big.NewInt(0), big.NewInt(0), nil, nil}
   218  
   219  	// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
   220  	// and accepted by the Ethereum core developers into the Clique consensus.
   221  	//
   222  	// This configuration is intentionally not using keyed fields to force anyone
   223  	// adding flags to the config to also have to set these fields.
   224  	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, &CliqueConfig{Period: 0, Epoch: 30000}, nil, false, 32, 32, big.NewInt(0), big.NewInt(0), nil, nil}
   225  
   226  	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), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil, false, 32, 32, big.NewInt(0), big.NewInt(0), nil, nil}
   227  	TestRules       = TestChainConfig.Rules(new(big.Int))
   228  
   229  	QuorumTestChainConfig = &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), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil, true, 64, 32, big.NewInt(0), big.NewInt(0), nil, big.NewInt(0)}
   230  )
   231  
   232  // TrustedCheckpoint represents a set of post-processed trie roots (CHT and
   233  // BloomTrie) associated with the appropriate section index and head hash. It is
   234  // used to start light syncing from this checkpoint and avoid downloading the
   235  // entire header chain while still being able to securely access old headers/logs.
   236  type TrustedCheckpoint struct {
   237  	SectionIndex uint64      `json:"sectionIndex"`
   238  	SectionHead  common.Hash `json:"sectionHead"`
   239  	CHTRoot      common.Hash `json:"chtRoot"`
   240  	BloomRoot    common.Hash `json:"bloomRoot"`
   241  }
   242  
   243  // HashEqual returns an indicator comparing the itself hash with given one.
   244  func (c *TrustedCheckpoint) HashEqual(hash common.Hash) bool {
   245  	if c.Empty() {
   246  		return hash == common.Hash{}
   247  	}
   248  	return c.Hash() == hash
   249  }
   250  
   251  // Hash returns the hash of checkpoint's four key fields(index, sectionHead, chtRoot and bloomTrieRoot).
   252  func (c *TrustedCheckpoint) Hash() common.Hash {
   253  	buf := make([]byte, 8+3*common.HashLength)
   254  	binary.BigEndian.PutUint64(buf, c.SectionIndex)
   255  	copy(buf[8:], c.SectionHead.Bytes())
   256  	copy(buf[8+common.HashLength:], c.CHTRoot.Bytes())
   257  	copy(buf[8+2*common.HashLength:], c.BloomRoot.Bytes())
   258  	return crypto.Keccak256Hash(buf)
   259  }
   260  
   261  // Empty returns an indicator whether the checkpoint is regarded as empty.
   262  func (c *TrustedCheckpoint) Empty() bool {
   263  	return c.SectionHead == (common.Hash{}) || c.CHTRoot == (common.Hash{}) || c.BloomRoot == (common.Hash{})
   264  }
   265  
   266  // CheckpointOracleConfig represents a set of checkpoint contract(which acts as an oracle)
   267  // config which used for light client checkpoint syncing.
   268  type CheckpointOracleConfig struct {
   269  	Address   common.Address   `json:"address"`
   270  	Signers   []common.Address `json:"signers"`
   271  	Threshold uint64           `json:"threshold"`
   272  }
   273  
   274  type MaxCodeConfigStruct struct {
   275  	Block *big.Int `json:"block,omitempty"`
   276  	Size  uint64   `json:"size,omitempty"`
   277  }
   278  
   279  // ChainConfig is the core config which determines the blockchain settings.
   280  //
   281  // ChainConfig is stored in the database on a per block basis. This means
   282  // that any network, identified by its genesis block, can have its own
   283  // set of configuration options.
   284  type ChainConfig struct {
   285  	ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
   286  
   287  	HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)
   288  
   289  	DAOForkBlock   *big.Int `json:"daoForkBlock,omitempty"`   // TheDAO hard-fork switch block (nil = no fork)
   290  	DAOForkSupport bool     `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
   291  
   292  	// EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
   293  	EIP150Block *big.Int    `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork)
   294  	EIP150Hash  common.Hash `json:"eip150Hash,omitempty"`  // EIP150 HF hash (needed for header only clients as only gas pricing changed)
   295  
   296  	EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
   297  	EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block
   298  
   299  	ByzantiumBlock      *big.Int `json:"byzantiumBlock,omitempty"`      // Byzantium switch block (nil = no fork, 0 = already on byzantium)
   300  	ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
   301  	PetersburgBlock     *big.Int `json:"petersburgBlock,omitempty"`     // Petersburg switch block (nil = same as Constantinople)
   302  	IstanbulBlock       *big.Int `json:"istanbulBlock,omitempty"`       // Istanbul switch block (nil = no fork, 0 = already on istanbul)
   303  	EWASMBlock          *big.Int `json:"ewasmBlock,omitempty"`          // EWASM switch block (nil = no fork, 0 = already activated)
   304  
   305  	// Various consensus engines
   306  	Ethash   *EthashConfig   `json:"ethash,omitempty"`
   307  	Clique   *CliqueConfig   `json:"clique,omitempty"`
   308  	Istanbul *IstanbulConfig `json:"istanbul,omitempty"`
   309  
   310  	IsQuorum             bool   `json:"isQuorum"`     // Quorum flag
   311  	TransactionSizeLimit uint64 `json:"txnSizeLimit"` // Quorum - transaction size limit
   312  	MaxCodeSize          uint64 `json:"maxCodeSize"`  // Quorum -  maximum CodeSize of contract
   313  	// Quorum
   314  	//
   315  	// QIP714Block implements the permissions related changes
   316  	QIP714Block            *big.Int `json:"qip714Block,omitempty"`
   317  	MaxCodeSizeChangeBlock *big.Int `json:"maxCodeSizeChangeBlock,omitempty"`
   318  	// to track multiple changes to maxCodeSize
   319  	MaxCodeSizeConfig []MaxCodeConfigStruct `json:"maxCodeSizeConfig,omitempty"`
   320  	// Quorum
   321  
   322  	PrivacyEnhancementsBlock *big.Int `json:"privacyEnhancementsBlock,omitempty"`
   323  }
   324  
   325  // EthashConfig is the consensus engine configs for proof-of-work based sealing.
   326  type EthashConfig struct{}
   327  
   328  // String implements the stringer interface, returning the consensus engine details.
   329  func (c *EthashConfig) String() string {
   330  	return "ethash"
   331  }
   332  
   333  // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
   334  type CliqueConfig struct {
   335  	Period                 uint64 `json:"period"`                 // Number of seconds between blocks to enforce
   336  	Epoch                  uint64 `json:"epoch"`                  // Epoch length to reset votes and checkpoint
   337  	AllowedFutureBlockTime uint64 `json:"allowedFutureBlockTime"` // Max time (in seconds) from current time allowed for blocks, before they're considered future blocks
   338  }
   339  
   340  // String implements the stringer interface, returning the consensus engine details.
   341  func (c *CliqueConfig) String() string {
   342  	return "clique"
   343  }
   344  
   345  // IstanbulConfig is the consensus engine configs for Istanbul based sealing.
   346  type IstanbulConfig struct {
   347  	Epoch          uint64   `json:"epoch"`                    // Epoch length to reset votes and checkpoint
   348  	ProposerPolicy uint64   `json:"policy"`                   // The policy for proposer selection
   349  	Ceil2Nby3Block *big.Int `json:"ceil2Nby3Block,omitempty"` // Number of confirmations required to move from one state to next [2F + 1 to Ceil(2N/3)]
   350  }
   351  
   352  // String implements the stringer interface, returning the consensus engine details.
   353  func (c *IstanbulConfig) String() string {
   354  	return "istanbul"
   355  }
   356  
   357  // String implements the fmt.Stringer interface.
   358  func (c *ChainConfig) String() string {
   359  	var engine interface{}
   360  	switch {
   361  	case c.Ethash != nil:
   362  		engine = c.Ethash
   363  	case c.Clique != nil:
   364  		engine = c.Clique
   365  	case c.Istanbul != nil:
   366  		engine = c.Istanbul
   367  	default:
   368  		engine = "unknown"
   369  	}
   370  	return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v IsQuorum: %v Constantinople: %v TransactionSizeLimit: %v MaxCodeSize: %v Petersburg: %v Istanbul: %v PrivacyEnhancements: %v Engine: %v}",
   371  		c.ChainID,
   372  		c.HomesteadBlock,
   373  		c.DAOForkBlock,
   374  		c.DAOForkSupport,
   375  		c.EIP150Block,
   376  		c.EIP155Block,
   377  		c.EIP158Block,
   378  		c.ByzantiumBlock,
   379  		c.IsQuorum,
   380  		c.ConstantinopleBlock,
   381  		c.TransactionSizeLimit,
   382  		c.MaxCodeSize,
   383  		c.PetersburgBlock,
   384  		c.IstanbulBlock,
   385  		c.PrivacyEnhancementsBlock,
   386  		engine,
   387  	)
   388  }
   389  
   390  // Quorum - validate code size and transaction size limit
   391  func (c *ChainConfig) IsValid() error {
   392  
   393  	if c.TransactionSizeLimit < 32 || c.TransactionSizeLimit > 128 {
   394  		return errors.New("Genesis transaction size limit must be between 32 and 128")
   395  	}
   396  
   397  	if c.MaxCodeSize != 0 && (c.MaxCodeSize < 24 || c.MaxCodeSize > 128) {
   398  		return errors.New("Genesis max code size must be between 24 and 128")
   399  	}
   400  
   401  	return nil
   402  }
   403  
   404  // IsHomestead returns whether num is either equal to the homestead block or greater.
   405  func (c *ChainConfig) IsHomestead(num *big.Int) bool {
   406  	return isForked(c.HomesteadBlock, num)
   407  }
   408  
   409  // IsDAOFork returns whether num is either equal to the DAO fork block or greater.
   410  func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
   411  	return isForked(c.DAOForkBlock, num)
   412  }
   413  
   414  // IsEIP150 returns whether num is either equal to the EIP150 fork block or greater.
   415  func (c *ChainConfig) IsEIP150(num *big.Int) bool {
   416  	return isForked(c.EIP150Block, num)
   417  }
   418  
   419  // IsEIP155 returns whether num is either equal to the EIP155 fork block or greater.
   420  func (c *ChainConfig) IsEIP155(num *big.Int) bool {
   421  	return isForked(c.EIP155Block, num)
   422  }
   423  
   424  // IsEIP158 returns whether num is either equal to the EIP158 fork block or greater.
   425  func (c *ChainConfig) IsEIP158(num *big.Int) bool {
   426  	return isForked(c.EIP158Block, num)
   427  }
   428  
   429  // IsByzantium returns whether num is either equal to the Byzantium fork block or greater.
   430  func (c *ChainConfig) IsByzantium(num *big.Int) bool {
   431  	return isForked(c.ByzantiumBlock, num)
   432  }
   433  
   434  // IsConstantinople returns whether num is either equal to the Constantinople fork block or greater.
   435  func (c *ChainConfig) IsConstantinople(num *big.Int) bool {
   436  	return isForked(c.ConstantinopleBlock, num)
   437  }
   438  
   439  // IsPetersburg returns whether num is either
   440  // - equal to or greater than the PetersburgBlock fork block,
   441  // - OR is nil, and Constantinople is active
   442  func (c *ChainConfig) IsPetersburg(num *big.Int) bool {
   443  	return isForked(c.PetersburgBlock, num) || c.PetersburgBlock == nil && isForked(c.ConstantinopleBlock, num)
   444  }
   445  
   446  // IsIstanbul returns whether num is either equal to the Istanbul fork block or greater.
   447  func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
   448  	return isForked(c.IstanbulBlock, num)
   449  }
   450  
   451  // IsEWASM returns whether num represents a block number after the EWASM fork
   452  func (c *ChainConfig) IsEWASM(num *big.Int) bool {
   453  	return isForked(c.EWASMBlock, num)
   454  }
   455  
   456  // Quorum
   457  //
   458  // IsQIP714 returns whether num represents a block number where permissions is enabled
   459  func (c *ChainConfig) IsQIP714(num *big.Int) bool {
   460  	return isForked(c.QIP714Block, num)
   461  }
   462  
   463  // IsMaxCodeSizeChangeBlock returns whether num represents a block number
   464  // where maxCodeSize change was done
   465  func (c *ChainConfig) IsMaxCodeSizeChangeBlock(num *big.Int) bool {
   466  	return isForked(c.MaxCodeSizeChangeBlock, num)
   467  }
   468  
   469  // Quorum
   470  //
   471  // GetMaxCodeSize returns maxCodeSize for the given block number
   472  func (c *ChainConfig) GetMaxCodeSize(num *big.Int) int {
   473  	maxCodeSize := MaxCodeSize
   474  
   475  	if len(c.MaxCodeSizeConfig) > 0 {
   476  		for _, data := range c.MaxCodeSizeConfig {
   477  			if data.Block.Cmp(num) > 0 {
   478  				break
   479  			}
   480  			maxCodeSize = int(data.Size) * 1024
   481  		}
   482  	} else if c.MaxCodeSize > 0 {
   483  		if c.MaxCodeSizeChangeBlock != nil && c.MaxCodeSizeChangeBlock.Cmp(big.NewInt(0)) >= 0 {
   484  			if c.IsMaxCodeSizeChangeBlock(num) {
   485  				maxCodeSize = int(c.MaxCodeSize) * 1024
   486  			}
   487  		} else {
   488  			maxCodeSize = int(c.MaxCodeSize) * 1024
   489  		}
   490  	}
   491  	return maxCodeSize
   492  }
   493  
   494  // validates the maxCodeSizeConfig data passed in config
   495  func (c *ChainConfig) CheckMaxCodeConfigData() error {
   496  	if c.MaxCodeSize != 0 || (c.MaxCodeSizeChangeBlock != nil && c.MaxCodeSizeChangeBlock.Cmp(big.NewInt(0)) >= 0) {
   497  		return errors.New("maxCodeSize & maxCodeSizeChangeBlock deprecated. Consider using maxCodeSizeConfig")
   498  	}
   499  	// validate max code size data
   500  	// 1. Code size should not be less than 24 and greater than 128
   501  	// 2. block entries are in ascending order
   502  	prevBlock := big.NewInt(0)
   503  	for _, data := range c.MaxCodeSizeConfig {
   504  		if data.Size < 24 || data.Size > 128 {
   505  			return errors.New("Genesis max code size must be between 24 and 128")
   506  		}
   507  		if data.Block == nil {
   508  			return errors.New("Block number not given in maxCodeSizeConfig data")
   509  		}
   510  		if data.Block.Cmp(prevBlock) < 0 {
   511  			return errors.New("invalid maxCodeSize detail, block order has to be ascending")
   512  		}
   513  		prevBlock = data.Block
   514  	}
   515  
   516  	return nil
   517  }
   518  
   519  // checks if changes to maxCodeSizeConfig proposed are compatible
   520  // with already existing genesis data
   521  func isMaxCodeSizeConfigCompatible(c1, c2 *ChainConfig, head *big.Int) (error, *big.Int, *big.Int) {
   522  	if len(c1.MaxCodeSizeConfig) == 0 && len(c2.MaxCodeSizeConfig) == 0 {
   523  		// maxCodeSizeConfig not used. return
   524  		return nil, big.NewInt(0), big.NewInt(0)
   525  	}
   526  
   527  	// existing config had maxCodeSizeConfig and new one does not have the same return error
   528  	if len(c1.MaxCodeSizeConfig) > 0 && len(c2.MaxCodeSizeConfig) == 0 {
   529  		return fmt.Errorf("genesis file missing max code size information"), head, head
   530  	}
   531  
   532  	if len(c2.MaxCodeSizeConfig) > 0 && len(c1.MaxCodeSizeConfig) == 0 {
   533  		return nil, big.NewInt(0), big.NewInt(0)
   534  	}
   535  
   536  	// check the number of records below current head in both configs
   537  	// if they do not match throw an error
   538  	c1RecsBelowHead := 0
   539  	for _, data := range c1.MaxCodeSizeConfig {
   540  		if data.Block.Cmp(head) <= 0 {
   541  			c1RecsBelowHead++
   542  		} else {
   543  			break
   544  		}
   545  	}
   546  
   547  	c2RecsBelowHead := 0
   548  	for _, data := range c2.MaxCodeSizeConfig {
   549  		if data.Block.Cmp(head) <= 0 {
   550  			c2RecsBelowHead++
   551  		} else {
   552  			break
   553  		}
   554  	}
   555  
   556  	// if the count of past records is not matching return error
   557  	if c1RecsBelowHead != c2RecsBelowHead {
   558  		return errors.New("maxCodeSizeConfig data incompatible. updating maxCodeSize for past"), head, head
   559  	}
   560  
   561  	// validate that each past record is matching exactly. if not return error
   562  	for i := 0; i < c1RecsBelowHead; i++ {
   563  		if c1.MaxCodeSizeConfig[i].Block.Cmp(c2.MaxCodeSizeConfig[i].Block) != 0 ||
   564  			c1.MaxCodeSizeConfig[i].Size != c2.MaxCodeSizeConfig[i].Size {
   565  			return errors.New("maxCodeSizeConfig data incompatible. maxCodeSize historical data does not match"), head, head
   566  		}
   567  	}
   568  
   569  	return nil, big.NewInt(0), big.NewInt(0)
   570  }
   571  
   572  // IsPrivacyEnhancementsEnabled returns whether num represents a block number after the PrivacyEnhancementsEnabled fork
   573  func (c *ChainConfig) IsPrivacyEnhancementsEnabled(num *big.Int) bool {
   574  	return isForked(c.PrivacyEnhancementsBlock, num)
   575  }
   576  
   577  // /Quorum
   578  
   579  // CheckCompatible checks whether scheduled fork transitions have been imported
   580  // with a mismatching chain configuration.
   581  func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64, isQuorumEIP155Activated bool) *ConfigCompatError {
   582  	bhead := new(big.Int).SetUint64(height)
   583  
   584  	// check if the maxCodesize data passed is compatible 1st
   585  	// this is being handled separately as it can have breaks
   586  	// at multiple block heights and cannot be handled with in
   587  	// checkCompatible
   588  
   589  	// compare the maxCodeSize data between the old and new config
   590  	err, cBlock, newCfgBlock := isMaxCodeSizeConfigCompatible(c, newcfg, bhead)
   591  	if err != nil {
   592  		return newCompatError(err.Error(), cBlock, newCfgBlock)
   593  	}
   594  
   595  	// Iterate checkCompatible to find the lowest conflict.
   596  	var lasterr *ConfigCompatError
   597  	for {
   598  		err := c.checkCompatible(newcfg, bhead, isQuorumEIP155Activated)
   599  		if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
   600  			break
   601  		}
   602  		lasterr = err
   603  		bhead.SetUint64(err.RewindTo)
   604  	}
   605  	return lasterr
   606  }
   607  
   608  // CheckConfigForkOrder checks that we don't "skip" any forks, geth isn't pluggable enough
   609  // to guarantee that forks
   610  func (c *ChainConfig) CheckConfigForkOrder() error {
   611  	type fork struct {
   612  		name  string
   613  		block *big.Int
   614  	}
   615  	var lastFork fork
   616  	for _, cur := range []fork{
   617  		{"homesteadBlock", c.HomesteadBlock},
   618  		{"eip150Block", c.EIP150Block},
   619  		{"eip155Block", c.EIP155Block},
   620  		{"eip158Block", c.EIP158Block},
   621  		{"byzantiumBlock", c.ByzantiumBlock},
   622  		{"constantinopleBlock", c.ConstantinopleBlock},
   623  		{"petersburgBlock", c.PetersburgBlock},
   624  		{"istanbulBlock", c.IstanbulBlock},
   625  	} {
   626  		if lastFork.name != "" {
   627  			// Next one must be higher number
   628  			if lastFork.block == nil && cur.block != nil {
   629  				return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at %v",
   630  					lastFork.name, cur.name, cur.block)
   631  			}
   632  			if lastFork.block != nil && cur.block != nil {
   633  				if lastFork.block.Cmp(cur.block) > 0 {
   634  					return fmt.Errorf("unsupported fork ordering: %v enabled at %v, but %v enabled at %v",
   635  						lastFork.name, lastFork.block, cur.name, cur.block)
   636  				}
   637  			}
   638  		}
   639  		lastFork = cur
   640  	}
   641  	return nil
   642  }
   643  
   644  func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int, isQuorumEIP155Activated bool) *ConfigCompatError {
   645  	if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
   646  		return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
   647  	}
   648  	if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
   649  		return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
   650  	}
   651  	if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
   652  		return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
   653  	}
   654  	if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
   655  		return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
   656  	}
   657  	if isQuorumEIP155Activated && c.ChainID != nil && isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
   658  		return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
   659  	}
   660  	if isQuorumEIP155Activated && c.ChainID != nil && c.IsEIP155(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
   661  		return newCompatError("EIP155 chain ID", c.ChainID, newcfg.ChainID)
   662  	}
   663  	if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
   664  		return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
   665  	}
   666  	if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
   667  		return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
   668  	}
   669  	if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
   670  		return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
   671  	}
   672  	if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) {
   673  		return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock)
   674  	}
   675  	if isForkIncompatible(c.PetersburgBlock, newcfg.PetersburgBlock, head) {
   676  		return newCompatError("Petersburg fork block", c.PetersburgBlock, newcfg.PetersburgBlock)
   677  	}
   678  	if isForkIncompatible(c.IstanbulBlock, newcfg.IstanbulBlock, head) {
   679  		return newCompatError("Istanbul fork block", c.IstanbulBlock, newcfg.IstanbulBlock)
   680  	}
   681  	if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) {
   682  		return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock)
   683  	}
   684  	if c.Istanbul != nil && newcfg.Istanbul != nil && isForkIncompatible(c.Istanbul.Ceil2Nby3Block, newcfg.Istanbul.Ceil2Nby3Block, head) {
   685  		return newCompatError("Ceil 2N/3 fork block", c.Istanbul.Ceil2Nby3Block, newcfg.Istanbul.Ceil2Nby3Block)
   686  	}
   687  	if isForkIncompatible(c.QIP714Block, newcfg.QIP714Block, head) {
   688  		return newCompatError("permissions fork block", c.QIP714Block, newcfg.QIP714Block)
   689  	}
   690  	if newcfg.MaxCodeSizeChangeBlock != nil && isForkIncompatible(c.MaxCodeSizeChangeBlock, newcfg.MaxCodeSizeChangeBlock, head) {
   691  		return newCompatError("max code size change fork block", c.MaxCodeSizeChangeBlock, newcfg.MaxCodeSizeChangeBlock)
   692  	}
   693  	if isForkIncompatible(c.PrivacyEnhancementsBlock, newcfg.PrivacyEnhancementsBlock, head) {
   694  		return newCompatError("Privacy Enhancements fork block", c.PrivacyEnhancementsBlock, newcfg.PrivacyEnhancementsBlock)
   695  	}
   696  	return nil
   697  }
   698  
   699  // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
   700  // block s2 because head is already past the fork.
   701  func isForkIncompatible(s1, s2, head *big.Int) bool {
   702  	return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
   703  }
   704  
   705  // isForked returns whether a fork scheduled at block s is active at the given head block.
   706  func isForked(s, head *big.Int) bool {
   707  	if s == nil || head == nil {
   708  		return false
   709  	}
   710  	return s.Cmp(head) <= 0
   711  }
   712  
   713  func configNumEqual(x, y *big.Int) bool {
   714  	if x == nil {
   715  		return y == nil
   716  	}
   717  	if y == nil {
   718  		return x == nil
   719  	}
   720  	return x.Cmp(y) == 0
   721  }
   722  
   723  // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
   724  // ChainConfig that would alter the past.
   725  type ConfigCompatError struct {
   726  	What string
   727  	// block numbers of the stored and new configurations
   728  	StoredConfig, NewConfig *big.Int
   729  	// the block number to which the local chain must be rewound to correct the error
   730  	RewindTo uint64
   731  }
   732  
   733  func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
   734  	var rew *big.Int
   735  	switch {
   736  	case storedblock == nil:
   737  		rew = newblock
   738  	case newblock == nil || storedblock.Cmp(newblock) < 0:
   739  		rew = storedblock
   740  	default:
   741  		rew = newblock
   742  	}
   743  	err := &ConfigCompatError{what, storedblock, newblock, 0}
   744  	if rew != nil && rew.Sign() > 0 {
   745  		err.RewindTo = rew.Uint64() - 1
   746  	}
   747  	return err
   748  }
   749  
   750  func (err *ConfigCompatError) Error() string {
   751  	return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
   752  }
   753  
   754  // Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
   755  // that do not have or require information about the block.
   756  //
   757  // Rules is a one time interface meaning that it shouldn't be used in between transition
   758  // phases.
   759  type Rules struct {
   760  	ChainID                                                 *big.Int
   761  	IsHomestead, IsEIP150, IsEIP155, IsEIP158               bool
   762  	IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
   763  	IsPrivacyEnhancementsEnabled                            bool
   764  }
   765  
   766  // Rules ensures c's ChainID is not nil.
   767  func (c *ChainConfig) Rules(num *big.Int) Rules {
   768  	chainID := c.ChainID
   769  	if chainID == nil {
   770  		chainID = new(big.Int)
   771  	}
   772  	return Rules{
   773  		ChainID:                      new(big.Int).Set(chainID),
   774  		IsHomestead:                  c.IsHomestead(num),
   775  		IsEIP150:                     c.IsEIP150(num),
   776  		IsEIP155:                     c.IsEIP155(num),
   777  		IsEIP158:                     c.IsEIP158(num),
   778  		IsByzantium:                  c.IsByzantium(num),
   779  		IsConstantinople:             c.IsConstantinople(num),
   780  		IsPetersburg:                 c.IsPetersburg(num),
   781  		IsIstanbul:                   c.IsIstanbul(num),
   782  		IsPrivacyEnhancementsEnabled: c.IsPrivacyEnhancementsEnabled(num),
   783  	}
   784  }