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