config.go 6.0 KB

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