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