github.com/luckypickle/go-ethereum-vet@v1.14.2/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 "io" 24 "os" 25 "reflect" 26 "unicode" 27 28 cli "gopkg.in/urfave/cli.v1" 29 30 "github.com/luckypickle/go-ethereum-vet/cmd/utils" 31 "github.com/luckypickle/go-ethereum-vet/eth" 32 "github.com/luckypickle/go-ethereum-vet/node" 33 "github.com/luckypickle/go-ethereum-vet/params" 34 whisper "github.com/luckypickle/go-ethereum-vet/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) 102 cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh") 103 cfg.WSModules = append(cfg.WSModules, "eth", "shh") 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 134 utils.SetShhConfig(ctx, stack, &cfg.Shh) 135 136 return stack, cfg 137 } 138 139 // enableWhisper returns true in case one of the whisper flags is set. 140 func enableWhisper(ctx *cli.Context) bool { 141 for _, flag := range whisperFlags { 142 if ctx.GlobalIsSet(flag.GetName()) { 143 return true 144 } 145 } 146 return false 147 } 148 149 func makeFullNode(ctx *cli.Context) *node.Node { 150 stack, cfg := makeConfigNode(ctx) 151 152 utils.RegisterEthService(stack, &cfg.Eth) 153 154 // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode 155 shhEnabled := enableWhisper(ctx) 156 shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name) 157 if shhEnabled || shhAutoEnabled { 158 if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) { 159 cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name)) 160 } 161 if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) { 162 cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name) 163 } 164 utils.RegisterShhService(stack, &cfg.Shh) 165 } 166 167 // Add the Ethereum Stats daemon if requested. 168 if cfg.Ethstats.URL != "" { 169 utils.RegisterEthStatsService(stack, cfg.Ethstats.URL) 170 } 171 return stack 172 } 173 174 // dumpConfig is the dumpconfig command. 175 func dumpConfig(ctx *cli.Context) error { 176 _, cfg := makeConfigNode(ctx) 177 comment := "" 178 179 if cfg.Eth.Genesis != nil { 180 cfg.Eth.Genesis = nil 181 comment += "# Note: this config doesn't contain the genesis block.\n\n" 182 } 183 184 out, err := tomlSettings.Marshal(&cfg) 185 if err != nil { 186 return err 187 } 188 io.WriteString(os.Stdout, comment) 189 os.Stdout.Write(out) 190 return nil 191 }