config.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/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(nodeFlags, rpcFlags...),
  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. type gethConfig struct {
  69. Eth ethconfig.Config
  70. Node node.Config
  71. Ethstats ethstatsConfig
  72. Metrics metrics.Config
  73. }
  74. func loadConfig(file string, cfg *gethConfig) error {
  75. f, err := os.Open(file)
  76. if err != nil {
  77. return err
  78. }
  79. defer f.Close()
  80. err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
  81. // Add file name to errors that have a line number.
  82. if _, ok := err.(*toml.LineError); ok {
  83. err = errors.New(file + ", " + err.Error())
  84. }
  85. return err
  86. }
  87. func defaultNodeConfig() node.Config {
  88. cfg := node.DefaultConfig
  89. cfg.Name = clientIdentifier
  90. cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
  91. cfg.HTTPModules = append(cfg.HTTPModules, "eth")
  92. cfg.WSModules = append(cfg.WSModules, "eth")
  93. cfg.IPCPath = "geth.ipc"
  94. return cfg
  95. }
  96. // makeConfigNode loads geth configuration and creates a blank node instance.
  97. func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
  98. // Load defaults.
  99. cfg := gethConfig{
  100. Eth: ethconfig.Defaults,
  101. Node: defaultNodeConfig(),
  102. Metrics: metrics.DefaultConfig,
  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. applyMetricConfig(ctx, &cfg)
  121. return stack, cfg
  122. }
  123. // makeFullNode loads geth configuration and creates the Ethereum backend.
  124. func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
  125. stack, cfg := makeConfigNode(ctx)
  126. if ctx.GlobalIsSet(utils.OverrideBerlinFlag.Name) {
  127. cfg.Eth.OverrideBerlin = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideBerlinFlag.Name))
  128. }
  129. backend := utils.RegisterEthService(stack, &cfg.Eth)
  130. // Configure GraphQL if requested
  131. if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
  132. utils.RegisterGraphQLService(stack, backend, cfg.Node)
  133. }
  134. // Add the Ethereum Stats daemon if requested.
  135. if cfg.Ethstats.URL != "" {
  136. utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL)
  137. }
  138. return stack, backend
  139. }
  140. // dumpConfig is the dumpconfig command.
  141. func dumpConfig(ctx *cli.Context) error {
  142. _, cfg := makeConfigNode(ctx)
  143. comment := ""
  144. if cfg.Eth.Genesis != nil {
  145. cfg.Eth.Genesis = nil
  146. comment += "# Note: this config doesn't contain the genesis block.\n\n"
  147. }
  148. out, err := tomlSettings.Marshal(&cfg)
  149. if err != nil {
  150. return err
  151. }
  152. dump := os.Stdout
  153. if ctx.NArg() > 0 {
  154. dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  155. if err != nil {
  156. return err
  157. }
  158. defer dump.Close()
  159. }
  160. dump.WriteString(comment)
  161. dump.Write(out)
  162. return nil
  163. }
  164. func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) {
  165. if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) {
  166. cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name)
  167. }
  168. if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) {
  169. cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name)
  170. }
  171. if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) {
  172. cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name)
  173. }
  174. if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) {
  175. cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name)
  176. }
  177. if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) {
  178. cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name)
  179. }
  180. if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) {
  181. cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name)
  182. }
  183. if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) {
  184. cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name)
  185. }
  186. if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) {
  187. cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name)
  188. }
  189. if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) {
  190. cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name)
  191. }
  192. if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) {
  193. cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name)
  194. }
  195. }