genesis.go 3.7 KB

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