github.com/klaytn/klaytn@v1.10.2/params/config.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from params/config.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package params
    22  
    23  import (
    24  	"encoding/json"
    25  	"fmt"
    26  	"math/big"
    27  
    28  	"github.com/klaytn/klaytn/common"
    29  	"github.com/klaytn/klaytn/log"
    30  )
    31  
    32  // Genesis hashes to enforce below configs on.
    33  var (
    34  	CypressGenesisHash      = common.HexToHash("0xc72e5293c3c3ba38ed8ae910f780e4caaa9fb95e79784f7ab74c3c262ea7137e") // cypress genesis hash to enforce below configs on
    35  	BaobabGenesisHash       = common.HexToHash("0xe33ff05ceec2581ca9496f38a2bf9baad5d4eed629e896ccb33d1dc991bc4b4a") // baobab genesis hash to enforce below configs on
    36  	AuthorAddressForTesting = common.HexToAddress("0xc0ea08a2d404d3172d2add29a45be56da40e2949")
    37  	mintingAmount, _        = new(big.Int).SetString("9600000000000000000", 10)
    38  	logger                  = log.NewModuleLogger(log.Governance)
    39  )
    40  
    41  var (
    42  	// CypressChainConfig is the chain parameters to run a node on the cypress main network.
    43  	CypressChainConfig = &ChainConfig{
    44  		ChainID:                  big.NewInt(int64(CypressNetworkId)),
    45  		IstanbulCompatibleBlock:  big.NewInt(86816005),
    46  		LondonCompatibleBlock:    big.NewInt(86816005),
    47  		EthTxTypeCompatibleBlock: big.NewInt(86816005),
    48  		MagmaCompatibleBlock:     big.NewInt(99841497),
    49  		KoreCompatibleBlock:      big.NewInt(119750400),
    50  		Kip103CompatibleBlock:    big.NewInt(119750400),
    51  		Kip103ContractAddress:    common.HexToAddress("0xD5ad6D61Dd87EdabE2332607C328f5cc96aeCB95"),
    52  		DeriveShaImpl:            2,
    53  		Governance: &GovernanceConfig{
    54  			GoverningNode:  common.HexToAddress("0x52d41ca72af615a1ac3301b0a93efa222ecc7541"),
    55  			GovernanceMode: "single",
    56  			Reward: &RewardConfig{
    57  				MintingAmount:          mintingAmount,
    58  				Ratio:                  "34/54/12",
    59  				UseGiniCoeff:           true,
    60  				DeferredTxFee:          true,
    61  				StakingUpdateInterval:  86400,
    62  				ProposerUpdateInterval: 3600,
    63  				MinimumStake:           big.NewInt(5000000),
    64  			},
    65  		},
    66  		Istanbul: &IstanbulConfig{
    67  			Epoch:          604800,
    68  			ProposerPolicy: 2,
    69  			SubGroupSize:   22,
    70  		},
    71  		UnitPrice: 25000000000,
    72  	}
    73  
    74  	// BaobabChainConfig contains the chain parameters to run a node on the Baobab test network.
    75  	BaobabChainConfig = &ChainConfig{
    76  		ChainID:                  big.NewInt(int64(BaobabNetworkId)),
    77  		IstanbulCompatibleBlock:  big.NewInt(75373312),
    78  		LondonCompatibleBlock:    big.NewInt(80295291),
    79  		EthTxTypeCompatibleBlock: big.NewInt(86513895),
    80  		MagmaCompatibleBlock:     big.NewInt(98347376),
    81  		KoreCompatibleBlock:      big.NewInt(111736800),
    82  		Kip103CompatibleBlock:    big.NewInt(119145600),
    83  		Kip103ContractAddress:    common.HexToAddress("0xD5ad6D61Dd87EdabE2332607C328f5cc96aeCB95"),
    84  		DeriveShaImpl:            2,
    85  		Governance: &GovernanceConfig{
    86  			GoverningNode:  common.HexToAddress("0x99fb17d324fa0e07f23b49d09028ac0919414db6"),
    87  			GovernanceMode: "single",
    88  			Reward: &RewardConfig{
    89  				MintingAmount:          mintingAmount,
    90  				Ratio:                  "34/54/12",
    91  				UseGiniCoeff:           true,
    92  				DeferredTxFee:          true,
    93  				StakingUpdateInterval:  86400,
    94  				ProposerUpdateInterval: 3600,
    95  				MinimumStake:           big.NewInt(5000000),
    96  			},
    97  		},
    98  		Istanbul: &IstanbulConfig{
    99  			Epoch:          604800,
   100  			ProposerPolicy: 2,
   101  			SubGroupSize:   22,
   102  		},
   103  		UnitPrice: 25000000000,
   104  	}
   105  
   106  	// AllGxhashProtocolChanges contains every protocol change (GxIPs) introduced
   107  	// and accepted by the klaytn developers into the Klaytn consensus.
   108  	//
   109  	// This configuration is intentionally not using keyed fields to force anyone
   110  	// adding flags to the config to also have to set these fields.
   111  	AllGxhashProtocolChanges = &ChainConfig{
   112  		ChainID:  big.NewInt(0),
   113  		Gxhash:   new(GxhashConfig),
   114  		Clique:   nil,
   115  		Istanbul: nil,
   116  	}
   117  
   118  	// AllCliqueProtocolChanges contains every protocol change (GxIPs) introduced
   119  	// and accepted by the klaytn developers into the Clique consensus.
   120  	//
   121  	// This configuration is intentionally not using keyed fields to force anyone
   122  	// adding flags to the config to also have to set these fields.
   123  	AllCliqueProtocolChanges = &ChainConfig{
   124  		ChainID:  big.NewInt(0),
   125  		Gxhash:   nil,
   126  		Clique:   &CliqueConfig{Period: 0, Epoch: 30000},
   127  		Istanbul: nil,
   128  	}
   129  
   130  	TestChainConfig = &ChainConfig{
   131  		ChainID:       big.NewInt(1),
   132  		Gxhash:        new(GxhashConfig),
   133  		Clique:        nil,
   134  		Istanbul:      nil,
   135  		UnitPrice:     1, // NOTE-Klaytn Use UnitPrice 1 for tests
   136  		DeriveShaImpl: 0,
   137  	}
   138  	TestRules = TestChainConfig.Rules(new(big.Int))
   139  
   140  	// istanbul BFT
   141  	BFTTestChainConfig = &ChainConfig{
   142  		ChainID:  big.NewInt(1),
   143  		Gxhash:   new(GxhashConfig),
   144  		Clique:   nil,
   145  		Istanbul: nil,
   146  	}
   147  )
   148  
   149  // VMLogTarget sets the output target of vmlog.
   150  // The values below can be OR'ed.
   151  //   - 0x0: no output (default)
   152  //   - 0x1: file (DATADIR/logs/vm.log)
   153  //   - 0x2: stdout (like logger.DEBUG)
   154  var VMLogTarget = 0x0
   155  
   156  const (
   157  	VMLogToFile   = 0x1
   158  	VMLogToStdout = 0x2
   159  	VMLogToAll    = VMLogToFile | VMLogToStdout
   160  
   161  	UpperGasLimit = uint64(999999999999)
   162  )
   163  
   164  const (
   165  	PasswordLength = 16
   166  )
   167  
   168  // ChainConfig is the blockchain config which determines the blockchain settings.
   169  //
   170  // ChainConfig is stored in the database on a per block basis. This means
   171  // that any network, identified by its genesis block, can have its own
   172  // set of configuration options.
   173  type ChainConfig struct {
   174  	ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
   175  
   176  	// "Compatible" means that it is EVM compatible(the opcode and precompiled contracts are the same as Ethereum EVM).
   177  	// In other words, not all the hard fork items are included.
   178  	IstanbulCompatibleBlock  *big.Int `json:"istanbulCompatibleBlock,omitempty"`  // IstanbulCompatibleBlock switch block (nil = no fork, 0 = already on istanbul)
   179  	LondonCompatibleBlock    *big.Int `json:"londonCompatibleBlock,omitempty"`    // LondonCompatibleBlock switch block (nil = no fork, 0 = already on london)
   180  	EthTxTypeCompatibleBlock *big.Int `json:"ethTxTypeCompatibleBlock,omitempty"` // EthTxTypeCompatibleBlock switch block (nil = no fork, 0 = already on ethTxType)
   181  	MagmaCompatibleBlock     *big.Int `json:"magmaCompatibleBlock,omitempty"`     // MagmaCompatible switch block (nil = no fork, 0 already on Magma)
   182  	KoreCompatibleBlock      *big.Int `json:"koreCompatibleBlock,omitempty"`      // KoreCompatible switch block (nil = no fork, 0 already on Kore)
   183  
   184  	// KIP103 is a special purpose hardfork feature that can be executed only once
   185  	// Both Kip103CompatibleBlock and Kip103ContractAddress should be specified to enable KIP103
   186  	Kip103CompatibleBlock *big.Int       `json:"kip103CompatibleBlock,omitempty"` // Kip103Compatible activate block (nil = no fork)
   187  	Kip103ContractAddress common.Address `json:"kip103ContractAddress,omitempty"` // Kip103 contract address already deployed on the network
   188  
   189  	// Various consensus engines
   190  	Gxhash   *GxhashConfig   `json:"gxhash,omitempty"` // (deprecated) not supported engine
   191  	Clique   *CliqueConfig   `json:"clique,omitempty"`
   192  	Istanbul *IstanbulConfig `json:"istanbul,omitempty"`
   193  
   194  	UnitPrice     uint64            `json:"unitPrice"`
   195  	DeriveShaImpl int               `json:"deriveShaImpl"`
   196  	Governance    *GovernanceConfig `json:"governance"`
   197  }
   198  
   199  // GovernanceConfig stores governance information for a network
   200  type GovernanceConfig struct {
   201  	GoverningNode    common.Address `json:"governingNode"`
   202  	GovernanceMode   string         `json:"governanceMode"`
   203  	GovParamContract common.Address `json:"govParamContract"`
   204  	Reward           *RewardConfig  `json:"reward,omitempty"`
   205  	KIP71            *KIP71Config   `json:"kip71,omitempty"`
   206  }
   207  
   208  func (g *GovernanceConfig) DeferredTxFee() bool {
   209  	return g.Reward.DeferredTxFee
   210  }
   211  
   212  // RewardConfig stores information about the network's token economy
   213  type RewardConfig struct {
   214  	MintingAmount          *big.Int `json:"mintingAmount"`
   215  	Ratio                  string   `json:"ratio"`                  // Define how much portion of reward be distributed to CN/KFF/KCF
   216  	Kip82Ratio             string   `json:"kip82ratio,omitempty"`   // Define how much portion of reward be distributed to proposer/stakers
   217  	UseGiniCoeff           bool     `json:"useGiniCoeff"`           // Decide if Gini Coefficient will be used or not
   218  	DeferredTxFee          bool     `json:"deferredTxFee"`          // Decide if TX fee will be handled instantly or handled later at block finalization
   219  	StakingUpdateInterval  uint64   `json:"stakingUpdateInterval"`  // Interval when staking information is updated
   220  	ProposerUpdateInterval uint64   `json:"proposerUpdateInterval"` // Interval when proposer information is updated
   221  	MinimumStake           *big.Int `json:"minimumStake"`           // Minimum amount of peb to join CCO
   222  }
   223  
   224  // Magma governance parameters
   225  type KIP71Config struct {
   226  	LowerBoundBaseFee         uint64 `json:"lowerboundbasefee"`         // Minimum base fee for dynamic gas price
   227  	UpperBoundBaseFee         uint64 `json:"upperboundbasefee"`         // Maximum base fee for dynamic gas price
   228  	GasTarget                 uint64 `json:"gastarget"`                 // Gauge parameter increasing or decreasing gas price
   229  	MaxBlockGasUsedForBaseFee uint64 `json:"maxblockgasusedforbasefee"` // Maximum network and process capacity to allow in a block
   230  	BaseFeeDenominator        uint64 `json:"basefeedenominator"`        // For normalizing effect of the rapid change like impulse gas used
   231  }
   232  
   233  // IstanbulConfig is the consensus engine configs for Istanbul based sealing.
   234  type IstanbulConfig struct {
   235  	Epoch          uint64 `json:"epoch"`  // Epoch length to reset votes and checkpoint
   236  	ProposerPolicy uint64 `json:"policy"` // The policy for proposer selection; 0: Round Robin, 1: Sticky, 2: Weighted Random
   237  	SubGroupSize   uint64 `json:"sub"`
   238  }
   239  
   240  // GxhashConfig is the consensus engine configs for proof-of-work based sealing.
   241  // Deprecated: Use IstanbulConfig or CliqueConfig.
   242  type GxhashConfig struct{}
   243  
   244  // String implements the stringer interface, returning the consensus engine details.
   245  func (c *GxhashConfig) String() string {
   246  	return "gxhash"
   247  }
   248  
   249  // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
   250  type CliqueConfig struct {
   251  	Period uint64 `json:"period"` // Number of seconds between blocks to enforce
   252  	Epoch  uint64 `json:"epoch"`  // Epoch length to reset votes and checkpoint
   253  }
   254  
   255  // String implements the stringer interface, returning the consensus engine details.
   256  func (c *CliqueConfig) String() string {
   257  	return "clique"
   258  }
   259  
   260  // String implements the stringer interface, returning the consensus engine details.
   261  func (c *IstanbulConfig) String() string {
   262  	return "istanbul"
   263  }
   264  
   265  // String implements the fmt.Stringer interface.
   266  func (c *ChainConfig) String() string {
   267  	var engine interface{}
   268  	switch {
   269  	case c.Gxhash != nil:
   270  		engine = c.Gxhash
   271  	case c.Clique != nil:
   272  		engine = c.Clique
   273  	case c.Istanbul != nil:
   274  		engine = c.Istanbul
   275  	default:
   276  		engine = "unknown"
   277  	}
   278  
   279  	kip103 := fmt.Sprintf("KIP103CompatibleBlock: %v KIP103ContractAddress %s", c.Kip103CompatibleBlock, c.Kip103ContractAddress.String())
   280  
   281  	if c.Istanbul != nil {
   282  		return fmt.Sprintf("{ChainID: %v IstanbulCompatibleBlock: %v LondonCompatibleBlock: %v EthTxTypeCompatibleBlock: %v MagmaCompatibleBlock: %v KoreCompatibleBlock: %v %s SubGroupSize: %d UnitPrice: %d DeriveShaImpl: %d Engine: %v}",
   283  			c.ChainID,
   284  			c.IstanbulCompatibleBlock,
   285  			c.LondonCompatibleBlock,
   286  			c.EthTxTypeCompatibleBlock,
   287  			c.MagmaCompatibleBlock,
   288  			c.KoreCompatibleBlock,
   289  			kip103,
   290  			c.Istanbul.SubGroupSize,
   291  			c.UnitPrice,
   292  			c.DeriveShaImpl,
   293  			engine,
   294  		)
   295  	} else {
   296  		return fmt.Sprintf("{ChainID: %v IstanbulCompatibleBlock: %v LondonCompatibleBlock: %v EthTxTypeCompatibleBlock: %v MagmaCompatibleBlock: %v KoreCompatibleBlock: %v %s UnitPrice: %d DeriveShaImpl: %d Engine: %v }",
   297  			c.ChainID,
   298  			c.IstanbulCompatibleBlock,
   299  			c.LondonCompatibleBlock,
   300  			c.EthTxTypeCompatibleBlock,
   301  			c.MagmaCompatibleBlock,
   302  			c.KoreCompatibleBlock,
   303  			kip103,
   304  			c.UnitPrice,
   305  			c.DeriveShaImpl,
   306  			engine,
   307  		)
   308  	}
   309  }
   310  
   311  func (c *ChainConfig) Copy() *ChainConfig {
   312  	r := &ChainConfig{}
   313  	j, _ := json.Marshal(c)
   314  	json.Unmarshal(j, r)
   315  	return r
   316  }
   317  
   318  // IsIstanbulForkEnabled returns whether num is either equal to the istanbul block or greater.
   319  func (c *ChainConfig) IsIstanbulForkEnabled(num *big.Int) bool {
   320  	return isForked(c.IstanbulCompatibleBlock, num)
   321  }
   322  
   323  // IsLondonForkEnabled returns whether num is either equal to the london block or greater.
   324  func (c *ChainConfig) IsLondonForkEnabled(num *big.Int) bool {
   325  	return isForked(c.LondonCompatibleBlock, num)
   326  }
   327  
   328  // IsEthTxTypeForkEnabled returns whether num is either equal to the ethTxType block or greater.
   329  func (c *ChainConfig) IsEthTxTypeForkEnabled(num *big.Int) bool {
   330  	return isForked(c.EthTxTypeCompatibleBlock, num)
   331  }
   332  
   333  // IsMagmaForkedEnabled returns whether num is either equal to the magma block or greater.
   334  func (c *ChainConfig) IsMagmaForkEnabled(num *big.Int) bool {
   335  	return isForked(c.MagmaCompatibleBlock, num)
   336  }
   337  
   338  // IsKoreForkedEnabled returns whether num is either equal to the kore block or greater.
   339  func (c *ChainConfig) IsKoreForkEnabled(num *big.Int) bool {
   340  	return isForked(c.KoreCompatibleBlock, num)
   341  }
   342  
   343  // IsKIP103ForkBlock returns whether num is equal to the kore block.
   344  func (c *ChainConfig) IsKIP103ForkBlock(num *big.Int) bool {
   345  	if c.Kip103CompatibleBlock == nil || num == nil {
   346  		return false
   347  	}
   348  	return c.Kip103CompatibleBlock.Cmp(num) == 0
   349  }
   350  
   351  // CheckCompatible checks whether scheduled fork transitions have been imported
   352  // with a mismatching chain configuration.
   353  func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
   354  	bhead := new(big.Int).SetUint64(height)
   355  
   356  	// Iterate checkCompatible to find the lowest conflict.
   357  	var lasterr *ConfigCompatError
   358  	for {
   359  		err := c.checkCompatible(newcfg, bhead)
   360  		if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
   361  			break
   362  		}
   363  		lasterr = err
   364  		bhead.SetUint64(err.RewindTo)
   365  	}
   366  	return lasterr
   367  }
   368  
   369  // CheckConfigForkOrder checks that we don't "skip" any forks, geth isn't pluggable enough
   370  // to guarantee that forks can be implemented in a different order than on official networks
   371  func (c *ChainConfig) CheckConfigForkOrder() error {
   372  	type fork struct {
   373  		name     string
   374  		block    *big.Int
   375  		optional bool // if true, the fork may be nil and next fork is still allowed
   376  	}
   377  	var lastFork fork
   378  	for _, cur := range []fork{
   379  		{name: "istanbulBlock", block: c.IstanbulCompatibleBlock},
   380  		{name: "londonBlock", block: c.LondonCompatibleBlock},
   381  		{name: "ethTxTypeBlock", block: c.EthTxTypeCompatibleBlock},
   382  		{name: "magmaBlock", block: c.MagmaCompatibleBlock},
   383  		{name: "koreBlock", block: c.KoreCompatibleBlock},
   384  	} {
   385  		if lastFork.name != "" {
   386  			// Next one must be higher number
   387  			if lastFork.block == nil && cur.block != nil {
   388  				return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at %v",
   389  					lastFork.name, cur.name, cur.block)
   390  			}
   391  			if lastFork.block != nil && cur.block != nil {
   392  				if lastFork.block.Cmp(cur.block) > 0 {
   393  					return fmt.Errorf("unsupported fork ordering: %v enabled at %v, but %v enabled at %v",
   394  						lastFork.name, lastFork.block, cur.name, cur.block)
   395  				}
   396  			}
   397  		}
   398  		// If it was optional and not set, then ignore it
   399  		if !cur.optional || cur.block != nil {
   400  			lastFork = cur
   401  		}
   402  	}
   403  	return nil
   404  }
   405  
   406  func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
   407  	if isForkIncompatible(c.IstanbulCompatibleBlock, newcfg.IstanbulCompatibleBlock, head) {
   408  		return newCompatError("Istanbul Block", c.IstanbulCompatibleBlock, newcfg.IstanbulCompatibleBlock)
   409  	}
   410  	if isForkIncompatible(c.LondonCompatibleBlock, newcfg.LondonCompatibleBlock, head) {
   411  		return newCompatError("London Block", c.LondonCompatibleBlock, newcfg.LondonCompatibleBlock)
   412  	}
   413  	if isForkIncompatible(c.EthTxTypeCompatibleBlock, newcfg.EthTxTypeCompatibleBlock, head) {
   414  		return newCompatError("EthTxType Block", c.EthTxTypeCompatibleBlock, newcfg.EthTxTypeCompatibleBlock)
   415  	}
   416  	if isForkIncompatible(c.MagmaCompatibleBlock, newcfg.MagmaCompatibleBlock, head) {
   417  		return newCompatError("Magma Block", c.MagmaCompatibleBlock, newcfg.MagmaCompatibleBlock)
   418  	}
   419  	if isForkIncompatible(c.KoreCompatibleBlock, newcfg.KoreCompatibleBlock, head) {
   420  		return newCompatError("Kore Block", c.KoreCompatibleBlock, newcfg.KoreCompatibleBlock)
   421  	}
   422  	return nil
   423  }
   424  
   425  // SetDefaultsForGenesis fills undefined chain config with default values.
   426  // Only used for generating genesis.
   427  // Empty values from genesis.json will be left out from genesis.
   428  func (c *ChainConfig) SetDefaultsForGenesis() {
   429  	if c.Clique == nil && c.Istanbul == nil {
   430  		c.Istanbul = GetDefaultIstanbulConfig()
   431  		logger.Warn("Override the default Istanbul config to the chain config")
   432  	}
   433  
   434  	if c.Governance == nil {
   435  		c.Governance = GetDefaultGovernanceConfigForGenesis()
   436  		logger.Warn("Override the default governance config to the chain config")
   437  	}
   438  
   439  	if c.Governance.Reward == nil {
   440  		c.Governance.Reward = GetDefaultRewardConfigForGenesis()
   441  		logger.Warn("Override the default governance reward config to the chain config", "reward",
   442  			c.Governance.Reward)
   443  	}
   444  
   445  	// StakingUpdateInterval must be nonzero because it is used as denominator
   446  	if c.Governance.Reward.StakingUpdateInterval == 0 {
   447  		c.Governance.Reward.StakingUpdateInterval = StakingUpdateInterval()
   448  		logger.Warn("Override the default staking update interval to the chain config", "interval",
   449  			c.Governance.Reward.StakingUpdateInterval)
   450  	}
   451  
   452  	// ProposerUpdateInterval must be nonzero because it is used as denominator
   453  	if c.Governance.Reward.ProposerUpdateInterval == 0 {
   454  		c.Governance.Reward.ProposerUpdateInterval = ProposerUpdateInterval()
   455  		logger.Warn("Override the default proposer update interval to the chain config", "interval",
   456  			c.Governance.Reward.ProposerUpdateInterval)
   457  	}
   458  }
   459  
   460  // SetDefaults fills undefined chain config with default values
   461  // so that nil pointer does not exist in the chain config
   462  func (c *ChainConfig) SetDefaults() {
   463  	c.SetDefaultsForGenesis()
   464  
   465  	if c.Governance.KIP71 == nil {
   466  		c.Governance.KIP71 = GetDefaultKIP71Config()
   467  	}
   468  	if c.Governance.Reward.Kip82Ratio == "" {
   469  		c.Governance.Reward.Kip82Ratio = DefaultKip82Ratio
   470  	}
   471  }
   472  
   473  // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
   474  // block s2 because head is already past the fork.
   475  func isForkIncompatible(s1, s2, head *big.Int) bool {
   476  	return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
   477  }
   478  
   479  // isForked returns whether a fork scheduled at block s is active at the given head block.
   480  func isForked(s, head *big.Int) bool {
   481  	if s == nil || head == nil {
   482  		return false
   483  	}
   484  	return s.Cmp(head) <= 0
   485  }
   486  
   487  func configNumEqual(x, y *big.Int) bool {
   488  	if x == nil {
   489  		return y == nil
   490  	}
   491  	if y == nil {
   492  		return x == nil
   493  	}
   494  	return x.Cmp(y) == 0
   495  }
   496  
   497  // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
   498  // ChainConfig that would alter the past.
   499  type ConfigCompatError struct {
   500  	What string
   501  	// block numbers of the stored and new configurations
   502  	StoredConfig, NewConfig *big.Int
   503  	// the block number to which the local chain must be rewound to correct the error
   504  	RewindTo uint64
   505  }
   506  
   507  func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
   508  	var rew *big.Int
   509  	switch {
   510  	case storedblock == nil:
   511  		rew = newblock
   512  	case newblock == nil || storedblock.Cmp(newblock) < 0:
   513  		rew = storedblock
   514  	default:
   515  		rew = newblock
   516  	}
   517  	err := &ConfigCompatError{what, storedblock, newblock, 0}
   518  	if rew != nil && rew.Sign() > 0 {
   519  		err.RewindTo = rew.Uint64() - 1
   520  	}
   521  	return err
   522  }
   523  
   524  func (err *ConfigCompatError) Error() string {
   525  	return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
   526  }
   527  
   528  // Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
   529  // that do not have or require information about the block.
   530  //
   531  // Rules is a one time interface meaning that it shouldn't be used in between transition
   532  // phases.
   533  type Rules struct {
   534  	ChainID    *big.Int
   535  	IsIstanbul bool
   536  	IsLondon   bool
   537  	IsMagma    bool
   538  	IsKore     bool
   539  }
   540  
   541  // Rules ensures c's ChainID is not nil.
   542  func (c *ChainConfig) Rules(num *big.Int) Rules {
   543  	chainID := c.ChainID
   544  	if chainID == nil {
   545  		chainID = new(big.Int)
   546  	}
   547  	return Rules{
   548  		ChainID:    new(big.Int).Set(chainID),
   549  		IsIstanbul: c.IsIstanbulForkEnabled(num),
   550  		IsLondon:   c.IsLondonForkEnabled(num),
   551  		IsMagma:    c.IsMagmaForkEnabled(num),
   552  		IsKore:     c.IsKoreForkEnabled(num),
   553  	}
   554  }
   555  
   556  // cypress genesis config
   557  func GetDefaultGovernanceConfigForGenesis() *GovernanceConfig {
   558  	gov := &GovernanceConfig{
   559  		GovernanceMode: DefaultGovernanceMode,
   560  		GoverningNode:  common.HexToAddress(DefaultGoverningNode),
   561  		Reward:         GetDefaultRewardConfigForGenesis(),
   562  	}
   563  	return gov
   564  }
   565  
   566  func GetDefaultGovernanceConfig() *GovernanceConfig {
   567  	gov := &GovernanceConfig{
   568  		GovernanceMode:   DefaultGovernanceMode,
   569  		GoverningNode:    common.HexToAddress(DefaultGoverningNode),
   570  		GovParamContract: common.HexToAddress(DefaultGovParamContract),
   571  		Reward:           GetDefaultRewardConfig(),
   572  		KIP71:            GetDefaultKIP71Config(),
   573  	}
   574  	return gov
   575  }
   576  
   577  func GetDefaultIstanbulConfig() *IstanbulConfig {
   578  	return &IstanbulConfig{
   579  		Epoch:          DefaultEpoch,
   580  		ProposerPolicy: DefaultProposerPolicy,
   581  		SubGroupSize:   DefaultSubGroupSize,
   582  	}
   583  }
   584  
   585  func GetDefaultRewardConfigForGenesis() *RewardConfig {
   586  	return &RewardConfig{
   587  		MintingAmount:          DefaultMintingAmount,
   588  		Ratio:                  DefaultRatio,
   589  		UseGiniCoeff:           DefaultUseGiniCoeff,
   590  		DeferredTxFee:          DefaultDefferedTxFee,
   591  		StakingUpdateInterval:  DefaultStakeUpdateInterval,
   592  		ProposerUpdateInterval: DefaultProposerRefreshInterval,
   593  		MinimumStake:           DefaultMinimumStake,
   594  	}
   595  }
   596  
   597  func GetDefaultRewardConfig() *RewardConfig {
   598  	return &RewardConfig{
   599  		MintingAmount:          DefaultMintingAmount,
   600  		Ratio:                  DefaultRatio,
   601  		Kip82Ratio:             DefaultKip82Ratio,
   602  		UseGiniCoeff:           DefaultUseGiniCoeff,
   603  		DeferredTxFee:          DefaultDefferedTxFee,
   604  		StakingUpdateInterval:  DefaultStakeUpdateInterval,
   605  		ProposerUpdateInterval: DefaultProposerRefreshInterval,
   606  		MinimumStake:           DefaultMinimumStake,
   607  	}
   608  }
   609  
   610  func GetDefaultKIP71Config() *KIP71Config {
   611  	return &KIP71Config{
   612  		LowerBoundBaseFee:         DefaultLowerBoundBaseFee,
   613  		UpperBoundBaseFee:         DefaultUpperBoundBaseFee,
   614  		GasTarget:                 DefaultGasTarget,
   615  		MaxBlockGasUsedForBaseFee: DefaultMaxBlockGasUsedForBaseFee,
   616  		BaseFeeDenominator:        DefaultBaseFeeDenominator,
   617  	}
   618  }
   619  
   620  func GetDefaultCliqueConfig() *CliqueConfig {
   621  	return &CliqueConfig{
   622  		Epoch:  DefaultEpoch,
   623  		Period: DefaultPeriod,
   624  	}
   625  }