github.com/gochain-io/gochain@v2.2.26+incompatible/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 "os" 26 "reflect" 27 "unicode" 28 29 cli "gopkg.in/urfave/cli.v1" 30 31 "github.com/gochain-io/gochain/cmd/utils" 32 "github.com/gochain-io/gochain/dashboard" 33 "github.com/gochain-io/gochain/eth" 34 "github.com/gochain-io/gochain/netstats" 35 "github.com/gochain-io/gochain/node" 36 "github.com/gochain-io/gochain/params" 37 whisper "github.com/gochain-io/gochain/whisper/whisperv5" 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 Dashboard dashboard.Config 81 } 82 83 func loadConfig(file string, cfg *gochainConfig) 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 = "gochain.ipc" 105 return cfg 106 } 107 108 func makeConfigNode(ctx *cli.Context) (*node.Node, gochainConfig) { 109 // Load defaults. 110 cfg := gochainConfig{ 111 Eth: eth.DefaultConfig, 112 Shh: whisper.DefaultConfig, 113 Node: defaultNodeConfig(), 114 Dashboard: dashboard.DefaultConfig, 115 } 116 117 // Load config file. 118 if file := ctx.GlobalString(configFileFlag.Name); file != "" { 119 if err := loadConfig(file, &cfg); err != nil { 120 utils.Fatalf("%v", err) 121 } 122 } 123 124 // Apply flags. 125 utils.SetNodeConfig(ctx, &cfg.Node) 126 stack, err := node.New(&cfg.Node) 127 if err != nil { 128 utils.Fatalf("Failed to create the protocol stack: %v", err) 129 } 130 utils.SetEthConfig(ctx, stack, &cfg.Eth) 131 if ctx.GlobalIsSet(utils.NetStatsURLFlag.Name) { 132 cfg.Netstats, err = netstats.ParseConfig(ctx.GlobalString(utils.NetStatsURLFlag.Name)) 133 if err != nil { 134 utils.Fatalf("Failed to parse netstats flag: %v", err) 135 } 136 } 137 138 utils.SetShhConfig(ctx, stack, &cfg.Shh) 139 utils.SetDashboardConfig(ctx, &cfg.Dashboard) 140 141 return stack, cfg 142 } 143 144 // enableWhisper returns true in case one of the whisper flags is set. 145 func enableWhisper(ctx *cli.Context) bool { 146 for _, flag := range whisperFlags { 147 if ctx.GlobalIsSet(flag.GetName()) { 148 return true 149 } 150 } 151 return false 152 } 153 154 func makeFullNode(ctx *cli.Context) *node.Node { 155 stack, cfg := makeConfigNode(ctx) 156 157 utils.RegisterEthService(context.TODO(), stack, &cfg.Eth) 158 159 if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) { 160 utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit) 161 } 162 // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode 163 shhEnabled := enableWhisper(ctx) 164 shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name) 165 if shhEnabled || shhAutoEnabled { 166 if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) { 167 cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name)) 168 } 169 if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) { 170 cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name) 171 } 172 utils.RegisterShhService(stack, &cfg.Shh) 173 } 174 175 // Add the GoChain Stats daemon if requested. 176 if cfg.Netstats.URL != "" { 177 utils.RegisterNetStatsService(stack, cfg.Netstats) 178 } 179 return stack 180 } 181 182 // dumpConfig is the dumpconfig command. 183 func dumpConfig(ctx *cli.Context) error { 184 _, cfg := makeConfigNode(ctx) 185 comment := "" 186 187 if cfg.Eth.Genesis != nil { 188 cfg.Eth.Genesis = nil 189 comment += "# Note: this config doesn't contain the genesis block.\n\n" 190 } 191 192 out, err := tomlSettings.Marshal(&cfg) 193 if err != nil { 194 return err 195 } 196 io.WriteString(os.Stdout, comment) 197 os.Stdout.Write(out) 198 return nil 199 }