main.go 7.4 KB

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