genesis.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. root, stateBatch := statedb.CommitBatch()
  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: 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. if err := stateBatch.Write(); err != nil {
  87. return nil, fmt.Errorf("cannot write state: %v", err)
  88. }
  89. if err := WriteTd(chainDb, block.Hash(), difficulty); err != nil {
  90. return nil, err
  91. }
  92. if err := WriteBlock(chainDb, block); err != nil {
  93. return nil, err
  94. }
  95. if err := PutBlockReceipts(chainDb, block, nil); err != nil {
  96. return nil, err
  97. }
  98. if err := WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64()); err != nil {
  99. return nil, err
  100. }
  101. if err := WriteHeadBlockHash(chainDb, block.Hash()); err != nil {
  102. return nil, err
  103. }
  104. return block, nil
  105. }
  106. // GenesisBlockForTesting creates a block in which addr has the given wei balance.
  107. // The state trie of the block is written to db.
  108. func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
  109. statedb := state.New(common.Hash{}, db)
  110. obj := statedb.GetOrNewStateObject(addr)
  111. obj.SetBalance(balance)
  112. root, err := statedb.Commit()
  113. if err != nil {
  114. panic(fmt.Sprintf("cannot write state: %v", err))
  115. }
  116. block := types.NewBlock(&types.Header{
  117. Difficulty: params.GenesisDifficulty,
  118. GasLimit: params.GenesisGasLimit,
  119. Root: root,
  120. }, nil, nil, nil)
  121. return block
  122. }
  123. type GenesisAccount struct {
  124. Address common.Address
  125. Balance *big.Int
  126. }
  127. func WriteGenesisBlockForTesting(db ethdb.Database, accounts ...GenesisAccount) *types.Block {
  128. accountJson := "{"
  129. for i, account := range accounts {
  130. if i != 0 {
  131. accountJson += ","
  132. }
  133. accountJson += fmt.Sprintf(`"0x%x":{"balance":"0x%x"}`, account.Address, account.Balance.Bytes())
  134. }
  135. accountJson += "}"
  136. testGenesis := fmt.Sprintf(`{
  137. "nonce":"0x%x",
  138. "gasLimit":"0x%x",
  139. "difficulty":"0x%x",
  140. "alloc": %s
  141. }`, types.EncodeNonce(0), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes(), accountJson)
  142. block, _ := WriteGenesisBlock(db, strings.NewReader(testGenesis))
  143. return block
  144. }
  145. func WriteTestNetGenesisBlock(chainDb ethdb.Database, nonce uint64) (*types.Block, error) {
  146. testGenesis := fmt.Sprintf(`{
  147. "nonce": "0x%x",
  148. "difficulty": "0x20000",
  149. "mixhash": "0x00000000000000000000000000000000000000647572616c65787365646c6578",
  150. "coinbase": "0x0000000000000000000000000000000000000000",
  151. "timestamp": "0x00",
  152. "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  153. "extraData": "0x",
  154. "gasLimit": "0x2FEFD8",
  155. "alloc": {
  156. "0000000000000000000000000000000000000001": { "balance": "1" },
  157. "0000000000000000000000000000000000000002": { "balance": "1" },
  158. "0000000000000000000000000000000000000003": { "balance": "1" },
  159. "0000000000000000000000000000000000000004": { "balance": "1" },
  160. "102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c": { "balance": "1606938044258990275541962092341162602522202993782792835301376" }
  161. }
  162. }`, types.EncodeNonce(nonce))
  163. return WriteGenesisBlock(chainDb, strings.NewReader(testGenesis))
  164. }
  165. func WriteOlympicGenesisBlock(chainDb ethdb.Database, nonce uint64) (*types.Block, error) {
  166. testGenesis := fmt.Sprintf(`{
  167. "nonce":"0x%x",
  168. "gasLimit":"0x%x",
  169. "difficulty":"0x%x",
  170. "alloc": {
  171. "0000000000000000000000000000000000000001": {"balance": "1"},
  172. "0000000000000000000000000000000000000002": {"balance": "1"},
  173. "0000000000000000000000000000000000000003": {"balance": "1"},
  174. "0000000000000000000000000000000000000004": {"balance": "1"},
  175. "dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  176. "e4157b34ea9615cfbde6b4fda419828124b70c78": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  177. "b9c015918bdaba24b4ff057a92a3873d6eb201be": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  178. "6c386a4b26f73c802f34673f7248bb118f97424a": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  179. "cd2a3d9f938e13cd947ec05abc7fe734df8dd826": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  180. "2ef47100e0787b915105fd5e3f4ff6752079d5cb": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  181. "e6716f9544a56c530d868e4bfbacb172315bdead": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  182. "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": {"balance": "1606938044258990275541962092341162602522202993782792835301376"}
  183. }
  184. }`, types.EncodeNonce(nonce), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes())
  185. return WriteGenesisBlock(chainDb, strings.NewReader(testGenesis))
  186. }