github.com/benorgera/go-ethereum@v1.10.18-0.20220401011646-b3f57b1a73ba/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 "math/big" 24 "os" 25 "reflect" 26 "unicode" 27 28 "gopkg.in/urfave/cli.v1" 29 30 "github.com/ethereum/go-ethereum/accounts/external" 31 "github.com/ethereum/go-ethereum/accounts/keystore" 32 "github.com/ethereum/go-ethereum/accounts/scwallet" 33 "github.com/ethereum/go-ethereum/accounts/usbwallet" 34 "github.com/ethereum/go-ethereum/cmd/utils" 35 "github.com/ethereum/go-ethereum/core/rawdb" 36 "github.com/ethereum/go-ethereum/eth/ethconfig" 37 "github.com/ethereum/go-ethereum/internal/ethapi" 38 "github.com/ethereum/go-ethereum/log" 39 "github.com/ethereum/go-ethereum/metrics" 40 "github.com/ethereum/go-ethereum/node" 41 "github.com/ethereum/go-ethereum/params" 42 "github.com/naoina/toml" 43 ) 44 45 var ( 46 dumpConfigCommand = cli.Command{ 47 Action: utils.MigrateFlags(dumpConfig), 48 Name: "dumpconfig", 49 Usage: "Show configuration values", 50 ArgsUsage: "", 51 Flags: append(nodeFlags, rpcFlags...), 52 Category: "MISCELLANEOUS COMMANDS", 53 Description: `The dumpconfig command shows configuration values.`, 54 } 55 56 configFileFlag = cli.StringFlag{ 57 Name: "config", 58 Usage: "TOML configuration file", 59 } 60 ) 61 62 // These settings ensure that TOML keys use the same names as Go struct fields. 63 var tomlSettings = toml.Config{ 64 NormFieldName: func(rt reflect.Type, key string) string { 65 return key 66 }, 67 FieldToKey: func(rt reflect.Type, field string) string { 68 return field 69 }, 70 MissingField: func(rt reflect.Type, field string) error { 71 id := fmt.Sprintf("%s.%s", rt.String(), field) 72 if deprecated(id) { 73 log.Warn("Config field is deprecated and won't have an effect", "name", id) 74 return nil 75 } 76 var link string 77 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 78 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 79 } 80 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 81 }, 82 } 83 84 type ethstatsConfig struct { 85 URL string `toml:",omitempty"` 86 } 87 88 type gethConfig struct { 89 Eth ethconfig.Config 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 136 // Apply flags. 137 utils.SetNodeConfig(ctx, &cfg.Node) 138 stack, err := node.New(&cfg.Node) 139 if err != nil { 140 utils.Fatalf("Failed to create the protocol stack: %v", err) 141 } 142 // Node doesn't by default populate account manager backends 143 if err := setAccountManagerBackends(stack); err != nil { 144 utils.Fatalf("Failed to set account manager backends: %v", err) 145 } 146 147 utils.SetEthConfig(ctx, stack, &cfg.Eth) 148 if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) { 149 cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name) 150 } 151 applyMetricConfig(ctx, &cfg) 152 153 return stack, cfg 154 } 155 156 // makeFullNode loads geth configuration and creates the Ethereum backend. 157 func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { 158 stack, cfg := makeConfigNode(ctx) 159 if ctx.GlobalIsSet(utils.OverrideArrowGlacierFlag.Name) { 160 cfg.Eth.OverrideArrowGlacier = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideArrowGlacierFlag.Name)) 161 } 162 if ctx.GlobalIsSet(utils.OverrideTerminalTotalDifficulty.Name) { 163 cfg.Eth.OverrideTerminalTotalDifficulty = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideTerminalTotalDifficulty.Name)) 164 } 165 backend, eth := utils.RegisterEthService(stack, &cfg.Eth) 166 // Warn users to migrate if they have a legacy freezer format. 167 if eth != nil { 168 firstIdx := uint64(0) 169 // Hack to speed up check for mainnet because we know 170 // the first non-empty block. 171 ghash := rawdb.ReadCanonicalHash(eth.ChainDb(), 0) 172 if cfg.Eth.NetworkId == 1 && ghash == params.MainnetGenesisHash { 173 firstIdx = 46147 174 } 175 isLegacy, _, err := dbHasLegacyReceipts(eth.ChainDb(), firstIdx) 176 if err != nil { 177 log.Error("Failed to check db for legacy receipts", "err", err) 178 } else if isLegacy { 179 log.Warn("Database has receipts with a legacy format. Please run `geth db freezer-migrate`.") 180 } 181 } 182 183 // Configure GraphQL if requested 184 if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) { 185 utils.RegisterGraphQLService(stack, backend, cfg.Node) 186 } 187 // Add the Ethereum Stats daemon if requested. 188 if cfg.Ethstats.URL != "" { 189 utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL) 190 } 191 return stack, backend 192 } 193 194 // dumpConfig is the dumpconfig command. 195 func dumpConfig(ctx *cli.Context) error { 196 _, cfg := makeConfigNode(ctx) 197 comment := "" 198 199 if cfg.Eth.Genesis != nil { 200 cfg.Eth.Genesis = nil 201 comment += "# Note: this config doesn't contain the genesis block.\n\n" 202 } 203 204 out, err := tomlSettings.Marshal(&cfg) 205 if err != nil { 206 return err 207 } 208 209 dump := os.Stdout 210 if ctx.NArg() > 0 { 211 dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) 212 if err != nil { 213 return err 214 } 215 defer dump.Close() 216 } 217 dump.WriteString(comment) 218 dump.Write(out) 219 220 return nil 221 } 222 223 func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) { 224 if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) { 225 cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name) 226 } 227 if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) { 228 cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name) 229 } 230 if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) { 231 cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name) 232 } 233 if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) { 234 cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name) 235 } 236 if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) { 237 cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name) 238 } 239 if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) { 240 cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name) 241 } 242 if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) { 243 cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name) 244 } 245 if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) { 246 cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name) 247 } 248 if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) { 249 cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name) 250 } 251 if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) { 252 cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name) 253 } 254 if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBV2Flag.Name) { 255 cfg.Metrics.EnableInfluxDBV2 = ctx.GlobalBool(utils.MetricsEnableInfluxDBV2Flag.Name) 256 } 257 if ctx.GlobalIsSet(utils.MetricsInfluxDBTokenFlag.Name) { 258 cfg.Metrics.InfluxDBToken = ctx.GlobalString(utils.MetricsInfluxDBTokenFlag.Name) 259 } 260 if ctx.GlobalIsSet(utils.MetricsInfluxDBBucketFlag.Name) { 261 cfg.Metrics.InfluxDBBucket = ctx.GlobalString(utils.MetricsInfluxDBBucketFlag.Name) 262 } 263 if ctx.GlobalIsSet(utils.MetricsInfluxDBOrganizationFlag.Name) { 264 cfg.Metrics.InfluxDBOrganization = ctx.GlobalString(utils.MetricsInfluxDBOrganizationFlag.Name) 265 } 266 } 267 268 func deprecated(field string) bool { 269 switch field { 270 case "ethconfig.Config.EVMInterpreter": 271 return true 272 case "ethconfig.Config.EWASMInterpreter": 273 return true 274 default: 275 return false 276 } 277 } 278 279 func setAccountManagerBackends(stack *node.Node) error { 280 conf := stack.Config() 281 am := stack.AccountManager() 282 keydir := stack.KeyStoreDir() 283 scryptN := keystore.StandardScryptN 284 scryptP := keystore.StandardScryptP 285 if conf.UseLightweightKDF { 286 scryptN = keystore.LightScryptN 287 scryptP = keystore.LightScryptP 288 } 289 290 // Assemble the supported backends 291 if len(conf.ExternalSigner) > 0 { 292 log.Info("Using external signer", "url", conf.ExternalSigner) 293 if extapi, err := external.NewExternalBackend(conf.ExternalSigner); err == nil { 294 am.AddBackend(extapi) 295 return nil 296 } else { 297 return fmt.Errorf("error connecting to external signer: %v", err) 298 } 299 } 300 301 // For now, we're using EITHER external signer OR local signers. 302 // If/when we implement some form of lockfile for USB and keystore wallets, 303 // we can have both, but it's very confusing for the user to see the same 304 // accounts in both externally and locally, plus very racey. 305 am.AddBackend(keystore.NewKeyStore(keydir, scryptN, scryptP)) 306 if conf.USB { 307 // Start a USB hub for Ledger hardware wallets 308 if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil { 309 log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err)) 310 } else { 311 am.AddBackend(ledgerhub) 312 } 313 // Start a USB hub for Trezor hardware wallets (HID version) 314 if trezorhub, err := usbwallet.NewTrezorHubWithHID(); err != nil { 315 log.Warn(fmt.Sprintf("Failed to start HID Trezor hub, disabling: %v", err)) 316 } else { 317 am.AddBackend(trezorhub) 318 } 319 // Start a USB hub for Trezor hardware wallets (WebUSB version) 320 if trezorhub, err := usbwallet.NewTrezorHubWithWebUSB(); err != nil { 321 log.Warn(fmt.Sprintf("Failed to start WebUSB Trezor hub, disabling: %v", err)) 322 } else { 323 am.AddBackend(trezorhub) 324 } 325 } 326 if len(conf.SmartCardDaemonPath) > 0 { 327 // Start a smart card hub 328 if schub, err := scwallet.NewHub(conf.SmartCardDaemonPath, scwallet.Scheme, keydir); err != nil { 329 log.Warn(fmt.Sprintf("Failed to start smart card hub, disabling: %v", err)) 330 } else { 331 am.AddBackend(schub) 332 } 333 } 334 335 return nil 336 }