github.com/bcskill/bcschain/v3@v3.4.9-beta2/cmd/gochain/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 "context" 22 "errors" 23 "fmt" 24 "io" 25 "math/big" 26 "os" 27 "reflect" 28 "unicode" 29 30 cli "github.com/urfave/cli" 31 32 "github.com/bcskill/bcschain/v3/cmd/utils" 33 "github.com/bcskill/bcschain/v3/eth" 34 "github.com/bcskill/bcschain/v3/netstats" 35 "github.com/bcskill/bcschain/v3/node" 36 "github.com/bcskill/bcschain/v3/params" 37 whisper "github.com/bcskill/bcschain/v3/whisper/whisperv6" 38 "github.com/naoina/toml" 39 ) 40 41 var ( 42 dumpConfigCommand = cli.Command{ 43 Action: utils.MigrateFlags(dumpConfig), 44 Name: "dumpconfig", 45 Usage: "Show configuration values", 46 ArgsUsage: "", 47 Flags: append(append(nodeFlags, rpcFlags...), whisperFlags...), 48 Category: "MISCELLANEOUS COMMANDS", 49 Description: `The dumpconfig command shows configuration values.`, 50 } 51 52 configFileFlag = cli.StringFlag{ 53 Name: "config", 54 Usage: "TOML configuration file", 55 } 56 ) 57 58 // These settings ensure that TOML keys use the same names as Go struct fields. 59 var tomlSettings = toml.Config{ 60 NormFieldName: func(rt reflect.Type, key string) string { 61 return key 62 }, 63 FieldToKey: func(rt reflect.Type, field string) string { 64 return field 65 }, 66 MissingField: func(rt reflect.Type, field string) error { 67 link := "" 68 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 69 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 70 } 71 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 72 }, 73 } 74 75 type gochainConfig struct { 76 Eth eth.Config 77 Shh whisper.Config 78 Node node.Config 79 Netstats netstats.Config 80 } 81 82 func loadConfig(file string, cfg *gochainConfig) error { 83 f, err := os.Open(file) 84 if err != nil { 85 return err 86 } 87 defer f.Close() 88 89 err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg) 90 // Add file name to errors that have a line number. 91 if _, ok := err.(*toml.LineError); ok { 92 err = errors.New(file + ", " + err.Error()) 93 } 94 return err 95 } 96 97 func defaultNodeConfig() node.Config { 98 cfg := node.DefaultConfig 99 cfg.Name = clientIdentifier 100 cfg.Version = params.VersionWithCommit(gitCommit) 101 cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh") 102 cfg.WSModules = append(cfg.WSModules, "eth", "shh") 103 cfg.IPCPath = "gochain.ipc" 104 return cfg 105 } 106 107 func makeConfigNode(ctx *cli.Context) (*node.Node, gochainConfig) { 108 // Load defaults. 109 cfg := gochainConfig{ 110 Eth: eth.DefaultConfig, 111 Shh: whisper.DefaultConfig, 112 Node: defaultNodeConfig(), 113 } 114 115 // Load config file. 116 if file := ctx.GlobalString(configFileFlag.Name); file != "" { 117 if err := loadConfig(file, &cfg); err != nil { 118 utils.Fatalf("%v", err) 119 } 120 } 121 122 // Apply flags. 123 utils.SetNodeConfig(ctx, &cfg.Node) 124 stack, err := node.New(&cfg.Node) 125 if err != nil { 126 utils.Fatalf("Failed to create the protocol stack: %v", err) 127 } 128 utils.SetEthConfig(ctx, stack, &cfg.Eth) 129 if ctx.GlobalIsSet(utils.NetStatsURLFlag.Name) { 130 cfg.Netstats, err = netstats.ParseConfig(ctx.GlobalString(utils.NetStatsURLFlag.Name)) 131 if err != nil { 132 utils.Fatalf("Failed to parse netstats flag: %v", err) 133 } 134 } 135 136 utils.SetShhConfig(ctx, stack, &cfg.Shh) 137 138 return stack, cfg 139 } 140 141 // enableWhisper returns true in case one of the whisper flags is set. 142 func enableWhisper(ctx *cli.Context) bool { 143 for _, flag := range whisperFlags { 144 if ctx.GlobalIsSet(flag.GetName()) { 145 return true 146 } 147 } 148 return false 149 } 150 151 func makeFullNode(ctx *cli.Context) *node.Node { 152 stack, cfg := makeConfigNode(ctx) 153 if ctx.GlobalIsSet(utils.ConstantinopleOverrideFlag.Name) { 154 cfg.Eth.ConstantinopleOverride = new(big.Int).SetUint64(ctx.GlobalUint64(utils.ConstantinopleOverrideFlag.Name)) 155 } 156 utils.RegisterEthService(context.TODO(), 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) && 161 (ctx.GlobalIsSet(utils.DeveloperFlag.Name) || ctx.GlobalIsSet(utils.LocalFlag.Name)) 162 if shhEnabled || shhAutoEnabled { 163 if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) { 164 cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name)) 165 } 166 if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) { 167 cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name) 168 } 169 utils.RegisterShhService(stack, &cfg.Shh) 170 } 171 172 // Add the GoChain Stats daemon if requested. 173 if cfg.Netstats.URL != "" { 174 utils.RegisterNetStatsService(stack, cfg.Netstats) 175 } 176 return stack 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 io.WriteString(os.Stdout, comment) 194 os.Stdout.Write(out) 195 return nil 196 }