wizard_genesis.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // Add a batch of precompile balances to avoid them getting deleted
  109. for i := int64(0); i < 256; i++ {
  110. genesis.Alloc[common.BigToAddress(big.NewInt(i))] = core.GenesisAccount{Balance: big.NewInt(1)}
  111. }
  112. // Query the user for some custom extras
  113. fmt.Println()
  114. fmt.Println("Specify your chain/network ID if you want an explicit one (default = random)")
  115. genesis.Config.ChainId = new(big.Int).SetUint64(uint64(w.readDefaultInt(rand.Intn(65536))))
  116. // All done, store the genesis and flush to disk
  117. log.Info("Configured new genesis block")
  118. w.conf.Genesis = genesis
  119. w.conf.flush()
  120. }
  121. // manageGenesis permits the modification of chain configuration parameters in
  122. // a genesis config and the export of the entire genesis spec.
  123. func (w *wizard) manageGenesis() {
  124. // Figure out whether to modify or export the genesis
  125. fmt.Println()
  126. fmt.Println(" 1. Modify existing fork rules")
  127. fmt.Println(" 2. Export genesis configuration")
  128. fmt.Println(" 3. Remove genesis configuration")
  129. choice := w.read()
  130. switch {
  131. case choice == "1":
  132. // Fork rule updating requested, iterate over each fork
  133. fmt.Println()
  134. fmt.Printf("Which block should Homestead come into effect? (default = %v)\n", w.conf.Genesis.Config.HomesteadBlock)
  135. w.conf.Genesis.Config.HomesteadBlock = w.readDefaultBigInt(w.conf.Genesis.Config.HomesteadBlock)
  136. fmt.Println()
  137. fmt.Printf("Which block should EIP150 come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP150Block)
  138. w.conf.Genesis.Config.EIP150Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP150Block)
  139. fmt.Println()
  140. fmt.Printf("Which block should EIP155 come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP155Block)
  141. w.conf.Genesis.Config.EIP155Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP155Block)
  142. fmt.Println()
  143. fmt.Printf("Which block should EIP158 come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP158Block)
  144. w.conf.Genesis.Config.EIP158Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP158Block)
  145. fmt.Println()
  146. fmt.Printf("Which block should Byzantium come into effect? (default = %v)\n", w.conf.Genesis.Config.ByzantiumBlock)
  147. w.conf.Genesis.Config.ByzantiumBlock = w.readDefaultBigInt(w.conf.Genesis.Config.ByzantiumBlock)
  148. out, _ := json.MarshalIndent(w.conf.Genesis.Config, "", " ")
  149. fmt.Printf("Chain configuration updated:\n\n%s\n", out)
  150. case choice == "2":
  151. // Save whatever genesis configuration we currently have
  152. fmt.Println()
  153. fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network)
  154. out, _ := json.MarshalIndent(w.conf.Genesis, "", " ")
  155. if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil {
  156. log.Error("Failed to save genesis file", "err", err)
  157. }
  158. log.Info("Exported existing genesis block")
  159. case choice == "3":
  160. // Make sure we don't have any services running
  161. if len(w.conf.servers()) > 0 {
  162. log.Error("Genesis reset requires all services and servers torn down")
  163. return
  164. }
  165. log.Info("Genesis block destroyed")
  166. w.conf.Genesis = nil
  167. w.conf.flush()
  168. default:
  169. log.Error("That's not something I can do")
  170. }
  171. }