github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/params/config.go (about)

     1  // Copyright 2018 Wanchain Foundation Ltd
     2  // Copyright 2016 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  package params
    19  
    20  import (
    21  	"fmt"
    22  	"math/big"
    23  
    24  	"github.com/wanchain/go-wanchain/common"
    25  )
    26  
    27  var (
    28  	MainnetGenesisHash = common.HexToHash("0x0376899c001618fc7d5ab4f31cfd7f57ca3a896ccc1581a57d8f129ecf40b840") // Mainnet genesis hash to enforce below configs on
    29  	TestnetGenesisHash = common.HexToHash("0xa37b811609a9d1e898fb49b3901728023e5e72e18e58643d9a7a82db483bfeb0") // Testnet genesis hash to enforce below configs on
    30  	PlutoGenesisHash   = common.HexToHash("0x7b67a3f28e0d12b57e5fdaa445c4d6dbe68bffa9b808e944e5c67726669d62b6") // Pluto genesis hash to enforce below configs on
    31  
    32  	InternalGenesisHash = common.HexToHash("0xb1dc31a86510003c23b9ddee0e194775807262529b8dafa6dc23d9315364d2b3")
    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  
    40  		//HomesteadBlock: big.NewInt(1150000),
    41  		//DAOForkBlock:   big.NewInt(1920000),
    42  		//DAOForkSupport: true,
    43  		//EIP150Block:    big.NewInt(2463000),
    44  		//EIP150Hash:     common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
    45  		//EIP155Block:    big.NewInt(2675000),
    46  		//EIP158Block:    big.NewInt(2675000),
    47  
    48  		//ByzantiumBlock: big.NewInt(4370000),
    49  		ByzantiumBlock: big.NewInt(0),
    50  		Ethash:         new(EthashConfig),
    51  	}
    52  
    53  	WanchainChainConfig = &ChainConfig{
    54  		ChainId: big.NewInt(1),
    55  		//HomesteadBlock: big.NewInt(0),
    56  		//DAOForkBlock:   nil,
    57  		//DAOForkSupport: true,
    58  		//EIP150Block:    big.NewInt(0),
    59  		//EIP150Hash:     common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
    60  		//EIP155Block:    big.NewInt(0),
    61  		//EIP158Block:    big.NewInt(0),
    62  		ByzantiumBlock: big.NewInt(0),
    63  		Ethash:         new(EthashConfig),
    64  	}
    65  
    66  	// TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network.
    67  	TestnetChainConfig = &ChainConfig{
    68  		ChainId: big.NewInt(3),
    69  		//HomesteadBlock: big.NewInt(0),
    70  		//DAOForkBlock:   nil,
    71  		//DAOForkSupport: true,
    72  		//EIP150Block:    big.NewInt(0),
    73  		//EIP150Hash:     common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
    74  		//EIP155Block:    big.NewInt(10),
    75  		//EIP158Block:    big.NewInt(10),
    76  		ByzantiumBlock: big.NewInt(0),
    77  
    78  		Ethash: new(EthashConfig),
    79  	}
    80  
    81  	// RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
    82  	InternalChainConfig = &ChainConfig{
    83  		ChainId: big.NewInt(4),
    84  		//HomesteadBlock: big.NewInt(1),
    85  		//DAOForkBlock:   nil,
    86  		//DAOForkSupport: true,
    87  		//EIP150Block:    big.NewInt(2),
    88  		//EIP150Hash:     common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
    89  		//EIP155Block:    big.NewInt(3),
    90  		//EIP158Block:    big.NewInt(3),
    91  		ByzantiumBlock: big.NewInt(0),
    92  
    93  		Ethash: new(EthashConfig),
    94  	}
    95  	// PlutoChainConfig contains the chain parameters to run a node on the Pluto test network.
    96  	PlutoChainConfig = &ChainConfig{
    97  		ChainId: big.NewInt(6),
    98  		//HomesteadBlock: big.NewInt(0),
    99  		//DAOForkBlock:   nil,
   100  		//DAOForkSupport: true,
   101  		//EIP150Block:    big.NewInt(0),
   102  		//EIP150Hash:     common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
   103  		//EIP155Block:    big.NewInt(3),
   104  		//EIP158Block:    big.NewInt(3),
   105  		ByzantiumBlock: big.NewInt(0),
   106  
   107  		Pluto: &PlutoConfig{
   108  			Period: 6,
   109  			Epoch:  6000,
   110  		},
   111  	}
   112  
   113  	// AllProtocolChanges contains every protocol change (EIPs)
   114  	// introduced and accepted by the Ethereum core developers.
   115  	//
   116  	// This configuration is intentionally not using keyed fields.
   117  	// This configuration must *always* have all forks enabled, which
   118  	// means that all fields must be set at all times. This forces
   119  	// anyone adding flags to the config to also have to set these
   120  	// fields.
   121  	AllProtocolChanges = &ChainConfig{big.NewInt(1337) /* big.NewInt(0),*/ /*nil, false,*/ /* big.NewInt(0), common.Hash{},*/ /*big.NewInt(0),*/ /*big.NewInt(0),*/, big.NewInt(0), new(EthashConfig), nil, nil}
   122  
   123  	TestChainConfig = &ChainConfig{
   124  		ChainId:        big.NewInt(1),
   125  		ByzantiumBlock: big.NewInt(0),
   126  		Ethash:         new(EthashConfig),
   127  	}
   128  
   129  	TestRules = TestChainConfig.Rules(new(big.Int))
   130  )
   131  
   132  // ChainConfig is the core config which determines the blockchain settings.
   133  //
   134  // ChainConfig is stored in the database on a per block basis. This means
   135  // that any network, identified by its genesis block, can have its own
   136  // set of configuration options.
   137  type ChainConfig struct {
   138  	ChainId *big.Int `json:"chainId"` // Chain id identifies the current chain and is used for replay protection
   139  
   140  	//HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)
   141  
   142  	//DAOForkBlock   *big.Int `json:"daoForkBlock,omitempty"`   // TheDAO hard-fork switch block (nil = no fork)
   143  	//DAOForkSupport bool     `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
   144  
   145  	// EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
   146  	//EIP150Block *big.Int    `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork)
   147  	//EIP150Hash  common.Hash `json:"eip150Hash,omitempty"`  // EIP150 HF hash (needed for header only clients as only gas pricing changed)
   148  
   149  	//EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
   150  	//EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block
   151  
   152  	ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium)
   153  
   154  	// Various consensus engines
   155  	Ethash *EthashConfig `json:"ethash,omitempty"`
   156  	Clique *CliqueConfig `json:"clique,omitempty"`
   157  	Pluto  *PlutoConfig  `json:"pluto,omitempty"`
   158  }
   159  
   160  // EthashConfig is the consensus engine configs for proof-of-work based sealing.
   161  type EthashConfig struct{}
   162  
   163  // String implements the stringer interface, returning the consensus engine details.
   164  func (c *EthashConfig) String() string {
   165  	return "ethash"
   166  }
   167  
   168  // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
   169  type CliqueConfig struct {
   170  	Period uint64 `json:"period"` // Number of seconds between blocks to enforce
   171  	Epoch  uint64 `json:"epoch"`  // Epoch length to reset votes and checkpoint
   172  }
   173  
   174  // String implements the stringer interface, returning the consensus engine details.
   175  func (c *CliqueConfig) String() string {
   176  	return "clique"
   177  }
   178  
   179  // PlutoConfig is the consensus engine configs for proof-of-authority based sealing.
   180  type PlutoConfig struct {
   181  	Period uint64 `json:"period"` // Number of seconds between blocks to enforce
   182  	Epoch  uint64 `json:"epoch"`  // Epoch length to reset votes and checkpoint
   183  }
   184  
   185  // String implements the stringer interface, returning the consensus engine details.
   186  func (c *PlutoConfig) String() string {
   187  	return "pluto"
   188  }
   189  
   190  // String implements the fmt.Stringer interface.
   191  func (c *ChainConfig) String() string {
   192  	var engine interface{}
   193  	switch {
   194  	case c.Ethash != nil:
   195  		engine = c.Ethash
   196  	case c.Clique != nil:
   197  		engine = c.Clique
   198  	case c.Pluto != nil:
   199  		engine = c.Pluto
   200  	default:
   201  		engine = "unknown"
   202  	}
   203  	//return fmt.Sprintf("{ChainID: %v Homestead: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Engine: %v}",
   204  	return fmt.Sprintf("{ChainID: %v Byzantium: %v Engine: %v}",
   205  		c.ChainId,
   206  		//c.HomesteadBlock,
   207  		//c.DAOForkBlock,
   208  		//c.DAOForkSupport,
   209  		//c.EIP150Block,
   210  		//c.EIP155Block,
   211  		//c.EIP158Block,
   212  
   213  		c.ByzantiumBlock,
   214  		engine,
   215  	)
   216  }
   217  
   218  // IsHomestead returns whether num is either equal to the homestead block or greater.
   219  //func (c *ChainConfig) IsHomestead(num *big.Int) bool {
   220  //	return isForked(c.HomesteadBlock, num)
   221  //}
   222  
   223  // IsDAO returns whether num is either equal to the DAO fork block or greater.
   224  //func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
   225  //	return isForked(c.DAOForkBlock, num)
   226  //}
   227  
   228  //func (c *ChainConfig) IsEIP150(num *big.Int) bool {
   229  //	return isForked(c.EIP150Block, num)
   230  //}
   231  
   232  //func (c *ChainConfig) IsEIP155(num *big.Int) bool {
   233  //	return isForked(c.EIP155Block, num)
   234  //}
   235  
   236  //func (c *ChainConfig) IsEIP158(num *big.Int) bool {
   237  //	return isForked(c.EIP158Block, num)
   238  //}
   239  
   240  //func (c *ChainConfig) IsByzantium(num *big.Int) bool {
   241  //	return isForked(c.ByzantiumBlock, num)
   242  //}
   243  
   244  // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice).
   245  //
   246  // The returned GasTable's fields shouldn't, under any circumstances, be changed.
   247  func (c *ChainConfig) GasTable(num *big.Int) GasTable {
   248  	//if num == nil {
   249  	//	return GasTableHomestead
   250  	//}
   251  	//switch {
   252  
   253  	//case c.IsEIP158(num):
   254  	//	return GasTableEIP158
   255  	/*
   256  		case c.IsEIP150(num):
   257  			return GasTableEIP150
   258  	*/
   259  	//default:
   260  	//	return GasTableHomestead
   261  	//}
   262  
   263  	return GasTableEIP158
   264  }
   265  
   266  // CheckCompatible checks whether scheduled fork transitions have been imported
   267  // with a mismatching chain configuration.
   268  func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
   269  	bhead := new(big.Int).SetUint64(height)
   270  	// Iterate checkCompatible to find the lowest conflict.
   271  	var lasterr *ConfigCompatError
   272  	for {
   273  		err := c.checkCompatible(newcfg, bhead)
   274  		if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
   275  			break
   276  		}
   277  		lasterr = err
   278  		bhead.SetUint64(err.RewindTo)
   279  	}
   280  	return lasterr
   281  }
   282  
   283  func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
   284  
   285  	//if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
   286  	//	return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
   287  	//}
   288  
   289  	//if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
   290  	//	return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
   291  	//}
   292  
   293  	//if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
   294  	//	return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
   295  	//}
   296  
   297  	//if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
   298  	//	return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
   299  	//}
   300  
   301  	//if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
   302  	//	return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
   303  	//}
   304  	//
   305  	//if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
   306  	//	return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
   307  	//}
   308  
   309  	//if c.IsEIP158(head) && !configNumEqual(c.ChainId, newcfg.ChainId) {
   310  	//	return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
   311  	//}
   312  
   313  	//if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
   314  	//	return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
   315  	//}
   316  
   317  	return nil
   318  }
   319  
   320  // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
   321  // block s2 because head is already past the fork.
   322  func isForkIncompatible(s1, s2, head *big.Int) bool {
   323  	return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
   324  }
   325  
   326  // isForked returns whether a fork scheduled at block s is active at the given head block.
   327  func isForked(s, head *big.Int) bool {
   328  	if s == nil || head == nil {
   329  		return false
   330  	}
   331  	return s.Cmp(head) <= 0
   332  }
   333  
   334  func configNumEqual(x, y *big.Int) bool {
   335  	if x == nil {
   336  		return y == nil
   337  	}
   338  	if y == nil {
   339  		return x == nil
   340  	}
   341  	return x.Cmp(y) == 0
   342  }
   343  
   344  // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
   345  // ChainConfig that would alter the past.
   346  type ConfigCompatError struct {
   347  	What string
   348  	// block numbers of the stored and new configurations
   349  	StoredConfig, NewConfig *big.Int
   350  	// the block number to which the local chain must be rewound to correct the error
   351  	RewindTo uint64
   352  }
   353  
   354  func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
   355  	var rew *big.Int
   356  	switch {
   357  	case storedblock == nil:
   358  		rew = newblock
   359  	case newblock == nil || storedblock.Cmp(newblock) < 0:
   360  		rew = storedblock
   361  	default:
   362  		rew = newblock
   363  	}
   364  	err := &ConfigCompatError{what, storedblock, newblock, 0}
   365  	if rew != nil && rew.Sign() > 0 {
   366  		err.RewindTo = rew.Uint64() - 1
   367  	}
   368  	return err
   369  }
   370  
   371  func (err *ConfigCompatError) Error() string {
   372  	return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
   373  }
   374  
   375  // Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions
   376  // that do not have or require information about the block.
   377  //
   378  // Rules is a one time interface meaning that it shouldn't be used in between transition
   379  // phases.
   380  type Rules struct {
   381  	ChainId *big.Int
   382  	//IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
   383  	//IsByzantium                               bool
   384  }
   385  
   386  func (c *ChainConfig) Rules(num *big.Int) Rules {
   387  	chainId := c.ChainId
   388  	if chainId == nil {
   389  		chainId = new(big.Int)
   390  	}
   391  	//return Rules{ChainId: new(big.Int).Set(chainId), IsHomestead: /*c.IsHomestead(num)*/false, IsEIP150: false/*c.IsEIP150(num)*/, IsEIP155: false/*c.IsEIP155(num)*/, IsEIP158:false/* c.IsEIP158(num)*/, IsByzantium: c.IsByzantium(num)}
   392  
   393  	return Rules{ChainId: new(big.Int).Set(chainId)}
   394  }