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