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