main.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. "encoding/hex"
  20. "fmt"
  21. "os"
  22. "runtime"
  23. "strings"
  24. "time"
  25. "github.com/ethereum/go-ethereum/cmd/utils"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/console"
  28. "github.com/ethereum/go-ethereum/contracts/release"
  29. "github.com/ethereum/go-ethereum/eth"
  30. "github.com/ethereum/go-ethereum/internal/debug"
  31. "github.com/ethereum/go-ethereum/logger"
  32. "github.com/ethereum/go-ethereum/logger/glog"
  33. "github.com/ethereum/go-ethereum/metrics"
  34. "github.com/ethereum/go-ethereum/node"
  35. "github.com/ethereum/go-ethereum/params"
  36. "github.com/ethereum/go-ethereum/rlp"
  37. "gopkg.in/urfave/cli.v1"
  38. )
  39. const (
  40. clientIdentifier = "geth" // Client identifier to advertise over the network
  41. )
  42. var (
  43. // Git SHA1 commit hash of the release (set via linker flags)
  44. gitCommit = ""
  45. // Ethereum address of the Geth release oracle.
  46. relOracle = common.HexToAddress("0xfa7b9770ca4cb04296cac84f37736d4041251cdf")
  47. // The app that holds all commands and flags.
  48. app = utils.NewApp(gitCommit, "the go-ethereum command line interface")
  49. )
  50. func init() {
  51. // Initialize the CLI app and start Geth
  52. app.Action = geth
  53. app.HideVersion = true // we have a command to print the version
  54. app.Copyright = "Copyright 2013-2016 The go-ethereum Authors"
  55. app.Commands = []cli.Command{
  56. // See chaincmd.go:
  57. initCommand,
  58. importCommand,
  59. exportCommand,
  60. upgradedbCommand,
  61. removedbCommand,
  62. dumpCommand,
  63. // See monitorcmd.go:
  64. monitorCommand,
  65. // See accountcmd.go:
  66. accountCommand,
  67. walletCommand,
  68. // See consolecmd.go:
  69. consoleCommand,
  70. attachCommand,
  71. javascriptCommand,
  72. // See misccmd.go:
  73. makedagCommand,
  74. versionCommand,
  75. licenseCommand,
  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.OlympicFlag,
  85. utils.FastSyncFlag,
  86. utils.LightModeFlag,
  87. utils.LightServFlag,
  88. utils.LightPeersFlag,
  89. utils.LightKDFFlag,
  90. utils.CacheFlag,
  91. utils.TrieCacheGenFlag,
  92. utils.JSpathFlag,
  93. utils.ListenPortFlag,
  94. utils.MaxPeersFlag,
  95. utils.MaxPendingPeersFlag,
  96. utils.EtherbaseFlag,
  97. utils.GasPriceFlag,
  98. utils.MinerThreadsFlag,
  99. utils.MiningEnabledFlag,
  100. utils.AutoDAGFlag,
  101. utils.TargetGasLimitFlag,
  102. utils.NATFlag,
  103. utils.NatspecEnabledFlag,
  104. utils.NoDiscoverFlag,
  105. utils.DiscoveryV5Flag,
  106. utils.NetrestrictFlag,
  107. utils.NodeKeyFileFlag,
  108. utils.NodeKeyHexFlag,
  109. utils.RPCEnabledFlag,
  110. utils.RPCListenAddrFlag,
  111. utils.RPCPortFlag,
  112. utils.RPCApiFlag,
  113. utils.WSEnabledFlag,
  114. utils.WSListenAddrFlag,
  115. utils.WSPortFlag,
  116. utils.WSApiFlag,
  117. utils.WSAllowedOriginsFlag,
  118. utils.IPCDisabledFlag,
  119. utils.IPCApiFlag,
  120. utils.IPCPathFlag,
  121. utils.ExecFlag,
  122. utils.PreloadJSFlag,
  123. utils.WhisperEnabledFlag,
  124. utils.DevModeFlag,
  125. utils.TestNetFlag,
  126. utils.VMForceJitFlag,
  127. utils.VMJitCacheFlag,
  128. utils.VMEnableJitFlag,
  129. utils.NetworkIdFlag,
  130. utils.RPCCORSDomainFlag,
  131. utils.EthStatsURLFlag,
  132. utils.MetricsEnabledFlag,
  133. utils.FakePoWFlag,
  134. utils.SolcPathFlag,
  135. utils.GpoMinGasPriceFlag,
  136. utils.GpoMaxGasPriceFlag,
  137. utils.GpoFullBlockRatioFlag,
  138. utils.GpobaseStepDownFlag,
  139. utils.GpobaseStepUpFlag,
  140. utils.GpobaseCorrectionFactorFlag,
  141. utils.ExtraDataFlag,
  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. // This should be the only place where reporting is enabled
  152. // because it is not intended to run while testing.
  153. // In addition to this check, bad block reports are sent only
  154. // for chains with the main network genesis block and network id 1.
  155. eth.EnableBadBlockReporting = true
  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. func makeFullNode(ctx *cli.Context) *node.Node {
  181. // Create the default extradata and construct the base node
  182. var clientInfo = struct {
  183. Version uint
  184. Name string
  185. GoVersion string
  186. Os string
  187. }{uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch), clientIdentifier, runtime.Version(), runtime.GOOS}
  188. extra, err := rlp.EncodeToBytes(clientInfo)
  189. if err != nil {
  190. glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
  191. }
  192. if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
  193. glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
  194. glog.V(logger.Debug).Infof("extra: %x\n", extra)
  195. extra = nil
  196. }
  197. stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
  198. utils.RegisterEthService(ctx, stack, extra)
  199. // Whisper must be explicitly enabled, but is auto-enabled in --dev mode.
  200. shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name)
  201. shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
  202. if shhEnabled || shhAutoEnabled {
  203. utils.RegisterShhService(stack)
  204. }
  205. // Add the Ethereum Stats daemon if requested
  206. if url := ctx.GlobalString(utils.EthStatsURLFlag.Name); url != "" {
  207. utils.RegisterEthStatsService(stack, url)
  208. }
  209. // Add the release oracle service so it boots along with node.
  210. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  211. config := release.Config{
  212. Oracle: relOracle,
  213. Major: uint32(params.VersionMajor),
  214. Minor: uint32(params.VersionMinor),
  215. Patch: uint32(params.VersionPatch),
  216. }
  217. commit, _ := hex.DecodeString(gitCommit)
  218. copy(config.Commit[:], commit)
  219. return release.NewReleaseService(ctx, config)
  220. }); err != nil {
  221. utils.Fatalf("Failed to register the Geth release oracle service: %v", err)
  222. }
  223. return stack
  224. }
  225. // startNode boots up the system node and all registered protocols, after which
  226. // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
  227. // miner.
  228. func startNode(ctx *cli.Context, stack *node.Node) {
  229. // Start up the node itself
  230. utils.StartNode(stack)
  231. // Unlock any account specifically requested
  232. accman := stack.AccountManager()
  233. passwords := utils.MakePasswordList(ctx)
  234. accounts := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
  235. for i, account := range accounts {
  236. if trimmed := strings.TrimSpace(account); trimmed != "" {
  237. unlockAccount(ctx, accman, trimmed, i, passwords)
  238. }
  239. }
  240. // Start auxiliary services if enabled
  241. if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
  242. var ethereum *eth.Ethereum
  243. if err := stack.Service(&ethereum); err != nil {
  244. utils.Fatalf("ethereum service not running: %v", err)
  245. }
  246. if err := ethereum.StartMining(ctx.GlobalInt(utils.MinerThreadsFlag.Name)); err != nil {
  247. utils.Fatalf("Failed to start mining: %v", err)
  248. }
  249. }
  250. }