github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/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/DxChainNetwork/dxc/cmd/utils" 31 "github.com/DxChainNetwork/dxc/eth/catalyst" 32 "github.com/DxChainNetwork/dxc/eth/ethconfig" 33 "github.com/DxChainNetwork/dxc/internal/debug" 34 "github.com/DxChainNetwork/dxc/internal/ethapi" 35 "github.com/DxChainNetwork/dxc/log" 36 "github.com/DxChainNetwork/dxc/metrics" 37 "github.com/DxChainNetwork/dxc/node" 38 "github.com/DxChainNetwork/dxc/p2p/enode" 39 "github.com/DxChainNetwork/dxc/params" 40 "github.com/naoina/toml" 41 ) 42 43 var ( 44 dumpConfigCommand = cli.Command{ 45 Action: utils.MigrateFlags(dumpConfig), 46 Name: "dumpconfig", 47 Usage: "Show configuration values", 48 ArgsUsage: "", 49 Flags: append(nodeFlags, rpcFlags...), 50 Category: "MISCELLANEOUS COMMANDS", 51 Description: `The dumpconfig command shows configuration values.`, 52 } 53 54 configFileFlag = cli.StringFlag{ 55 Name: "config", 56 Usage: "TOML configuration file", 57 } 58 ) 59 60 // These settings ensure that TOML keys use the same names as Go struct fields. 61 var tomlSettings = toml.Config{ 62 NormFieldName: func(rt reflect.Type, key string) string { 63 return key 64 }, 65 FieldToKey: func(rt reflect.Type, field string) string { 66 return field 67 }, 68 MissingField: func(rt reflect.Type, field string) error { 69 id := fmt.Sprintf("%s.%s", rt.String(), field) 70 if deprecated(id) { 71 log.Warn("Config field is deprecated and won't have an effect", "name", id) 72 return nil 73 } 74 var link string 75 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 76 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 77 } 78 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 79 }, 80 } 81 82 type ethstatsConfig struct { 83 URL string `toml:",omitempty"` 84 } 85 86 type gethConfig struct { 87 Eth ethconfig.Config 88 Node node.Config 89 Ethstats ethstatsConfig 90 Metrics metrics.Config 91 } 92 93 func loadConfig(file string, cfg *gethConfig) error { 94 f, err := os.Open(file) 95 if err != nil { 96 return err 97 } 98 defer f.Close() 99 100 err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg) 101 // Add file name to errors that have a line number. 102 if _, ok := err.(*toml.LineError); ok { 103 err = errors.New(file + ", " + err.Error()) 104 } 105 return err 106 } 107 108 func defaultNodeConfig() node.Config { 109 cfg := node.DefaultConfig 110 cfg.Name = clientIdentifier 111 cfg.Version = params.VersionWithCommit(gitCommit, gitDate) 112 cfg.HTTPModules = append(cfg.HTTPModules, "eth") 113 cfg.WSModules = append(cfg.WSModules, "eth") 114 cfg.IPCPath = "geth.ipc" 115 return cfg 116 } 117 118 // makeConfigNode loads geth configuration and creates a blank node instance. 119 func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { 120 // Load defaults. 121 cfg := gethConfig{ 122 Eth: ethconfig.Defaults, 123 Node: defaultNodeConfig(), 124 Metrics: metrics.DefaultConfig, 125 } 126 127 // Load config file. 128 if file := ctx.GlobalString(configFileFlag.Name); file != "" { 129 if err := loadConfig(file, &cfg); err != nil { 130 utils.Fatalf("%v", err) 131 } 132 } 133 134 // Apply flags. 135 utils.SetNodeConfig(ctx, &cfg.Node) 136 stack, err := node.New(&cfg.Node) 137 if err != nil { 138 utils.Fatalf("Failed to create the protocol stack: %v", err) 139 } 140 utils.SetEthConfig(ctx, stack, &cfg.Eth) 141 if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) { 142 cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name) 143 } 144 applyMetricConfig(ctx, &cfg) 145 146 return stack, cfg 147 } 148 149 // makeFullNode loads geth configuration and creates the Ethereum backend. 150 func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { 151 stack, cfg := makeConfigNode(ctx) 152 if ctx.GlobalIsSet(utils.OverrideLondonFlag.Name) { 153 cfg.Eth.OverrideLondon = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideLondonFlag.Name)) 154 } 155 backend, eth := utils.RegisterEthService(stack, &cfg.Eth) 156 debug.ID = enode.PubkeyToIDV4(&cfg.Node.NodeKey().PublicKey).TerminalString() 157 158 // Configure catalyst. 159 if ctx.GlobalBool(utils.CatalystFlag.Name) { 160 if eth == nil { 161 utils.Fatalf("Catalyst does not work in light client mode.") 162 } 163 if err := catalyst.Register(stack, eth); err != nil { 164 utils.Fatalf("%v", err) 165 } 166 } 167 168 // Configure GraphQL if requested 169 if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) { 170 utils.RegisterGraphQLService(stack, backend, cfg.Node) 171 } 172 // Add the Ethereum Stats daemon if requested. 173 if cfg.Ethstats.URL != "" { 174 utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL) 175 } 176 return stack, backend 177 } 178 179 // dumpConfig is the dumpconfig command. 180 func dumpConfig(ctx *cli.Context) error { 181 _, cfg := makeConfigNode(ctx) 182 comment := "" 183 184 if cfg.Eth.Genesis != nil { 185 cfg.Eth.Genesis = nil 186 comment += "# Note: this config doesn't contain the genesis block.\n\n" 187 } 188 189 out, err := tomlSettings.Marshal(&cfg) 190 if err != nil { 191 return err 192 } 193 194 dump := os.Stdout 195 if ctx.NArg() > 0 { 196 dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) 197 if err != nil { 198 return err 199 } 200 defer dump.Close() 201 } 202 dump.WriteString(comment) 203 dump.Write(out) 204 205 return nil 206 } 207 208 func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) { 209 if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) { 210 cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name) 211 } 212 if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) { 213 cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name) 214 } 215 if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) { 216 cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name) 217 } 218 if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) { 219 cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name) 220 } 221 if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) { 222 cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name) 223 } 224 if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) { 225 cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name) 226 } 227 if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) { 228 cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name) 229 } 230 if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) { 231 cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name) 232 } 233 if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) { 234 cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name) 235 } 236 if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) { 237 cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name) 238 } 239 if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBV2Flag.Name) { 240 cfg.Metrics.EnableInfluxDBV2 = ctx.GlobalBool(utils.MetricsEnableInfluxDBV2Flag.Name) 241 } 242 if ctx.GlobalIsSet(utils.MetricsInfluxDBTokenFlag.Name) { 243 cfg.Metrics.InfluxDBToken = ctx.GlobalString(utils.MetricsInfluxDBTokenFlag.Name) 244 } 245 if ctx.GlobalIsSet(utils.MetricsInfluxDBBucketFlag.Name) { 246 cfg.Metrics.InfluxDBBucket = ctx.GlobalString(utils.MetricsInfluxDBBucketFlag.Name) 247 } 248 if ctx.GlobalIsSet(utils.MetricsInfluxDBOrganizationFlag.Name) { 249 cfg.Metrics.InfluxDBOrganization = ctx.GlobalString(utils.MetricsInfluxDBOrganizationFlag.Name) 250 } 251 } 252 253 func deprecated(field string) bool { 254 switch field { 255 case "ethconfig.Config.EVMInterpreter": 256 return true 257 case "ethconfig.Config.EWASMInterpreter": 258 return true 259 default: 260 return false 261 } 262 }