main.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // geth is the official command-line client for Ethereum.
  17. package main
  18. import (
  19. "fmt"
  20. "os"
  21. "runtime"
  22. "strings"
  23. "time"
  24. "github.com/ethereum/go-ethereum/accounts"
  25. "github.com/ethereum/go-ethereum/accounts/keystore"
  26. "github.com/ethereum/go-ethereum/cmd/utils"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/console"
  29. "github.com/ethereum/go-ethereum/eth"
  30. "github.com/ethereum/go-ethereum/ethclient"
  31. "github.com/ethereum/go-ethereum/internal/debug"
  32. "github.com/ethereum/go-ethereum/log"
  33. "github.com/ethereum/go-ethereum/metrics"
  34. "github.com/ethereum/go-ethereum/node"
  35. "gopkg.in/urfave/cli.v1"
  36. )
  37. const (
  38. clientIdentifier = "geth" // Client identifier to advertise over the network
  39. )
  40. var (
  41. // Git SHA1 commit hash of the release (set via linker flags)
  42. gitCommit = ""
  43. // Ethereum address of the Geth release oracle.
  44. relOracle = common.HexToAddress("0xfa7b9770ca4cb04296cac84f37736d4041251cdf")
  45. // The app that holds all commands and flags.
  46. app = utils.NewApp(gitCommit, "the go-ethereum command line interface")
  47. )
  48. func init() {
  49. // Initialize the CLI app and start Geth
  50. app.Action = geth
  51. app.HideVersion = true // we have a command to print the version
  52. app.Copyright = "Copyright 2013-2017 The go-ethereum Authors"
  53. app.Commands = []cli.Command{
  54. // See chaincmd.go:
  55. initCommand,
  56. importCommand,
  57. exportCommand,
  58. removedbCommand,
  59. dumpCommand,
  60. // See monitorcmd.go:
  61. monitorCommand,
  62. // See accountcmd.go:
  63. accountCommand,
  64. walletCommand,
  65. // See consolecmd.go:
  66. consoleCommand,
  67. attachCommand,
  68. javascriptCommand,
  69. // See misccmd.go:
  70. makedagCommand,
  71. versionCommand,
  72. bugCommand,
  73. licenseCommand,
  74. // See config.go
  75. dumpConfigCommand,
  76. }
  77. app.Flags = []cli.Flag{
  78. utils.IdentityFlag,
  79. utils.UnlockedAccountFlag,
  80. utils.PasswordFileFlag,
  81. utils.BootnodesFlag,
  82. utils.DataDirFlag,
  83. utils.KeyStoreDirFlag,
  84. utils.NoUSBFlag,
  85. utils.EthashCacheDirFlag,
  86. utils.EthashCachesInMemoryFlag,
  87. utils.EthashCachesOnDiskFlag,
  88. utils.EthashDatasetDirFlag,
  89. utils.EthashDatasetsInMemoryFlag,
  90. utils.EthashDatasetsOnDiskFlag,
  91. utils.FastSyncFlag,
  92. utils.LightModeFlag,
  93. utils.SyncModeFlag,
  94. utils.LightServFlag,
  95. utils.LightPeersFlag,
  96. utils.LightKDFFlag,
  97. utils.CacheFlag,
  98. utils.TrieCacheGenFlag,
  99. utils.JSpathFlag,
  100. utils.ListenPortFlag,
  101. utils.MaxPeersFlag,
  102. utils.MaxPendingPeersFlag,
  103. utils.EtherbaseFlag,
  104. utils.GasPriceFlag,
  105. utils.MinerThreadsFlag,
  106. utils.MiningEnabledFlag,
  107. utils.TargetGasLimitFlag,
  108. utils.NATFlag,
  109. utils.NoDiscoverFlag,
  110. utils.DiscoveryV5Flag,
  111. utils.NetrestrictFlag,
  112. utils.NodeKeyFileFlag,
  113. utils.NodeKeyHexFlag,
  114. utils.RPCEnabledFlag,
  115. utils.RPCListenAddrFlag,
  116. utils.RPCPortFlag,
  117. utils.RPCApiFlag,
  118. utils.WSEnabledFlag,
  119. utils.WSListenAddrFlag,
  120. utils.WSPortFlag,
  121. utils.WSApiFlag,
  122. utils.WSAllowedOriginsFlag,
  123. utils.IPCDisabledFlag,
  124. utils.IPCPathFlag,
  125. utils.ExecFlag,
  126. utils.PreloadJSFlag,
  127. utils.WhisperEnabledFlag,
  128. utils.DevModeFlag,
  129. utils.TestnetFlag,
  130. utils.RinkebyFlag,
  131. utils.VMEnableDebugFlag,
  132. utils.NetworkIdFlag,
  133. utils.RPCCORSDomainFlag,
  134. utils.EthStatsURLFlag,
  135. utils.MetricsEnabledFlag,
  136. utils.FakePoWFlag,
  137. utils.NoCompactionFlag,
  138. utils.GpoBlocksFlag,
  139. utils.GpoPercentileFlag,
  140. utils.ExtraDataFlag,
  141. configFileFlag,
  142. }
  143. app.Flags = append(app.Flags, debug.Flags...)
  144. app.Before = func(ctx *cli.Context) error {
  145. runtime.GOMAXPROCS(runtime.NumCPU())
  146. if err := debug.Setup(ctx); err != nil {
  147. return err
  148. }
  149. // Start system runtime metrics collection
  150. go metrics.CollectProcessMetrics(3 * time.Second)
  151. utils.SetupNetwork(ctx)
  152. return nil
  153. }
  154. app.After = func(ctx *cli.Context) error {
  155. debug.Exit()
  156. console.Stdin.Close() // Resets terminal mode.
  157. return nil
  158. }
  159. }
  160. func main() {
  161. if err := app.Run(os.Args); err != nil {
  162. fmt.Fprintln(os.Stderr, err)
  163. os.Exit(1)
  164. }
  165. }
  166. // geth is the main entry point into the system if no special subcommand is ran.
  167. // It creates a default node based on the command line arguments and runs it in
  168. // blocking mode, waiting for it to be shut down.
  169. func geth(ctx *cli.Context) error {
  170. node := makeFullNode(ctx)
  171. startNode(ctx, node)
  172. node.Wait()
  173. return nil
  174. }
  175. // startNode boots up the system node and all registered protocols, after which
  176. // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
  177. // miner.
  178. func startNode(ctx *cli.Context, stack *node.Node) {
  179. // Start up the node itself
  180. utils.StartNode(stack)
  181. // Unlock any account specifically requested
  182. ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
  183. passwords := utils.MakePasswordList(ctx)
  184. unlocks := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
  185. for i, account := range unlocks {
  186. if trimmed := strings.TrimSpace(account); trimmed != "" {
  187. unlockAccount(ctx, ks, trimmed, i, passwords)
  188. }
  189. }
  190. // Register wallet event handlers to open and auto-derive wallets
  191. events := make(chan accounts.WalletEvent, 16)
  192. stack.AccountManager().Subscribe(events)
  193. go func() {
  194. // Create an chain state reader for self-derivation
  195. rpcClient, err := stack.Attach()
  196. if err != nil {
  197. utils.Fatalf("Failed to attach to self: %v", err)
  198. }
  199. stateReader := ethclient.NewClient(rpcClient)
  200. // Open and self derive any wallets already attached
  201. for _, wallet := range stack.AccountManager().Wallets() {
  202. if err := wallet.Open(""); err != nil {
  203. log.Warn("Failed to open wallet", "url", wallet.URL(), "err", err)
  204. } else {
  205. wallet.SelfDerive(accounts.DefaultBaseDerivationPath, stateReader)
  206. }
  207. }
  208. // Listen for wallet event till termination
  209. for event := range events {
  210. if event.Arrive {
  211. if err := event.Wallet.Open(""); err != nil {
  212. log.Warn("New wallet appeared, failed to open", "url", event.Wallet.URL(), "err", err)
  213. } else {
  214. log.Info("New wallet appeared", "url", event.Wallet.URL(), "status", event.Wallet.Status())
  215. event.Wallet.SelfDerive(accounts.DefaultBaseDerivationPath, stateReader)
  216. }
  217. } else {
  218. log.Info("Old wallet dropped", "url", event.Wallet.URL())
  219. event.Wallet.Close()
  220. }
  221. }
  222. }()
  223. // Start auxiliary services if enabled
  224. if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
  225. var ethereum *eth.Ethereum
  226. if err := stack.Service(&ethereum); err != nil {
  227. utils.Fatalf("ethereum service not running: %v", err)
  228. }
  229. if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 {
  230. type threaded interface {
  231. SetThreads(threads int)
  232. }
  233. if th, ok := ethereum.Engine().(threaded); ok {
  234. th.SetThreads(threads)
  235. }
  236. }
  237. if err := ethereum.StartMining(true); err != nil {
  238. utils.Fatalf("Failed to start mining: %v", err)
  239. }
  240. }
  241. }