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