genesis.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package core
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/big"
  6. "os"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/core/state"
  9. "github.com/ethereum/go-ethereum/core/types"
  10. "github.com/ethereum/go-ethereum/params"
  11. )
  12. // GenesisBlock creates a genesis block with the given nonce.
  13. func GenesisBlock(nonce uint64, db common.Database) *types.Block {
  14. var accounts map[string]struct {
  15. Balance string
  16. Code string
  17. }
  18. err := json.Unmarshal(GenesisAccounts, &accounts)
  19. if err != nil {
  20. fmt.Println("unable to decode genesis json data:", err)
  21. os.Exit(1)
  22. }
  23. statedb := state.New(common.Hash{}, db)
  24. for addr, account := range accounts {
  25. codedAddr := common.Hex2Bytes(addr)
  26. accountState := statedb.CreateAccount(common.BytesToAddress(codedAddr))
  27. accountState.SetBalance(common.Big(account.Balance))
  28. accountState.SetCode(common.FromHex(account.Code))
  29. statedb.UpdateStateObject(accountState)
  30. }
  31. statedb.Sync()
  32. block := types.NewBlock(&types.Header{
  33. Difficulty: params.GenesisDifficulty,
  34. GasLimit: params.GenesisGasLimit,
  35. Nonce: types.EncodeNonce(nonce),
  36. Root: statedb.Root(),
  37. }, nil, nil, nil)
  38. block.Td = params.GenesisDifficulty
  39. return block
  40. }
  41. var GenesisAccounts = []byte(`{
  42. "0000000000000000000000000000000000000001": {"balance": "1"},
  43. "0000000000000000000000000000000000000002": {"balance": "1"},
  44. "0000000000000000000000000000000000000003": {"balance": "1"},
  45. "0000000000000000000000000000000000000004": {"balance": "1"},
  46. "dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  47. "e4157b34ea9615cfbde6b4fda419828124b70c78": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  48. "b9c015918bdaba24b4ff057a92a3873d6eb201be": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  49. "6c386a4b26f73c802f34673f7248bb118f97424a": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  50. "cd2a3d9f938e13cd947ec05abc7fe734df8dd826": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  51. "2ef47100e0787b915105fd5e3f4ff6752079d5cb": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  52. "e6716f9544a56c530d868e4bfbacb172315bdead": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  53. "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": {"balance": "1606938044258990275541962092341162602522202993782792835301376"}
  54. }`)
  55. // GenesisBlockForTesting creates a block in which addr has the given wei balance.
  56. // The state trie of the block is written to db.
  57. func GenesisBlockForTesting(db common.Database, addr common.Address, balance *big.Int) *types.Block {
  58. statedb := state.New(common.Hash{}, db)
  59. obj := statedb.GetOrNewStateObject(addr)
  60. obj.SetBalance(balance)
  61. statedb.SyncObjects()
  62. statedb.Sync()
  63. block := types.NewBlock(&types.Header{
  64. Difficulty: params.GenesisDifficulty,
  65. GasLimit: params.GenesisGasLimit,
  66. Root: statedb.Root(),
  67. }, nil, nil, nil)
  68. block.Td = params.GenesisDifficulty
  69. return block
  70. }