genesis.go 5.7 KB

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