github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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/big"
    22  
    23  	"github.com/vntchain/go-vnt/common"
    24  )
    25  
    26  // Genesis hashes to enforce below configs on.
    27  var (
    28  	MainnetGenesisHash = common.HexToHash("0xf2ba87b7a6b3c3ff2fc2c0e2b2985b9cfc7ca24daaa39ac04677855b1583e5ad")
    29  )
    30  
    31  var (
    32  	// MainnetChainConfig is the chain parameters to run a node on the main network.
    33  	MainnetChainConfig = &ChainConfig{
    34  		ChainID: big.NewInt(1),
    35  		Dpos: &DposConfig{
    36  			Period:       2,
    37  			WitnessesNum: 19,
    38  			WitnessesUrl: []string{
    39  				"/ip4/47.106.71.114/tcp/3001/ipfs/1kHfbk8u12U1HSyaqAe6f622wVMESHcvFZ8VcbGKwsrtT6H",
    40  				"/ip4/47.108.69.101/tcp/3001/ipfs/1kHb7UpvD2zbEgCCPzboTtJLENQ1YhEZ1H2A7QNTb18sHd4",
    41  				"/ip4/47.108.67.119/tcp/3001/ipfs/1kHjhzoNTxEpFvRky1oVvCUAPBUuGzBvBcXmkroDU9NhACg",
    42  				"/ip4/39.100.143.156/tcp/3001/ipfs/1kHG1essWxbjSUjwKMrJcva6Y7XfPoBN86pUemd4wnd2X2A",
    43  				"/ip4/118.190.59.122/tcp/3001/ipfs/1kHBXTtDX4JqStm4qBNCcCZar9isCyu74BPgX6b2odm7zw7",
    44  
    45  				"/ip4/118.190.59.100/tcp/3001/ipfs/1kHWbCuCgjLnERsaVr7FCSAWFiA7Sx1CdzstMQKKkEWdYSr",
    46  				"/ip4/47.56.69.191/tcp/3001/ipfs/1kHcupmVj3eLe6QdgrXRn5qetpQQc4XhYc6LXNhQUkRCDK5",
    47  				"/ip4/39.97.171.233/tcp/3001/ipfs/1kHDnV8YSUrHNLf6NGWY7QHxBjMLnKPxKFKvMdPLt1Gg7a6",
    48  				"/ip4/47.103.107.188/tcp/3001/ipfs/1kHTKpK29Kw2EvJ3C817yn6wNDgpXw6oWAkUz8AzAozrVHD",
    49  				"/ip4/47.103.57.160/tcp/3001/ipfs/1kHD3RiEFGZE2SQQgGhX1yzipcR8sE6cSaTj3Xi1yvF1EBL",
    50  
    51  				"/ip4/47.254.235.57/tcp/3001/ipfs/1kHMwGapAtV92rXgkxRx4dgjUZnXVtE6wUa8Bk3yzFYLz4h",
    52  				"/ip4/120.77.236.120/tcp/3001/ipfs/1kHHW1DrdwETgtZmDdERZEDgKiAfP2SDfxz2oKuzjuJK2B9",
    53  				"/ip4/47.111.131.2/tcp/3001/ipfs/1kHCcTMZ8EjRm23nffDsYEEARohWwF4ks6zdsaBs3JbzKnA",
    54  				"/ip4/47.88.217.237/tcp/3001/ipfs/1kHVYJ6tckDB5gChvDFo46esBWzz6aWsMaEXfxJPNwruHWZ",
    55  				"/ip4/47.91.19.11/tcp/3001/ipfs/1kHBa9E1onVKmruWvefJHSNFGokkNc3ESZebnq2oJFRDFDG",
    56  
    57  				"/ip4/47.254.20.76/tcp/3001/ipfs/1kHKUGfEQ4nzpWG4SkQAUPicrKHCVo32WTgNEQbM75rNEbx",
    58  				"/ip4/47.93.191.135/tcp/3001/ipfs/1kHVzyb8mczCYNB3suPCXac7HMPEc4XyvibHE2hQsh8ehEk",
    59  				"/ip4/101.132.191.42/tcp/3001/ipfs/1kHCoMGo8ANpjFNMyYFZLTHruBJ5DrAXYVBvfQP2CZWKf6h",
    60  				"/ip4/39.104.62.26/tcp/3001/ipfs/1kHHvSatMYVHDdDFNmMyta8tNY4c4VvtifkBAxfjNG7wSpU",
    61  			},
    62  		},
    63  	}
    64  
    65  	MainnetChainWitnesses = []common.Address{
    66  		common.HexToAddress("0x91837ff26639700c9688cf8f3fe92bd8b2ec806d"),
    67  		common.HexToAddress("0x3c60a032ba3c6177e50188748e55e5894fb241e4"),
    68  		common.HexToAddress("0xaa2b5f39fb2a4aee56db3ee19567f699d30df1a1"),
    69  		common.HexToAddress("0x61a6e04c737483d72c20de6e71dd8cbb6f6c747d"),
    70  		common.HexToAddress("0x186bae02dc3444d2bb3d39504fefdc9754860481"),
    71  
    72  		common.HexToAddress("0xf4c8fd44490493000b8776fd1597752bd9ede431"),
    73  		common.HexToAddress("0x4e94885ed5cfe31a00c7496176f59fdc5e5c7a71"),
    74  		common.HexToAddress("0x4b47c3262a9d2c309b692c9220898ff728054c00"),
    75  		common.HexToAddress("0x31ba9c8cf34d7cc0957a95744b245322af427786"),
    76  		common.HexToAddress("0x4dcfcd45b253119c0d3db6b9ba9e154167dd6a58"),
    77  
    78  		common.HexToAddress("0xe6c745142283dbbe4b4a03e969525e25031939fa"),
    79  		common.HexToAddress("0xc61a92dd1713f9ba2214f0ce92e3d408ba4d426d"),
    80  		common.HexToAddress("0xc221a4d0b30dee366bc7899dd29e0f7ac9a7e45a"),
    81  		common.HexToAddress("0xddfd32c4d33915685b926ba5eaab3860db1690cd"),
    82  		common.HexToAddress("0xd338d81c4723982c815a294de3b38608dad9962c"),
    83  
    84  		common.HexToAddress("0x6cd54fc6da0f044c43d4550d87ae10b9e1cea351"),
    85  		common.HexToAddress("0xd328d8864649ed050b3d8e9d77f94c75299fd243"),
    86  		common.HexToAddress("0x386dd85ad17b6bd60d2d142473b54bf9d5439842"),
    87  		common.HexToAddress("0x4b8a6cff7b9e008caa936aadd33d9be048623d53"),
    88  	}
    89  
    90  	// TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network.
    91  	TestnetChainConfig = &ChainConfig{
    92  		ChainID: big.NewInt(3),
    93  		Dpos: &DposConfig{
    94  			Period:       2,
    95  			WitnessesNum: 19,
    96  		},
    97  	}
    98  
    99  	// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
   100  	// and accepted by the hubble core developers into the Clique consensus.
   101  	//
   102  	// This configuration is intentionally not using keyed fields to force anyone
   103  	// adding flags to the config to also have to set these fields.
   104  	AllCliqueProtocolChanges = &ChainConfig{
   105  		big.NewInt(1337),
   106  		big.NewInt(0),
   107  		&DposConfig{
   108  			Period:       2,
   109  			WitnessesNum: 4,
   110  		},
   111  	}
   112  
   113  	TestChainConfig = &ChainConfig{
   114  		big.NewInt(1),
   115  		big.NewInt(0),
   116  		&DposConfig{
   117  			Period:       2,
   118  			WitnessesNum: 4,
   119  		}}
   120  	TestRules = TestChainConfig.Rules(new(big.Int))
   121  )
   122  
   123  // ChainConfig is the core config which determines the blockchain settings.
   124  //
   125  // ChainConfig is stored in the database on a per block basis. This means
   126  // that any network, identified by its genesis block, can have its own
   127  // set of configuration options.
   128  type ChainConfig struct {
   129  	ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
   130  
   131  	HubbleBlock *big.Int `json:"HubbleBlock,omitempty"` // Hubble switch block (nil = no fork, 0 = already hubble)
   132  
   133  	// Various consensus engines
   134  	Dpos *DposConfig `json:"dpos,omitempty"`
   135  }
   136  
   137  type DposConfig struct {
   138  	Period       uint64   `json:"period"`       // Number of seconds between blocks to enforce
   139  	WitnessesNum int      `json:"witnessesnum"` // Number of witnesses
   140  	WitnessesUrl []string `json:"witnessesUrl"`
   141  }
   142  
   143  // String implements the stringer interface, returning the consensus engine details.
   144  func (c *DposConfig) String() string {
   145  	return "dpos"
   146  }
   147  
   148  // String implements the fmt.Stringer interface.
   149  func (c *ChainConfig) String() string {
   150  	var engine interface{}
   151  	switch {
   152  	case c.Dpos != nil:
   153  		engine = c.Dpos
   154  	default:
   155  		engine = "unknown"
   156  	}
   157  
   158  	return fmt.Sprintf("{ChainID: %v Hubble: %v Engine: %v}",
   159  		c.ChainID,
   160  		c.HubbleBlock,
   161  		engine,
   162  	)
   163  }
   164  
   165  // IsHubble returns whether num is either equal to the hubble block or greater.
   166  func (c *ChainConfig) IsHubble(num *big.Int) bool {
   167  	return isForked(c.HubbleBlock, num)
   168  }
   169  
   170  // GasTable returns the gas table corresponding to the current phase .
   171  //
   172  // The returned GasTable's fields shouldn't, under any circumstances, be changed.
   173  func (c *ChainConfig) GasTable(num *big.Int) GasTable {
   174  	if num == nil {
   175  		return GasTableHubble
   176  	}
   177  	switch {
   178  	default:
   179  		return GasTableHubble
   180  	}
   181  }
   182  
   183  // CheckCompatible checks whether scheduled fork transitions have been imported
   184  // with a mismatching chain configuration.
   185  func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
   186  	bhead := new(big.Int).SetUint64(height)
   187  
   188  	// Iterate checkCompatible to find the lowest conflict.
   189  	var lasterr *ConfigCompatError
   190  	for {
   191  		err := c.checkCompatible(newcfg, bhead)
   192  		if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
   193  			break
   194  		}
   195  		lasterr = err
   196  		bhead.SetUint64(err.RewindTo)
   197  	}
   198  	return lasterr
   199  }
   200  
   201  func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
   202  	if isForkIncompatible(c.HubbleBlock, newcfg.HubbleBlock, head) {
   203  		return newCompatError("Hubble fork block", c.HubbleBlock, newcfg.HubbleBlock)
   204  	}
   205  	return nil
   206  }
   207  
   208  // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
   209  // block s2 because head is already past the fork.
   210  func isForkIncompatible(s1, s2, head *big.Int) bool {
   211  	return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
   212  }
   213  
   214  // isForked returns whether a fork scheduled at block s is active at the given head block.
   215  func isForked(s, head *big.Int) bool {
   216  	if s == nil || head == nil {
   217  		return false
   218  	}
   219  	return s.Cmp(head) <= 0
   220  }
   221  
   222  func configNumEqual(x, y *big.Int) bool {
   223  	if x == nil {
   224  		return y == nil
   225  	}
   226  	if y == nil {
   227  		return x == nil
   228  	}
   229  	return x.Cmp(y) == 0
   230  }
   231  
   232  // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
   233  // ChainConfig that would alter the past.
   234  type ConfigCompatError struct {
   235  	What string
   236  	// block numbers of the stored and new configurations
   237  	StoredConfig, NewConfig *big.Int
   238  	// the block number to which the local chain must be rewound to correct the error
   239  	RewindTo uint64
   240  }
   241  
   242  func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
   243  	var rew *big.Int
   244  	switch {
   245  	case storedblock == nil:
   246  		rew = newblock
   247  	case newblock == nil || storedblock.Cmp(newblock) < 0:
   248  		rew = storedblock
   249  	default:
   250  		rew = newblock
   251  	}
   252  	err := &ConfigCompatError{what, storedblock, newblock, 0}
   253  	if rew != nil && rew.Sign() > 0 {
   254  		err.RewindTo = rew.Uint64() - 1
   255  	}
   256  	return err
   257  }
   258  
   259  func (err *ConfigCompatError) Error() string {
   260  	return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
   261  }
   262  
   263  // Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions
   264  // that do not have or require information about the block.
   265  //
   266  // Rules is a one time interface meaning that it shouldn't be used in between transition
   267  // phases.
   268  type Rules struct {
   269  	ChainID  *big.Int
   270  	IsHubble bool
   271  }
   272  
   273  // Rules ensures c's ChainID is not nil.
   274  func (c *ChainConfig) Rules(num *big.Int) Rules {
   275  	chainID := c.ChainID
   276  	if chainID == nil {
   277  		chainID = new(big.Int)
   278  	}
   279  	return Rules{ChainID: new(big.Int).Set(chainID), IsHubble: c.IsHubble(num)}
   280  }