github.com/ebceco/ebc@v1.8.19-0.20190309150932-8cb0b9e06484/cmd/ebc/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 godebug "runtime/debug" 25 "sort" 26 "strconv" 27 "strings" 28 "time" 29 30 "github.com/ebceco/ebc/accounts" 31 "github.com/ebceco/ebc/accounts/keystore" 32 "github.com/ebceco/ebc/cmd/utils" 33 "github.com/ebceco/ebc/console" 34 "github.com/ebceco/ebc/eth" 35 "github.com/ebceco/ebc/ethclient" 36 "github.com/ebceco/ebc/internal/debug" 37 "github.com/ebceco/ebc/log" 38 "github.com/ebceco/ebc/metrics" 39 "github.com/ebceco/ebc/node" 40 "github.com/elastic/gosigar" 41 cli "gopkg.in/urfave/cli.v1" 42 ) 43 44 const ( 45 clientIdentifier = "ebc" // Client identifier to advertise over the network 46 ) 47 48 var ( 49 // Git SHA1 commit hash of the release (set via linker flags) 50 gitCommit = "" 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.EthashCacheDirFlag, 69 utils.EthashCachesInMemoryFlag, 70 utils.EthashCachesOnDiskFlag, 71 utils.EthashDatasetDirFlag, 72 utils.EthashDatasetsInMemoryFlag, 73 utils.EthashDatasetsOnDiskFlag, 74 utils.TxPoolLocalsFlag, 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.SyncModeFlag, 86 utils.GCModeFlag, 87 utils.LightServFlag, 88 utils.LightPeersFlag, 89 utils.LightKDFFlag, 90 utils.WhitelistFlag, 91 utils.CacheFlag, 92 utils.CacheDatabaseFlag, 93 utils.CacheTrieFlag, 94 utils.CacheGCFlag, 95 utils.TrieCacheGenFlag, 96 utils.ListenPortFlag, 97 utils.MaxPeersFlag, 98 utils.MaxPendingPeersFlag, 99 utils.MiningEnabledFlag, 100 utils.MinerThreadsFlag, 101 utils.MinerLegacyThreadsFlag, 102 utils.MinerNotifyFlag, 103 utils.MinerGasTargetFlag, 104 utils.MinerLegacyGasTargetFlag, 105 utils.MinerGasLimitFlag, 106 utils.MinerGasPriceFlag, 107 utils.MinerLegacyGasPriceFlag, 108 utils.MinerEtherbaseFlag, 109 utils.MinerLegacyEtherbaseFlag, 110 utils.MinerExtraDataFlag, 111 utils.MinerLegacyExtraDataFlag, 112 utils.MinerRecommitIntervalFlag, 113 utils.MinerNoVerfiyFlag, 114 utils.NATFlag, 115 utils.NoDiscoverFlag, 116 utils.DiscoveryV5Flag, 117 utils.NetrestrictFlag, 118 utils.NodeKeyFileFlag, 119 utils.NodeKeyHexFlag, 120 utils.DeveloperFlag, 121 utils.DeveloperPeriodFlag, 122 utils.TestnetFlag, 123 utils.RinkebyFlag, 124 utils.VMEnableDebugFlag, 125 utils.NetworkIdFlag, 126 utils.ConstantinopleOverrideFlag, 127 utils.RPCCORSDomainFlag, 128 utils.RPCVirtualHostsFlag, 129 utils.EthStatsURLFlag, 130 utils.MetricsEnabledFlag, 131 utils.FakePoWFlag, 132 utils.NoCompactionFlag, 133 utils.GpoBlocksFlag, 134 utils.GpoPercentileFlag, 135 utils.EWASMInterpreterFlag, 136 utils.EVMInterpreterFlag, 137 configFileFlag, 138 } 139 140 rpcFlags = []cli.Flag{ 141 utils.RPCEnabledFlag, 142 utils.RPCListenAddrFlag, 143 utils.RPCPortFlag, 144 utils.RPCApiFlag, 145 utils.WSEnabledFlag, 146 utils.WSListenAddrFlag, 147 utils.WSPortFlag, 148 utils.WSApiFlag, 149 utils.WSAllowedOriginsFlag, 150 utils.IPCDisabledFlag, 151 utils.IPCPathFlag, 152 } 153 154 whisperFlags = []cli.Flag{ 155 utils.WhisperEnabledFlag, 156 utils.WhisperMaxMessageSizeFlag, 157 utils.WhisperMinPOWFlag, 158 utils.WhisperRestrictConnectionBetweenLightClientsFlag, 159 } 160 161 metricsFlags = []cli.Flag{ 162 utils.MetricsEnableInfluxDBFlag, 163 utils.MetricsInfluxDBEndpointFlag, 164 utils.MetricsInfluxDBDatabaseFlag, 165 utils.MetricsInfluxDBUsernameFlag, 166 utils.MetricsInfluxDBPasswordFlag, 167 utils.MetricsInfluxDBHostTagFlag, 168 } 169 ) 170 171 func init() { 172 // Initialize the CLI app and start Geth 173 app.Action = ebc 174 app.HideVersion = true // we have a command to print the version 175 app.Copyright = "Copyright 2013-2018 The go-ethereum Authors" 176 app.Commands = []cli.Command{ 177 // See chaincmd.go: 178 initCommand, 179 importCommand, 180 exportCommand, 181 importPreimagesCommand, 182 exportPreimagesCommand, 183 copydbCommand, 184 removedbCommand, 185 dumpCommand, 186 // See monitorcmd.go: 187 monitorCommand, 188 // See accountcmd.go: 189 accountCommand, 190 walletCommand, 191 // See consolecmd.go: 192 consoleCommand, 193 attachCommand, 194 javascriptCommand, 195 // See misccmd.go: 196 makecacheCommand, 197 makedagCommand, 198 versionCommand, 199 bugCommand, 200 licenseCommand, 201 // See config.go 202 dumpConfigCommand, 203 } 204 sort.Sort(cli.CommandsByName(app.Commands)) 205 206 app.Flags = append(app.Flags, nodeFlags...) 207 app.Flags = append(app.Flags, rpcFlags...) 208 app.Flags = append(app.Flags, consoleFlags...) 209 app.Flags = append(app.Flags, debug.Flags...) 210 app.Flags = append(app.Flags, whisperFlags...) 211 app.Flags = append(app.Flags, metricsFlags...) 212 213 app.Before = func(ctx *cli.Context) error { 214 logdir := "" 215 if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) { 216 logdir = (&node.Config{DataDir: utils.MakeDataDir(ctx)}).ResolvePath("logs") 217 } 218 if err := debug.Setup(ctx, logdir); err != nil { 219 return err 220 } 221 // Cap the cache allowance and tune the garbage collector 222 var mem gosigar.Mem 223 if err := mem.Get(); err == nil { 224 allowance := int(mem.Total / 1024 / 1024 / 3) 225 if cache := ctx.GlobalInt(utils.CacheFlag.Name); cache > allowance { 226 log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance) 227 ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(allowance)) 228 } 229 } 230 // Ensure Go's GC ignores the database cache for trigger percentage 231 cache := ctx.GlobalInt(utils.CacheFlag.Name) 232 gogc := math.Max(20, math.Min(100, 100/(float64(cache)/1024))) 233 234 log.Debug("Sanitizing Go's GC trigger", "percent", int(gogc)) 235 godebug.SetGCPercent(int(gogc)) 236 237 // Start metrics export if enabled 238 utils.SetupMetrics(ctx) 239 240 // Start system runtime metrics collection 241 go metrics.CollectProcessMetrics(3 * time.Second) 242 243 return nil 244 } 245 246 app.After = func(ctx *cli.Context) error { 247 debug.Exit() 248 console.Stdin.Close() // Resets terminal mode. 249 return nil 250 } 251 } 252 253 func main() { 254 if err := app.Run(os.Args); err != nil { 255 fmt.Fprintln(os.Stderr, err) 256 os.Exit(1) 257 } 258 } 259 260 // geth is the main entry point into the system if no special subcommand is ran. 261 // It creates a default node based on the command line arguments and runs it in 262 // blocking mode, waiting for it to be shut down. 263 func ebc(ctx *cli.Context) error { 264 if args := ctx.Args(); len(args) > 0 { 265 return fmt.Errorf("invalid command: %q", args[0]) 266 } 267 node := makeFullNode(ctx) 268 startNode(ctx, node) 269 node.Wait() 270 return nil 271 } 272 273 // startNode boots up the system node and all registered protocols, after which 274 // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the 275 // miner. 276 func startNode(ctx *cli.Context, stack *node.Node) { 277 debug.Memsize.Add("node", stack) 278 279 // Start up the node itself 280 utils.StartNode(stack) 281 282 // Unlock any account specifically requested 283 ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 284 285 passwords := utils.MakePasswordList(ctx) 286 unlocks := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",") 287 for i, account := range unlocks { 288 if trimmed := strings.TrimSpace(account); trimmed != "" { 289 unlockAccount(ctx, ks, trimmed, i, passwords) 290 } 291 } 292 // Register wallet event handlers to open and auto-derive wallets 293 events := make(chan accounts.WalletEvent, 16) 294 stack.AccountManager().Subscribe(events) 295 296 go func() { 297 // Create a chain state reader for self-derivation 298 rpcClient, err := stack.Attach() 299 if err != nil { 300 utils.Fatalf("Failed to attach to self: %v", err) 301 } 302 stateReader := ethclient.NewClient(rpcClient) 303 304 // Open any wallets already attached 305 for _, wallet := range stack.AccountManager().Wallets() { 306 if err := wallet.Open(""); err != nil { 307 log.Warn("Failed to open wallet", "url", wallet.URL(), "err", err) 308 } 309 } 310 // Listen for wallet event till termination 311 for event := range events { 312 switch event.Kind { 313 case accounts.WalletArrived: 314 if err := event.Wallet.Open(""); err != nil { 315 log.Warn("New wallet appeared, failed to open", "url", event.Wallet.URL(), "err", err) 316 } 317 case accounts.WalletOpened: 318 status, _ := event.Wallet.Status() 319 log.Info("New wallet appeared", "url", event.Wallet.URL(), "status", status) 320 321 derivationPath := accounts.DefaultBaseDerivationPath 322 if event.Wallet.URL().Scheme == "ledger" { 323 derivationPath = accounts.DefaultLedgerBaseDerivationPath 324 } 325 event.Wallet.SelfDerive(derivationPath, stateReader) 326 327 case accounts.WalletDropped: 328 log.Info("Old wallet dropped", "url", event.Wallet.URL()) 329 event.Wallet.Close() 330 } 331 } 332 }() 333 // Start auxiliary services if enabled 334 if ctx.GlobalBool(utils.MiningEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) { 335 // Mining only makes sense if a full Ethereum node is running 336 if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" { 337 utils.Fatalf("Light clients do not support mining") 338 } 339 var ethereum *eth.Ethereum 340 if err := stack.Service(ðereum); err != nil { 341 utils.Fatalf("Ethereum service not running: %v", err) 342 } 343 // Set the gas price to the limits from the CLI and start mining 344 gasprice := utils.GlobalBig(ctx, utils.MinerLegacyGasPriceFlag.Name) 345 if ctx.IsSet(utils.MinerGasPriceFlag.Name) { 346 gasprice = utils.GlobalBig(ctx, utils.MinerGasPriceFlag.Name) 347 } 348 ethereum.TxPool().SetGasPrice(gasprice) 349 350 threads := ctx.GlobalInt(utils.MinerLegacyThreadsFlag.Name) 351 if ctx.GlobalIsSet(utils.MinerThreadsFlag.Name) { 352 threads = ctx.GlobalInt(utils.MinerThreadsFlag.Name) 353 } 354 if err := ethereum.StartMining(threads); err != nil { 355 utils.Fatalf("Failed to start mining: %v", err) 356 } 357 } 358 }