github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/core/genesis.go (about)

     1  // Copyright 2014 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"encoding/json"
    23  	"errors"
    24  	"fmt"
    25  	"math/big"
    26  	"strings"
    27  
    28  	"github.com/SmartMeshFoundation/Spectrum/common"
    29  	"github.com/SmartMeshFoundation/Spectrum/common/hexutil"
    30  	"github.com/SmartMeshFoundation/Spectrum/common/math"
    31  	"github.com/SmartMeshFoundation/Spectrum/core/state"
    32  	"github.com/SmartMeshFoundation/Spectrum/core/types"
    33  	"github.com/SmartMeshFoundation/Spectrum/ethdb"
    34  	"github.com/SmartMeshFoundation/Spectrum/log"
    35  	"github.com/SmartMeshFoundation/Spectrum/params"
    36  	"github.com/SmartMeshFoundation/Spectrum/rlp"
    37  )
    38  
    39  //go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go
    40  //go:generate gencodec -type GenesisAccount -field-override genesisAccountMarshaling -out gen_genesis_account.go
    41  
    42  var errGenesisNoConfig = errors.New("genesis has no chain configuration")
    43  
    44  // Genesis specifies the header fields, state of a genesis block. It also defines hard
    45  // fork switch-over blocks through the chain configuration.
    46  type Genesis struct {
    47  	Config     *params.ChainConfig `json:"config"`
    48  	Nonce      uint64              `json:"nonce"`
    49  	Timestamp  uint64              `json:"timestamp"`
    50  	ExtraData  []byte              `json:"extraData"`
    51  	GasLimit   uint64              `json:"gasLimit"   gencodec:"required"`
    52  	Difficulty *big.Int            `json:"difficulty" gencodec:"required"`
    53  	Mixhash    common.Hash         `json:"mixHash"`
    54  	Coinbase   common.Address      `json:"coinbase"`
    55  	Alloc      GenesisAlloc        `json:"alloc"      gencodec:"required"`
    56  
    57  	// These fields are used for consensus tests. Please don't use them
    58  	// in actual genesis blocks.
    59  	Number     uint64      `json:"number"`
    60  	GasUsed    uint64      `json:"gasUsed"`
    61  	ParentHash common.Hash `json:"parentHash"`
    62  }
    63  
    64  // GenesisAlloc specifies the initial state that is part of the genesis block.
    65  type GenesisAlloc map[common.Address]GenesisAccount
    66  
    67  func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error {
    68  	m := make(map[common.UnprefixedAddress]GenesisAccount)
    69  	if err := json.Unmarshal(data, &m); err != nil {
    70  		return err
    71  	}
    72  	*ga = make(GenesisAlloc)
    73  	for addr, a := range m {
    74  		(*ga)[common.Address(addr)] = a
    75  	}
    76  	return nil
    77  }
    78  
    79  // GenesisAccount is an account in the state of the genesis block.
    80  type GenesisAccount struct {
    81  	Code       []byte                      `json:"code,omitempty"`
    82  	Storage    map[common.Hash]common.Hash `json:"storage,omitempty"`
    83  	Balance    *big.Int                    `json:"balance" gencodec:"required"`
    84  	Nonce      uint64                      `json:"nonce,omitempty"`
    85  	PrivateKey []byte                      `json:"secretKey,omitempty"` // for tests
    86  }
    87  
    88  // field type overrides for gencodec
    89  type genesisSpecMarshaling struct {
    90  	Nonce      math.HexOrDecimal64
    91  	Timestamp  math.HexOrDecimal64
    92  	ExtraData  hexutil.Bytes
    93  	GasLimit   math.HexOrDecimal64
    94  	GasUsed    math.HexOrDecimal64
    95  	Number     math.HexOrDecimal64
    96  	Difficulty *math.HexOrDecimal256
    97  	Alloc      map[common.UnprefixedAddress]GenesisAccount
    98  }
    99  
   100  type genesisAccountMarshaling struct {
   101  	Code       hexutil.Bytes
   102  	Balance    *math.HexOrDecimal256
   103  	Nonce      math.HexOrDecimal64
   104  	Storage    map[storageJSON]storageJSON
   105  	PrivateKey hexutil.Bytes
   106  }
   107  
   108  // storageJSON represents a 256 bit byte array, but allows less than 256 bits when
   109  // unmarshaling from hex.
   110  type storageJSON common.Hash
   111  
   112  func (h *storageJSON) UnmarshalText(text []byte) error {
   113  	text = bytes.TrimPrefix(text, []byte("0x"))
   114  	if len(text) > 64 {
   115  		return fmt.Errorf("too many hex characters in storage key/value %q", text)
   116  	}
   117  	offset := len(h) - len(text)/2 // pad on the left
   118  	if _, err := hex.Decode(h[offset:], text); err != nil {
   119  		fmt.Println(err)
   120  		return fmt.Errorf("invalid hex storage key/value %q", text)
   121  	}
   122  	return nil
   123  }
   124  
   125  func (h storageJSON) MarshalText() ([]byte, error) {
   126  	return hexutil.Bytes(h[:]).MarshalText()
   127  }
   128  
   129  // GenesisMismatchError is raised when trying to overwrite an existing
   130  // genesis block with an incompatible one.
   131  type GenesisMismatchError struct {
   132  	Stored, New common.Hash
   133  }
   134  
   135  func (e *GenesisMismatchError) Error() string {
   136  	return fmt.Sprintf("database already contains an incompatible genesis block (have %x, new %x)", e.Stored[:8], e.New[:8])
   137  }
   138  
   139  // SetupGenesisBlock writes or updates the genesis block in db.
   140  // The block that will be used is:
   141  //
   142  //                          genesis == nil       genesis != nil
   143  //                       +------------------------------------------
   144  //     db has no genesis |  main-net default  |  genesis
   145  //     db has genesis    |  from DB           |  genesis (if compatible)
   146  //
   147  // The stored chain configuration will be updated if it is compatible (i.e. does not
   148  // specify a fork block below the local head block). In case of a conflict, the
   149  // error is a *params.ConfigCompatError and the new, unwritten config is returned.
   150  //
   151  // The returned chain configuration is never nil.
   152  func SetupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig, common.Hash, error) {
   153  	if genesis != nil && genesis.Config == nil {
   154  		return params.AllEthashProtocolChanges, common.Hash{}, errGenesisNoConfig
   155  	}
   156  
   157  	// Just commit the new block if there is no stored genesis block.
   158  	stored := GetCanonicalHash(db, 0)
   159  	if (stored == common.Hash{}) {
   160  		if genesis == nil {
   161  			log.Info("Writing default main-net genesis block")
   162  			genesis = DefaultGenesisBlock()
   163  		} else {
   164  			log.Info("Writing custom genesis block")
   165  		}
   166  		block, err := genesis.Commit(db)
   167  		fmt.Println("==genesis==> current\t:", block.Hash().Hex())
   168  		fmt.Println("==genesis==> main\t:", params.MainnetGenesisHash.Hex())
   169  		fmt.Println("==genesis==> test\t:", params.TestnetGenesisHash.Hex())
   170  		fmt.Println("==genesis==> dev\t:", params.DevnetGenesisHash.Hex())
   171  
   172  		return genesis.Config, block.Hash(), err
   173  	}
   174  
   175  	// Check whether the genesis block is already written.
   176  	if genesis != nil {
   177  		block, _ := genesis.ToBlock()
   178  		hash := block.Hash()
   179  		if hash != stored {
   180  			return genesis.Config, block.Hash(), &GenesisMismatchError{stored, hash}
   181  		}
   182  	}
   183  
   184  	// Get the existing chain configuration.
   185  	newcfg := genesis.configOrDefault(stored)
   186  	storedcfg, err := GetChainConfig(db, stored)
   187  	if err != nil {
   188  		if err == ErrChainConfigNotFound {
   189  			// This case happens if a genesis write was interrupted.
   190  			log.Warn("Found genesis block without chain config")
   191  			err = WriteChainConfig(db, stored, newcfg)
   192  		}
   193  		return newcfg, stored, err
   194  	}
   195  	// Special case: don't change the existing config of a non-mainnet chain if no new
   196  	// config is supplied. These chains would get AllProtocolChanges (and a compat error)
   197  	// if we just continued here.
   198  	if genesis == nil && stored != params.MainnetGenesisHash {
   199  		return storedcfg, stored, nil
   200  	}
   201  
   202  	// Check config compatibility and write the config. Compatibility errors
   203  	// are returned to the caller unless we're already at block zero.
   204  	height := GetBlockNumber(db, GetHeadHeaderHash(db))
   205  	if height == missingNumber {
   206  		return newcfg, stored, fmt.Errorf("missing block number for head header hash")
   207  	}
   208  	compatErr := storedcfg.CheckCompatible(newcfg, height)
   209  	if compatErr != nil && height != 0 && compatErr.RewindTo != 0 {
   210  		return newcfg, stored, compatErr
   211  	}
   212  	return newcfg, stored, WriteChainConfig(db, stored, newcfg)
   213  }
   214  
   215  func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
   216  	switch {
   217  	case g != nil:
   218  		return g.Config
   219  	case ghash == params.MainnetGenesisHash:
   220  		return params.MainnetChainConfig
   221  	case ghash == params.TestnetGenesisHash:
   222  		return params.TestnetChainConfig
   223  	case ghash == params.DevnetGenesisHash:
   224  		return params.DevnetChainConfig
   225  	default:
   226  		return params.AllEthashProtocolChanges
   227  	}
   228  }
   229  
   230  // ToBlock creates the block and state of a genesis specification.
   231  func (g *Genesis) ToBlock() (*types.Block, *state.StateDB) {
   232  	db, _ := ethdb.NewMemDatabase()
   233  	statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
   234  	for addr, account := range g.Alloc {
   235  		statedb.AddBalance(addr, account.Balance)
   236  		statedb.SetCode(addr, account.Code)
   237  		statedb.SetNonce(addr, account.Nonce)
   238  		for key, value := range account.Storage {
   239  			statedb.SetState(addr, key, value)
   240  		}
   241  	}
   242  	root := statedb.IntermediateRoot(false)
   243  	head := &types.Header{
   244  		Number:     new(big.Int).SetUint64(g.Number),
   245  		Nonce:      types.EncodeNonce(g.Nonce),
   246  		Time:       new(big.Int).SetUint64(g.Timestamp),
   247  		ParentHash: g.ParentHash,
   248  		Extra:      g.ExtraData,
   249  		GasLimit:   new(big.Int).SetUint64(g.GasLimit),
   250  		GasUsed:    new(big.Int).SetUint64(g.GasUsed),
   251  		Difficulty: g.Difficulty,
   252  		MixDigest:  g.Mixhash,
   253  		Coinbase:   g.Coinbase,
   254  		Root:       root,
   255  	}
   256  	if g.GasLimit == 0 {
   257  		head.GasLimit = params.GenesisGasLimit
   258  	}
   259  	if g.Difficulty == nil {
   260  		head.Difficulty = params.GenesisDifficulty
   261  	}
   262  	return types.NewBlock(head, nil, nil, nil), statedb
   263  }
   264  
   265  // Commit writes the block and state of a genesis specification to the database.
   266  // The block is committed as the canonical head block.
   267  func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) {
   268  	block, statedb := g.ToBlock()
   269  	if block.Number().Sign() != 0 {
   270  		return nil, fmt.Errorf("can't commit genesis block with number > 0")
   271  	}
   272  	if _, err := statedb.CommitTo(db, false); err != nil {
   273  		return nil, fmt.Errorf("cannot write state: %v", err)
   274  	}
   275  	if err := WriteTd(db, block.Hash(), block.NumberU64(), g.Difficulty); err != nil {
   276  		return nil, err
   277  	}
   278  	if err := WriteBlock(db, block); err != nil {
   279  		return nil, err
   280  	}
   281  	if err := WriteBlockReceipts(db, block.Hash(), block.NumberU64(), nil); err != nil {
   282  		return nil, err
   283  	}
   284  	if err := WriteCanonicalHash(db, block.Hash(), block.NumberU64()); err != nil {
   285  		return nil, err
   286  	}
   287  	if err := WriteHeadBlockHash(db, block.Hash()); err != nil {
   288  		return nil, err
   289  	}
   290  	if err := WriteHeadHeaderHash(db, block.Hash()); err != nil {
   291  		return nil, err
   292  	}
   293  	config := g.Config
   294  	if config == nil {
   295  		config = params.AllEthashProtocolChanges
   296  	}
   297  	return block, WriteChainConfig(db, block.Hash(), config)
   298  }
   299  
   300  // MustCommit writes the genesis block and state to db, panicking on error.
   301  // The block is committed as the canonical head block.
   302  func (g *Genesis) MustCommit(db ethdb.Database) *types.Block {
   303  	block, err := g.Commit(db)
   304  	if err != nil {
   305  		panic(err)
   306  	}
   307  	return block
   308  }
   309  
   310  // GenesisBlockForTesting creates and writes a block in which addr has the given wei balance.
   311  func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
   312  	g := Genesis{Alloc: GenesisAlloc{addr: {Balance: balance}}}
   313  	return g.MustCommit(db)
   314  }
   315  
   316  // DefaultGenesisBlock returns the Ethereum main net genesis block.
   317  // https://github.com/ethereum/EIPs/issues/225
   318  func DefaultGenesisBlock() *Genesis {
   319  	genesisSigner := "0x00000000000000000000000000000000000000000000000000000000000000003a5fBaC6CA913599C5fde8c1638dB58d01De8A480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
   320  	return &Genesis{
   321  		Config:     params.MainnetChainConfig,
   322  		Nonce:      0,
   323  		ExtraData:  hexutil.MustDecode(genesisSigner),
   324  		GasLimit:   5000000,
   325  		Difficulty: big.NewInt(2),
   326  		Alloc:      decodePrealloc(mainnetAllocData),
   327  	}
   328  }
   329  
   330  // DefaultTestnetGenesisBlock returns the Ropsten network genesis block.
   331  func DefaultTestnetGenesisBlock() *Genesis {
   332  	genesisSigner := "0x00000000000000000000000000000000000000000000000000000000000000004110bd1ff0b73fa12c259acf39c950277f2667870000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
   333  	return &Genesis{
   334  		Config:     params.TestnetChainConfig,
   335  		Nonce:      0,
   336  		ExtraData:  hexutil.MustDecode(genesisSigner),
   337  		GasLimit:   16777216,
   338  		Difficulty: big.NewInt(1),
   339  		Alloc:      decodePrealloc(testnetAllocData),
   340  	}
   341  }
   342  
   343  // DefaultTestnetGenesisBlock returns the Ropsten network genesis block.
   344  func DeveloperGenesisBlock() *Genesis {
   345  	genesisSigner := "0x00000000000000000000000000000000000000000000000000000000000000009944D0CC757CD9391EE320FC17d5B6f61693f2c50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
   346  	return &Genesis{
   347  		Config:     params.DevnetChainConfig,
   348  		Nonce:      0,
   349  		ExtraData:  hexutil.MustDecode(genesisSigner),
   350  		GasLimit:   16777216,
   351  		Difficulty: big.NewInt(1),
   352  		Alloc:      decodePrealloc(devnetAllocData),
   353  	}
   354  }
   355  
   356  /*
   357  // DeveloperGenesisBlock returns the 'geth --dev' genesis block. Note, this must
   358  // be seeded with the
   359  func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
   360  	// Override the default period to the user requested one
   361  	config := *params.AllCliqueProtocolChanges
   362  	config.Clique.Period = period
   363  
   364  	// Assemble and return the genesis with the precompiles and faucet pre-funded
   365  	return &Genesis{
   366  		Config:     &config,
   367  		ExtraData:  append(append(make([]byte, 32), faucet[:]...), make([]byte, 65)...),
   368  		GasLimit:   6283185,
   369  		Difficulty: big.NewInt(1),
   370  		Alloc: map[common.Address]GenesisAccount{
   371  			common.BytesToAddress([]byte{1}): {Balance: big.NewInt(1)}, // ECRecover
   372  			common.BytesToAddress([]byte{2}): {Balance: big.NewInt(1)}, // SHA256
   373  			common.BytesToAddress([]byte{3}): {Balance: big.NewInt(1)}, // RIPEMD
   374  			common.BytesToAddress([]byte{4}): {Balance: big.NewInt(1)}, // Identity
   375  			common.BytesToAddress([]byte{5}): {Balance: big.NewInt(1)}, // ModExp
   376  			common.BytesToAddress([]byte{6}): {Balance: big.NewInt(1)}, // ECAdd
   377  			common.BytesToAddress([]byte{7}): {Balance: big.NewInt(1)}, // ECScalarMul
   378  			common.BytesToAddress([]byte{8}): {Balance: big.NewInt(1)}, // ECPairing
   379  			faucet:                           {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))},
   380  		},
   381  	}
   382  }
   383  */
   384  
   385  func decodePrealloc(data string) GenesisAlloc {
   386  	var p []struct{ Addr, Balance *big.Int }
   387  	if err := rlp.NewStream(strings.NewReader(data), 0).Decode(&p); err != nil {
   388  		panic(err)
   389  	}
   390  	ga := make(GenesisAlloc, len(p))
   391  	for _, account := range p {
   392  		ga[common.BigToAddress(account.Addr)] = GenesisAccount{Balance: account.Balance}
   393  	}
   394  	return ga
   395  }