github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/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/tacshi/go-ethereum/accounts/external" 31 "github.com/tacshi/go-ethereum/accounts/keystore" 32 "github.com/tacshi/go-ethereum/accounts/scwallet" 33 "github.com/tacshi/go-ethereum/accounts/usbwallet" 34 "github.com/tacshi/go-ethereum/cmd/utils" 35 "github.com/tacshi/go-ethereum/eth/downloader" 36 "github.com/tacshi/go-ethereum/eth/ethconfig" 37 "github.com/tacshi/go-ethereum/internal/ethapi" 38 "github.com/tacshi/go-ethereum/internal/flags" 39 "github.com/tacshi/go-ethereum/internal/version" 40 "github.com/tacshi/go-ethereum/log" 41 "github.com/tacshi/go-ethereum/metrics" 42 "github.com/tacshi/go-ethereum/node" 43 "github.com/tacshi/go-ethereum/params" 44 ) 45 46 var ( 47 dumpConfigCommand = &cli.Command{ 48 Action: dumpConfig, 49 Name: "dumpconfig", 50 Usage: "Export configuration values in a TOML format", 51 ArgsUsage: "<dumpfile (optional)>", 52 Flags: flags.Merge(nodeFlags, rpcFlags), 53 Description: `Export configuration values in TOML format (to stdout by default).`, 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.OverrideShanghai.Name) { 162 v := ctx.Uint64(utils.OverrideShanghai.Name) 163 cfg.Eth.OverrideShanghai = &v 164 } 165 backend, eth := utils.RegisterEthService(stack, &cfg.Eth) 166 167 // Configure log filter RPC API. 168 filterSystem := utils.RegisterFilterAPI(stack, backend, &cfg.Eth) 169 170 // Configure GraphQL if requested. 171 if ctx.IsSet(utils.GraphQLEnabledFlag.Name) { 172 utils.RegisterGraphQLService(stack, backend, filterSystem, &cfg.Node) 173 } 174 175 // Add the Ethereum Stats daemon if requested. 176 if cfg.Ethstats.URL != "" { 177 utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL) 178 } 179 180 // Configure full-sync tester service if requested 181 if ctx.IsSet(utils.SyncTargetFlag.Name) && cfg.Eth.SyncMode == downloader.FullSync { 182 utils.RegisterFullSyncTester(stack, eth, ctx.Path(utils.SyncTargetFlag.Name)) 183 } 184 return stack, backend 185 } 186 187 // dumpConfig is the dumpconfig command. 188 func dumpConfig(ctx *cli.Context) error { 189 _, cfg := makeConfigNode(ctx) 190 comment := "" 191 192 if cfg.Eth.Genesis != nil { 193 cfg.Eth.Genesis = nil 194 comment += "# Note: this config doesn't contain the genesis block.\n\n" 195 } 196 197 out, err := tomlSettings.Marshal(&cfg) 198 if err != nil { 199 return err 200 } 201 202 dump := os.Stdout 203 if ctx.NArg() > 0 { 204 dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) 205 if err != nil { 206 return err 207 } 208 defer dump.Close() 209 } 210 dump.WriteString(comment) 211 dump.Write(out) 212 213 return nil 214 } 215 216 func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) { 217 if ctx.IsSet(utils.MetricsEnabledFlag.Name) { 218 cfg.Metrics.Enabled = ctx.Bool(utils.MetricsEnabledFlag.Name) 219 } 220 if ctx.IsSet(utils.MetricsEnabledExpensiveFlag.Name) { 221 cfg.Metrics.EnabledExpensive = ctx.Bool(utils.MetricsEnabledExpensiveFlag.Name) 222 } 223 if ctx.IsSet(utils.MetricsHTTPFlag.Name) { 224 cfg.Metrics.HTTP = ctx.String(utils.MetricsHTTPFlag.Name) 225 } 226 if ctx.IsSet(utils.MetricsPortFlag.Name) { 227 cfg.Metrics.Port = ctx.Int(utils.MetricsPortFlag.Name) 228 } 229 if ctx.IsSet(utils.MetricsEnableInfluxDBFlag.Name) { 230 cfg.Metrics.EnableInfluxDB = ctx.Bool(utils.MetricsEnableInfluxDBFlag.Name) 231 } 232 if ctx.IsSet(utils.MetricsInfluxDBEndpointFlag.Name) { 233 cfg.Metrics.InfluxDBEndpoint = ctx.String(utils.MetricsInfluxDBEndpointFlag.Name) 234 } 235 if ctx.IsSet(utils.MetricsInfluxDBDatabaseFlag.Name) { 236 cfg.Metrics.InfluxDBDatabase = ctx.String(utils.MetricsInfluxDBDatabaseFlag.Name) 237 } 238 if ctx.IsSet(utils.MetricsInfluxDBUsernameFlag.Name) { 239 cfg.Metrics.InfluxDBUsername = ctx.String(utils.MetricsInfluxDBUsernameFlag.Name) 240 } 241 if ctx.IsSet(utils.MetricsInfluxDBPasswordFlag.Name) { 242 cfg.Metrics.InfluxDBPassword = ctx.String(utils.MetricsInfluxDBPasswordFlag.Name) 243 } 244 if ctx.IsSet(utils.MetricsInfluxDBTagsFlag.Name) { 245 cfg.Metrics.InfluxDBTags = ctx.String(utils.MetricsInfluxDBTagsFlag.Name) 246 } 247 if ctx.IsSet(utils.MetricsEnableInfluxDBV2Flag.Name) { 248 cfg.Metrics.EnableInfluxDBV2 = ctx.Bool(utils.MetricsEnableInfluxDBV2Flag.Name) 249 } 250 if ctx.IsSet(utils.MetricsInfluxDBTokenFlag.Name) { 251 cfg.Metrics.InfluxDBToken = ctx.String(utils.MetricsInfluxDBTokenFlag.Name) 252 } 253 if ctx.IsSet(utils.MetricsInfluxDBBucketFlag.Name) { 254 cfg.Metrics.InfluxDBBucket = ctx.String(utils.MetricsInfluxDBBucketFlag.Name) 255 } 256 if ctx.IsSet(utils.MetricsInfluxDBOrganizationFlag.Name) { 257 cfg.Metrics.InfluxDBOrganization = ctx.String(utils.MetricsInfluxDBOrganizationFlag.Name) 258 } 259 } 260 261 func deprecated(field string) bool { 262 switch field { 263 case "ethconfig.Config.EVMInterpreter": 264 return true 265 case "ethconfig.Config.EWASMInterpreter": 266 return true 267 default: 268 return false 269 } 270 } 271 272 func setAccountManagerBackends(stack *node.Node) error { 273 conf := stack.Config() 274 am := stack.AccountManager() 275 keydir := stack.KeyStoreDir() 276 scryptN := keystore.StandardScryptN 277 scryptP := keystore.StandardScryptP 278 if conf.UseLightweightKDF { 279 scryptN = keystore.LightScryptN 280 scryptP = keystore.LightScryptP 281 } 282 283 // Assemble the supported backends 284 if len(conf.ExternalSigner) > 0 { 285 log.Info("Using external signer", "url", conf.ExternalSigner) 286 if extapi, err := external.NewExternalBackend(conf.ExternalSigner); err == nil { 287 am.AddBackend(extapi) 288 return nil 289 } else { 290 return fmt.Errorf("error connecting to external signer: %v", err) 291 } 292 } 293 294 // For now, we're using EITHER external signer OR local signers. 295 // If/when we implement some form of lockfile for USB and keystore wallets, 296 // we can have both, but it's very confusing for the user to see the same 297 // accounts in both externally and locally, plus very racey. 298 am.AddBackend(keystore.NewKeyStore(keydir, scryptN, scryptP)) 299 if conf.USB { 300 // Start a USB hub for Ledger hardware wallets 301 if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil { 302 log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err)) 303 } else { 304 am.AddBackend(ledgerhub) 305 } 306 // Start a USB hub for Trezor hardware wallets (HID version) 307 if trezorhub, err := usbwallet.NewTrezorHubWithHID(); err != nil { 308 log.Warn(fmt.Sprintf("Failed to start HID Trezor hub, disabling: %v", err)) 309 } else { 310 am.AddBackend(trezorhub) 311 } 312 // Start a USB hub for Trezor hardware wallets (WebUSB version) 313 if trezorhub, err := usbwallet.NewTrezorHubWithWebUSB(); err != nil { 314 log.Warn(fmt.Sprintf("Failed to start WebUSB Trezor hub, disabling: %v", err)) 315 } else { 316 am.AddBackend(trezorhub) 317 } 318 } 319 if len(conf.SmartCardDaemonPath) > 0 { 320 // Start a smart card hub 321 if schub, err := scwallet.NewHub(conf.SmartCardDaemonPath, scwallet.Scheme, keydir); err != nil { 322 log.Warn(fmt.Sprintf("Failed to start smart card hub, disabling: %v", err)) 323 } else { 324 am.AddBackend(schub) 325 } 326 } 327 328 return nil 329 }