github.com/etherbanking/go-etherbanking@v1.7.1-0.20181009210156-cf649bca5aba/cmd/gebc/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 // gebc is the official command-line client for Etherbanking. 18 package main 19 20 import ( 21 "fmt" 22 "os" 23 "runtime" 24 "sort" 25 "strings" 26 "time" 27 28 "github.com/etherbanking/go-etherbanking/accounts" 29 "github.com/etherbanking/go-etherbanking/accounts/keystore" 30 "github.com/etherbanking/go-etherbanking/cmd/utils" 31 "github.com/etherbanking/go-etherbanking/common" 32 "github.com/etherbanking/go-etherbanking/console" 33 "github.com/etherbanking/go-etherbanking/eth" 34 "github.com/etherbanking/go-etherbanking/ethclient" 35 "github.com/etherbanking/go-etherbanking/internal/debug" 36 "github.com/etherbanking/go-etherbanking/log" 37 "github.com/etherbanking/go-etherbanking/metrics" 38 "github.com/etherbanking/go-etherbanking/node" 39 "gopkg.in/urfave/cli.v1" 40 ) 41 42 const ( 43 clientIdentifier = "gebc" // 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 Gebc 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 Gebc 141 app.Action = gebc 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 removedbCommand, 150 dumpCommand, 151 // See monitorcmd.go: 152 monitorCommand, 153 // See accountcmd.go: 154 accountCommand, 155 walletCommand, 156 // See consolecmd.go: 157 consoleCommand, 158 attachCommand, 159 javascriptCommand, 160 // See misccmd.go: 161 makecacheCommand, 162 makedagCommand, 163 versionCommand, 164 bugCommand, 165 licenseCommand, 166 // See config.go 167 dumpConfigCommand, 168 } 169 sort.Sort(cli.CommandsByName(app.Commands)) 170 171 app.Flags = append(app.Flags, nodeFlags...) 172 app.Flags = append(app.Flags, rpcFlags...) 173 app.Flags = append(app.Flags, consoleFlags...) 174 app.Flags = append(app.Flags, debug.Flags...) 175 app.Flags = append(app.Flags, whisperFlags...) 176 177 app.Before = func(ctx *cli.Context) error { 178 runtime.GOMAXPROCS(runtime.NumCPU()) 179 if err := debug.Setup(ctx); err != nil { 180 return err 181 } 182 // Start system runtime metrics collection 183 go metrics.CollectProcessMetrics(3 * time.Second) 184 185 utils.SetupNetwork(ctx) 186 return nil 187 } 188 189 app.After = func(ctx *cli.Context) error { 190 debug.Exit() 191 console.Stdin.Close() // Resets terminal mode. 192 return nil 193 } 194 } 195 196 func main() { 197 if err := app.Run(os.Args); err != nil { 198 fmt.Fprintln(os.Stderr, err) 199 os.Exit(1) 200 } 201 } 202 203 // gebc is the main entry point into the system if no special subcommand is ran. 204 // It creates a default node based on the command line arguments and runs it in 205 // blocking mode, waiting for it to be shut down. 206 func gebc(ctx *cli.Context) error { 207 node := makeFullNode(ctx) 208 startNode(ctx, node) 209 node.Wait() 210 return nil 211 } 212 213 // startNode boots up the system node and all registered protocols, after which 214 // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the 215 // miner. 216 func startNode(ctx *cli.Context, stack *node.Node) { 217 // Start up the node itself 218 utils.StartNode(stack) 219 220 // Unlock any account specifically requested 221 ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 222 223 passwords := utils.MakePasswordList(ctx) 224 unlocks := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",") 225 for i, account := range unlocks { 226 if trimmed := strings.TrimSpace(account); trimmed != "" { 227 unlockAccount(ctx, ks, trimmed, i, passwords) 228 } 229 } 230 // Register wallet event handlers to open and auto-derive wallets 231 events := make(chan accounts.WalletEvent, 16) 232 stack.AccountManager().Subscribe(events) 233 234 go func() { 235 // Create an chain state reader for self-derivation 236 rpcClient, err := stack.Attach() 237 if err != nil { 238 utils.Fatalf("Failed to attach to self: %v", err) 239 } 240 stateReader := ethclient.NewClient(rpcClient) 241 242 // Open any wallets already attached 243 for _, wallet := range stack.AccountManager().Wallets() { 244 if err := wallet.Open(""); err != nil { 245 log.Warn("Failed to open wallet", "url", wallet.URL(), "err", err) 246 } 247 } 248 // Listen for wallet event till termination 249 for event := range events { 250 switch event.Kind { 251 case accounts.WalletArrived: 252 if err := event.Wallet.Open(""); err != nil { 253 log.Warn("New wallet appeared, failed to open", "url", event.Wallet.URL(), "err", err) 254 } 255 case accounts.WalletOpened: 256 status, _ := event.Wallet.Status() 257 log.Info("New wallet appeared", "url", event.Wallet.URL(), "status", status) 258 259 if event.Wallet.URL().Scheme == "ledger" { 260 event.Wallet.SelfDerive(accounts.DefaultLedgerBaseDerivationPath, stateReader) 261 } else { 262 event.Wallet.SelfDerive(accounts.DefaultBaseDerivationPath, stateReader) 263 } 264 265 case accounts.WalletDropped: 266 log.Info("Old wallet dropped", "url", event.Wallet.URL()) 267 event.Wallet.Close() 268 } 269 } 270 }() 271 // Start auxiliary services if enabled 272 if ctx.GlobalBool(utils.MiningEnabledFlag.Name) { 273 // Mining only makes sense if a full Ethereum node is running 274 var ethereum *eth.Ethereum 275 if err := stack.Service(ðereum); err != nil { 276 utils.Fatalf("ethereum service not running: %v", err) 277 } 278 // Use a reduced number of threads if requested 279 if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 { 280 type threaded interface { 281 SetThreads(threads int) 282 } 283 if th, ok := ethereum.Engine().(threaded); ok { 284 th.SetThreads(threads) 285 } 286 } 287 // Set the gas price to the limits from the CLI and start mining 288 ethereum.TxPool().SetGasPrice(utils.GlobalBig(ctx, utils.GasPriceFlag.Name)) 289 if err := ethereum.StartMining(true); err != nil { 290 utils.Fatalf("Failed to start mining: %v", err) 291 } 292 } 293 }