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