config.go 5.4 KB

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