config.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. "encoding/hex"
  20. "errors"
  21. "fmt"
  22. "io"
  23. "os"
  24. "reflect"
  25. "unicode"
  26. cli "gopkg.in/urfave/cli.v1"
  27. "github.com/ethereum/go-ethereum/cmd/utils"
  28. "github.com/ethereum/go-ethereum/contracts/release"
  29. "github.com/ethereum/go-ethereum/eth"
  30. "github.com/ethereum/go-ethereum/node"
  31. "github.com/ethereum/go-ethereum/params"
  32. whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
  33. "github.com/naoina/toml"
  34. )
  35. var (
  36. dumpConfigCommand = cli.Command{
  37. Action: utils.MigrateFlags(dumpConfig),
  38. Name: "dumpconfig",
  39. Usage: "Show configuration values",
  40. ArgsUsage: "",
  41. Flags: append(append(nodeFlags, rpcFlags...), whisper.Flags...),
  42. Category: "MISCELLANEOUS COMMANDS",
  43. Description: `The dumpconfig command shows configuration values.`,
  44. }
  45. configFileFlag = cli.StringFlag{
  46. Name: "config",
  47. Usage: "TOML configuration file",
  48. }
  49. )
  50. // These settings ensure that TOML keys use the same names as Go struct fields.
  51. var tomlSettings = toml.Config{
  52. NormFieldName: func(rt reflect.Type, key string) string {
  53. return key
  54. },
  55. FieldToKey: func(rt reflect.Type, field string) string {
  56. return field
  57. },
  58. MissingField: func(rt reflect.Type, field string) error {
  59. link := ""
  60. if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
  61. link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
  62. }
  63. return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
  64. },
  65. }
  66. type ethstatsConfig struct {
  67. URL string `toml:",omitempty"`
  68. }
  69. type gethConfig struct {
  70. Eth eth.Config
  71. Shh whisper.Config
  72. Node node.Config
  73. Ethstats ethstatsConfig
  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. }
  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. return stack, cfg
  122. }
  123. // enableWhisper returns true in case one of the whisper flags is set.
  124. func enableWhisper(ctx *cli.Context) bool {
  125. for _, flag := range whisper.Flags {
  126. if ctx.GlobalIsSet(flag.GetName()) {
  127. return true
  128. }
  129. }
  130. return false
  131. }
  132. func makeFullNode(ctx *cli.Context) *node.Node {
  133. stack, cfg := makeConfigNode(ctx)
  134. utils.RegisterEthService(stack, &cfg.Eth)
  135. // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
  136. shhEnabled := enableWhisper(ctx)
  137. shhAutoEnabled := !ctx.GlobalIsSet(whisper.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
  138. if shhEnabled || shhAutoEnabled {
  139. if ctx.GlobalIsSet(whisper.MaxMessageSizeFlag.Name) {
  140. cfg.Shh.MaxMessageSize = uint32(ctx.Int(whisper.MaxMessageSizeFlag.Name))
  141. }
  142. if ctx.GlobalIsSet(whisper.MinPOWFlag.Name) {
  143. cfg.Shh.MinimumAcceptedPOW = ctx.Float64(whisper.MinPOWFlag.Name)
  144. }
  145. utils.RegisterShhService(stack, &cfg.Shh)
  146. }
  147. // Add the Ethereum Stats daemon if requested.
  148. if cfg.Ethstats.URL != "" {
  149. utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
  150. }
  151. // Add the release oracle service so it boots along with node.
  152. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  153. config := release.Config{
  154. Oracle: relOracle,
  155. Major: uint32(params.VersionMajor),
  156. Minor: uint32(params.VersionMinor),
  157. Patch: uint32(params.VersionPatch),
  158. }
  159. commit, _ := hex.DecodeString(gitCommit)
  160. copy(config.Commit[:], commit)
  161. return release.NewReleaseService(ctx, config)
  162. }); err != nil {
  163. utils.Fatalf("Failed to register the Geth release oracle service: %v", err)
  164. }
  165. return stack
  166. }
  167. // dumpConfig is the dumpconfig command.
  168. func dumpConfig(ctx *cli.Context) error {
  169. _, cfg := makeConfigNode(ctx)
  170. comment := ""
  171. if cfg.Eth.Genesis != nil {
  172. cfg.Eth.Genesis = nil
  173. comment += "# Note: this config doesn't contain the genesis block.\n\n"
  174. }
  175. out, err := tomlSettings.Marshal(&cfg)
  176. if err != nil {
  177. return err
  178. }
  179. io.WriteString(os.Stdout, comment)
  180. os.Stdout.Write(out)
  181. return nil
  182. }