github.com/puppeth/go-ethereum@v0.8.6-0.20171014130046-e9295163aa25/cmd/geth/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 "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 = "geth" // 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 Geth 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.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.NoCompactionFlag, 112 utils.GpoBlocksFlag, 113 utils.GpoPercentileFlag, 114 utils.ExtraDataFlag, 115 configFileFlag, 116 } 117 118 rpcFlags = []cli.Flag{ 119 utils.RPCEnabledFlag, 120 utils.RPCListenAddrFlag, 121 utils.RPCPortFlag, 122 utils.RPCApiFlag, 123 utils.WSEnabledFlag, 124 utils.WSListenAddrFlag, 125 utils.WSPortFlag, 126 utils.WSApiFlag, 127 utils.WSAllowedOriginsFlag, 128 utils.IPCDisabledFlag, 129 utils.IPCPathFlag, 130 } 131 132 whisperFlags = []cli.Flag{ 133 utils.WhisperEnabledFlag, 134 utils.WhisperMaxMessageSizeFlag, 135 utils.WhisperMinPOWFlag, 136 } 137 ) 138 139 func init() { 140 // Initialize the CLI app and start Geth 141 app.Action = geth 142 app.HideVersion = true // we have a command to print the version 143 app.Copyright = "Copyright 2013-2017 The go-ethereum Authors" 144 app.Commands = []cli.Command{ 145 // See chaincmd.go: 146 initCommand, 147 importCommand, 148 exportCommand, 149 copydbCommand, 150 removedbCommand, 151 dumpCommand, 152 // See monitorcmd.go: 153 monitorCommand, 154 // See accountcmd.go: 155 accountCommand, 156 walletCommand, 157 // See consolecmd.go: 158 consoleCommand, 159 attachCommand, 160 javascriptCommand, 161 // See misccmd.go: 162 makecacheCommand, 163 makedagCommand, 164 versionCommand, 165 bugCommand, 166 licenseCommand, 167 // See config.go 168 dumpConfigCommand, 169 } 170 sort.Sort(cli.CommandsByName(app.Commands)) 171 172 app.Flags = append(app.Flags, nodeFlags...) 173 app.Flags = append(app.Flags, rpcFlags...) 174 app.Flags = append(app.Flags, consoleFlags...) 175 app.Flags = append(app.Flags, debug.Flags...) 176 app.Flags = append(app.Flags, whisperFlags...) 177 178 app.Before = func(ctx *cli.Context) error { 179 runtime.GOMAXPROCS(runtime.NumCPU()) 180 if err := debug.Setup(ctx); err != nil { 181 return err 182 } 183 // Start system runtime metrics collection 184 go metrics.CollectProcessMetrics(3 * time.Second) 185 186 utils.SetupNetwork(ctx) 187 return nil 188 } 189 190 app.After = func(ctx *cli.Context) error { 191 debug.Exit() 192 console.Stdin.Close() // Resets terminal mode. 193 return nil 194 } 195 } 196 197 func main() { 198 if err := app.Run(os.Args); err != nil { 199 fmt.Fprintln(os.Stderr, err) 200 os.Exit(1) 201 } 202 } 203 204 // geth is the main entry point into the system if no special subcommand is ran. 205 // It creates a default node based on the command line arguments and runs it in 206 // blocking mode, waiting for it to be shut down. 207 func geth(ctx *cli.Context) error { 208 node := makeFullNode(ctx) 209 startNode(ctx, node) 210 node.Wait() 211 return nil 212 } 213 214 // startNode boots up the system node and all registered protocols, after which 215 // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the 216 // miner. 217 func startNode(ctx *cli.Context, stack *node.Node) { 218 // Start up the node itself 219 utils.StartNode(stack) 220 221 // Unlock any account specifically requested 222 ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 223 224 passwords := utils.MakePasswordList(ctx) 225 unlocks := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",") 226 for i, account := range unlocks { 227 if trimmed := strings.TrimSpace(account); trimmed != "" { 228 unlockAccount(ctx, ks, trimmed, i, passwords) 229 } 230 } 231 // Register wallet event handlers to open and auto-derive wallets 232 events := make(chan accounts.WalletEvent, 16) 233 stack.AccountManager().Subscribe(events) 234 235 go func() { 236 // Create an chain state reader for self-derivation 237 rpcClient, err := stack.Attach() 238 if err != nil { 239 utils.Fatalf("Failed to attach to self: %v", err) 240 } 241 stateReader := ethclient.NewClient(rpcClient) 242 243 // Open any wallets already attached 244 for _, wallet := range stack.AccountManager().Wallets() { 245 if err := wallet.Open(""); err != nil { 246 log.Warn("Failed to open wallet", "url", wallet.URL(), "err", err) 247 } 248 } 249 // Listen for wallet event till termination 250 for event := range events { 251 switch event.Kind { 252 case accounts.WalletArrived: 253 if err := event.Wallet.Open(""); err != nil { 254 log.Warn("New wallet appeared, failed to open", "url", event.Wallet.URL(), "err", err) 255 } 256 case accounts.WalletOpened: 257 status, _ := event.Wallet.Status() 258 log.Info("New wallet appeared", "url", event.Wallet.URL(), "status", status) 259 260 if event.Wallet.URL().Scheme == "ledger" { 261 event.Wallet.SelfDerive(accounts.DefaultLedgerBaseDerivationPath, stateReader) 262 } else { 263 event.Wallet.SelfDerive(accounts.DefaultBaseDerivationPath, stateReader) 264 } 265 266 case accounts.WalletDropped: 267 log.Info("Old wallet dropped", "url", event.Wallet.URL()) 268 event.Wallet.Close() 269 } 270 } 271 }() 272 // Start auxiliary services if enabled 273 if ctx.GlobalBool(utils.MiningEnabledFlag.Name) { 274 // Mining only makes sense if a full Ethereum node is running 275 var ethereum *eth.Ethereum 276 if err := stack.Service(ðereum); err != nil { 277 utils.Fatalf("ethereum service not running: %v", err) 278 } 279 // Use a reduced number of threads if requested 280 if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 { 281 type threaded interface { 282 SetThreads(threads int) 283 } 284 if th, ok := ethereum.Engine().(threaded); ok { 285 th.SetThreads(threads) 286 } 287 } 288 // Set the gas price to the limits from the CLI and start mining 289 ethereum.TxPool().SetGasPrice(utils.GlobalBig(ctx, utils.GasPriceFlag.Name)) 290 if err := ethereum.StartMining(true); err != nil { 291 utils.Fatalf("Failed to start mining: %v", err) 292 } 293 } 294 }