config.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. cli "gopkg.in/urfave/cli.v1"
  26. "github.com/ethereum/go-ethereum/cmd/utils"
  27. "github.com/ethereum/go-ethereum/dashboard"
  28. "github.com/ethereum/go-ethereum/eth"
  29. "github.com/ethereum/go-ethereum/graphql"
  30. "github.com/ethereum/go-ethereum/node"
  31. "github.com/ethereum/go-ethereum/params"
  32. whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
  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. type gethConfig struct {
  70. Eth eth.Config
  71. Shh whisper.Config
  72. Node node.Config
  73. Ethstats ethstatsConfig
  74. Dashboard dashboard.Config
  75. }
  76. func loadConfig(file string, cfg *gethConfig) error {
  77. f, err := os.Open(file)
  78. if err != nil {
  79. return err
  80. }
  81. defer f.Close()
  82. err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
  83. // Add file name to errors that have a line number.
  84. if _, ok := err.(*toml.LineError); ok {
  85. err = errors.New(file + ", " + err.Error())
  86. }
  87. return err
  88. }
  89. func defaultNodeConfig() node.Config {
  90. cfg := node.DefaultConfig
  91. cfg.Name = clientIdentifier
  92. cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
  93. cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
  94. cfg.WSModules = append(cfg.WSModules, "eth", "shh")
  95. cfg.IPCPath = "geth.ipc"
  96. return cfg
  97. }
  98. func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
  99. // Load defaults.
  100. cfg := gethConfig{
  101. Eth: eth.DefaultConfig,
  102. Shh: whisper.DefaultConfig,
  103. Node: defaultNodeConfig(),
  104. Dashboard: dashboard.DefaultConfig,
  105. }
  106. // Load config file.
  107. if file := ctx.GlobalString(configFileFlag.Name); file != "" {
  108. if err := loadConfig(file, &cfg); err != nil {
  109. utils.Fatalf("%v", err)
  110. }
  111. }
  112. // Apply flags.
  113. utils.SetULC(ctx, &cfg.Eth)
  114. utils.SetNodeConfig(ctx, &cfg.Node)
  115. stack, err := node.New(&cfg.Node)
  116. if err != nil {
  117. utils.Fatalf("Failed to create the protocol stack: %v", err)
  118. }
  119. utils.SetEthConfig(ctx, stack, &cfg.Eth)
  120. if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
  121. cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
  122. }
  123. utils.SetShhConfig(ctx, stack, &cfg.Shh)
  124. utils.SetDashboardConfig(ctx, &cfg.Dashboard)
  125. return stack, cfg
  126. }
  127. // enableWhisper returns true in case one of the whisper flags is set.
  128. func enableWhisper(ctx *cli.Context) bool {
  129. for _, flag := range whisperFlags {
  130. if ctx.GlobalIsSet(flag.GetName()) {
  131. return true
  132. }
  133. }
  134. return false
  135. }
  136. func makeFullNode(ctx *cli.Context) *node.Node {
  137. stack, cfg := makeConfigNode(ctx)
  138. if ctx.GlobalIsSet(utils.ConstantinopleOverrideFlag.Name) {
  139. cfg.Eth.ConstantinopleOverride = new(big.Int).SetUint64(ctx.GlobalUint64(utils.ConstantinopleOverrideFlag.Name))
  140. }
  141. utils.RegisterEthService(stack, &cfg.Eth)
  142. if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
  143. utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
  144. }
  145. // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
  146. shhEnabled := enableWhisper(ctx)
  147. shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
  148. if shhEnabled || shhAutoEnabled {
  149. if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
  150. cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
  151. }
  152. if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
  153. cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
  154. }
  155. if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
  156. cfg.Shh.RestrictConnectionBetweenLightClients = true
  157. }
  158. utils.RegisterShhService(stack, &cfg.Shh)
  159. }
  160. // Configure GraphQL if required
  161. if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
  162. if err := graphql.RegisterGraphQLService(stack, cfg.Node.GraphQLEndpoint(), cfg.Node.GraphQLCors, cfg.Node.GraphQLVirtualHosts, cfg.Node.HTTPTimeouts); err != nil {
  163. utils.Fatalf("Failed to register the Ethereum service: %v", err)
  164. }
  165. }
  166. // Add the Ethereum Stats daemon if requested.
  167. if cfg.Ethstats.URL != "" {
  168. utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
  169. }
  170. return stack
  171. }
  172. // dumpConfig is the dumpconfig command.
  173. func dumpConfig(ctx *cli.Context) error {
  174. _, cfg := makeConfigNode(ctx)
  175. comment := ""
  176. if cfg.Eth.Genesis != nil {
  177. cfg.Eth.Genesis = nil
  178. comment += "# Note: this config doesn't contain the genesis block.\n\n"
  179. }
  180. out, err := tomlSettings.Marshal(&cfg)
  181. if err != nil {
  182. return err
  183. }
  184. dump := os.Stdout
  185. if ctx.NArg() > 0 {
  186. dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  187. if err != nil {
  188. return err
  189. }
  190. defer dump.Close()
  191. }
  192. dump.WriteString(comment)
  193. dump.Write(out)
  194. return nil
  195. }