wizard_genesis.go 9.8 KB

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