config.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/eth"
  27. "github.com/ethereum/go-ethereum/internal/ethapi"
  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. }
  73. func loadConfig(file string, cfg *gethConfig) error {
  74. f, err := os.Open(file)
  75. if err != nil {
  76. return err
  77. }
  78. defer f.Close()
  79. err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
  80. // Add file name to errors that have a line number.
  81. if _, ok := err.(*toml.LineError); ok {
  82. err = errors.New(file + ", " + err.Error())
  83. }
  84. return err
  85. }
  86. func defaultNodeConfig() node.Config {
  87. cfg := node.DefaultConfig
  88. cfg.Name = clientIdentifier
  89. cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
  90. cfg.HTTPModules = append(cfg.HTTPModules, "eth")
  91. cfg.WSModules = append(cfg.WSModules, "eth")
  92. cfg.IPCPath = "geth.ipc"
  93. return cfg
  94. }
  95. func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
  96. // Load defaults.
  97. cfg := gethConfig{
  98. Eth: eth.DefaultConfig,
  99. Shh: whisper.DefaultConfig,
  100. Node: defaultNodeConfig(),
  101. }
  102. // Load config file.
  103. if file := ctx.GlobalString(configFileFlag.Name); file != "" {
  104. if err := loadConfig(file, &cfg); err != nil {
  105. utils.Fatalf("%v", err)
  106. }
  107. }
  108. // Apply flags.
  109. utils.SetNodeConfig(ctx, &cfg.Node)
  110. stack, err := node.New(&cfg.Node)
  111. if err != nil {
  112. utils.Fatalf("Failed to create the protocol stack: %v", err)
  113. }
  114. utils.SetEthConfig(ctx, stack, &cfg.Eth)
  115. if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
  116. cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
  117. }
  118. utils.SetShhConfig(ctx, stack, &cfg.Shh)
  119. return stack, cfg
  120. }
  121. // enableWhisper returns true in case one of the whisper flags is set.
  122. func enableWhisper(ctx *cli.Context) bool {
  123. for _, flag := range whisperFlags {
  124. if ctx.GlobalIsSet(flag.GetName()) {
  125. return true
  126. }
  127. }
  128. return false
  129. }
  130. func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
  131. stack, cfg := makeConfigNode(ctx)
  132. backend := utils.RegisterEthService(stack, &cfg.Eth)
  133. // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
  134. shhEnabled := enableWhisper(ctx)
  135. shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
  136. if shhEnabled || shhAutoEnabled {
  137. if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
  138. cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
  139. }
  140. if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
  141. cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
  142. }
  143. if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
  144. cfg.Shh.RestrictConnectionBetweenLightClients = true
  145. }
  146. utils.RegisterShhService(stack, &cfg.Shh)
  147. }
  148. // Configure GraphQL if requested
  149. if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
  150. utils.RegisterGraphQLService(stack, backend, cfg.Node)
  151. }
  152. // Add the Ethereum Stats daemon if requested.
  153. if cfg.Ethstats.URL != "" {
  154. utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL)
  155. }
  156. return stack, backend
  157. }
  158. // dumpConfig is the dumpconfig command.
  159. func dumpConfig(ctx *cli.Context) error {
  160. _, cfg := makeConfigNode(ctx)
  161. comment := ""
  162. if cfg.Eth.Genesis != nil {
  163. cfg.Eth.Genesis = nil
  164. comment += "# Note: this config doesn't contain the genesis block.\n\n"
  165. }
  166. out, err := tomlSettings.Marshal(&cfg)
  167. if err != nil {
  168. return err
  169. }
  170. dump := os.Stdout
  171. if ctx.NArg() > 0 {
  172. dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  173. if err != nil {
  174. return err
  175. }
  176. defer dump.Close()
  177. }
  178. dump.WriteString(comment)
  179. dump.Write(out)
  180. return nil
  181. }