genesis.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. "compress/gzip"
  19. "encoding/base64"
  20. "encoding/json"
  21. "fmt"
  22. "io"
  23. "io/ioutil"
  24. "math/big"
  25. "strings"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/ethdb"
  30. "github.com/ethereum/go-ethereum/logger"
  31. "github.com/ethereum/go-ethereum/logger/glog"
  32. "github.com/ethereum/go-ethereum/params"
  33. )
  34. // WriteGenesisBlock writes the genesis block to the database as block number 0
  35. func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block, error) {
  36. contents, err := ioutil.ReadAll(reader)
  37. if err != nil {
  38. return nil, err
  39. }
  40. var genesis struct {
  41. Nonce string
  42. Timestamp string
  43. ParentHash string
  44. ExtraData string
  45. GasLimit string
  46. Difficulty string
  47. Mixhash string
  48. Coinbase string
  49. Alloc map[string]struct {
  50. Code string
  51. Storage map[string]string
  52. Balance string
  53. }
  54. }
  55. if err := json.Unmarshal(contents, &genesis); err != nil {
  56. return nil, err
  57. }
  58. // creating with empty hash always works
  59. statedb, _ := state.New(common.Hash{}, chainDb)
  60. for addr, account := range genesis.Alloc {
  61. address := common.HexToAddress(addr)
  62. statedb.AddBalance(address, common.String2Big(account.Balance))
  63. statedb.SetCode(address, common.Hex2Bytes(account.Code))
  64. for key, value := range account.Storage {
  65. statedb.SetState(address, common.HexToHash(key), common.HexToHash(value))
  66. }
  67. }
  68. root, stateBatch := statedb.CommitBatch()
  69. difficulty := common.String2Big(genesis.Difficulty)
  70. block := types.NewBlock(&types.Header{
  71. Nonce: types.EncodeNonce(common.String2Big(genesis.Nonce).Uint64()),
  72. Time: common.String2Big(genesis.Timestamp),
  73. ParentHash: common.HexToHash(genesis.ParentHash),
  74. Extra: common.FromHex(genesis.ExtraData),
  75. GasLimit: common.String2Big(genesis.GasLimit),
  76. Difficulty: difficulty,
  77. MixDigest: common.HexToHash(genesis.Mixhash),
  78. Coinbase: common.HexToAddress(genesis.Coinbase),
  79. Root: root,
  80. }, nil, nil, nil)
  81. if block := GetBlock(chainDb, block.Hash()); block != nil {
  82. glog.V(logger.Info).Infoln("Genesis block already in chain. Writing canonical number")
  83. err := WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
  84. if err != nil {
  85. return nil, err
  86. }
  87. return block, nil
  88. }
  89. if err := stateBatch.Write(); err != nil {
  90. return nil, fmt.Errorf("cannot write state: %v", err)
  91. }
  92. if err := WriteTd(chainDb, block.Hash(), difficulty); err != nil {
  93. return nil, err
  94. }
  95. if err := WriteBlock(chainDb, block); err != nil {
  96. return nil, err
  97. }
  98. if err := WriteBlockReceipts(chainDb, block.Hash(), nil); err != nil {
  99. return nil, err
  100. }
  101. if err := WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64()); err != nil {
  102. return nil, err
  103. }
  104. if err := WriteHeadBlockHash(chainDb, block.Hash()); err != nil {
  105. return nil, err
  106. }
  107. return block, nil
  108. }
  109. // GenesisBlockForTesting creates a block in which addr has the given wei balance.
  110. // The state trie of the block is written to db. the passed db needs to contain a state root
  111. func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
  112. statedb, _ := state.New(common.Hash{}, db)
  113. obj := statedb.GetOrNewStateObject(addr)
  114. obj.SetBalance(balance)
  115. root, err := statedb.Commit()
  116. if err != nil {
  117. panic(fmt.Sprintf("cannot write state: %v", err))
  118. }
  119. block := types.NewBlock(&types.Header{
  120. Difficulty: params.GenesisDifficulty,
  121. GasLimit: params.GenesisGasLimit,
  122. Root: root,
  123. }, nil, nil, nil)
  124. return block
  125. }
  126. type GenesisAccount struct {
  127. Address common.Address
  128. Balance *big.Int
  129. }
  130. func WriteGenesisBlockForTesting(db ethdb.Database, accounts ...GenesisAccount) *types.Block {
  131. accountJson := "{"
  132. for i, account := range accounts {
  133. if i != 0 {
  134. accountJson += ","
  135. }
  136. accountJson += fmt.Sprintf(`"0x%x":{"balance":"0x%x"}`, account.Address, account.Balance.Bytes())
  137. }
  138. accountJson += "}"
  139. testGenesis := fmt.Sprintf(`{
  140. "nonce":"0x%x",
  141. "gasLimit":"0x%x",
  142. "difficulty":"0x%x",
  143. "alloc": %s
  144. }`, types.EncodeNonce(0), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes(), accountJson)
  145. block, _ := WriteGenesisBlock(db, strings.NewReader(testGenesis))
  146. return block
  147. }
  148. // WriteDefaultGenesisBlock assembles the official Ethereum genesis block and
  149. // writes it - along with all associated state - into a chain database.
  150. func WriteDefaultGenesisBlock(chainDb ethdb.Database) (*types.Block, error) {
  151. return WriteGenesisBlock(chainDb, strings.NewReader(DefaultGenesisBlock()))
  152. }
  153. // WriteTestNetGenesisBlock assembles the Morden test network genesis block and
  154. // writes it - along with all associated state - into a chain database.
  155. func WriteTestNetGenesisBlock(chainDb ethdb.Database) (*types.Block, error) {
  156. return WriteGenesisBlock(chainDb, strings.NewReader(TestNetGenesisBlock()))
  157. }
  158. // WriteOlympicGenesisBlock assembles the Olympic genesis block and writes it
  159. // along with all associated state into a chain database.
  160. func WriteOlympicGenesisBlock(db ethdb.Database) (*types.Block, error) {
  161. return WriteGenesisBlock(db, strings.NewReader(OlympicGenesisBlock()))
  162. }
  163. // DefaultGenesisBlock assembles a JSON string representing the default Ethereum
  164. // genesis block.
  165. func DefaultGenesisBlock() string {
  166. reader, err := gzip.NewReader(base64.NewDecoder(base64.StdEncoding, strings.NewReader(defaultGenesisBlock)))
  167. if err != nil {
  168. panic(fmt.Sprintf("failed to access default genesis: %v", err))
  169. }
  170. blob, err := ioutil.ReadAll(reader)
  171. if err != nil {
  172. panic(fmt.Sprintf("failed to load default genesis: %v", err))
  173. }
  174. return string(blob)
  175. }
  176. // OlympicGenesisBlock assembles a JSON string representing the Olympic genesis
  177. // block.
  178. func OlympicGenesisBlock() string {
  179. return fmt.Sprintf(`{
  180. "nonce":"0x%x",
  181. "gasLimit":"0x%x",
  182. "difficulty":"0x%x",
  183. "alloc": {
  184. "0000000000000000000000000000000000000001": {"balance": "1"},
  185. "0000000000000000000000000000000000000002": {"balance": "1"},
  186. "0000000000000000000000000000000000000003": {"balance": "1"},
  187. "0000000000000000000000000000000000000004": {"balance": "1"},
  188. "dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  189. "e4157b34ea9615cfbde6b4fda419828124b70c78": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  190. "b9c015918bdaba24b4ff057a92a3873d6eb201be": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  191. "6c386a4b26f73c802f34673f7248bb118f97424a": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  192. "cd2a3d9f938e13cd947ec05abc7fe734df8dd826": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  193. "2ef47100e0787b915105fd5e3f4ff6752079d5cb": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  194. "e6716f9544a56c530d868e4bfbacb172315bdead": {"balance": "1606938044258990275541962092341162602522202993782792835301376"},
  195. "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": {"balance": "1606938044258990275541962092341162602522202993782792835301376"}
  196. }
  197. }`, types.EncodeNonce(42), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes())
  198. }
  199. // TestNetGenesisBlock assembles a JSON string representing the Morden test net
  200. // genenis block.
  201. func TestNetGenesisBlock() string {
  202. return fmt.Sprintf(`{
  203. "nonce": "0x%x",
  204. "difficulty": "0x20000",
  205. "mixhash": "0x00000000000000000000000000000000000000647572616c65787365646c6578",
  206. "coinbase": "0x0000000000000000000000000000000000000000",
  207. "timestamp": "0x00",
  208. "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  209. "extraData": "0x",
  210. "gasLimit": "0x2FEFD8",
  211. "alloc": {
  212. "0000000000000000000000000000000000000001": { "balance": "1" },
  213. "0000000000000000000000000000000000000002": { "balance": "1" },
  214. "0000000000000000000000000000000000000003": { "balance": "1" },
  215. "0000000000000000000000000000000000000004": { "balance": "1" },
  216. "102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c": { "balance": "1606938044258990275541962092341162602522202993782792835301376" }
  217. }
  218. }`, types.EncodeNonce(0x6d6f7264656e))
  219. }