wizard_node.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "encoding/json"
  19. "fmt"
  20. "time"
  21. "github.com/ethereum/go-ethereum/accounts/keystore"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/log"
  24. )
  25. // deployNode creates a new node configuration based on some user input.
  26. func (w *wizard) deployNode(boot bool) {
  27. // Do some sanity check before the user wastes time on input
  28. if w.conf.Genesis == nil {
  29. log.Error("No genesis block configured")
  30. return
  31. }
  32. if w.conf.ethstats == "" {
  33. log.Error("No ethstats server configured")
  34. return
  35. }
  36. // Select the server to interact with
  37. server := w.selectServer()
  38. if server == "" {
  39. return
  40. }
  41. client := w.servers[server]
  42. // Retrieve any active node configurations from the server
  43. infos, err := checkNode(client, w.network, boot)
  44. if err != nil {
  45. if boot {
  46. infos = &nodeInfos{port: 30303, peersTotal: 512, peersLight: 256}
  47. } else {
  48. infos = &nodeInfos{port: 30303, peersTotal: 50, peersLight: 0, gasTarget: 4.7, gasPrice: 18}
  49. }
  50. }
  51. existed := err == nil
  52. infos.genesis, _ = json.MarshalIndent(w.conf.Genesis, "", " ")
  53. infos.network = w.conf.Genesis.Config.ChainId.Int64()
  54. // Figure out where the user wants to store the persistent data
  55. fmt.Println()
  56. if infos.datadir == "" {
  57. fmt.Printf("Where should data be stored on the remote machine?\n")
  58. infos.datadir = w.readString()
  59. } else {
  60. fmt.Printf("Where should data be stored on the remote machine? (default = %s)\n", infos.datadir)
  61. infos.datadir = w.readDefaultString(infos.datadir)
  62. }
  63. if w.conf.Genesis.Config.Ethash != nil && !boot {
  64. fmt.Println()
  65. if infos.ethashdir == "" {
  66. fmt.Printf("Where should the ethash mining DAGs be stored on the remote machine?\n")
  67. infos.ethashdir = w.readString()
  68. } else {
  69. fmt.Printf("Where should the ethash mining DAGs be stored on the remote machine? (default = %s)\n", infos.ethashdir)
  70. infos.ethashdir = w.readDefaultString(infos.ethashdir)
  71. }
  72. }
  73. // Figure out which port to listen on
  74. fmt.Println()
  75. fmt.Printf("Which TCP/UDP port to listen on? (default = %d)\n", infos.port)
  76. infos.port = w.readDefaultInt(infos.port)
  77. // Figure out how many peers to allow (different based on node type)
  78. fmt.Println()
  79. fmt.Printf("How many peers to allow connecting? (default = %d)\n", infos.peersTotal)
  80. infos.peersTotal = w.readDefaultInt(infos.peersTotal)
  81. // Figure out how many light peers to allow (different based on node type)
  82. fmt.Println()
  83. fmt.Printf("How many light peers to allow connecting? (default = %d)\n", infos.peersLight)
  84. infos.peersLight = w.readDefaultInt(infos.peersLight)
  85. // Set a proper name to report on the stats page
  86. fmt.Println()
  87. if infos.ethstats == "" {
  88. fmt.Printf("What should the node be called on the stats page?\n")
  89. infos.ethstats = w.readString() + ":" + w.conf.ethstats
  90. } else {
  91. fmt.Printf("What should the node be called on the stats page? (default = %s)\n", infos.ethstats)
  92. infos.ethstats = w.readDefaultString(infos.ethstats) + ":" + w.conf.ethstats
  93. }
  94. // If the node is a miner/signer, load up needed credentials
  95. if !boot {
  96. if w.conf.Genesis.Config.Ethash != nil {
  97. // Ethash based miners only need an etherbase to mine against
  98. fmt.Println()
  99. if infos.etherbase == "" {
  100. fmt.Printf("What address should the miner user?\n")
  101. for {
  102. if address := w.readAddress(); address != nil {
  103. infos.etherbase = address.Hex()
  104. break
  105. }
  106. }
  107. } else {
  108. fmt.Printf("What address should the miner user? (default = %s)\n", infos.etherbase)
  109. infos.etherbase = w.readDefaultAddress(common.HexToAddress(infos.etherbase)).Hex()
  110. }
  111. } else if w.conf.Genesis.Config.Clique != nil {
  112. // If a previous signer was already set, offer to reuse it
  113. if infos.keyJSON != "" {
  114. if key, err := keystore.DecryptKey([]byte(infos.keyJSON), infos.keyPass); err != nil {
  115. infos.keyJSON, infos.keyPass = "", ""
  116. } else {
  117. fmt.Println()
  118. fmt.Printf("Reuse previous (%s) signing account (y/n)? (default = yes)\n", key.Address.Hex())
  119. if w.readDefaultString("y") != "y" {
  120. infos.keyJSON, infos.keyPass = "", ""
  121. }
  122. }
  123. }
  124. // Clique based signers need a keyfile and unlock password, ask if unavailable
  125. if infos.keyJSON == "" {
  126. fmt.Println()
  127. fmt.Println("Please paste the signer's key JSON:")
  128. infos.keyJSON = w.readJSON()
  129. fmt.Println()
  130. fmt.Println("What's the unlock password for the account? (won't be echoed)")
  131. infos.keyPass = w.readPassword()
  132. if _, err := keystore.DecryptKey([]byte(infos.keyJSON), infos.keyPass); err != nil {
  133. log.Error("Failed to decrypt key with given passphrase")
  134. return
  135. }
  136. }
  137. }
  138. // Establish the gas dynamics to be enforced by the signer
  139. fmt.Println()
  140. fmt.Printf("What gas limit should empty blocks target (MGas)? (default = %0.3f)\n", infos.gasTarget)
  141. infos.gasTarget = w.readDefaultFloat(infos.gasTarget)
  142. fmt.Println()
  143. fmt.Printf("What gas price should the signer require (GWei)? (default = %0.3f)\n", infos.gasPrice)
  144. infos.gasPrice = w.readDefaultFloat(infos.gasPrice)
  145. }
  146. // Try to deploy the full node on the host
  147. nocache := false
  148. if existed {
  149. fmt.Println()
  150. fmt.Printf("Should the node be built from scratch (y/n)? (default = no)\n")
  151. nocache = w.readDefaultString("n") != "n"
  152. }
  153. if out, err := deployNode(client, w.network, w.conf.bootnodes, infos, nocache); err != nil {
  154. log.Error("Failed to deploy Ethereum node container", "err", err)
  155. if len(out) > 0 {
  156. fmt.Printf("%s\n", out)
  157. }
  158. return
  159. }
  160. // All ok, run a network scan to pick any changes up
  161. log.Info("Waiting for node to finish booting")
  162. time.Sleep(3 * time.Second)
  163. w.networkStats()
  164. }