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