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