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