github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/cmd/geth/main.go (about)

     1  // Copyright 2014 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  // geth is the official command-line client for Ethereum.
    18  package main
    19  
    20  import (
    21  	"fmt"
    22  	"math"
    23  	"os"
    24  	godebug "runtime/debug"
    25  	"sort"
    26  	"strconv"
    27  	"strings"
    28  	"time"
    29  
    30  	gopsutil "github.com/shirou/gopsutil/mem"
    31  	"github.com/zhiqiangxu/go-ethereum/accounts"
    32  	"github.com/zhiqiangxu/go-ethereum/accounts/keystore"
    33  	"github.com/zhiqiangxu/go-ethereum/cmd/utils"
    34  	"github.com/zhiqiangxu/go-ethereum/common"
    35  	"github.com/zhiqiangxu/go-ethereum/console/prompt"
    36  	"github.com/zhiqiangxu/go-ethereum/eth"
    37  	"github.com/zhiqiangxu/go-ethereum/eth/downloader"
    38  	"github.com/zhiqiangxu/go-ethereum/ethclient"
    39  	"github.com/zhiqiangxu/go-ethereum/internal/debug"
    40  	"github.com/zhiqiangxu/go-ethereum/les"
    41  	"github.com/zhiqiangxu/go-ethereum/log"
    42  	"github.com/zhiqiangxu/go-ethereum/metrics"
    43  	"github.com/zhiqiangxu/go-ethereum/node"
    44  	cli "gopkg.in/urfave/cli.v1"
    45  )
    46  
    47  const (
    48  	clientIdentifier = "geth" // Client identifier to advertise over the network
    49  )
    50  
    51  var (
    52  	// Git SHA1 commit hash of the release (set via linker flags)
    53  	gitCommit = ""
    54  	gitDate   = ""
    55  	// The app that holds all commands and flags.
    56  	app = utils.NewApp(gitCommit, gitDate, "the go-ethereum command line interface")
    57  	// flags that configure the node
    58  	nodeFlags = []cli.Flag{
    59  		utils.IdentityFlag,
    60  		utils.UnlockedAccountFlag,
    61  		utils.PasswordFileFlag,
    62  		utils.BootnodesFlag,
    63  		utils.LegacyBootnodesV4Flag,
    64  		utils.LegacyBootnodesV5Flag,
    65  		utils.DataDirFlag,
    66  		utils.AncientFlag,
    67  		utils.KeyStoreDirFlag,
    68  		utils.ExternalSignerFlag,
    69  		utils.NoUSBFlag,
    70  		utils.SmartCardDaemonPathFlag,
    71  		utils.EthashCacheDirFlag,
    72  		utils.EthashCachesInMemoryFlag,
    73  		utils.EthashCachesOnDiskFlag,
    74  		utils.EthashCachesLockMmapFlag,
    75  		utils.EthashDatasetDirFlag,
    76  		utils.EthashDatasetsInMemoryFlag,
    77  		utils.EthashDatasetsOnDiskFlag,
    78  		utils.EthashDatasetsLockMmapFlag,
    79  		utils.TxPoolLocalsFlag,
    80  		utils.TxPoolNoLocalsFlag,
    81  		utils.TxPoolJournalFlag,
    82  		utils.TxPoolRejournalFlag,
    83  		utils.TxPoolPriceLimitFlag,
    84  		utils.TxPoolPriceBumpFlag,
    85  		utils.TxPoolAccountSlotsFlag,
    86  		utils.TxPoolGlobalSlotsFlag,
    87  		utils.TxPoolAccountQueueFlag,
    88  		utils.TxPoolGlobalQueueFlag,
    89  		utils.TxPoolLifetimeFlag,
    90  		utils.SyncModeFlag,
    91  		utils.ExitWhenSyncedFlag,
    92  		utils.GCModeFlag,
    93  		utils.SnapshotFlag,
    94  		utils.TxLookupLimitFlag,
    95  		utils.LightServeFlag,
    96  		utils.LegacyLightServFlag,
    97  		utils.LightIngressFlag,
    98  		utils.LightEgressFlag,
    99  		utils.LightMaxPeersFlag,
   100  		utils.LegacyLightPeersFlag,
   101  		utils.LightKDFFlag,
   102  		utils.UltraLightServersFlag,
   103  		utils.UltraLightFractionFlag,
   104  		utils.UltraLightOnlyAnnounceFlag,
   105  		utils.WhitelistFlag,
   106  		utils.CacheFlag,
   107  		utils.CacheDatabaseFlag,
   108  		utils.CacheTrieFlag,
   109  		utils.CacheGCFlag,
   110  		utils.CacheSnapshotFlag,
   111  		utils.CacheNoPrefetchFlag,
   112  		utils.ListenPortFlag,
   113  		utils.MaxPeersFlag,
   114  		utils.MaxPendingPeersFlag,
   115  		utils.MiningEnabledFlag,
   116  		utils.MinerThreadsFlag,
   117  		utils.LegacyMinerThreadsFlag,
   118  		utils.MinerNotifyFlag,
   119  		utils.MinerGasTargetFlag,
   120  		utils.LegacyMinerGasTargetFlag,
   121  		utils.MinerGasLimitFlag,
   122  		utils.MinerGasPriceFlag,
   123  		utils.LegacyMinerGasPriceFlag,
   124  		utils.MinerEtherbaseFlag,
   125  		utils.LegacyMinerEtherbaseFlag,
   126  		utils.MinerExtraDataFlag,
   127  		utils.LegacyMinerExtraDataFlag,
   128  		utils.MinerRecommitIntervalFlag,
   129  		utils.MinerNoVerfiyFlag,
   130  		utils.NATFlag,
   131  		utils.NoDiscoverFlag,
   132  		utils.DiscoveryV5Flag,
   133  		utils.NetrestrictFlag,
   134  		utils.NodeKeyFileFlag,
   135  		utils.NodeKeyHexFlag,
   136  		utils.DNSDiscoveryFlag,
   137  		utils.DeveloperFlag,
   138  		utils.DeveloperPeriodFlag,
   139  		utils.LegacyTestnetFlag,
   140  		utils.RopstenFlag,
   141  		utils.RinkebyFlag,
   142  		utils.GoerliFlag,
   143  		utils.YoloV1Flag,
   144  		utils.VMEnableDebugFlag,
   145  		utils.NetworkIdFlag,
   146  		utils.EthStatsURLFlag,
   147  		utils.FakePoWFlag,
   148  		utils.NoCompactionFlag,
   149  		utils.GpoBlocksFlag,
   150  		utils.LegacyGpoBlocksFlag,
   151  		utils.GpoPercentileFlag,
   152  		utils.LegacyGpoPercentileFlag,
   153  		utils.EWASMInterpreterFlag,
   154  		utils.EVMInterpreterFlag,
   155  		configFileFlag,
   156  	}
   157  
   158  	rpcFlags = []cli.Flag{
   159  		utils.HTTPEnabledFlag,
   160  		utils.HTTPListenAddrFlag,
   161  		utils.HTTPPortFlag,
   162  		utils.HTTPCORSDomainFlag,
   163  		utils.HTTPVirtualHostsFlag,
   164  		utils.LegacyRPCEnabledFlag,
   165  		utils.LegacyRPCListenAddrFlag,
   166  		utils.LegacyRPCPortFlag,
   167  		utils.LegacyRPCCORSDomainFlag,
   168  		utils.LegacyRPCVirtualHostsFlag,
   169  		utils.GraphQLEnabledFlag,
   170  		utils.GraphQLListenAddrFlag,
   171  		utils.GraphQLPortFlag,
   172  		utils.GraphQLCORSDomainFlag,
   173  		utils.GraphQLVirtualHostsFlag,
   174  		utils.HTTPApiFlag,
   175  		utils.LegacyRPCApiFlag,
   176  		utils.WSEnabledFlag,
   177  		utils.WSListenAddrFlag,
   178  		utils.LegacyWSListenAddrFlag,
   179  		utils.WSPortFlag,
   180  		utils.LegacyWSPortFlag,
   181  		utils.WSApiFlag,
   182  		utils.LegacyWSApiFlag,
   183  		utils.WSAllowedOriginsFlag,
   184  		utils.LegacyWSAllowedOriginsFlag,
   185  		utils.IPCDisabledFlag,
   186  		utils.IPCPathFlag,
   187  		utils.InsecureUnlockAllowedFlag,
   188  		utils.RPCGlobalGasCap,
   189  	}
   190  
   191  	whisperFlags = []cli.Flag{
   192  		utils.WhisperEnabledFlag,
   193  		utils.WhisperMaxMessageSizeFlag,
   194  		utils.WhisperMinPOWFlag,
   195  		utils.WhisperRestrictConnectionBetweenLightClientsFlag,
   196  	}
   197  
   198  	metricsFlags = []cli.Flag{
   199  		utils.MetricsEnabledFlag,
   200  		utils.MetricsEnabledExpensiveFlag,
   201  		utils.MetricsEnableInfluxDBFlag,
   202  		utils.MetricsInfluxDBEndpointFlag,
   203  		utils.MetricsInfluxDBDatabaseFlag,
   204  		utils.MetricsInfluxDBUsernameFlag,
   205  		utils.MetricsInfluxDBPasswordFlag,
   206  		utils.MetricsInfluxDBTagsFlag,
   207  	}
   208  )
   209  
   210  func init() {
   211  	// Initialize the CLI app and start Geth
   212  	app.Action = geth
   213  	app.HideVersion = true // we have a command to print the version
   214  	app.Copyright = "Copyright 2013-2020 The go-ethereum Authors"
   215  	app.Commands = []cli.Command{
   216  		// See chaincmd.go:
   217  		initCommand,
   218  		importCommand,
   219  		exportCommand,
   220  		importPreimagesCommand,
   221  		exportPreimagesCommand,
   222  		copydbCommand,
   223  		removedbCommand,
   224  		dumpCommand,
   225  		dumpGenesisCommand,
   226  		inspectCommand,
   227  		// See accountcmd.go:
   228  		accountCommand,
   229  		walletCommand,
   230  		// See consolecmd.go:
   231  		consoleCommand,
   232  		attachCommand,
   233  		javascriptCommand,
   234  		// See misccmd.go:
   235  		makecacheCommand,
   236  		makedagCommand,
   237  		versionCommand,
   238  		licenseCommand,
   239  		// See config.go
   240  		dumpConfigCommand,
   241  		// See retesteth.go
   242  		retestethCommand,
   243  		// See cmd/utils/flags_legacy.go
   244  		utils.ShowDeprecated,
   245  	}
   246  	sort.Sort(cli.CommandsByName(app.Commands))
   247  
   248  	app.Flags = append(app.Flags, nodeFlags...)
   249  	app.Flags = append(app.Flags, rpcFlags...)
   250  	app.Flags = append(app.Flags, consoleFlags...)
   251  	app.Flags = append(app.Flags, debug.Flags...)
   252  	app.Flags = append(app.Flags, debug.DeprecatedFlags...)
   253  	app.Flags = append(app.Flags, whisperFlags...)
   254  	app.Flags = append(app.Flags, metricsFlags...)
   255  
   256  	app.Before = func(ctx *cli.Context) error {
   257  		return debug.Setup(ctx)
   258  	}
   259  	app.After = func(ctx *cli.Context) error {
   260  		debug.Exit()
   261  		prompt.Stdin.Close() // Resets terminal mode.
   262  		return nil
   263  	}
   264  }
   265  
   266  func main() {
   267  	if err := app.Run(os.Args); err != nil {
   268  		fmt.Fprintln(os.Stderr, err)
   269  		os.Exit(1)
   270  	}
   271  }
   272  
   273  // prepare manipulates memory cache allowance and setups metric system.
   274  // This function should be called before launching devp2p stack.
   275  func prepare(ctx *cli.Context) {
   276  	// If we're running a known preset, log it for convenience.
   277  	switch {
   278  	case ctx.GlobalIsSet(utils.LegacyTestnetFlag.Name):
   279  		log.Info("Starting Geth on Ropsten testnet...")
   280  		log.Warn("The --testnet flag is ambiguous! Please specify one of --goerli, --rinkeby, or --ropsten.")
   281  		log.Warn("The generic --testnet flag is deprecated and will be removed in the future!")
   282  
   283  	case ctx.GlobalIsSet(utils.RopstenFlag.Name):
   284  		log.Info("Starting Geth on Ropsten testnet...")
   285  
   286  	case ctx.GlobalIsSet(utils.RinkebyFlag.Name):
   287  		log.Info("Starting Geth on Rinkeby testnet...")
   288  
   289  	case ctx.GlobalIsSet(utils.GoerliFlag.Name):
   290  		log.Info("Starting Geth on Görli testnet...")
   291  
   292  	case ctx.GlobalIsSet(utils.DeveloperFlag.Name):
   293  		log.Info("Starting Geth in ephemeral dev mode...")
   294  
   295  	case !ctx.GlobalIsSet(utils.NetworkIdFlag.Name):
   296  		log.Info("Starting Geth on Ethereum mainnet...")
   297  	}
   298  	// If we're a full node on mainnet without --cache specified, bump default cache allowance
   299  	if ctx.GlobalString(utils.SyncModeFlag.Name) != "light" && !ctx.GlobalIsSet(utils.CacheFlag.Name) && !ctx.GlobalIsSet(utils.NetworkIdFlag.Name) {
   300  		// Make sure we're not on any supported preconfigured testnet either
   301  		if !ctx.GlobalIsSet(utils.LegacyTestnetFlag.Name) && !ctx.GlobalIsSet(utils.RopstenFlag.Name) && !ctx.GlobalIsSet(utils.RinkebyFlag.Name) && !ctx.GlobalIsSet(utils.GoerliFlag.Name) && !ctx.GlobalIsSet(utils.DeveloperFlag.Name) {
   302  			// Nope, we're really on mainnet. Bump that cache up!
   303  			log.Info("Bumping default cache on mainnet", "provided", ctx.GlobalInt(utils.CacheFlag.Name), "updated", 4096)
   304  			ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(4096))
   305  		}
   306  	}
   307  	// If we're running a light client on any network, drop the cache to some meaningfully low amount
   308  	if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" && !ctx.GlobalIsSet(utils.CacheFlag.Name) {
   309  		log.Info("Dropping default light client cache", "provided", ctx.GlobalInt(utils.CacheFlag.Name), "updated", 128)
   310  		ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(128))
   311  	}
   312  	// Cap the cache allowance and tune the garbage collector
   313  	mem, err := gopsutil.VirtualMemory()
   314  	if err == nil {
   315  		if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
   316  			log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
   317  			mem.Total = 2 * 1024 * 1024 * 1024
   318  		}
   319  		allowance := int(mem.Total / 1024 / 1024 / 3)
   320  		if cache := ctx.GlobalInt(utils.CacheFlag.Name); cache > allowance {
   321  			log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
   322  			ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(allowance))
   323  		}
   324  	}
   325  	// Ensure Go's GC ignores the database cache for trigger percentage
   326  	cache := ctx.GlobalInt(utils.CacheFlag.Name)
   327  	gogc := math.Max(20, math.Min(100, 100/(float64(cache)/1024)))
   328  
   329  	log.Debug("Sanitizing Go's GC trigger", "percent", int(gogc))
   330  	godebug.SetGCPercent(int(gogc))
   331  
   332  	// Start metrics export if enabled
   333  	utils.SetupMetrics(ctx)
   334  
   335  	// Start system runtime metrics collection
   336  	go metrics.CollectProcessMetrics(3 * time.Second)
   337  }
   338  
   339  // geth is the main entry point into the system if no special subcommand is ran.
   340  // It creates a default node based on the command line arguments and runs it in
   341  // blocking mode, waiting for it to be shut down.
   342  func geth(ctx *cli.Context) error {
   343  	if args := ctx.Args(); len(args) > 0 {
   344  		return fmt.Errorf("invalid command: %q", args[0])
   345  	}
   346  	prepare(ctx)
   347  	node := makeFullNode(ctx)
   348  	defer node.Close()
   349  	startNode(ctx, node)
   350  	node.Wait()
   351  	return nil
   352  }
   353  
   354  // startNode boots up the system node and all registered protocols, after which
   355  // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
   356  // miner.
   357  func startNode(ctx *cli.Context, stack *node.Node) {
   358  	debug.Memsize.Add("node", stack)
   359  
   360  	// Start up the node itself
   361  	utils.StartNode(stack)
   362  
   363  	// Unlock any account specifically requested
   364  	unlockAccounts(ctx, stack)
   365  
   366  	// Register wallet event handlers to open and auto-derive wallets
   367  	events := make(chan accounts.WalletEvent, 16)
   368  	stack.AccountManager().Subscribe(events)
   369  
   370  	// Create a client to interact with local geth node.
   371  	rpcClient, err := stack.Attach()
   372  	if err != nil {
   373  		utils.Fatalf("Failed to attach to self: %v", err)
   374  	}
   375  	ethClient := ethclient.NewClient(rpcClient)
   376  
   377  	// Set contract backend for ethereum service if local node
   378  	// is serving LES requests.
   379  	if ctx.GlobalInt(utils.LegacyLightServFlag.Name) > 0 || ctx.GlobalInt(utils.LightServeFlag.Name) > 0 {
   380  		var ethService *eth.Ethereum
   381  		if err := stack.Service(&ethService); err != nil {
   382  			utils.Fatalf("Failed to retrieve ethereum service: %v", err)
   383  		}
   384  		ethService.SetContractBackend(ethClient)
   385  	}
   386  	// Set contract backend for les service if local node is
   387  	// running as a light client.
   388  	if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" {
   389  		var lesService *les.LightEthereum
   390  		if err := stack.Service(&lesService); err != nil {
   391  			utils.Fatalf("Failed to retrieve light ethereum service: %v", err)
   392  		}
   393  		lesService.SetContractBackend(ethClient)
   394  	}
   395  
   396  	go func() {
   397  		// Open any wallets already attached
   398  		for _, wallet := range stack.AccountManager().Wallets() {
   399  			if err := wallet.Open(""); err != nil {
   400  				log.Warn("Failed to open wallet", "url", wallet.URL(), "err", err)
   401  			}
   402  		}
   403  		// Listen for wallet event till termination
   404  		for event := range events {
   405  			switch event.Kind {
   406  			case accounts.WalletArrived:
   407  				if err := event.Wallet.Open(""); err != nil {
   408  					log.Warn("New wallet appeared, failed to open", "url", event.Wallet.URL(), "err", err)
   409  				}
   410  			case accounts.WalletOpened:
   411  				status, _ := event.Wallet.Status()
   412  				log.Info("New wallet appeared", "url", event.Wallet.URL(), "status", status)
   413  
   414  				var derivationPaths []accounts.DerivationPath
   415  				if event.Wallet.URL().Scheme == "ledger" {
   416  					derivationPaths = append(derivationPaths, accounts.LegacyLedgerBaseDerivationPath)
   417  				}
   418  				derivationPaths = append(derivationPaths, accounts.DefaultBaseDerivationPath)
   419  
   420  				event.Wallet.SelfDerive(derivationPaths, ethClient)
   421  
   422  			case accounts.WalletDropped:
   423  				log.Info("Old wallet dropped", "url", event.Wallet.URL())
   424  				event.Wallet.Close()
   425  			}
   426  		}
   427  	}()
   428  
   429  	// Spawn a standalone goroutine for status synchronization monitoring,
   430  	// close the node when synchronization is complete if user required.
   431  	if ctx.GlobalBool(utils.ExitWhenSyncedFlag.Name) {
   432  		go func() {
   433  			sub := stack.EventMux().Subscribe(downloader.DoneEvent{})
   434  			defer sub.Unsubscribe()
   435  			for {
   436  				event := <-sub.Chan()
   437  				if event == nil {
   438  					continue
   439  				}
   440  				done, ok := event.Data.(downloader.DoneEvent)
   441  				if !ok {
   442  					continue
   443  				}
   444  				if timestamp := time.Unix(int64(done.Latest.Time), 0); time.Since(timestamp) < 10*time.Minute {
   445  					log.Info("Synchronisation completed", "latestnum", done.Latest.Number, "latesthash", done.Latest.Hash(),
   446  						"age", common.PrettyAge(timestamp))
   447  					stack.Stop()
   448  				}
   449  			}
   450  		}()
   451  	}
   452  
   453  	// Start auxiliary services if enabled
   454  	if ctx.GlobalBool(utils.MiningEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) {
   455  		// Mining only makes sense if a full Ethereum node is running
   456  		if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" {
   457  			utils.Fatalf("Light clients do not support mining")
   458  		}
   459  		var ethereum *eth.Ethereum
   460  		if err := stack.Service(&ethereum); err != nil {
   461  			utils.Fatalf("Ethereum service not running: %v", err)
   462  		}
   463  		// Set the gas price to the limits from the CLI and start mining
   464  		gasprice := utils.GlobalBig(ctx, utils.MinerGasPriceFlag.Name)
   465  		if ctx.GlobalIsSet(utils.LegacyMinerGasPriceFlag.Name) && !ctx.GlobalIsSet(utils.MinerGasPriceFlag.Name) {
   466  			gasprice = utils.GlobalBig(ctx, utils.LegacyMinerGasPriceFlag.Name)
   467  		}
   468  		ethereum.TxPool().SetGasPrice(gasprice)
   469  
   470  		threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name)
   471  		if ctx.GlobalIsSet(utils.LegacyMinerThreadsFlag.Name) && !ctx.GlobalIsSet(utils.MinerThreadsFlag.Name) {
   472  			threads = ctx.GlobalInt(utils.LegacyMinerThreadsFlag.Name)
   473  			log.Warn("The flag --minerthreads is deprecated and will be removed in the future, please use --miner.threads")
   474  		}
   475  
   476  		if err := ethereum.StartMining(threads); err != nil {
   477  			utils.Fatalf("Failed to start mining: %v", err)
   478  		}
   479  	}
   480  }
   481  
   482  // unlockAccounts unlocks any account specifically requested.
   483  func unlockAccounts(ctx *cli.Context, stack *node.Node) {
   484  	var unlocks []string
   485  	inputs := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
   486  	for _, input := range inputs {
   487  		if trimmed := strings.TrimSpace(input); trimmed != "" {
   488  			unlocks = append(unlocks, trimmed)
   489  		}
   490  	}
   491  	// Short circuit if there is no account to unlock.
   492  	if len(unlocks) == 0 {
   493  		return
   494  	}
   495  	// If insecure account unlocking is not allowed if node's APIs are exposed to external.
   496  	// Print warning log to user and skip unlocking.
   497  	if !stack.Config().InsecureUnlockAllowed && stack.Config().ExtRPCEnabled() {
   498  		utils.Fatalf("Account unlock with HTTP access is forbidden!")
   499  	}
   500  	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
   501  	passwords := utils.MakePasswordList(ctx)
   502  	for i, account := range unlocks {
   503  		unlockAccount(ks, account, i, passwords)
   504  	}
   505  }