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