main.go 7.2 KB

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