wizard_genesis.go 11 KB

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