config.go 7.6 KB

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