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