config.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "github.com/naoina/toml"
  33. )
  34. var (
  35. dumpConfigCommand = cli.Command{
  36. Action: dumpConfig,
  37. Name: "dumpconfig",
  38. Usage: "Show configuration values",
  39. ArgsUsage: "",
  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. 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)
  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. Node: defaultNodeConfig(),
  99. }
  100. // Load config file.
  101. if file := ctx.GlobalString(configFileFlag.Name); file != "" {
  102. if err := loadConfig(file, &cfg); err != nil {
  103. utils.Fatalf("%v", err)
  104. }
  105. }
  106. // Apply flags.
  107. utils.SetNodeConfig(ctx, &cfg.Node)
  108. stack, err := node.New(&cfg.Node)
  109. if err != nil {
  110. utils.Fatalf("Failed to create the protocol stack: %v", err)
  111. }
  112. utils.SetEthConfig(ctx, stack, &cfg.Eth)
  113. if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
  114. cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
  115. }
  116. return stack, cfg
  117. }
  118. func makeFullNode(ctx *cli.Context) *node.Node {
  119. stack, cfg := makeConfigNode(ctx)
  120. utils.RegisterEthService(stack, &cfg.Eth)
  121. // Whisper must be explicitly enabled, but is auto-enabled in --dev mode.
  122. shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name)
  123. shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
  124. if shhEnabled || shhAutoEnabled {
  125. utils.RegisterShhService(stack)
  126. }
  127. // Add the Ethereum Stats daemon if requested.
  128. if cfg.Ethstats.URL != "" {
  129. utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
  130. }
  131. // Add the release oracle service so it boots along with node.
  132. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  133. config := release.Config{
  134. Oracle: relOracle,
  135. Major: uint32(params.VersionMajor),
  136. Minor: uint32(params.VersionMinor),
  137. Patch: uint32(params.VersionPatch),
  138. }
  139. commit, _ := hex.DecodeString(gitCommit)
  140. copy(config.Commit[:], commit)
  141. return release.NewReleaseService(ctx, config)
  142. }); err != nil {
  143. utils.Fatalf("Failed to register the Geth release oracle service: %v", err)
  144. }
  145. return stack
  146. }
  147. // dumpConfig is the dumpconfig command.
  148. func dumpConfig(ctx *cli.Context) error {
  149. _, cfg := makeConfigNode(ctx)
  150. comment := ""
  151. if cfg.Eth.Genesis != nil {
  152. cfg.Eth.Genesis = nil
  153. comment += "# Note: this config doesn't contain the genesis block.\n\n"
  154. }
  155. out, err := tomlSettings.Marshal(&cfg)
  156. if err != nil {
  157. return err
  158. }
  159. io.WriteString(os.Stdout, comment)
  160. os.Stdout.Write(out)
  161. return nil
  162. }