github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/go-ethereum-master/cmd/swarm/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 "errors" 21 "fmt" 22 "io" 23 "os" 24 "reflect" 25 "strconv" 26 "strings" 27 "time" 28 "unicode" 29 30 cli "gopkg.in/urfave/cli.v1" 31 32 "github.com/ethereum/go-ethereum/cmd/utils" 33 "github.com/ethereum/go-ethereum/common" 34 "github.com/ethereum/go-ethereum/log" 35 "github.com/ethereum/go-ethereum/node" 36 "github.com/naoina/toml" 37 38 bzzapi "github.com/ethereum/go-ethereum/swarm/api" 39 ) 40 41 const SWARM_VERSION = "0.3" 42 43 var ( 44 //flag definition for the dumpconfig command 45 DumpConfigCommand = cli.Command{ 46 Action: utils.MigrateFlags(dumpConfig), 47 Name: "dumpconfig", 48 Usage: "Show configuration values", 49 ArgsUsage: "", 50 Flags: app.Flags, 51 Category: "MISCELLANEOUS COMMANDS", 52 Description: `The dumpconfig command shows configuration values.`, 53 } 54 55 //flag definition for the config file command 56 SwarmTomlConfigPathFlag = cli.StringFlag{ 57 Name: "config", 58 Usage: "TOML configuration file", 59 } 60 ) 61 62 //constants for environment variables 63 const ( 64 SWARM_ENV_CHEQUEBOOK_ADDR = "SWARM_CHEQUEBOOK_ADDR" 65 SWARM_ENV_ACCOUNT = "SWARM_ACCOUNT" 66 SWARM_ENV_LISTEN_ADDR = "SWARM_LISTEN_ADDR" 67 SWARM_ENV_PORT = "SWARM_PORT" 68 SWARM_ENV_NETWORK_ID = "SWARM_NETWORK_ID" 69 SWARM_ENV_SWAP_ENABLE = "SWARM_SWAP_ENABLE" 70 SWARM_ENV_SWAP_API = "SWARM_SWAP_API" 71 SWARM_ENV_SYNC_DISABLE = "SWARM_SYNC_DISABLE" 72 SWARM_ENV_SYNC_UPDATE_DELAY = "SWARM_ENV_SYNC_UPDATE_DELAY" 73 SWARM_ENV_DELIVERY_SKIP_CHECK = "SWARM_DELIVERY_SKIP_CHECK" 74 SWARM_ENV_ENS_API = "SWARM_ENS_API" 75 SWARM_ENV_ENS_ADDR = "SWARM_ENS_ADDR" 76 SWARM_ENV_CORS = "SWARM_CORS" 77 SWARM_ENV_BOOTNODES = "SWARM_BOOTNODES" 78 SWARM_ENV_PSS_ENABLE = "SWARM_PSS_ENABLE" 79 SWARM_ENV_STORE_PATH = "SWARM_STORE_PATH" 80 SWARM_ENV_STORE_CAPACITY = "SWARM_STORE_CAPACITY" 81 SWARM_ENV_STORE_CACHE_CAPACITY = "SWARM_STORE_CACHE_CAPACITY" 82 GETH_ENV_DATADIR = "GETH_DATADIR" 83 ) 84 85 // These settings ensure that TOML keys use the same names as Go struct fields. 86 var tomlSettings = toml.Config{ 87 NormFieldName: func(rt reflect.Type, key string) string { 88 return key 89 }, 90 FieldToKey: func(rt reflect.Type, field string) string { 91 return field 92 }, 93 MissingField: func(rt reflect.Type, field string) error { 94 link := "" 95 if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { 96 link = fmt.Sprintf(", check github.com/ethereum/go-ethereum/swarm/api/config.go for available fields") 97 } 98 return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) 99 }, 100 } 101 102 //before booting the swarm node, build the configuration 103 func buildConfig(ctx *cli.Context) (config *bzzapi.Config, err error) { 104 //start by creating a default config 105 config = bzzapi.NewConfig() 106 //first load settings from config file (if provided) 107 config, err = configFileOverride(config, ctx) 108 if err != nil { 109 return nil, err 110 } 111 //override settings provided by environment variables 112 config = envVarsOverride(config) 113 //override settings provided by command line 114 config = cmdLineOverride(config, ctx) 115 //validate configuration parameters 116 err = validateConfig(config) 117 118 return 119 } 120 121 //finally, after the configuration build phase is finished, initialize 122 func initSwarmNode(config *bzzapi.Config, stack *node.Node, ctx *cli.Context) { 123 //at this point, all vars should be set in the Config 124 //get the account for the provided swarm account 125 prvkey := getAccount(config.BzzAccount, ctx, stack) 126 //set the resolved config path (geth --datadir) 127 config.Path = stack.InstanceDir() 128 //finally, initialize the configuration 129 config.Init(prvkey) 130 //configuration phase completed here 131 log.Debug("Starting Swarm with the following parameters:") 132 //after having created the config, print it to screen 133 log.Debug(printConfig(config)) 134 } 135 136 //override the current config with whatever is in the config file, if a config file has been provided 137 func configFileOverride(config *bzzapi.Config, ctx *cli.Context) (*bzzapi.Config, error) { 138 var err error 139 140 //only do something if the -config flag has been set 141 if ctx.GlobalIsSet(SwarmTomlConfigPathFlag.Name) { 142 var filepath string 143 if filepath = ctx.GlobalString(SwarmTomlConfigPathFlag.Name); filepath == "" { 144 utils.Fatalf("Config file flag provided with invalid file path") 145 } 146 f, err := os.Open(filepath) 147 if err != nil { 148 return nil, err 149 } 150 defer f.Close() 151 152 //decode the TOML file into a Config struct 153 //note that we are decoding into the existing defaultConfig; 154 //if an entry is not present in the file, the default entry is kept 155 err = tomlSettings.NewDecoder(f).Decode(&config) 156 // Add file name to errors that have a line number. 157 if _, ok := err.(*toml.LineError); ok { 158 err = errors.New(filepath + ", " + err.Error()) 159 } 160 } 161 return config, err 162 } 163 164 //override the current config with whatever is provided through the command line 165 //most values are not allowed a zero value (empty string), if not otherwise noted 166 func cmdLineOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Config { 167 168 if keyid := ctx.GlobalString(SwarmAccountFlag.Name); keyid != "" { 169 currentConfig.BzzAccount = keyid 170 } 171 172 if chbookaddr := ctx.GlobalString(ChequebookAddrFlag.Name); chbookaddr != "" { 173 currentConfig.Contract = common.HexToAddress(chbookaddr) 174 } 175 176 if networkid := ctx.GlobalString(SwarmNetworkIdFlag.Name); networkid != "" { 177 if id, _ := strconv.Atoi(networkid); id != 0 { 178 currentConfig.NetworkID = uint64(id) 179 } 180 } 181 182 if ctx.GlobalIsSet(utils.DataDirFlag.Name) { 183 if datadir := ctx.GlobalString(utils.DataDirFlag.Name); datadir != "" { 184 currentConfig.Path = datadir 185 } 186 } 187 188 bzzport := ctx.GlobalString(SwarmPortFlag.Name) 189 if len(bzzport) > 0 { 190 currentConfig.Port = bzzport 191 } 192 193 if bzzaddr := ctx.GlobalString(SwarmListenAddrFlag.Name); bzzaddr != "" { 194 currentConfig.ListenAddr = bzzaddr 195 } 196 197 if ctx.GlobalIsSet(SwarmSwapEnabledFlag.Name) { 198 currentConfig.SwapEnabled = true 199 } 200 201 if ctx.GlobalIsSet(SwarmSyncDisabledFlag.Name) { 202 currentConfig.SyncEnabled = false 203 } 204 205 if d := ctx.GlobalDuration(SwarmSyncUpdateDelay.Name); d > 0 { 206 currentConfig.SyncUpdateDelay = d 207 } 208 209 if ctx.GlobalIsSet(SwarmDeliverySkipCheckFlag.Name) { 210 currentConfig.DeliverySkipCheck = true 211 } 212 213 currentConfig.SwapAPI = ctx.GlobalString(SwarmSwapAPIFlag.Name) 214 if currentConfig.SwapEnabled && currentConfig.SwapAPI == "" { 215 utils.Fatalf(SWARM_ERR_SWAP_SET_NO_API) 216 } 217 218 if ctx.GlobalIsSet(EnsAPIFlag.Name) { 219 ensAPIs := ctx.GlobalStringSlice(EnsAPIFlag.Name) 220 // preserve backward compatibility to disable ENS with --ens-api="" 221 if len(ensAPIs) == 1 && ensAPIs[0] == "" { 222 ensAPIs = nil 223 } 224 currentConfig.EnsAPIs = ensAPIs 225 } 226 227 if cors := ctx.GlobalString(CorsStringFlag.Name); cors != "" { 228 currentConfig.Cors = cors 229 } 230 231 if ctx.GlobalIsSet(utils.BootnodesFlag.Name) { 232 currentConfig.BootNodes = ctx.GlobalString(utils.BootnodesFlag.Name) 233 } 234 235 if storePath := ctx.GlobalString(SwarmStorePath.Name); storePath != "" { 236 currentConfig.LocalStoreParams.ChunkDbPath = storePath 237 } 238 239 if storeCapacity := ctx.GlobalUint64(SwarmStoreCapacity.Name); storeCapacity != 0 { 240 currentConfig.LocalStoreParams.DbCapacity = storeCapacity 241 } 242 243 if storeCacheCapacity := ctx.GlobalUint(SwarmStoreCacheCapacity.Name); storeCacheCapacity != 0 { 244 currentConfig.LocalStoreParams.CacheCapacity = storeCacheCapacity 245 } 246 247 return currentConfig 248 249 } 250 251 //override the current config with whatver is provided in environment variables 252 //most values are not allowed a zero value (empty string), if not otherwise noted 253 func envVarsOverride(currentConfig *bzzapi.Config) (config *bzzapi.Config) { 254 255 if keyid := os.Getenv(SWARM_ENV_ACCOUNT); keyid != "" { 256 currentConfig.BzzAccount = keyid 257 } 258 259 if chbookaddr := os.Getenv(SWARM_ENV_CHEQUEBOOK_ADDR); chbookaddr != "" { 260 currentConfig.Contract = common.HexToAddress(chbookaddr) 261 } 262 263 if networkid := os.Getenv(SWARM_ENV_NETWORK_ID); networkid != "" { 264 if id, _ := strconv.Atoi(networkid); id != 0 { 265 currentConfig.NetworkID = uint64(id) 266 } 267 } 268 269 if datadir := os.Getenv(GETH_ENV_DATADIR); datadir != "" { 270 currentConfig.Path = datadir 271 } 272 273 bzzport := os.Getenv(SWARM_ENV_PORT) 274 if len(bzzport) > 0 { 275 currentConfig.Port = bzzport 276 } 277 278 if bzzaddr := os.Getenv(SWARM_ENV_LISTEN_ADDR); bzzaddr != "" { 279 currentConfig.ListenAddr = bzzaddr 280 } 281 282 if swapenable := os.Getenv(SWARM_ENV_SWAP_ENABLE); swapenable != "" { 283 if swap, err := strconv.ParseBool(swapenable); err != nil { 284 currentConfig.SwapEnabled = swap 285 } 286 } 287 288 if syncdisable := os.Getenv(SWARM_ENV_SYNC_DISABLE); syncdisable != "" { 289 if sync, err := strconv.ParseBool(syncdisable); err != nil { 290 currentConfig.SyncEnabled = !sync 291 } 292 } 293 294 if v := os.Getenv(SWARM_ENV_DELIVERY_SKIP_CHECK); v != "" { 295 if skipCheck, err := strconv.ParseBool(v); err != nil { 296 currentConfig.DeliverySkipCheck = skipCheck 297 } 298 } 299 300 if v := os.Getenv(SWARM_ENV_SYNC_UPDATE_DELAY); v != "" { 301 if d, err := time.ParseDuration(v); err != nil { 302 currentConfig.SyncUpdateDelay = d 303 } 304 } 305 306 if swapapi := os.Getenv(SWARM_ENV_SWAP_API); swapapi != "" { 307 currentConfig.SwapAPI = swapapi 308 } 309 310 if currentConfig.SwapEnabled && currentConfig.SwapAPI == "" { 311 utils.Fatalf(SWARM_ERR_SWAP_SET_NO_API) 312 } 313 314 if ensapi := os.Getenv(SWARM_ENV_ENS_API); ensapi != "" { 315 currentConfig.EnsAPIs = strings.Split(ensapi, ",") 316 } 317 318 if ensaddr := os.Getenv(SWARM_ENV_ENS_ADDR); ensaddr != "" { 319 currentConfig.EnsRoot = common.HexToAddress(ensaddr) 320 } 321 322 if cors := os.Getenv(SWARM_ENV_CORS); cors != "" { 323 currentConfig.Cors = cors 324 } 325 326 if bootnodes := os.Getenv(SWARM_ENV_BOOTNODES); bootnodes != "" { 327 currentConfig.BootNodes = bootnodes 328 } 329 330 return currentConfig 331 } 332 333 // dumpConfig is the dumpconfig command. 334 // writes a default config to STDOUT 335 func dumpConfig(ctx *cli.Context) error { 336 cfg, err := buildConfig(ctx) 337 if err != nil { 338 utils.Fatalf(fmt.Sprintf("Uh oh - dumpconfig triggered an error %v", err)) 339 } 340 comment := "" 341 out, err := tomlSettings.Marshal(&cfg) 342 if err != nil { 343 return err 344 } 345 io.WriteString(os.Stdout, comment) 346 os.Stdout.Write(out) 347 return nil 348 } 349 350 //validate configuration parameters 351 func validateConfig(cfg *bzzapi.Config) (err error) { 352 for _, ensAPI := range cfg.EnsAPIs { 353 if ensAPI != "" { 354 if err := validateEnsAPIs(ensAPI); err != nil { 355 return fmt.Errorf("invalid format [tld:][contract-addr@]url for ENS API endpoint configuration %q: %v", ensAPI, err) 356 } 357 } 358 } 359 return nil 360 } 361 362 //validate EnsAPIs configuration parameter 363 func validateEnsAPIs(s string) (err error) { 364 // missing contract address 365 if strings.HasPrefix(s, "@") { 366 return errors.New("missing contract address") 367 } 368 // missing url 369 if strings.HasSuffix(s, "@") { 370 return errors.New("missing url") 371 } 372 // missing tld 373 if strings.HasPrefix(s, ":") { 374 return errors.New("missing tld") 375 } 376 // missing url 377 if strings.HasSuffix(s, ":") { 378 return errors.New("missing url") 379 } 380 return nil 381 } 382 383 //print a Config as string 384 func printConfig(config *bzzapi.Config) string { 385 out, err := tomlSettings.Marshal(&config) 386 if err != nil { 387 return fmt.Sprintf("Something is not right with the configuration: %v", err) 388 } 389 return string(out) 390 }