config.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2017 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. package main
  17. import (
  18. "bufio"
  19. "errors"
  20. "fmt"
  21. "os"
  22. "reflect"
  23. "unicode"
  24. cli "gopkg.in/urfave/cli.v1"
  25. "github.com/ethereum/go-ethereum/cmd/utils"
  26. "github.com/ethereum/go-ethereum/dashboard"
  27. "github.com/ethereum/go-ethereum/eth"
  28. "github.com/ethereum/go-ethereum/node"
  29. "github.com/ethereum/go-ethereum/params"
  30. whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
  31. "github.com/naoina/toml"
  32. )
  33. var (
  34. dumpConfigCommand = cli.Command{
  35. Action: utils.MigrateFlags(dumpConfig),
  36. Name: "dumpconfig",
  37. Usage: "Show configuration values",
  38. ArgsUsage: "",
  39. Flags: append(append(nodeFlags, rpcFlags...), whisperFlags...),
  40. Category: "MISCELLANEOUS COMMANDS",
  41. Description: `The dumpconfig command shows configuration values.`,
  42. }
  43. configFileFlag = cli.StringFlag{
  44. Name: "config",
  45. Usage: "TOML configuration file",
  46. }
  47. )
  48. // These settings ensure that TOML keys use the same names as Go struct fields.
  49. var tomlSettings = toml.Config{
  50. NormFieldName: func(rt reflect.Type, key string) string {
  51. return key
  52. },
  53. FieldToKey: func(rt reflect.Type, field string) string {
  54. return field
  55. },
  56. MissingField: func(rt reflect.Type, field string) error {
  57. link := ""
  58. if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
  59. link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
  60. }
  61. return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
  62. },
  63. }
  64. type ethstatsConfig struct {
  65. URL string `toml:",omitempty"`
  66. }
  67. type gethConfig struct {
  68. Eth eth.Config
  69. Shh whisper.Config
  70. Node node.Config
  71. Ethstats ethstatsConfig
  72. Dashboard dashboard.Config
  73. }
  74. func loadConfig(file string, cfg *gethConfig) error {
  75. f, err := os.Open(file)
  76. if err != nil {
  77. return err
  78. }
  79. defer f.Close()
  80. err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
  81. // Add file name to errors that have a line number.
  82. if _, ok := err.(*toml.LineError); ok {
  83. err = errors.New(file + ", " + err.Error())
  84. }
  85. return err
  86. }
  87. func defaultNodeConfig() node.Config {
  88. cfg := node.DefaultConfig
  89. cfg.Name = clientIdentifier
  90. cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
  91. cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
  92. cfg.WSModules = append(cfg.WSModules, "eth", "shh")
  93. cfg.IPCPath = "geth.ipc"
  94. return cfg
  95. }
  96. func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
  97. // Load defaults.
  98. cfg := gethConfig{
  99. Eth: eth.DefaultConfig,
  100. Shh: whisper.DefaultConfig,
  101. Node: defaultNodeConfig(),
  102. Dashboard: dashboard.DefaultConfig,
  103. }
  104. // Load config file.
  105. if file := ctx.GlobalString(configFileFlag.Name); file != "" {
  106. if err := loadConfig(file, &cfg); err != nil {
  107. utils.Fatalf("%v", err)
  108. }
  109. }
  110. // Apply flags.
  111. utils.SetNodeConfig(ctx, &cfg.Node)
  112. stack, err := node.New(&cfg.Node)
  113. if err != nil {
  114. utils.Fatalf("Failed to create the protocol stack: %v", err)
  115. }
  116. utils.SetEthConfig(ctx, stack, &cfg.Eth)
  117. if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
  118. cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
  119. }
  120. utils.SetShhConfig(ctx, stack, &cfg.Shh)
  121. utils.SetDashboardConfig(ctx, &cfg.Dashboard)
  122. return stack, cfg
  123. }
  124. // enableWhisper returns true in case one of the whisper flags is set.
  125. func enableWhisper(ctx *cli.Context) bool {
  126. for _, flag := range whisperFlags {
  127. if ctx.GlobalIsSet(flag.GetName()) {
  128. return true
  129. }
  130. }
  131. return false
  132. }
  133. func makeFullNode(ctx *cli.Context) *node.Node {
  134. stack, cfg := makeConfigNode(ctx)
  135. utils.RegisterEthService(stack, &cfg.Eth)
  136. if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
  137. utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
  138. }
  139. // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
  140. shhEnabled := enableWhisper(ctx)
  141. shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
  142. if shhEnabled || shhAutoEnabled {
  143. if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
  144. cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
  145. }
  146. if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
  147. cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
  148. }
  149. if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
  150. cfg.Shh.RestrictConnectionBetweenLightClients = true
  151. }
  152. utils.RegisterShhService(stack, &cfg.Shh)
  153. }
  154. // Configure GraphQL if requested
  155. if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
  156. utils.RegisterGraphQLService(stack, cfg.Node.GraphQLEndpoint(), cfg.Node.GraphQLCors, cfg.Node.GraphQLVirtualHosts, cfg.Node.HTTPTimeouts)
  157. }
  158. // Add the Ethereum Stats daemon if requested.
  159. if cfg.Ethstats.URL != "" {
  160. utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
  161. }
  162. return stack
  163. }
  164. // dumpConfig is the dumpconfig command.
  165. func dumpConfig(ctx *cli.Context) error {
  166. _, cfg := makeConfigNode(ctx)
  167. comment := ""
  168. if cfg.Eth.Genesis != nil {
  169. cfg.Eth.Genesis = nil
  170. comment += "# Note: this config doesn't contain the genesis block.\n\n"
  171. }
  172. out, err := tomlSettings.Marshal(&cfg)
  173. if err != nil {
  174. return err
  175. }
  176. dump := os.Stdout
  177. if ctx.NArg() > 0 {
  178. dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  179. if err != nil {
  180. return err
  181. }
  182. defer dump.Close()
  183. }
  184. dump.WriteString(comment)
  185. dump.Write(out)
  186. return nil
  187. }