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