github.com/juliankolbe/go-ethereum@v1.9.992/cmd/geth/config.go (about) 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 17 package main 18 19 import ( 20 "bufio" 21 "errors" 22 "fmt" 23 "os" 24 "reflect" 25 "unicode" 26 27 "gopkg.in/urfave/cli.v1" 28 29 "github.com/juliankolbe/go-ethereum/cmd/utils" 30 "github.com/juliankolbe/go-ethereum/eth/ethconfig" 31 "github.com/juliankolbe/go-ethereum/internal/ethapi" 32 "github.com/juliankolbe/go-ethereum/log" 33 "github.com/juliankolbe/go-ethereum/metrics" 34 "github.com/juliankolbe/go-ethereum/node" 35 "github.com/juliankolbe/go-ethereum/params" 36 "github.com/naoina/toml" 37 ) 38 39 var ( 40 dumpConfigCommand = cli.Command{ 41 Action: utils.MigrateFlags(dumpConfig), 42 Name: "dumpconfig", 43 Usage: "Show configuration values", 44 ArgsUsage: "", 45 Flags: append(append(nodeFlags, rpcFlags...), whisperFlags...), 46 Category: "MISCELLANEOUS COMMANDS", 47 Description: `The dumpconfig command shows configuration values.`, 48 } 49 50 configFileFlag = cli.StringFlag{ 51 Name: "config", 52 Usage: "TOML configuration file", 53 } 54 ) 55 56 // These settings ensure that TOML keys use the same names as Go struct fields. 57 var tomlSettings = toml.Config{ 58 NormFieldName: func(rt reflect.Type, key string) string { 59 return key 60 }, 61 FieldToKey: func(rt reflect.Type, field string) string { 62 return field 63 }, 64 MissingField: func(rt reflect.Type, field string) error { 65 link := "" 66 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 67 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 68 } 69 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 70 }, 71 } 72 73 type ethstatsConfig struct { 74 URL string `toml:",omitempty"` 75 } 76 77 // whisper has been deprecated, but clients out there might still have [Shh] 78 // in their config, which will crash. Cut them some slack by keeping the 79 // config, and displaying a message that those config switches are ineffectual. 80 // To be removed circa Q1 2021 -- @gballet. 81 type whisperDeprecatedConfig struct { 82 MaxMessageSize uint32 `toml:",omitempty"` 83 MinimumAcceptedPOW float64 `toml:",omitempty"` 84 RestrictConnectionBetweenLightClients bool `toml:",omitempty"` 85 } 86 87 type gethConfig struct { 88 Eth ethconfig.Config 89 Shh whisperDeprecatedConfig 90 Node node.Config 91 Ethstats ethstatsConfig 92 Metrics metrics.Config 93 } 94 95 func loadConfig(file string, cfg *gethConfig) error { 96 f, err := os.Open(file) 97 if err != nil { 98 return err 99 } 100 defer f.Close() 101 102 err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg) 103 // Add file name to errors that have a line number. 104 if _, ok := err.(*toml.LineError); ok { 105 err = errors.New(file + ", " + err.Error()) 106 } 107 return err 108 } 109 110 func defaultNodeConfig() node.Config { 111 cfg := node.DefaultConfig 112 cfg.Name = clientIdentifier 113 cfg.Version = params.VersionWithCommit(gitCommit, gitDate) 114 cfg.HTTPModules = append(cfg.HTTPModules, "eth") 115 cfg.WSModules = append(cfg.WSModules, "eth") 116 cfg.IPCPath = "geth.ipc" 117 return cfg 118 } 119 120 // makeConfigNode loads geth configuration and creates a blank node instance. 121 func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { 122 // Load defaults. 123 cfg := gethConfig{ 124 Eth: ethconfig.Defaults, 125 Node: defaultNodeConfig(), 126 Metrics: metrics.DefaultConfig, 127 } 128 129 // Load config file. 130 if file := ctx.GlobalString(configFileFlag.Name); file != "" { 131 if err := loadConfig(file, &cfg); err != nil { 132 utils.Fatalf("%v", err) 133 } 134 135 if cfg.Shh != (whisperDeprecatedConfig{}) { 136 log.Warn("Deprecated whisper config detected. Whisper has been moved to github.com/ethereum/whisper") 137 } 138 } 139 // Apply flags. 140 utils.SetNodeConfig(ctx, &cfg.Node) 141 stack, err := node.New(&cfg.Node) 142 if err != nil { 143 utils.Fatalf("Failed to create the protocol stack: %v", err) 144 } 145 utils.SetEthConfig(ctx, stack, &cfg.Eth) 146 if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) { 147 cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name) 148 } 149 utils.SetShhConfig(ctx, stack) 150 151 applyMetricConfig(ctx, &cfg) 152 153 return stack, cfg 154 } 155 156 // enableWhisper returns true in case one of the whisper flags is set. 157 func checkWhisper(ctx *cli.Context) { 158 for _, flag := range whisperFlags { 159 if ctx.GlobalIsSet(flag.GetName()) { 160 log.Warn("deprecated whisper flag detected. Whisper has been moved to github.com/ethereum/whisper") 161 } 162 } 163 } 164 165 // makeFullNode loads geth configuration and creates the Ethereum backend. 166 func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { 167 stack, cfg := makeConfigNode(ctx) 168 169 backend := utils.RegisterEthService(stack, &cfg.Eth) 170 171 checkWhisper(ctx) 172 // Configure GraphQL if requested 173 if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) { 174 utils.RegisterGraphQLService(stack, backend, cfg.Node) 175 } 176 // Add the Ethereum Stats daemon if requested. 177 if cfg.Ethstats.URL != "" { 178 utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL) 179 } 180 return stack, backend 181 } 182 183 // dumpConfig is the dumpconfig command. 184 func dumpConfig(ctx *cli.Context) error { 185 _, cfg := makeConfigNode(ctx) 186 comment := "" 187 188 if cfg.Eth.Genesis != nil { 189 cfg.Eth.Genesis = nil 190 comment += "# Note: this config doesn't contain the genesis block.\n\n" 191 } 192 193 out, err := tomlSettings.Marshal(&cfg) 194 if err != nil { 195 return err 196 } 197 198 dump := os.Stdout 199 if ctx.NArg() > 0 { 200 dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) 201 if err != nil { 202 return err 203 } 204 defer dump.Close() 205 } 206 dump.WriteString(comment) 207 dump.Write(out) 208 209 return nil 210 } 211 212 func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) { 213 if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) { 214 cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name) 215 } 216 if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) { 217 cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name) 218 } 219 if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) { 220 cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name) 221 } 222 if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) { 223 cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name) 224 } 225 if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) { 226 cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name) 227 } 228 if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) { 229 cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name) 230 } 231 if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) { 232 cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name) 233 } 234 if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) { 235 cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name) 236 } 237 if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) { 238 cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name) 239 } 240 if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) { 241 cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name) 242 } 243 }