github.com/JFJun/bsc@v1.0.0/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 cli "gopkg.in/urfave/cli.v1" 29 30 "github.com/JFJun/bsc/cmd/utils" 31 "github.com/JFJun/bsc/eth" 32 "github.com/JFJun/bsc/node" 33 "github.com/JFJun/bsc/params" 34 whisper "github.com/JFJun/bsc/whisper/whisperv6" 35 "github.com/naoina/toml" 36 ) 37 38 var ( 39 dumpConfigCommand = cli.Command{ 40 Action: utils.MigrateFlags(dumpConfig), 41 Name: "dumpconfig", 42 Usage: "Show configuration values", 43 ArgsUsage: "", 44 Flags: append(append(nodeFlags, rpcFlags...), whisperFlags...), 45 Category: "MISCELLANEOUS COMMANDS", 46 Description: `The dumpconfig command shows configuration values.`, 47 } 48 49 configFileFlag = cli.StringFlag{ 50 Name: "config", 51 Usage: "TOML configuration file", 52 } 53 ) 54 55 // These settings ensure that TOML keys use the same names as Go struct fields. 56 var tomlSettings = toml.Config{ 57 NormFieldName: func(rt reflect.Type, key string) string { 58 return key 59 }, 60 FieldToKey: func(rt reflect.Type, field string) string { 61 return field 62 }, 63 MissingField: func(rt reflect.Type, field string) error { 64 link := "" 65 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 66 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 67 } 68 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 69 }, 70 } 71 72 type ethstatsConfig struct { 73 URL string `toml:",omitempty"` 74 } 75 76 type gethConfig struct { 77 Eth eth.Config 78 Shh whisper.Config 79 Node node.Config 80 Ethstats ethstatsConfig 81 } 82 83 func loadConfig(file string, cfg *gethConfig) error { 84 f, err := os.Open(file) 85 if err != nil { 86 return err 87 } 88 defer f.Close() 89 90 err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg) 91 // Add file name to errors that have a line number. 92 if _, ok := err.(*toml.LineError); ok { 93 err = errors.New(file + ", " + err.Error()) 94 } 95 return err 96 } 97 98 func defaultNodeConfig() node.Config { 99 cfg := node.DefaultConfig 100 cfg.Name = clientIdentifier 101 cfg.Version = params.VersionWithCommit(gitCommit, gitDate) 102 cfg.HTTPModules = append(cfg.HTTPModules, "eth") 103 cfg.WSModules = append(cfg.WSModules, "eth") 104 cfg.IPCPath = "geth.ipc" 105 return cfg 106 } 107 108 func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { 109 // Load defaults. 110 cfg := gethConfig{ 111 Eth: eth.DefaultConfig, 112 Shh: whisper.DefaultConfig, 113 Node: defaultNodeConfig(), 114 } 115 116 // Load config file. 117 if file := ctx.GlobalString(configFileFlag.Name); file != "" { 118 if err := loadConfig(file, &cfg); err != nil { 119 utils.Fatalf("%v", err) 120 } 121 } 122 123 // Apply flags. 124 utils.SetNodeConfig(ctx, &cfg.Node) 125 stack, err := node.New(&cfg.Node) 126 if err != nil { 127 utils.Fatalf("Failed to create the protocol stack: %v", err) 128 } 129 utils.SetEthConfig(ctx, stack, &cfg.Eth) 130 if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) { 131 cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name) 132 } 133 utils.SetShhConfig(ctx, stack, &cfg.Shh) 134 135 return stack, cfg 136 } 137 138 // enableWhisper returns true in case one of the whisper flags is set. 139 func enableWhisper(ctx *cli.Context) bool { 140 for _, flag := range whisperFlags { 141 if ctx.GlobalIsSet(flag.GetName()) { 142 return true 143 } 144 } 145 return false 146 } 147 148 func makeFullNode(ctx *cli.Context) *node.Node { 149 stack, cfg := makeConfigNode(ctx) 150 if ctx.GlobalIsSet(utils.OverrideIstanbulFlag.Name) { 151 cfg.Eth.OverrideIstanbul = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideIstanbulFlag.Name)) 152 } 153 if ctx.GlobalIsSet(utils.OverrideMuirGlacierFlag.Name) { 154 cfg.Eth.OverrideMuirGlacier = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideMuirGlacierFlag.Name)) 155 } 156 utils.RegisterEthService(stack, &cfg.Eth) 157 158 // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode 159 shhEnabled := enableWhisper(ctx) 160 shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name) 161 if shhEnabled || shhAutoEnabled { 162 if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) { 163 cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name)) 164 } 165 if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) { 166 cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name) 167 } 168 if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) { 169 cfg.Shh.RestrictConnectionBetweenLightClients = true 170 } 171 utils.RegisterShhService(stack, &cfg.Shh) 172 } 173 // Configure GraphQL if requested 174 if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) { 175 utils.RegisterGraphQLService(stack, cfg.Node.GraphQLEndpoint(), cfg.Node.GraphQLCors, cfg.Node.GraphQLVirtualHosts, cfg.Node.HTTPTimeouts) 176 } 177 // Add the Ethereum Stats daemon if requested. 178 if cfg.Ethstats.URL != "" { 179 utils.RegisterEthStatsService(stack, cfg.Ethstats.URL) 180 } 181 return stack 182 } 183 184 // dumpConfig is the dumpconfig command. 185 func dumpConfig(ctx *cli.Context) error { 186 _, cfg := makeConfigNode(ctx) 187 comment := "" 188 189 if cfg.Eth.Genesis != nil { 190 cfg.Eth.Genesis = nil 191 comment += "# Note: this config doesn't contain the genesis block.\n\n" 192 } 193 194 out, err := tomlSettings.Marshal(&cfg) 195 if err != nil { 196 return err 197 } 198 199 dump := os.Stdout 200 if ctx.NArg() > 0 { 201 dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) 202 if err != nil { 203 return err 204 } 205 defer dump.Close() 206 } 207 dump.WriteString(comment) 208 dump.Write(out) 209 210 return nil 211 }