github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/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 "github.com/naoina/toml" 29 "gopkg.in/urfave/cli.v1" 30 31 "github.com/scroll-tech/go-ethereum/accounts/external" 32 "github.com/scroll-tech/go-ethereum/accounts/keystore" 33 "github.com/scroll-tech/go-ethereum/accounts/scwallet" 34 "github.com/scroll-tech/go-ethereum/accounts/usbwallet" 35 "github.com/scroll-tech/go-ethereum/cmd/utils" 36 "github.com/scroll-tech/go-ethereum/eth/catalyst" 37 "github.com/scroll-tech/go-ethereum/eth/ethconfig" 38 "github.com/scroll-tech/go-ethereum/internal/debug" 39 "github.com/scroll-tech/go-ethereum/internal/ethapi" 40 "github.com/scroll-tech/go-ethereum/log" 41 "github.com/scroll-tech/go-ethereum/metrics" 42 "github.com/scroll-tech/go-ethereum/node" 43 "github.com/scroll-tech/go-ethereum/params" 44 ) 45 46 var ( 47 dumpConfigCommand = cli.Command{ 48 Action: utils.MigrateFlags(dumpConfig), 49 Name: "dumpconfig", 50 Usage: "Show configuration values", 51 ArgsUsage: "", 52 Flags: append(nodeFlags, rpcFlags...), 53 Category: "MISCELLANEOUS COMMANDS", 54 Description: `The dumpconfig command shows configuration values.`, 55 } 56 57 configFileFlag = cli.StringFlag{ 58 Name: "config", 59 Usage: "TOML configuration file", 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 cfg := node.DefaultConfig 113 cfg.Name = clientIdentifier 114 cfg.Version = params.VersionWithCommit(gitCommit, gitDate) 115 cfg.HTTPModules = append(cfg.HTTPModules, "eth") 116 cfg.WSModules = append(cfg.WSModules, "eth") 117 cfg.IPCPath = "geth.ipc" 118 return cfg 119 } 120 121 // makeConfigNode loads geth configuration and creates a blank node instance. 122 func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { 123 // Load defaults. 124 cfg := gethConfig{ 125 Eth: ethconfig.Defaults, 126 Node: defaultNodeConfig(), 127 Metrics: metrics.DefaultConfig, 128 } 129 130 // Load config file. 131 if file := ctx.GlobalString(configFileFlag.Name); file != "" { 132 if err := loadConfig(file, &cfg); err != nil { 133 utils.Fatalf("%v", err) 134 } 135 } 136 137 // Apply flags. 138 utils.SetNodeConfig(ctx, &cfg.Node) 139 stack, err := node.New(&cfg.Node) 140 if err != nil { 141 utils.Fatalf("Failed to create the protocol stack: %v", err) 142 } 143 // Node doesn't by default populate account manager backends 144 if err := setAccountManagerBackends(stack); err != nil { 145 utils.Fatalf("Failed to set account manager backends: %v", err) 146 } 147 148 utils.SetEthConfig(ctx, stack, &cfg.Eth) 149 if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) { 150 cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name) 151 } 152 applyTraceConfig(ctx, &cfg.Eth) 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.GlobalIsSet(utils.OverrideArrowGlacierFlag.Name) { 162 cfg.Eth.OverrideArrowGlacier = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideArrowGlacierFlag.Name)) 163 } 164 backend, eth := utils.RegisterEthService(stack, &cfg.Eth) 165 166 // Configure catalyst. 167 if ctx.GlobalBool(utils.CatalystFlag.Name) { 168 if eth == nil { 169 utils.Fatalf("Catalyst does not work in light client mode.") 170 } 171 if err := catalyst.Register(stack, eth); err != nil { 172 utils.Fatalf("%v", err) 173 } 174 } 175 176 // Configure GraphQL if requested 177 if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) { 178 utils.RegisterGraphQLService(stack, backend, cfg.Node) 179 } 180 // Add the Ethereum Stats daemon if requested. 181 if cfg.Ethstats.URL != "" { 182 utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL) 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 applyTraceConfig(ctx *cli.Context, cfg *ethconfig.Config) { 217 subCfg := debug.ConfigTrace(ctx) 218 cfg.MPTWitness = subCfg.MPTWitness 219 } 220 221 func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) { 222 if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) { 223 cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name) 224 } 225 if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) { 226 cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name) 227 } 228 if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) { 229 cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name) 230 } 231 if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) { 232 cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name) 233 } 234 if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) { 235 cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name) 236 } 237 if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) { 238 cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name) 239 } 240 if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) { 241 cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name) 242 } 243 if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) { 244 cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name) 245 } 246 if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) { 247 cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name) 248 } 249 if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) { 250 cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name) 251 } 252 if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBV2Flag.Name) { 253 cfg.Metrics.EnableInfluxDBV2 = ctx.GlobalBool(utils.MetricsEnableInfluxDBV2Flag.Name) 254 } 255 if ctx.GlobalIsSet(utils.MetricsInfluxDBTokenFlag.Name) { 256 cfg.Metrics.InfluxDBToken = ctx.GlobalString(utils.MetricsInfluxDBTokenFlag.Name) 257 } 258 if ctx.GlobalIsSet(utils.MetricsInfluxDBBucketFlag.Name) { 259 cfg.Metrics.InfluxDBBucket = ctx.GlobalString(utils.MetricsInfluxDBBucketFlag.Name) 260 } 261 if ctx.GlobalIsSet(utils.MetricsInfluxDBOrganizationFlag.Name) { 262 cfg.Metrics.InfluxDBOrganization = ctx.GlobalString(utils.MetricsInfluxDBOrganizationFlag.Name) 263 } 264 } 265 266 func deprecated(field string) bool { 267 switch field { 268 case "ethconfig.Config.EVMInterpreter": 269 return true 270 case "ethconfig.Config.EWASMInterpreter": 271 return true 272 default: 273 return false 274 } 275 } 276 277 func setAccountManagerBackends(stack *node.Node) error { 278 conf := stack.Config() 279 am := stack.AccountManager() 280 keydir := stack.KeyStoreDir() 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 extapi, err := external.NewExternalBackend(conf.ExternalSigner); err == nil { 292 am.AddBackend(extapi) 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 }