genesis.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. package core
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "io"
  21. "io/ioutil"
  22. "math/big"
  23. "strings"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core/state"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/logger"
  29. "github.com/ethereum/go-ethereum/logger/glog"
  30. "github.com/ethereum/go-ethereum/params"
  31. )
  32. // WriteGenesisBlock writes the genesis block to the database as block number 0
  33. func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block, error) {
  34. contents, err := ioutil.ReadAll(reader)
  35. if err != nil {
  36. return nil, err
  37. }
  38. var genesis struct {
  39. Nonce string
  40. Timestamp string
  41. ParentHash string
  42. ExtraData string
  43. GasLimit string
  44. Difficulty string
  45. Mixhash string
  46. Coinbase string
  47. Alloc map[string]struct {
  48. Code string
  49. Storage map[string]string
  50. Balance string
  51. }
  52. }
  53. if err := json.Unmarshal(contents, &genesis); err != nil {
  54. return nil, err
  55. }
  56. statedb := state.New(common.Hash{}, chainDb)
  57. for addr, account := range genesis.Alloc {
  58. address := common.HexToAddress(addr)
  59. statedb.AddBalance(address, common.String2Big(account.Balance))
  60. statedb.SetCode(address, common.Hex2Bytes(account.Code))
  61. for key, value := range account.Storage {
  62. statedb.SetState(address, common.HexToHash(key), common.HexToHash(value))
  63. }
  64. }
  65. statedb.SyncObjects()
  66. difficulty := common.String2Big(genesis.Difficulty)
  67. block := types.NewBlock(&types.Header{
  68. Nonce: types.EncodeNonce(common.String2Big(genesis.Nonce).Uint64()),
  69. Time: common.String2Big(genesis.Timestamp),
  70. ParentHash: common.HexToHash(genesis.ParentHash),
  71. Extra: common.FromHex(genesis.ExtraData),
  72. GasLimit: common.String2Big(genesis.GasLimit),
  73. Difficulty: difficulty,
  74. MixDigest: common.HexToHash(genesis.Mixhash),
  75. Coinbase: common.HexToAddress(genesis.Coinbase),
  76. Root: statedb.Root(),
  77. }, nil, nil, nil)
  78. if block := GetBlock(chainDb, block.Hash()); block != nil {
  79. glog.V(logger.Info).Infoln("Genesis block already in chain. Writing canonical number")
  80. err := WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
  81. if err != nil {
  82. return nil, err
  83. }
  84. return block, nil
  85. }
  86. statedb.Sync()
  87. if err := WriteTd(chainDb, block.Hash(), difficulty); err != nil {
  88. return nil, err
  89. }
  90. if err := WriteBlock(chainDb, block); err != nil {
  91. return nil, err
  92. }
  93. if err := WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64()); err != nil {
  94. return nil, err
  95. }
  96. if err := WriteHeadBlockHash(chainDb, block.Hash()); err != nil {
  97. return nil, err
  98. }
  99. return block, nil
  100. }
  101. // GenesisBlockForTesting creates a block in which addr has the given wei balance.
  102. // The state trie of the block is written to db.
  103. func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
  104. statedb := state.New(common.Hash{}, db)
  105. obj := statedb.GetOrNewStateObject(addr)
  106. obj.SetBalance(balance)
  107. statedb.SyncObjects()
  108. statedb.Sync()
  109. block := types.NewBlock(&types.Header{
  110. Difficulty: params.GenesisDifficulty,
  111. GasLimit: params.GenesisGasLimit,
  112. Root: statedb.Root(),
  113. }, nil, nil, nil)
  114. return block
  115. }
  116. func WriteGenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
  117. testGenesis := fmt.Sprintf(`{
  118. "nonce":"0x%x",
  119. "gasLimit":"0x%x",
  120. "difficulty":"0x%x",
  121. "alloc": {
  122. "0x%x":{"balance":"0x%x"}
  123. }
  124. }`, types.EncodeNonce(0), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes(), addr, balance.Bytes())
  125. block, _ := WriteGenesisBlock(db, strings.NewReader(testGenesis))
  126. return block
  127. }
  128. func WriteTestNetGenesisBlock(chainDb ethdb.Database, nonce uint64) (*types.Block, error) {
  129. testGenesis := fmt.Sprintf(`{
  130. "nonce":"0x%x",
  131. "gasLimit":"0x%x",
  132. "difficulty":"0x%x",
  133. "alloc": {
  134. "0000000000000000000000000000000000000001": {"balance": "1"},
  135. "0000000000000000000000000000000000000002": {"balance": "1"},
  136. "0000000000000000000000000000000000000003": {"balance": "1"},
  137. "0000000000000000000000000000000000000004": {"balance": "1"},
  138. "dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  139. "e4157b34ea9615cfbde6b4fda419828124b70c78": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  140. "b9c015918bdaba24b4ff057a92a3873d6eb201be": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  141. "6c386a4b26f73c802f34673f7248bb118f97424a": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  142. "cd2a3d9f938e13cd947ec05abc7fe734df8dd826": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  143. "2ef47100e0787b915105fd5e3f4ff6752079d5cb": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  144. "e6716f9544a56c530d868e4bfbacb172315bdead": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  145. "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": {"balance": "1606938044258990275541962092341162602522202993782792835301376"}
  146. }
  147. }`, types.EncodeNonce(nonce), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes())
  148. return WriteGenesisBlock(chainDb, strings.NewReader(testGenesis))
  149. }