wizard_genesis.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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(0),
  44. EIP150Block: big.NewInt(0),
  45. EIP155Block: big.NewInt(0),
  46. EIP158Block: big.NewInt(0),
  47. ByzantiumBlock: big.NewInt(0),
  48. ConstantinopleBlock: big.NewInt(0),
  49. PetersburgBlock: 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: %v", 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. out, _ := json.MarshalIndent(w.conf.Genesis.Config, "", " ")
  209. fmt.Printf("Chain configuration updated:\n\n%s\n", out)
  210. w.conf.flush()
  211. case "2":
  212. // Save whatever genesis configuration we currently have
  213. fmt.Println()
  214. fmt.Printf("Which folder to save the genesis specs into? (default = current)\n")
  215. fmt.Printf(" Will create %s.json, %s-aleth.json, %s-harmony.json, %s-parity.json\n", w.network, w.network, w.network, w.network)
  216. folder := w.readDefaultString(".")
  217. if err := os.MkdirAll(folder, 0755); err != nil {
  218. log.Error("Failed to create spec folder", "folder", folder, "err", err)
  219. return
  220. }
  221. out, _ := json.MarshalIndent(w.conf.Genesis, "", " ")
  222. // Export the native genesis spec used by puppeth and Geth
  223. gethJson := filepath.Join(folder, fmt.Sprintf("%s.json", w.network))
  224. if err := ioutil.WriteFile((gethJson), out, 0644); err != nil {
  225. log.Error("Failed to save genesis file", "err", err)
  226. return
  227. }
  228. log.Info("Saved native genesis chain spec", "path", gethJson)
  229. // Export the genesis spec used by Aleth (formerly C++ Ethereum)
  230. if spec, err := newAlethGenesisSpec(w.network, w.conf.Genesis); err != nil {
  231. log.Error("Failed to create Aleth chain spec", "err", err)
  232. } else {
  233. saveGenesis(folder, w.network, "aleth", spec)
  234. }
  235. // Export the genesis spec used by Parity
  236. if spec, err := newParityChainSpec(w.network, w.conf.Genesis, []string{}); err != nil {
  237. log.Error("Failed to create Parity chain spec", "err", err)
  238. } else {
  239. saveGenesis(folder, w.network, "parity", spec)
  240. }
  241. // Export the genesis spec used by Harmony (formerly EthereumJ
  242. saveGenesis(folder, w.network, "harmony", w.conf.Genesis)
  243. case "3":
  244. // Make sure we don't have any services running
  245. if len(w.conf.servers()) > 0 {
  246. log.Error("Genesis reset requires all services and servers torn down")
  247. return
  248. }
  249. log.Info("Genesis block destroyed")
  250. w.conf.Genesis = nil
  251. w.conf.flush()
  252. default:
  253. log.Error("That's not something I can do")
  254. return
  255. }
  256. }
  257. // saveGenesis JSON encodes an arbitrary genesis spec into a pre-defined file.
  258. func saveGenesis(folder, network, client string, spec interface{}) {
  259. path := filepath.Join(folder, fmt.Sprintf("%s-%s.json", network, client))
  260. out, _ := json.Marshal(spec)
  261. if err := ioutil.WriteFile(path, out, 0644); err != nil {
  262. log.Error("Failed to save genesis file", "client", client, "err", err)
  263. return
  264. }
  265. log.Info("Saved genesis chain spec", "client", client, "path", path)
  266. }