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