wizard_genesis.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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"
  22. "math/big"
  23. "math/rand"
  24. "net/http"
  25. "os"
  26. "path/filepath"
  27. "time"
  28. "github.com/ethereum/go-ethereum/common"
  29. "github.com/ethereum/go-ethereum/core"
  30. "github.com/ethereum/go-ethereum/log"
  31. "github.com/ethereum/go-ethereum/params"
  32. )
  33. // makeGenesis creates a new genesis struct based on some user input.
  34. func (w *wizard) makeGenesis() {
  35. // Construct a default genesis block
  36. genesis := &core.Genesis{
  37. Timestamp: uint64(time.Now().Unix()),
  38. GasLimit: 4700000,
  39. Difficulty: big.NewInt(524288),
  40. Alloc: make(core.GenesisAlloc),
  41. Config: &params.ChainConfig{
  42. HomesteadBlock: big.NewInt(0),
  43. EIP150Block: big.NewInt(0),
  44. EIP155Block: big.NewInt(0),
  45. EIP158Block: big.NewInt(0),
  46. ByzantiumBlock: big.NewInt(0),
  47. ConstantinopleBlock: big.NewInt(0),
  48. PetersburgBlock: big.NewInt(0),
  49. IstanbulBlock: big.NewInt(0),
  50. },
  51. }
  52. // Figure out which consensus engine to choose
  53. fmt.Println()
  54. fmt.Println("Which consensus engine to use? (default = clique)")
  55. fmt.Println(" 1. Ethash - proof-of-work")
  56. fmt.Println(" 2. Clique - proof-of-authority")
  57. choice := w.read()
  58. switch {
  59. case choice == "1":
  60. // In case of ethash, we're pretty much done
  61. genesis.Config.Ethash = new(params.EthashConfig)
  62. genesis.ExtraData = make([]byte, 32)
  63. case choice == "" || choice == "2":
  64. // In the case of clique, configure the consensus parameters
  65. genesis.Difficulty = big.NewInt(1)
  66. genesis.Config.Clique = &params.CliqueConfig{
  67. Period: 15,
  68. Epoch: 30000,
  69. }
  70. fmt.Println()
  71. fmt.Println("How many seconds should blocks take? (default = 15)")
  72. genesis.Config.Clique.Period = uint64(w.readDefaultInt(15))
  73. // We also need the initial list of signers
  74. fmt.Println()
  75. fmt.Println("Which accounts are allowed to seal? (mandatory at least one)")
  76. var signers []common.Address
  77. for {
  78. if address := w.readAddress(); address != nil {
  79. signers = append(signers, *address)
  80. continue
  81. }
  82. if len(signers) > 0 {
  83. break
  84. }
  85. }
  86. // Sort the signers and embed into the extra-data section
  87. for i := 0; i < len(signers); i++ {
  88. for j := i + 1; j < len(signers); j++ {
  89. if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
  90. signers[i], signers[j] = signers[j], signers[i]
  91. }
  92. }
  93. }
  94. genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
  95. for i, signer := range signers {
  96. copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
  97. }
  98. default:
  99. log.Crit("Invalid consensus engine choice", "choice", choice)
  100. }
  101. // Consensus all set, just ask for initial funds and go
  102. fmt.Println()
  103. fmt.Println("Which accounts should be pre-funded? (advisable at least one)")
  104. for {
  105. // Read the address of the account to fund
  106. if address := w.readAddress(); address != nil {
  107. genesis.Alloc[*address] = core.GenesisAccount{
  108. Balance: new(big.Int).Lsh(big.NewInt(1), 256-7), // 2^256 / 128 (allow many pre-funds without balance overflows)
  109. }
  110. continue
  111. }
  112. break
  113. }
  114. fmt.Println()
  115. fmt.Println("Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? (advisable yes)")
  116. if w.readDefaultYesNo(true) {
  117. // Add a batch of precompile balances to avoid them getting deleted
  118. for i := int64(0); i < 256; i++ {
  119. genesis.Alloc[common.BigToAddress(big.NewInt(i))] = core.GenesisAccount{Balance: big.NewInt(1)}
  120. }
  121. }
  122. // Query the user for some custom extras
  123. fmt.Println()
  124. fmt.Println("Specify your chain/network ID if you want an explicit one (default = random)")
  125. genesis.Config.ChainID = new(big.Int).SetUint64(uint64(w.readDefaultInt(rand.Intn(65536))))
  126. // All done, store the genesis and flush to disk
  127. log.Info("Configured new genesis block")
  128. w.conf.Genesis = genesis
  129. w.conf.flush()
  130. }
  131. // importGenesis imports a Geth genesis spec into puppeth.
  132. func (w *wizard) importGenesis() {
  133. // Request the genesis JSON spec URL from the user
  134. fmt.Println()
  135. fmt.Println("Where's the genesis file? (local file or http/https url)")
  136. url := w.readURL()
  137. // Convert the various allowed URLs to a reader stream
  138. var reader io.Reader
  139. switch url.Scheme {
  140. case "http", "https":
  141. // Remote web URL, retrieve it via an HTTP client
  142. res, err := http.Get(url.String())
  143. if err != nil {
  144. log.Error("Failed to retrieve remote genesis", "err", err)
  145. return
  146. }
  147. defer res.Body.Close()
  148. reader = res.Body
  149. case "":
  150. // Schemaless URL, interpret as a local file
  151. file, err := os.Open(url.String())
  152. if err != nil {
  153. log.Error("Failed to open local genesis", "err", err)
  154. return
  155. }
  156. defer file.Close()
  157. reader = file
  158. default:
  159. log.Error("Unsupported genesis URL scheme", "scheme", url.Scheme)
  160. return
  161. }
  162. // Parse the genesis file and inject it successful
  163. var genesis core.Genesis
  164. if err := json.NewDecoder(reader).Decode(&genesis); err != nil {
  165. log.Error("Invalid genesis spec", "err", err)
  166. return
  167. }
  168. log.Info("Imported genesis block")
  169. w.conf.Genesis = &genesis
  170. w.conf.flush()
  171. }
  172. // manageGenesis permits the modification of chain configuration parameters in
  173. // a genesis config and the export of the entire genesis spec.
  174. func (w *wizard) manageGenesis() {
  175. // Figure out whether to modify or export the genesis
  176. fmt.Println()
  177. fmt.Println(" 1. Modify existing configurations")
  178. fmt.Println(" 2. Export genesis configurations")
  179. fmt.Println(" 3. Remove genesis configuration")
  180. choice := w.read()
  181. switch choice {
  182. case "1":
  183. // Fork rule updating requested, iterate over each fork
  184. fmt.Println()
  185. fmt.Printf("Which block should Homestead come into effect? (default = %v)\n", w.conf.Genesis.Config.HomesteadBlock)
  186. w.conf.Genesis.Config.HomesteadBlock = w.readDefaultBigInt(w.conf.Genesis.Config.HomesteadBlock)
  187. fmt.Println()
  188. fmt.Printf("Which block should EIP150 (Tangerine Whistle) come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP150Block)
  189. w.conf.Genesis.Config.EIP150Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP150Block)
  190. fmt.Println()
  191. fmt.Printf("Which block should EIP155 (Spurious Dragon) come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP155Block)
  192. w.conf.Genesis.Config.EIP155Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP155Block)
  193. fmt.Println()
  194. fmt.Printf("Which block should EIP158/161 (also Spurious Dragon) come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP158Block)
  195. w.conf.Genesis.Config.EIP158Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP158Block)
  196. fmt.Println()
  197. fmt.Printf("Which block should Byzantium come into effect? (default = %v)\n", w.conf.Genesis.Config.ByzantiumBlock)
  198. w.conf.Genesis.Config.ByzantiumBlock = w.readDefaultBigInt(w.conf.Genesis.Config.ByzantiumBlock)
  199. fmt.Println()
  200. fmt.Printf("Which block should Constantinople come into effect? (default = %v)\n", w.conf.Genesis.Config.ConstantinopleBlock)
  201. w.conf.Genesis.Config.ConstantinopleBlock = w.readDefaultBigInt(w.conf.Genesis.Config.ConstantinopleBlock)
  202. if w.conf.Genesis.Config.PetersburgBlock == nil {
  203. w.conf.Genesis.Config.PetersburgBlock = w.conf.Genesis.Config.ConstantinopleBlock
  204. }
  205. fmt.Println()
  206. fmt.Printf("Which block should Petersburg come into effect? (default = %v)\n", w.conf.Genesis.Config.PetersburgBlock)
  207. w.conf.Genesis.Config.PetersburgBlock = w.readDefaultBigInt(w.conf.Genesis.Config.PetersburgBlock)
  208. fmt.Println()
  209. fmt.Printf("Which block should Istanbul come into effect? (default = %v)\n", w.conf.Genesis.Config.IstanbulBlock)
  210. w.conf.Genesis.Config.IstanbulBlock = w.readDefaultBigInt(w.conf.Genesis.Config.IstanbulBlock)
  211. fmt.Println()
  212. fmt.Printf("Which block should Berlin come into effect? (default = %v)\n", w.conf.Genesis.Config.BerlinBlock)
  213. w.conf.Genesis.Config.BerlinBlock = w.readDefaultBigInt(w.conf.Genesis.Config.BerlinBlock)
  214. fmt.Println()
  215. fmt.Printf("Which block should London come into effect? (default = %v)\n", w.conf.Genesis.Config.LondonBlock)
  216. w.conf.Genesis.Config.LondonBlock = w.readDefaultBigInt(w.conf.Genesis.Config.LondonBlock)
  217. out, _ := json.MarshalIndent(w.conf.Genesis.Config, "", " ")
  218. fmt.Printf("Chain configuration updated:\n\n%s\n", out)
  219. w.conf.flush()
  220. case "2":
  221. // Save whatever genesis configuration we currently have
  222. fmt.Println()
  223. fmt.Printf("Which folder to save the genesis spec into? (default = current)\n")
  224. fmt.Printf(" Will create %s.json\n", w.network)
  225. folder := w.readDefaultString(".")
  226. if err := os.MkdirAll(folder, 0755); err != nil {
  227. log.Error("Failed to create spec folder", "folder", folder, "err", err)
  228. return
  229. }
  230. out, _ := json.MarshalIndent(w.conf.Genesis, "", " ")
  231. // Export the native genesis spec used by puppeth and Geth
  232. gethJson := filepath.Join(folder, fmt.Sprintf("%s.json", w.network))
  233. if err := os.WriteFile(gethJson, out, 0644); err != nil {
  234. log.Error("Failed to save genesis file", "err", err)
  235. return
  236. }
  237. log.Info("Saved native genesis chain spec", "path", gethJson)
  238. case "3":
  239. // Make sure we don't have any services running
  240. if len(w.conf.servers()) > 0 {
  241. log.Error("Genesis reset requires all services and servers torn down")
  242. return
  243. }
  244. log.Info("Genesis block destroyed")
  245. w.conf.Genesis = nil
  246. w.conf.flush()
  247. default:
  248. log.Error("That's not something I can do")
  249. return
  250. }
  251. }