main.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.FastSyncFlag,
  85. utils.LightModeFlag,
  86. utils.LightServFlag,
  87. utils.LightPeersFlag,
  88. utils.LightKDFFlag,
  89. utils.CacheFlag,
  90. utils.TrieCacheGenFlag,
  91. utils.JSpathFlag,
  92. utils.ListenPortFlag,
  93. utils.MaxPeersFlag,
  94. utils.MaxPendingPeersFlag,
  95. utils.EtherbaseFlag,
  96. utils.GasPriceFlag,
  97. utils.MinerThreadsFlag,
  98. utils.MiningEnabledFlag,
  99. utils.AutoDAGFlag,
  100. utils.TargetGasLimitFlag,
  101. utils.NATFlag,
  102. utils.NoDiscoverFlag,
  103. utils.DiscoveryV5Flag,
  104. utils.NetrestrictFlag,
  105. utils.NodeKeyFileFlag,
  106. utils.NodeKeyHexFlag,
  107. utils.RPCEnabledFlag,
  108. utils.RPCListenAddrFlag,
  109. utils.RPCPortFlag,
  110. utils.RPCApiFlag,
  111. utils.WSEnabledFlag,
  112. utils.WSListenAddrFlag,
  113. utils.WSPortFlag,
  114. utils.WSApiFlag,
  115. utils.WSAllowedOriginsFlag,
  116. utils.IPCDisabledFlag,
  117. utils.IPCApiFlag,
  118. utils.IPCPathFlag,
  119. utils.ExecFlag,
  120. utils.PreloadJSFlag,
  121. utils.WhisperEnabledFlag,
  122. utils.DevModeFlag,
  123. utils.TestNetFlag,
  124. utils.VMForceJitFlag,
  125. utils.VMJitCacheFlag,
  126. utils.VMEnableJitFlag,
  127. utils.VMEnableDebugFlag,
  128. utils.NetworkIdFlag,
  129. utils.RPCCORSDomainFlag,
  130. utils.EthStatsURLFlag,
  131. utils.MetricsEnabledFlag,
  132. utils.FakePoWFlag,
  133. utils.SolcPathFlag,
  134. utils.GpoMinGasPriceFlag,
  135. utils.GpoMaxGasPriceFlag,
  136. utils.GpoFullBlockRatioFlag,
  137. utils.GpobaseStepDownFlag,
  138. utils.GpobaseStepUpFlag,
  139. utils.GpobaseCorrectionFactorFlag,
  140. utils.ExtraDataFlag,
  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. // This should be the only place where reporting is enabled
  151. // because it is not intended to run while testing.
  152. // In addition to this check, bad block reports are sent only
  153. // for chains with the main network genesis block and network id 1.
  154. eth.EnableBadBlockReporting = true
  155. utils.SetupNetwork(ctx)
  156. return nil
  157. }
  158. app.After = func(ctx *cli.Context) error {
  159. debug.Exit()
  160. console.Stdin.Close() // Resets terminal mode.
  161. return nil
  162. }
  163. }
  164. func main() {
  165. if err := app.Run(os.Args); err != nil {
  166. fmt.Fprintln(os.Stderr, err)
  167. os.Exit(1)
  168. }
  169. }
  170. // geth is the main entry point into the system if no special subcommand is ran.
  171. // It creates a default node based on the command line arguments and runs it in
  172. // blocking mode, waiting for it to be shut down.
  173. func geth(ctx *cli.Context) error {
  174. node := makeFullNode(ctx)
  175. startNode(ctx, node)
  176. node.Wait()
  177. return nil
  178. }
  179. func makeFullNode(ctx *cli.Context) *node.Node {
  180. // Create the default extradata and construct the base node
  181. var clientInfo = struct {
  182. Version uint
  183. Name string
  184. GoVersion string
  185. Os string
  186. }{uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch), clientIdentifier, runtime.Version(), runtime.GOOS}
  187. extra, err := rlp.EncodeToBytes(clientInfo)
  188. if err != nil {
  189. glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
  190. }
  191. if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
  192. glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
  193. glog.V(logger.Debug).Infof("extra: %x\n", extra)
  194. extra = nil
  195. }
  196. stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
  197. utils.RegisterEthService(ctx, stack, extra)
  198. // Whisper must be explicitly enabled, but is auto-enabled in --dev mode.
  199. shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name)
  200. shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
  201. if shhEnabled || shhAutoEnabled {
  202. utils.RegisterShhService(stack)
  203. }
  204. // Add the Ethereum Stats daemon if requested
  205. if url := ctx.GlobalString(utils.EthStatsURLFlag.Name); url != "" {
  206. utils.RegisterEthStatsService(stack, url)
  207. }
  208. // Add the release oracle service so it boots along with node.
  209. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  210. config := release.Config{
  211. Oracle: relOracle,
  212. Major: uint32(params.VersionMajor),
  213. Minor: uint32(params.VersionMinor),
  214. Patch: uint32(params.VersionPatch),
  215. }
  216. commit, _ := hex.DecodeString(gitCommit)
  217. copy(config.Commit[:], commit)
  218. return release.NewReleaseService(ctx, config)
  219. }); err != nil {
  220. utils.Fatalf("Failed to register the Geth release oracle service: %v", err)
  221. }
  222. return stack
  223. }
  224. // startNode boots up the system node and all registered protocols, after which
  225. // it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
  226. // miner.
  227. func startNode(ctx *cli.Context, stack *node.Node) {
  228. // Start up the node itself
  229. utils.StartNode(stack)
  230. // Unlock any account specifically requested
  231. accman := stack.AccountManager()
  232. passwords := utils.MakePasswordList(ctx)
  233. accounts := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
  234. for i, account := range accounts {
  235. if trimmed := strings.TrimSpace(account); trimmed != "" {
  236. unlockAccount(ctx, accman, trimmed, i, passwords)
  237. }
  238. }
  239. // Start auxiliary services if enabled
  240. if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
  241. var ethereum *eth.Ethereum
  242. if err := stack.Service(&ethereum); err != nil {
  243. utils.Fatalf("ethereum service not running: %v", err)
  244. }
  245. if err := ethereum.StartMining(ctx.GlobalInt(utils.MinerThreadsFlag.Name)); err != nil {
  246. utils.Fatalf("Failed to start mining: %v", err)
  247. }
  248. }
  249. }