config.go 6.8 KB

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