github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/cmd/puppeth/wizard_node.go (about)

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