github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/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 "github.com/urfave/cli/v2" 28 29 "github.com/naoina/toml" 30 "github.com/tirogen/go-ethereum/accounts/external" 31 "github.com/tirogen/go-ethereum/accounts/keystore" 32 "github.com/tirogen/go-ethereum/accounts/scwallet" 33 "github.com/tirogen/go-ethereum/accounts/usbwallet" 34 "github.com/tirogen/go-ethereum/cmd/utils" 35 "github.com/tirogen/go-ethereum/eth/downloader" 36 "github.com/tirogen/go-ethereum/eth/ethconfig" 37 "github.com/tirogen/go-ethereum/internal/ethapi" 38 "github.com/tirogen/go-ethereum/internal/flags" 39 "github.com/tirogen/go-ethereum/internal/version" 40 "github.com/tirogen/go-ethereum/log" 41 "github.com/tirogen/go-ethereum/metrics" 42 "github.com/tirogen/go-ethereum/node" 43 "github.com/tirogen/go-ethereum/params" 44 ) 45 46 var ( 47 dumpConfigCommand = &cli.Command{ 48 Action: dumpConfig, 49 Name: "dumpconfig", 50 Usage: "Show configuration values", 51 ArgsUsage: "", 52 Flags: flags.Merge(nodeFlags, rpcFlags), 53 Description: `The dumpconfig command shows configuration values.`, 54 } 55 56 configFileFlag = &cli.StringFlag{ 57 Name: "config", 58 Usage: "TOML configuration file", 59 Category: flags.EthCategory, 60 } 61 ) 62 63 // These settings ensure that TOML keys use the same names as Go struct fields. 64 var tomlSettings = toml.Config{ 65 NormFieldName: func(rt reflect.Type, key string) string { 66 return key 67 }, 68 FieldToKey: func(rt reflect.Type, field string) string { 69 return field 70 }, 71 MissingField: func(rt reflect.Type, field string) error { 72 id := fmt.Sprintf("%s.%s", rt.String(), field) 73 if deprecated(id) { 74 log.Warn("Config field is deprecated and won't have an effect", "name", id) 75 return nil 76 } 77 var link string 78 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 79 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 80 } 81 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 82 }, 83 } 84 85 type ethstatsConfig struct { 86 URL string `toml:",omitempty"` 87 } 88 89 type gethConfig struct { 90 Eth ethconfig.Config 91 Node node.Config 92 Ethstats ethstatsConfig 93 Metrics metrics.Config 94 } 95 96 func loadConfig(file string, cfg *gethConfig) error { 97 f, err := os.Open(file) 98 if err != nil { 99 return err 100 } 101 defer f.Close() 102 103 err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg) 104 // Add file name to errors that have a line number. 105 if _, ok := err.(*toml.LineError); ok { 106 err = errors.New(file + ", " + err.Error()) 107 } 108 return err 109 } 110 111 func defaultNodeConfig() node.Config { 112 git, _ := version.VCS() 113 cfg := node.DefaultConfig 114 cfg.Name = clientIdentifier 115 cfg.Version = params.VersionWithCommit(git.Commit, git.Date) 116 cfg.HTTPModules = append(cfg.HTTPModules, "eth") 117 cfg.WSModules = append(cfg.WSModules, "eth") 118 cfg.IPCPath = "geth.ipc" 119 return cfg 120 } 121 122 // makeConfigNode loads geth configuration and creates a blank node instance. 123 func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { 124 // Load defaults. 125 cfg := gethConfig{ 126 Eth: ethconfig.Defaults, 127 Node: defaultNodeConfig(), 128 Metrics: metrics.DefaultConfig, 129 } 130 131 // Load config file. 132 if file := ctx.String(configFileFlag.Name); file != "" { 133 if err := loadConfig(file, &cfg); err != nil { 134 utils.Fatalf("%v", err) 135 } 136 } 137 138 // Apply flags. 139 utils.SetNodeConfig(ctx, &cfg.Node) 140 stack, err := node.New(&cfg.Node) 141 if err != nil { 142 utils.Fatalf("Failed to create the protocol stack: %v", err) 143 } 144 // Node doesn't by default populate account manager backends 145 if err := setAccountManagerBackends(stack); err != nil { 146 utils.Fatalf("Failed to set account manager backends: %v", err) 147 } 148 149 utils.SetEthConfig(ctx, stack, &cfg.Eth) 150 if ctx.IsSet(utils.EthStatsURLFlag.Name) { 151 cfg.Ethstats.URL = ctx.String(utils.EthStatsURLFlag.Name) 152 } 153 applyMetricConfig(ctx, &cfg) 154 155 return stack, cfg 156 } 157 158 // makeFullNode loads geth configuration and creates the Ethereum backend. 159 func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { 160 stack, cfg := makeConfigNode(ctx) 161 if ctx.IsSet(utils.OverrideTerminalTotalDifficulty.Name) { 162 cfg.Eth.OverrideTerminalTotalDifficulty = flags.GlobalBig(ctx, utils.OverrideTerminalTotalDifficulty.Name) 163 } 164 if ctx.IsSet(utils.OverrideTerminalTotalDifficultyPassed.Name) { 165 override := ctx.Bool(utils.OverrideTerminalTotalDifficultyPassed.Name) 166 cfg.Eth.OverrideTerminalTotalDifficultyPassed = &override 167 } 168 169 backend, eth := utils.RegisterEthService(stack, &cfg.Eth) 170 171 // Configure log filter RPC API. 172 filterSystem := utils.RegisterFilterAPI(stack, backend, &cfg.Eth) 173 174 // Configure GraphQL if requested. 175 if ctx.IsSet(utils.GraphQLEnabledFlag.Name) { 176 utils.RegisterGraphQLService(stack, backend, filterSystem, &cfg.Node) 177 } 178 179 // Add the Ethereum Stats daemon if requested. 180 if cfg.Ethstats.URL != "" { 181 utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL) 182 } 183 184 // Configure full-sync tester service if requested 185 if ctx.IsSet(utils.SyncTargetFlag.Name) && cfg.Eth.SyncMode == downloader.FullSync { 186 utils.RegisterFullSyncTester(stack, eth, ctx.Path(utils.SyncTargetFlag.Name)) 187 } 188 return stack, backend 189 } 190 191 // dumpConfig is the dumpconfig command. 192 func dumpConfig(ctx *cli.Context) error { 193 _, cfg := makeConfigNode(ctx) 194 comment := "" 195 196 if cfg.Eth.Genesis != nil { 197 cfg.Eth.Genesis = nil 198 comment += "# Note: this config doesn't contain the genesis block.\n\n" 199 } 200 201 out, err := tomlSettings.Marshal(&cfg) 202 if err != nil { 203 return err 204 } 205 206 dump := os.Stdout 207 if ctx.NArg() > 0 { 208 dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) 209 if err != nil { 210 return err 211 } 212 defer dump.Close() 213 } 214 dump.WriteString(comment) 215 dump.Write(out) 216 217 return nil 218 } 219 220 func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) { 221 if ctx.IsSet(utils.MetricsEnabledFlag.Name) { 222 cfg.Metrics.Enabled = ctx.Bool(utils.MetricsEnabledFlag.Name) 223 } 224 if ctx.IsSet(utils.MetricsEnabledExpensiveFlag.Name) { 225 cfg.Metrics.EnabledExpensive = ctx.Bool(utils.MetricsEnabledExpensiveFlag.Name) 226 } 227 if ctx.IsSet(utils.MetricsHTTPFlag.Name) { 228 cfg.Metrics.HTTP = ctx.String(utils.MetricsHTTPFlag.Name) 229 } 230 if ctx.IsSet(utils.MetricsPortFlag.Name) { 231 cfg.Metrics.Port = ctx.Int(utils.MetricsPortFlag.Name) 232 } 233 if ctx.IsSet(utils.MetricsEnableInfluxDBFlag.Name) { 234 cfg.Metrics.EnableInfluxDB = ctx.Bool(utils.MetricsEnableInfluxDBFlag.Name) 235 } 236 if ctx.IsSet(utils.MetricsInfluxDBEndpointFlag.Name) { 237 cfg.Metrics.InfluxDBEndpoint = ctx.String(utils.MetricsInfluxDBEndpointFlag.Name) 238 } 239 if ctx.IsSet(utils.MetricsInfluxDBDatabaseFlag.Name) { 240 cfg.Metrics.InfluxDBDatabase = ctx.String(utils.MetricsInfluxDBDatabaseFlag.Name) 241 } 242 if ctx.IsSet(utils.MetricsInfluxDBUsernameFlag.Name) { 243 cfg.Metrics.InfluxDBUsername = ctx.String(utils.MetricsInfluxDBUsernameFlag.Name) 244 } 245 if ctx.IsSet(utils.MetricsInfluxDBPasswordFlag.Name) { 246 cfg.Metrics.InfluxDBPassword = ctx.String(utils.MetricsInfluxDBPasswordFlag.Name) 247 } 248 if ctx.IsSet(utils.MetricsInfluxDBTagsFlag.Name) { 249 cfg.Metrics.InfluxDBTags = ctx.String(utils.MetricsInfluxDBTagsFlag.Name) 250 } 251 if ctx.IsSet(utils.MetricsEnableInfluxDBV2Flag.Name) { 252 cfg.Metrics.EnableInfluxDBV2 = ctx.Bool(utils.MetricsEnableInfluxDBV2Flag.Name) 253 } 254 if ctx.IsSet(utils.MetricsInfluxDBTokenFlag.Name) { 255 cfg.Metrics.InfluxDBToken = ctx.String(utils.MetricsInfluxDBTokenFlag.Name) 256 } 257 if ctx.IsSet(utils.MetricsInfluxDBBucketFlag.Name) { 258 cfg.Metrics.InfluxDBBucket = ctx.String(utils.MetricsInfluxDBBucketFlag.Name) 259 } 260 if ctx.IsSet(utils.MetricsInfluxDBOrganizationFlag.Name) { 261 cfg.Metrics.InfluxDBOrganization = ctx.String(utils.MetricsInfluxDBOrganizationFlag.Name) 262 } 263 } 264 265 func deprecated(field string) bool { 266 switch field { 267 case "ethconfig.Config.EVMInterpreter": 268 return true 269 case "ethconfig.Config.EWASMInterpreter": 270 return true 271 default: 272 return false 273 } 274 } 275 276 func setAccountManagerBackends(stack *node.Node) error { 277 conf := stack.Config() 278 am := stack.AccountManager() 279 keydir := stack.KeyStoreDir() 280 scryptN := keystore.StandardScryptN 281 scryptP := keystore.StandardScryptP 282 if conf.UseLightweightKDF { 283 scryptN = keystore.LightScryptN 284 scryptP = keystore.LightScryptP 285 } 286 287 // Assemble the supported backends 288 if len(conf.ExternalSigner) > 0 { 289 log.Info("Using external signer", "url", conf.ExternalSigner) 290 if extapi, err := external.NewExternalBackend(conf.ExternalSigner); err == nil { 291 am.AddBackend(extapi) 292 return nil 293 } else { 294 return fmt.Errorf("error connecting to external signer: %v", err) 295 } 296 } 297 298 // For now, we're using EITHER external signer OR local signers. 299 // If/when we implement some form of lockfile for USB and keystore wallets, 300 // we can have both, but it's very confusing for the user to see the same 301 // accounts in both externally and locally, plus very racey. 302 am.AddBackend(keystore.NewKeyStore(keydir, scryptN, scryptP)) 303 if conf.USB { 304 // Start a USB hub for Ledger hardware wallets 305 if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil { 306 log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err)) 307 } else { 308 am.AddBackend(ledgerhub) 309 } 310 // Start a USB hub for Trezor hardware wallets (HID version) 311 if trezorhub, err := usbwallet.NewTrezorHubWithHID(); err != nil { 312 log.Warn(fmt.Sprintf("Failed to start HID Trezor hub, disabling: %v", err)) 313 } else { 314 am.AddBackend(trezorhub) 315 } 316 // Start a USB hub for Trezor hardware wallets (WebUSB version) 317 if trezorhub, err := usbwallet.NewTrezorHubWithWebUSB(); err != nil { 318 log.Warn(fmt.Sprintf("Failed to start WebUSB Trezor hub, disabling: %v", err)) 319 } else { 320 am.AddBackend(trezorhub) 321 } 322 } 323 if len(conf.SmartCardDaemonPath) > 0 { 324 // Start a smart card hub 325 if schub, err := scwallet.NewHub(conf.SmartCardDaemonPath, scwallet.Scheme, keydir); err != nil { 326 log.Warn(fmt.Sprintf("Failed to start smart card hub, disabling: %v", err)) 327 } else { 328 am.AddBackend(schub) 329 } 330 } 331 332 return nil 333 }