github.com/alanchchen/go-ethereum@v1.6.6-0.20170601190819-6171d01b1195/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 "encoding/hex" 22 "errors" 23 "fmt" 24 "io" 25 "os" 26 "reflect" 27 "unicode" 28 29 cli "gopkg.in/urfave/cli.v1" 30 31 "github.com/ethereum/go-ethereum/cmd/utils" 32 "github.com/ethereum/go-ethereum/contracts/release" 33 "github.com/ethereum/go-ethereum/eth" 34 "github.com/ethereum/go-ethereum/node" 35 "github.com/ethereum/go-ethereum/params" 36 "github.com/naoina/toml" 37 ) 38 39 var ( 40 dumpConfigCommand = cli.Command{ 41 Action: utils.MigrateFlags(dumpConfig), 42 Name: "dumpconfig", 43 Usage: "Show configuration values", 44 ArgsUsage: "", 45 Flags: append(nodeFlags, rpcFlags...), 46 Category: "MISCELLANEOUS COMMANDS", 47 Description: `The dumpconfig command shows configuration values.`, 48 } 49 50 configFileFlag = cli.StringFlag{ 51 Name: "config", 52 Usage: "TOML configuration file", 53 } 54 ) 55 56 // These settings ensure that TOML keys use the same names as Go struct fields. 57 var tomlSettings = toml.Config{ 58 NormFieldName: func(rt reflect.Type, key string) string { 59 return key 60 }, 61 FieldToKey: func(rt reflect.Type, field string) string { 62 return field 63 }, 64 MissingField: func(rt reflect.Type, field string) error { 65 link := "" 66 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 67 link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name()) 68 } 69 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 70 }, 71 } 72 73 type ethstatsConfig struct { 74 URL string `toml:",omitempty"` 75 } 76 77 type gethConfig struct { 78 Eth eth.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") 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 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.EthStatsURLFlag.Name) { 130 cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name) 131 } 132 133 return stack, cfg 134 } 135 136 func makeFullNode(ctx *cli.Context) *node.Node { 137 stack, cfg := makeConfigNode(ctx) 138 139 utils.RegisterEthService(stack, &cfg.Eth) 140 141 // Whisper must be explicitly enabled, but is auto-enabled in --dev mode. 142 shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name) 143 shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name) 144 if shhEnabled || shhAutoEnabled { 145 utils.RegisterShhService(stack) 146 } 147 148 // Add the Ethereum Stats daemon if requested. 149 if cfg.Ethstats.URL != "" { 150 utils.RegisterEthStatsService(stack, cfg.Ethstats.URL) 151 } 152 153 // Add the release oracle service so it boots along with node. 154 if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { 155 config := release.Config{ 156 Oracle: relOracle, 157 Major: uint32(params.VersionMajor), 158 Minor: uint32(params.VersionMinor), 159 Patch: uint32(params.VersionPatch), 160 } 161 commit, _ := hex.DecodeString(gitCommit) 162 copy(config.Commit[:], commit) 163 return release.NewReleaseService(ctx, config) 164 }); err != nil { 165 utils.Fatalf("Failed to register the Geth release oracle service: %v", err) 166 } 167 return stack 168 } 169 170 // dumpConfig is the dumpconfig command. 171 func dumpConfig(ctx *cli.Context) error { 172 _, cfg := makeConfigNode(ctx) 173 comment := "" 174 175 if cfg.Eth.Genesis != nil { 176 cfg.Eth.Genesis = nil 177 comment += "# Note: this config doesn't contain the genesis block.\n\n" 178 } 179 180 out, err := tomlSettings.Marshal(&cfg) 181 if err != nil { 182 return err 183 } 184 io.WriteString(os.Stdout, comment) 185 os.Stdout.Write(out) 186 return nil 187 }