config.go 5.7 KB

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