github.com/pixichain/go-pixicoin@v0.0.0-20220708132717-27ba739265ff/cmd/gmc/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  // pxc is the official command-line client for pixicoin.
    18  package main
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"runtime"
    24  	"sort"
    25  	"strings"
    26  	"time"
    27  
    28  	"github.com/ethereum/go-ethereum/accounts"
    29  	"github.com/ethereum/go-ethereum/accounts/keystore"
    30  	"github.com/ethereum/go-ethereum/cmd/utils"
    31  	"github.com/ethereum/go-ethereum/common"
    32  	"github.com/ethereum/go-ethereum/console"
    33  	"github.com/ethereum/go-ethereum/eth"
    34  	"github.com/ethereum/go-ethereum/ethclient"
    35  	"github.com/ethereum/go-ethereum/internal/debug"
    36  	"github.com/ethereum/go-ethereum/log"
    37  	"github.com/ethereum/go-ethereum/metrics"
    38  	"github.com/ethereum/go-ethereum/node"
    39  	"gopkg.in/urfave/cli.v1"
    40  )
    41  
    42  const (
    43  	clientIdentifier = "pxc" // Client identifier to advertise over the network
    44  )
    45  
    46  var (
    47  	// Git SHA1 commit hash of the release (set via linker flags)
    48  	gitCommit = ""
    49  	// Ethereum address of the pxc release oracle.
    50  	relOracle = common.HexToAddress("0xfa7b9770ca4cb04296cac84f37736d4041251cdf")
    51  	// The app that holds all commands and flags.
    52  	app = utils.NewApp(gitCommit, "the go-ethereum command line interface")
    53  	// flags that configure the node
    54  	nodeFlags = []cli.Flag{
    55  		utils.IdentityFlag,
    56  		utils.UnlockedAccountFlag,
    57  		utils.PasswordFileFlag,
    58  		utils.BootnodesFlag,
    59  		utils.BootnodesV4Flag,
    60  		utils.BootnodesV5Flag,
    61  		utils.DataDirFlag,
    62  		utils.KeyStoreDirFlag,
    63  		utils.NoUSBFlag,
    64  		utils.DashboardEnabledFlag,
    65  		utils.DashboardAddrFlag,
    66  		utils.DashboardPortFlag,
    67  		utils.DashboardRefreshFlag,
    68  		utils.DashboardAssetsFlag,
    69  		utils.EthashCacheDirFlag,
    70  		utils.EthashCachesInMemoryFlag,
    71  		utils.EthashCachesOnDiskFlag,
    72  		utils.EthashDatasetDirFlag,
    73  		utils.EthashDatasetsInMemoryFlag,
    74  		utils.EthashDatasetsOnDiskFlag,
    75  		utils.TxPoolNoLocalsFlag,
    76  		utils.TxPoolJournalFlag,
    77  		utils.TxPoolRejournalFlag,
    78  		utils.TxPoolPriceLimitFlag,
    79  		utils.TxPoolPriceBumpFlag,
    80  		utils.TxPoolAccountSlotsFlag,
    81  		utils.TxPoolGlobalSlotsFlag,
    82  		utils.TxPoolAccountQueueFlag,
    83  		utils.TxPoolGlobalQueueFlag,
    84  		utils.TxPoolLifetimeFlag,
    85  		utils.FastSyncFlag,
    86  		utils.LightModeFlag,
    87  		utils.SyncModeFlag,
    88  		utils.LightServFlag,
    89  		utils.LightPeersFlag,
    90  		utils.LightKDFFlag,
    91  		utils.CacheFlag,
    92  		utils.TrieCacheGenFlag,
    93  		utils.ListenPortFlag,
    94  		utils.MaxPeersFlag,
    95  		utils.MaxPendingPeersFlag,
    96  		utils.EtherbaseFlag,
    97  		utils.GasPriceFlag,
    98  		utils.MinerThreadsFlag,
    99  		utils.MiningEnabledFlag,
   100  		utils.TargetGasLimitFlag,
   101  		utils.NATFlag,
   102  		utils.NoDiscoverFlag,
   103  		utils.DiscoveryV5Flag,
   104  		utils.NetrestrictFlag,
   105  		utils.NodeKeyFileFlag,
   106  		utils.NodeKeyHexFlag,
   107  		utils.DeveloperFlag,
   108  		utils.DeveloperPeriodFlag,
   109  		utils.TestnetFlag,
   110  		utils.RinkebyFlag,
   111  		utils.VMEnableDebugFlag,
   112  		utils.NetworkIdFlag,
   113  		utils.RPCCORSDomainFlag,
   114  		utils.EthStatsURLFlag,
   115  		utils.MetricsEnabledFlag,
   116  		utils.FakePoWFlag,
   117  		utils.NoCompactionFlag,
   118  		utils.GpoBlocksFlag,
   119  		utils.GpoPercentileFlag,
   120  		utils.ExtraDataFlag,
   121  		configFileFlag,
   122  	}
   123  
   124  	rpcFlags = []cli.Flag{
   125  		utils.RPCEnabledFlag,
   126  		utils.RPCListenAddrFlag,
   127  		utils.RPCPortFlag,
   128  		utils.RPCApiFlag,
   129  		utils.WSEnabledFlag,
   130  		utils.WSListenAddrFlag,
   131  		utils.WSPortFlag,
   132  		utils.WSApiFlag,
   133  		utils.WSAllowedOriginsFlag,
   134  		utils.IPCDisabledFlag,
   135  		utils.IPCPathFlag,
   136  	}
   137  
   138  	whisperFlags = []cli.Flag{
   139  		utils.WhisperEnabledFlag,
   140  		utils.WhisperMaxMessageSizeFlag,
   141  		utils.WhisperMinPOWFlag,
   142  	}
   143  )
   144  
   145  func init() {
   146  	// Initialize the CLI app and start pxc
   147  	app.Action = pxc
   148  	app.HideVersion = true // we have a command to print the version
   149  	app.Copyright = `Copyright 2013-2017 The Go-Ethereum Authors
   150     Copyright 2016-2017 The Go-pixicoin Authors
   151  
   152     Go-pixicoin is free software: you can redistribute it and/or modify
   153     it under the terms of the GNU General Public License as published by
   154     the Free Software Foundation, either version 3 of the License, or
   155     (at your option) any later version.
   156  
   157     Go-pixicoin is distributed in the hope that it will be useful,
   158     but WITHOUT ANY WARRANTY; without even the implied warranty of
   159     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   160     GNU General Public License for more details.
   161  
   162     You should have received a copy of the GNU General Public License
   163     along with Go-pixicoin. If not, see <http://www.gnu.org/licenses/>.`
   164  	app.Commands = []cli.Command{
   165  		// See chaincmd.go:
   166  		initCommand,
   167  		importCommand,
   168  		exportCommand,
   169  		copydbCommand,
   170  		removedbCommand,
   171  		dumpCommand,
   172  		// See monitorcmd.go:
   173  		monitorCommand,
   174  		// See accountcmd.go:
   175  		accountCommand,
   176  		walletCommand,
   177  		// See consolecmd.go:
   178  		consoleCommand,
   179  		attachCommand,
   180  		javascriptCommand,
   181  		// See misccmd.go:
   182  		makecacheCommand,
   183  		makedagCommand,
   184  		versionCommand,
   185  		bugCommand,
   186  		licenseCommand,
   187  		// See config.go
   188  		dumpConfigCommand,
   189  	}
   190  	sort.Sort(cli.CommandsByName(app.Commands))
   191  
   192  	app.Flags = append(app.Flags, nodeFlags...)
   193  	app.Flags = append(app.Flags, rpcFlags...)
   194  	app.Flags = append(app.Flags, consoleFlags...)
   195  	app.Flags = append(app.Flags, debug.Flags...)
   196  	app.Flags = append(app.Flags, whisperFlags...)
   197  
   198  	app.Before = func(ctx *cli.Context) error {
   199  		runtime.GOMAXPROCS(runtime.NumCPU())
   200  		if err := debug.Setup(ctx); err != nil {
   201  			return err
   202  		}
   203  		// Start system runtime metrics collection
   204  		go metrics.CollectProcessMetrics(3 * time.Second)
   205  
   206  		utils.SetupNetwork(ctx)
   207  		return nil
   208  	}
   209  
   210  	app.After = func(ctx *cli.Context) error {
   211  		debug.Exit()
   212  		console.Stdin.Close() // Resets terminal mode.
   213  		return nil
   214  	}
   215  }
   216  
   217  func main() {
   218  	if err := app.Run(os.Args); err != nil {
   219  		fmt.Fprintln(os.Stderr, err)
   220  		os.Exit(1)
   221  	}
   222  }
   223  
   224  // pxc is the main entry point into the system if no special subcommand is ran.
   225  // It creates a default node based on the command line arguments and runs it in
   226  // blocking mode, waiting for it to be shut down.
   227  func pxc(ctx *cli.Context) error {
   228  	node := makeFullNode(ctx)
   229  	startNode(ctx, node)
   230  	node.Wait()
   231  	return nil
   232  }
   233  
   234  // startNode boots up the system node and all registered protocols, after which
   235  // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
   236  // miner.
   237  func startNode(ctx *cli.Context, stack *node.Node) {
   238  	// Start up the node itself
   239  	utils.StartNode(stack)
   240  
   241  	// Unlock any account specifically requested
   242  	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
   243  
   244  	passwords := utils.MakePasswordList(ctx)
   245  	unlocks := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
   246  	for i, account := range unlocks {
   247  		if trimmed := strings.TrimSpace(account); trimmed != "" {
   248  			unlockAccount(ctx, ks, trimmed, i, passwords)
   249  		}
   250  	}
   251  	// Register wallet event handlers to open and auto-derive wallets
   252  	events := make(chan accounts.WalletEvent, 16)
   253  	stack.AccountManager().Subscribe(events)
   254  
   255  	go func() {
   256  		// Create an chain state reader for self-derivation
   257  		rpcClient, err := stack.Attach()
   258  		if err != nil {
   259  			utils.Fatalf("Failed to attach to self: %v", err)
   260  		}
   261  		stateReader := ethclient.NewClient(rpcClient)
   262  
   263  		// Open any wallets already attached
   264  		for _, wallet := range stack.AccountManager().Wallets() {
   265  			if err := wallet.Open(""); err != nil {
   266  				log.Warn("Failed to open wallet", "url", wallet.URL(), "err", err)
   267  			}
   268  		}
   269  		// Listen for wallet event till termination
   270  		for event := range events {
   271  			switch event.Kind {
   272  			case accounts.WalletArrived:
   273  				if err := event.Wallet.Open(""); err != nil {
   274  					log.Warn("New wallet appeared, failed to open", "url", event.Wallet.URL(), "err", err)
   275  				}
   276  			case accounts.WalletOpened:
   277  				status, _ := event.Wallet.Status()
   278  				log.Info("New wallet appeared", "url", event.Wallet.URL(), "status", status)
   279  
   280  				if event.Wallet.URL().Scheme == "ledger" {
   281  					event.Wallet.SelfDerive(accounts.DefaultLedgerBaseDerivationPath, stateReader)
   282  				} else {
   283  					event.Wallet.SelfDerive(accounts.DefaultBaseDerivationPath, stateReader)
   284  				}
   285  
   286  			case accounts.WalletDropped:
   287  				log.Info("Old wallet dropped", "url", event.Wallet.URL())
   288  				event.Wallet.Close()
   289  			}
   290  		}
   291  	}()
   292  	// Start auxiliary services if enabled
   293  	if ctx.GlobalBool(utils.MiningEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) {
   294  		// Mining only makes sense if a full Ethereum node is running
   295  		var ethereum *eth.Ethereum
   296  		if err := stack.Service(&ethereum); err != nil {
   297  			utils.Fatalf("ethereum service not running: %v", err)
   298  		}
   299  		// Use a reduced number of threads if requested
   300  		if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 {
   301  			type threaded interface {
   302  				SetThreads(threads int)
   303  			}
   304  			if th, ok := ethereum.Engine().(threaded); ok {
   305  				th.SetThreads(threads)
   306  			}
   307  		}
   308  		// Set the gas price to the limits from the CLI and start mining
   309  		ethereum.TxPool().SetGasPrice(utils.GlobalBig(ctx, utils.GasPriceFlag.Name))
   310  		if err := ethereum.StartMining(true); err != nil {
   311  			utils.Fatalf("Failed to start mining: %v", err)
   312  		}
   313  	}
   314  }