config.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "gopkg.in/urfave/cli.v1"
  25. "github.com/ethereum/go-ethereum/cmd/utils"
  26. "github.com/ethereum/go-ethereum/eth/ethconfig"
  27. "github.com/ethereum/go-ethereum/internal/ethapi"
  28. "github.com/ethereum/go-ethereum/log"
  29. "github.com/ethereum/go-ethereum/metrics"
  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: 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. // whisper has been deprecated, but clients out there might still have [Shh]
  69. // in their config, which will crash. Cut them some slack by keeping the
  70. // config, and displaying a message that those config switches are ineffectual.
  71. // To be removed circa Q1 2021 -- @gballet.
  72. type whisperDeprecatedConfig struct {
  73. MaxMessageSize uint32 `toml:",omitempty"`
  74. MinimumAcceptedPOW float64 `toml:",omitempty"`
  75. RestrictConnectionBetweenLightClients bool `toml:",omitempty"`
  76. }
  77. type gethConfig struct {
  78. Eth ethconfig.Config
  79. Shh whisperDeprecatedConfig
  80. Node node.Config
  81. Ethstats ethstatsConfig
  82. Metrics metrics.Config
  83. }
  84. func loadConfig(file string, cfg *gethConfig) error {
  85. f, err := os.Open(file)
  86. if err != nil {
  87. return err
  88. }
  89. defer f.Close()
  90. err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
  91. // Add file name to errors that have a line number.
  92. if _, ok := err.(*toml.LineError); ok {
  93. err = errors.New(file + ", " + err.Error())
  94. }
  95. return err
  96. }
  97. func defaultNodeConfig() node.Config {
  98. cfg := node.DefaultConfig
  99. cfg.Name = clientIdentifier
  100. cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
  101. cfg.HTTPModules = append(cfg.HTTPModules, "eth")
  102. cfg.WSModules = append(cfg.WSModules, "eth")
  103. cfg.IPCPath = "geth.ipc"
  104. return cfg
  105. }
  106. // makeConfigNode loads geth configuration and creates a blank node instance.
  107. func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
  108. // Load defaults.
  109. cfg := gethConfig{
  110. Eth: ethconfig.Defaults,
  111. Node: defaultNodeConfig(),
  112. Metrics: metrics.DefaultConfig,
  113. }
  114. // Load config file.
  115. if file := ctx.GlobalString(configFileFlag.Name); file != "" {
  116. if err := loadConfig(file, &cfg); err != nil {
  117. utils.Fatalf("%v", err)
  118. }
  119. if cfg.Shh != (whisperDeprecatedConfig{}) {
  120. log.Warn("Deprecated whisper config detected. Whisper has been moved to github.com/ethereum/whisper")
  121. }
  122. }
  123. // Apply flags.
  124. utils.SetNodeConfig(ctx, &cfg.Node)
  125. stack, err := node.New(&cfg.Node)
  126. if err != nil {
  127. utils.Fatalf("Failed to create the protocol stack: %v", err)
  128. }
  129. utils.SetEthConfig(ctx, stack, &cfg.Eth)
  130. if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
  131. cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
  132. }
  133. utils.SetShhConfig(ctx, stack)
  134. applyMetricConfig(ctx, &cfg)
  135. return stack, cfg
  136. }
  137. // enableWhisper returns true in case one of the whisper flags is set.
  138. func checkWhisper(ctx *cli.Context) {
  139. for _, flag := range whisperFlags {
  140. if ctx.GlobalIsSet(flag.GetName()) {
  141. log.Warn("deprecated whisper flag detected. Whisper has been moved to github.com/ethereum/whisper")
  142. }
  143. }
  144. }
  145. // makeFullNode loads geth configuration and creates the Ethereum backend.
  146. func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
  147. stack, cfg := makeConfigNode(ctx)
  148. backend := utils.RegisterEthService(stack, &cfg.Eth)
  149. checkWhisper(ctx)
  150. // Configure GraphQL if requested
  151. if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
  152. utils.RegisterGraphQLService(stack, backend, cfg.Node)
  153. }
  154. // Add the Ethereum Stats daemon if requested.
  155. if cfg.Ethstats.URL != "" {
  156. utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL)
  157. }
  158. return stack, backend
  159. }
  160. // dumpConfig is the dumpconfig command.
  161. func dumpConfig(ctx *cli.Context) error {
  162. _, cfg := makeConfigNode(ctx)
  163. comment := ""
  164. if cfg.Eth.Genesis != nil {
  165. cfg.Eth.Genesis = nil
  166. comment += "# Note: this config doesn't contain the genesis block.\n\n"
  167. }
  168. out, err := tomlSettings.Marshal(&cfg)
  169. if err != nil {
  170. return err
  171. }
  172. dump := os.Stdout
  173. if ctx.NArg() > 0 {
  174. dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  175. if err != nil {
  176. return err
  177. }
  178. defer dump.Close()
  179. }
  180. dump.WriteString(comment)
  181. dump.Write(out)
  182. return nil
  183. }
  184. func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) {
  185. if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) {
  186. cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name)
  187. }
  188. if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) {
  189. cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name)
  190. }
  191. if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) {
  192. cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name)
  193. }
  194. if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) {
  195. cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name)
  196. }
  197. if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) {
  198. cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name)
  199. }
  200. if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) {
  201. cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name)
  202. }
  203. if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) {
  204. cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name)
  205. }
  206. if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) {
  207. cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name)
  208. }
  209. if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) {
  210. cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name)
  211. }
  212. if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) {
  213. cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name)
  214. }
  215. }