github.com/core-coin/go-core/v2@v2.1.9/cmd/gocore/main.go (about)

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