github.com/hhwill/poc-eth@v0.0.0-20240218063348-3bb107c90dbf/core/genesis.go (about)

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