wizard_genesis.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2017 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 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "io/ioutil"
  22. "math/big"
  23. "math/rand"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core"
  27. "github.com/ethereum/go-ethereum/log"
  28. "github.com/ethereum/go-ethereum/params"
  29. )
  30. // makeGenesis creates a new genesis struct based on some user input.
  31. func (w *wizard) makeGenesis() {
  32. // Construct a default genesis block
  33. genesis := &core.Genesis{
  34. Timestamp: uint64(time.Now().Unix()),
  35. GasLimit: 4700000,
  36. Difficulty: big.NewInt(524288),
  37. Alloc: make(core.GenesisAlloc),
  38. Config: &params.ChainConfig{
  39. HomesteadBlock: big.NewInt(1),
  40. EIP150Block: big.NewInt(2),
  41. EIP155Block: big.NewInt(3),
  42. EIP158Block: big.NewInt(3),
  43. ByzantiumBlock: big.NewInt(4),
  44. },
  45. }
  46. // Figure out which consensus engine to choose
  47. fmt.Println()
  48. fmt.Println("Which consensus engine to use? (default = clique)")
  49. fmt.Println(" 1. Ethash - proof-of-work")
  50. fmt.Println(" 2. Clique - proof-of-authority")
  51. choice := w.read()
  52. switch {
  53. case choice == "1":
  54. // In case of ethash, we're pretty much done
  55. genesis.Config.Ethash = new(params.EthashConfig)
  56. genesis.ExtraData = make([]byte, 32)
  57. case choice == "" || choice == "2":
  58. // In the case of clique, configure the consensus parameters
  59. genesis.Difficulty = big.NewInt(1)
  60. genesis.Config.Clique = &params.CliqueConfig{
  61. Period: 15,
  62. Epoch: 30000,
  63. }
  64. fmt.Println()
  65. fmt.Println("How many seconds should blocks take? (default = 15)")
  66. genesis.Config.Clique.Period = uint64(w.readDefaultInt(15))
  67. // We also need the initial list of signers
  68. fmt.Println()
  69. fmt.Println("Which accounts are allowed to seal? (mandatory at least one)")
  70. var signers []common.Address
  71. for {
  72. if address := w.readAddress(); address != nil {
  73. signers = append(signers, *address)
  74. continue
  75. }
  76. if len(signers) > 0 {
  77. break
  78. }
  79. }
  80. // Sort the signers and embed into the extra-data section
  81. for i := 0; i < len(signers); i++ {
  82. for j := i + 1; j < len(signers); j++ {
  83. if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
  84. signers[i], signers[j] = signers[j], signers[i]
  85. }
  86. }
  87. }
  88. genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
  89. for i, signer := range signers {
  90. copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
  91. }
  92. default:
  93. log.Crit("Invalid consensus engine choice", "choice", choice)
  94. }
  95. // Consensus all set, just ask for initial funds and go
  96. fmt.Println()
  97. fmt.Println("Which accounts should be pre-funded? (advisable at least one)")
  98. for {
  99. // Read the address of the account to fund
  100. if address := w.readAddress(); address != nil {
  101. genesis.Alloc[*address] = core.GenesisAccount{
  102. Balance: new(big.Int).Lsh(big.NewInt(1), 256-7), // 2^256 / 128 (allow many pre-funds without balance overflows)
  103. }
  104. continue
  105. }
  106. break
  107. }
  108. fmt.Println()
  109. fmt.Println("Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? (advisable yes)")
  110. if w.readYesNo(true) {
  111. // Add a batch of precompile balances to avoid them getting deleted
  112. for i := int64(0); i < 256; i++ {
  113. genesis.Alloc[common.BigToAddress(big.NewInt(i))] = core.GenesisAccount{Balance: big.NewInt(1)}
  114. }
  115. }
  116. // Query the user for some custom extras
  117. fmt.Println()
  118. fmt.Println("Specify your chain/network ID if you want an explicit one (default = random)")
  119. genesis.Config.ChainID = new(big.Int).SetUint64(uint64(w.readDefaultInt(rand.Intn(65536))))
  120. // All done, store the genesis and flush to disk
  121. log.Info("Configured new genesis block")
  122. w.conf.Genesis = genesis
  123. w.conf.flush()
  124. }
  125. // manageGenesis permits the modification of chain configuration parameters in
  126. // a genesis config and the export of the entire genesis spec.
  127. func (w *wizard) manageGenesis() {
  128. // Figure out whether to modify or export the genesis
  129. fmt.Println()
  130. fmt.Println(" 1. Modify existing fork rules")
  131. fmt.Println(" 2. Export genesis configurations")
  132. fmt.Println(" 3. Remove genesis configuration")
  133. choice := w.read()
  134. switch choice {
  135. case "1":
  136. // Fork rule updating requested, iterate over each fork
  137. fmt.Println()
  138. fmt.Printf("Which block should Homestead come into effect? (default = %v)\n", w.conf.Genesis.Config.HomesteadBlock)
  139. w.conf.Genesis.Config.HomesteadBlock = w.readDefaultBigInt(w.conf.Genesis.Config.HomesteadBlock)
  140. fmt.Println()
  141. fmt.Printf("Which block should EIP150 (Tangerine Whistle) come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP150Block)
  142. w.conf.Genesis.Config.EIP150Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP150Block)
  143. fmt.Println()
  144. fmt.Printf("Which block should EIP155 (Spurious Dragon) come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP155Block)
  145. w.conf.Genesis.Config.EIP155Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP155Block)
  146. fmt.Println()
  147. fmt.Printf("Which block should EIP158/161 (also Spurious Dragon) come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP158Block)
  148. w.conf.Genesis.Config.EIP158Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP158Block)
  149. fmt.Println()
  150. fmt.Printf("Which block should Byzantium come into effect? (default = %v)\n", w.conf.Genesis.Config.ByzantiumBlock)
  151. w.conf.Genesis.Config.ByzantiumBlock = w.readDefaultBigInt(w.conf.Genesis.Config.ByzantiumBlock)
  152. fmt.Println()
  153. fmt.Printf("Which block should Constantinople come into effect? (default = %v)\n", w.conf.Genesis.Config.ByzantiumBlock)
  154. w.conf.Genesis.Config.ConstantinopleBlock = w.readDefaultBigInt(w.conf.Genesis.Config.ConstantinopleBlock)
  155. out, _ := json.MarshalIndent(w.conf.Genesis.Config, "", " ")
  156. fmt.Printf("Chain configuration updated:\n\n%s\n", out)
  157. case "2":
  158. // Save whatever genesis configuration we currently have
  159. fmt.Println()
  160. fmt.Printf("Which base filename to save the genesis specifications into? (default = %s)\n", w.network)
  161. out, _ := json.MarshalIndent(w.conf.Genesis, "", " ")
  162. basename := w.readDefaultString(fmt.Sprintf("%s.json", w.network))
  163. gethJson := fmt.Sprintf("%s.json", basename)
  164. if err := ioutil.WriteFile((gethJson), out, 0644); err != nil {
  165. log.Error("Failed to save genesis file", "err", err)
  166. }
  167. log.Info("Saved geth genesis as %v", gethJson)
  168. if err := convertGenesis(w.conf.Genesis, basename, w.network, w.conf.bootnodes); err != nil {
  169. log.Error("Conversion failed", "err", err)
  170. }
  171. case "3":
  172. // Make sure we don't have any services running
  173. if len(w.conf.servers()) > 0 {
  174. log.Error("Genesis reset requires all services and servers torn down")
  175. return
  176. }
  177. log.Info("Genesis block destroyed")
  178. w.conf.Genesis = nil
  179. w.conf.flush()
  180. default:
  181. log.Error("That's not something I can do")
  182. }
  183. }
  184. func saveGenesis(basename, client string, spec interface{}) {
  185. filename := fmt.Sprintf("%s-%s.json", basename, client)
  186. out, _ := json.Marshal(spec)
  187. if err := ioutil.WriteFile(filename, out, 0644); err != nil {
  188. log.Error("failed to save genesis file", "client", client, "err", err)
  189. }
  190. log.Info("saved chainspec", "client", client, "filename", filename)
  191. }
  192. func convertGenesis(genesis *core.Genesis, basename string, network string, bootnodes []string) error {
  193. if spec, err := newAlethGenesisSpec(network, genesis); err == nil {
  194. saveGenesis(basename, "aleth", spec)
  195. } else {
  196. log.Error("failed to create chain spec", "client", "aleth", "err", err)
  197. }
  198. if spec, err := newParityChainSpec(network, genesis, []string{}); err == nil {
  199. saveGenesis(basename, "parity", spec)
  200. } else {
  201. log.Error("failed to create chain spec", "client", "parity", "err", err)
  202. }
  203. saveGenesis(basename, "harmony", genesis)
  204. return nil
  205. }