github.com/tuotoo/go-ethereum@v1.7.4-0.20171121184211-049797d40a24/cmd/puppeth/wizard_node.go (about)

     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  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/ethereum/go-ethereum/accounts/keystore"
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/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 ethstats 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  	infos.genesis, _ = json.MarshalIndent(w.conf.genesis, "", "  ")
    57  	infos.network = w.conf.genesis.Config.ChainId.Int64()
    58  
    59  	// Figure out where the user wants to store the persistent data
    60  	fmt.Println()
    61  	if infos.datadir == "" {
    62  		fmt.Printf("Where should data be stored on the remote machine?\n")
    63  		infos.datadir = w.readString()
    64  	} else {
    65  		fmt.Printf("Where should data be stored on the remote machine? (default = %s)\n", infos.datadir)
    66  		infos.datadir = w.readDefaultString(infos.datadir)
    67  	}
    68  	// Figure out which port to listen on
    69  	fmt.Println()
    70  	fmt.Printf("Which TCP/UDP port to listen on? (default = %d)\n", infos.portFull)
    71  	infos.portFull = w.readDefaultInt(infos.portFull)
    72  
    73  	// Figure out how many peers to allow (different based on node type)
    74  	fmt.Println()
    75  	fmt.Printf("How many peers to allow connecting? (default = %d)\n", infos.peersTotal)
    76  	infos.peersTotal = w.readDefaultInt(infos.peersTotal)
    77  
    78  	// Figure out how many light peers to allow (different based on node type)
    79  	fmt.Println()
    80  	fmt.Printf("How many light peers to allow connecting? (default = %d)\n", infos.peersLight)
    81  	infos.peersLight = w.readDefaultInt(infos.peersLight)
    82  
    83  	// Set a proper name to report on the stats page
    84  	fmt.Println()
    85  	if infos.ethstats == "" {
    86  		fmt.Printf("What should the node be called on the stats page?\n")
    87  		infos.ethstats = w.readString() + ":" + w.conf.ethstats
    88  	} else {
    89  		fmt.Printf("What should the node be called on the stats page? (default = %s)\n", infos.ethstats)
    90  		infos.ethstats = w.readDefaultString(infos.ethstats) + ":" + w.conf.ethstats
    91  	}
    92  	// If the node is a miner/signer, load up needed credentials
    93  	if !boot {
    94  		if w.conf.genesis.Config.Ethash != nil {
    95  			// Ethash based miners only need an etherbase to mine against
    96  			fmt.Println()
    97  			if infos.etherbase == "" {
    98  				fmt.Printf("What address should the miner user?\n")
    99  				for {
   100  					if address := w.readAddress(); address != nil {
   101  						infos.etherbase = address.Hex()
   102  						break
   103  					}
   104  				}
   105  			} else {
   106  				fmt.Printf("What address should the miner user? (default = %s)\n", infos.etherbase)
   107  				infos.etherbase = w.readDefaultAddress(common.HexToAddress(infos.etherbase)).Hex()
   108  			}
   109  		} else if w.conf.genesis.Config.Clique != nil {
   110  			// If a previous signer was already set, offer to reuse it
   111  			if infos.keyJSON != "" {
   112  				if key, err := keystore.DecryptKey([]byte(infos.keyJSON), infos.keyPass); err != nil {
   113  					infos.keyJSON, infos.keyPass = "", ""
   114  				} else {
   115  					fmt.Println()
   116  					fmt.Printf("Reuse previous (%s) signing account (y/n)? (default = yes)\n", key.Address.Hex())
   117  					if w.readDefaultString("y") != "y" {
   118  						infos.keyJSON, infos.keyPass = "", ""
   119  					}
   120  				}
   121  			}
   122  			// Clique based signers need a keyfile and unlock password, ask if unavailable
   123  			if infos.keyJSON == "" {
   124  				fmt.Println()
   125  				fmt.Println("Please paste the signer's key JSON:")
   126  				infos.keyJSON = w.readJSON()
   127  
   128  				fmt.Println()
   129  				fmt.Println("What's the unlock password for the account? (won't be echoed)")
   130  				infos.keyPass = w.readPassword()
   131  
   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  
   143  		fmt.Println()
   144  		fmt.Printf("What gas price should the signer require (GWei)? (default = %0.3f)\n", infos.gasPrice)
   145  		infos.gasPrice = w.readDefaultFloat(infos.gasPrice)
   146  	}
   147  	// Try to deploy the full node on the host
   148  	if out, err := deployNode(client, w.network, w.conf.bootFull, w.conf.bootLight, infos); err != nil {
   149  		log.Error("Failed to deploy Ethereum node container", "err", err)
   150  		if len(out) > 0 {
   151  			fmt.Printf("%s\n", out)
   152  		}
   153  		return
   154  	}
   155  	// All ok, run a network scan to pick any changes up
   156  	log.Info("Waiting for node to finish booting")
   157  	time.Sleep(3 * time.Second)
   158  
   159  	w.networkStats(false)
   160  }