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