github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/cmd/swarm/config.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:33</date>
    10  //</624450070510571520>
    11  
    12  
    13  package main
    14  
    15  import (
    16  	"errors"
    17  	"fmt"
    18  	"io"
    19  	"os"
    20  	"reflect"
    21  	"strconv"
    22  	"strings"
    23  	"time"
    24  	"unicode"
    25  
    26  	cli "gopkg.in/urfave/cli.v1"
    27  
    28  	"github.com/ethereum/go-ethereum/cmd/utils"
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/log"
    31  	"github.com/ethereum/go-ethereum/node"
    32  	"github.com/naoina/toml"
    33  
    34  	bzzapi "github.com/ethereum/go-ethereum/swarm/api"
    35  )
    36  
    37  var (
    38  //
    39  	DumpConfigCommand = cli.Command{
    40  		Action:      utils.MigrateFlags(dumpConfig),
    41  		Name:        "dumpconfig",
    42  		Usage:       "Show configuration values",
    43  		ArgsUsage:   "",
    44  		Flags:       app.Flags,
    45  		Category:    "MISCELLANEOUS COMMANDS",
    46  		Description: `The dumpconfig command shows configuration values.`,
    47  	}
    48  
    49  //
    50  	SwarmTomlConfigPathFlag = cli.StringFlag{
    51  		Name:  "config",
    52  		Usage: "TOML configuration file",
    53  	}
    54  )
    55  
    56  //
    57  const (
    58  	SWARM_ENV_CHEQUEBOOK_ADDR         = "SWARM_CHEQUEBOOK_ADDR"
    59  	SWARM_ENV_ACCOUNT                 = "SWARM_ACCOUNT"
    60  	SWARM_ENV_LISTEN_ADDR             = "SWARM_LISTEN_ADDR"
    61  	SWARM_ENV_PORT                    = "SWARM_PORT"
    62  	SWARM_ENV_NETWORK_ID              = "SWARM_NETWORK_ID"
    63  	SWARM_ENV_SWAP_ENABLE             = "SWARM_SWAP_ENABLE"
    64  	SWARM_ENV_SWAP_API                = "SWARM_SWAP_API"
    65  	SWARM_ENV_SYNC_DISABLE            = "SWARM_SYNC_DISABLE"
    66  	SWARM_ENV_SYNC_UPDATE_DELAY       = "SWARM_ENV_SYNC_UPDATE_DELAY"
    67  	SWARM_ENV_MAX_STREAM_PEER_SERVERS = "SWARM_ENV_MAX_STREAM_PEER_SERVERS"
    68  	SWARM_ENV_LIGHT_NODE_ENABLE       = "SWARM_LIGHT_NODE_ENABLE"
    69  	SWARM_ENV_DELIVERY_SKIP_CHECK     = "SWARM_DELIVERY_SKIP_CHECK"
    70  	SWARM_ENV_ENS_API                 = "SWARM_ENS_API"
    71  	SWARM_ENV_ENS_ADDR                = "SWARM_ENS_ADDR"
    72  	SWARM_ENV_CORS                    = "SWARM_CORS"
    73  	SWARM_ENV_BOOTNODES               = "SWARM_BOOTNODES"
    74  	SWARM_ENV_PSS_ENABLE              = "SWARM_PSS_ENABLE"
    75  	SWARM_ENV_STORE_PATH              = "SWARM_STORE_PATH"
    76  	SWARM_ENV_STORE_CAPACITY          = "SWARM_STORE_CAPACITY"
    77  	SWARM_ENV_STORE_CACHE_CAPACITY    = "SWARM_STORE_CACHE_CAPACITY"
    78  	SWARM_ACCESS_PASSWORD             = "SWARM_ACCESS_PASSWORD"
    79  	SWARM_AUTO_DEFAULTPATH            = "SWARM_AUTO_DEFAULTPATH"
    80  	GETH_ENV_DATADIR                  = "GETH_DATADIR"
    81  )
    82  
    83  //这些设置确保toml键使用与go struct字段相同的名称。
    84  var tomlSettings = toml.Config{
    85  	NormFieldName: func(rt reflect.Type, key string) string {
    86  		return key
    87  	},
    88  	FieldToKey: func(rt reflect.Type, field string) string {
    89  		return field
    90  	},
    91  	MissingField: func(rt reflect.Type, field string) error {
    92  		link := ""
    93  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    94  			link = fmt.Sprintf(", check github.com/ethereum/go-ethereum/swarm/api/config.go for available fields")
    95  		}
    96  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    97  	},
    98  }
    99  
   100  //在启动群节点之前,构建配置
   101  func buildConfig(ctx *cli.Context) (config *bzzapi.Config, err error) {
   102  //首先创建默认配置
   103  	config = bzzapi.NewConfig()
   104  //
   105  	config, err = configFileOverride(config, ctx)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  //覆盖环境变量提供的设置
   110  	config = envVarsOverride(config)
   111  //覆盖命令行提供的设置
   112  	config = cmdLineOverride(config, ctx)
   113  //
   114  	err = validateConfig(config)
   115  
   116  	return
   117  }
   118  
   119  //最后,在配置构建阶段完成后,初始化
   120  func initSwarmNode(config *bzzapi.Config, stack *node.Node, ctx *cli.Context) {
   121  //此时,应在配置中设置所有变量。
   122  //
   123  	prvkey := getAccount(config.BzzAccount, ctx, stack)
   124  //
   125  	config.Path = expandPath(stack.InstanceDir())
   126  //
   127  	config.Init(prvkey)
   128  //
   129  	log.Debug("Starting Swarm with the following parameters:")
   130  //创建配置后,将其打印到屏幕
   131  	log.Debug(printConfig(config))
   132  }
   133  
   134  //如果提供了配置文件,configfileoverride将用配置文件覆盖当前配置。
   135  func configFileOverride(config *bzzapi.Config, ctx *cli.Context) (*bzzapi.Config, error) {
   136  	var err error
   137  
   138  //
   139  	if ctx.GlobalIsSet(SwarmTomlConfigPathFlag.Name) {
   140  		var filepath string
   141  		if filepath = ctx.GlobalString(SwarmTomlConfigPathFlag.Name); filepath == "" {
   142  			utils.Fatalf("Config file flag provided with invalid file path")
   143  		}
   144  		var f *os.File
   145  		f, err = os.Open(filepath)
   146  		if err != nil {
   147  			return nil, err
   148  		}
   149  		defer f.Close()
   150  
   151  //
   152  //
   153  //如果文件中没有条目,则保留默认条目。
   154  		err = tomlSettings.NewDecoder(f).Decode(&config)
   155  //将文件名添加到具有行号的错误中。
   156  		if _, ok := err.(*toml.LineError); ok {
   157  			err = errors.New(filepath + ", " + err.Error())
   158  		}
   159  	}
   160  	return config, err
   161  }
   162  
   163  //
   164  //
   165  func cmdLineOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Config {
   166  
   167  	if keyid := ctx.GlobalString(SwarmAccountFlag.Name); keyid != "" {
   168  		currentConfig.BzzAccount = keyid
   169  	}
   170  
   171  	if chbookaddr := ctx.GlobalString(ChequebookAddrFlag.Name); chbookaddr != "" {
   172  		currentConfig.Contract = common.HexToAddress(chbookaddr)
   173  	}
   174  
   175  	if networkid := ctx.GlobalString(SwarmNetworkIdFlag.Name); networkid != "" {
   176  		id, err := strconv.ParseUint(networkid, 10, 64)
   177  		if err != nil {
   178  			utils.Fatalf("invalid cli flag %s: %v", SwarmNetworkIdFlag.Name, err)
   179  		}
   180  		if id != 0 {
   181  			currentConfig.NetworkID = id
   182  		}
   183  	}
   184  
   185  	if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
   186  		if datadir := ctx.GlobalString(utils.DataDirFlag.Name); datadir != "" {
   187  			currentConfig.Path = expandPath(datadir)
   188  		}
   189  	}
   190  
   191  	bzzport := ctx.GlobalString(SwarmPortFlag.Name)
   192  	if len(bzzport) > 0 {
   193  		currentConfig.Port = bzzport
   194  	}
   195  
   196  	if bzzaddr := ctx.GlobalString(SwarmListenAddrFlag.Name); bzzaddr != "" {
   197  		currentConfig.ListenAddr = bzzaddr
   198  	}
   199  
   200  	if ctx.GlobalIsSet(SwarmSwapEnabledFlag.Name) {
   201  		currentConfig.SwapEnabled = true
   202  	}
   203  
   204  	if ctx.GlobalIsSet(SwarmSyncDisabledFlag.Name) {
   205  		currentConfig.SyncEnabled = false
   206  	}
   207  
   208  	if d := ctx.GlobalDuration(SwarmSyncUpdateDelay.Name); d > 0 {
   209  		currentConfig.SyncUpdateDelay = d
   210  	}
   211  
   212  //包括0在内的任何值都可以接受
   213  	currentConfig.MaxStreamPeerServers = ctx.GlobalInt(SwarmMaxStreamPeerServersFlag.Name)
   214  
   215  	if ctx.GlobalIsSet(SwarmLightNodeEnabled.Name) {
   216  		currentConfig.LightNodeEnabled = true
   217  	}
   218  
   219  	if ctx.GlobalIsSet(SwarmDeliverySkipCheckFlag.Name) {
   220  		currentConfig.DeliverySkipCheck = true
   221  	}
   222  
   223  	currentConfig.SwapAPI = ctx.GlobalString(SwarmSwapAPIFlag.Name)
   224  	if currentConfig.SwapEnabled && currentConfig.SwapAPI == "" {
   225  		utils.Fatalf(SWARM_ERR_SWAP_SET_NO_API)
   226  	}
   227  
   228  	if ctx.GlobalIsSet(EnsAPIFlag.Name) {
   229  		ensAPIs := ctx.GlobalStringSlice(EnsAPIFlag.Name)
   230  //保留向后兼容性以禁用ens with--ens api=“”
   231  		if len(ensAPIs) == 1 && ensAPIs[0] == "" {
   232  			ensAPIs = nil
   233  		}
   234  		for i := range ensAPIs {
   235  			ensAPIs[i] = expandPath(ensAPIs[i])
   236  		}
   237  
   238  		currentConfig.EnsAPIs = ensAPIs
   239  	}
   240  
   241  	if cors := ctx.GlobalString(CorsStringFlag.Name); cors != "" {
   242  		currentConfig.Cors = cors
   243  	}
   244  
   245  	if storePath := ctx.GlobalString(SwarmStorePath.Name); storePath != "" {
   246  		currentConfig.LocalStoreParams.ChunkDbPath = storePath
   247  	}
   248  
   249  	if storeCapacity := ctx.GlobalUint64(SwarmStoreCapacity.Name); storeCapacity != 0 {
   250  		currentConfig.LocalStoreParams.DbCapacity = storeCapacity
   251  	}
   252  
   253  	if storeCacheCapacity := ctx.GlobalUint(SwarmStoreCacheCapacity.Name); storeCacheCapacity != 0 {
   254  		currentConfig.LocalStoreParams.CacheCapacity = storeCacheCapacity
   255  	}
   256  
   257  	return currentConfig
   258  
   259  }
   260  
   261  //
   262  //
   263  func envVarsOverride(currentConfig *bzzapi.Config) (config *bzzapi.Config) {
   264  
   265  	if keyid := os.Getenv(SWARM_ENV_ACCOUNT); keyid != "" {
   266  		currentConfig.BzzAccount = keyid
   267  	}
   268  
   269  	if chbookaddr := os.Getenv(SWARM_ENV_CHEQUEBOOK_ADDR); chbookaddr != "" {
   270  		currentConfig.Contract = common.HexToAddress(chbookaddr)
   271  	}
   272  
   273  	if networkid := os.Getenv(SWARM_ENV_NETWORK_ID); networkid != "" {
   274  		id, err := strconv.ParseUint(networkid, 10, 64)
   275  		if err != nil {
   276  			utils.Fatalf("invalid environment variable %s: %v", SWARM_ENV_NETWORK_ID, err)
   277  		}
   278  		if id != 0 {
   279  			currentConfig.NetworkID = id
   280  		}
   281  	}
   282  
   283  	if datadir := os.Getenv(GETH_ENV_DATADIR); datadir != "" {
   284  		currentConfig.Path = expandPath(datadir)
   285  	}
   286  
   287  	bzzport := os.Getenv(SWARM_ENV_PORT)
   288  	if len(bzzport) > 0 {
   289  		currentConfig.Port = bzzport
   290  	}
   291  
   292  	if bzzaddr := os.Getenv(SWARM_ENV_LISTEN_ADDR); bzzaddr != "" {
   293  		currentConfig.ListenAddr = bzzaddr
   294  	}
   295  
   296  	if swapenable := os.Getenv(SWARM_ENV_SWAP_ENABLE); swapenable != "" {
   297  		swap, err := strconv.ParseBool(swapenable)
   298  		if err != nil {
   299  			utils.Fatalf("invalid environment variable %s: %v", SWARM_ENV_SWAP_ENABLE, err)
   300  		}
   301  		currentConfig.SwapEnabled = swap
   302  	}
   303  
   304  	if syncdisable := os.Getenv(SWARM_ENV_SYNC_DISABLE); syncdisable != "" {
   305  		sync, err := strconv.ParseBool(syncdisable)
   306  		if err != nil {
   307  			utils.Fatalf("invalid environment variable %s: %v", SWARM_ENV_SYNC_DISABLE, err)
   308  		}
   309  		currentConfig.SyncEnabled = !sync
   310  	}
   311  
   312  	if v := os.Getenv(SWARM_ENV_DELIVERY_SKIP_CHECK); v != "" {
   313  		skipCheck, err := strconv.ParseBool(v)
   314  		if err != nil {
   315  			currentConfig.DeliverySkipCheck = skipCheck
   316  		}
   317  	}
   318  
   319  	if v := os.Getenv(SWARM_ENV_SYNC_UPDATE_DELAY); v != "" {
   320  		d, err := time.ParseDuration(v)
   321  		if err != nil {
   322  			utils.Fatalf("invalid environment variable %s: %v", SWARM_ENV_SYNC_UPDATE_DELAY, err)
   323  		}
   324  		currentConfig.SyncUpdateDelay = d
   325  	}
   326  
   327  	if max := os.Getenv(SWARM_ENV_MAX_STREAM_PEER_SERVERS); max != "" {
   328  		m, err := strconv.Atoi(max)
   329  		if err != nil {
   330  			utils.Fatalf("invalid environment variable %s: %v", SWARM_ENV_MAX_STREAM_PEER_SERVERS, err)
   331  		}
   332  		currentConfig.MaxStreamPeerServers = m
   333  	}
   334  
   335  	if lne := os.Getenv(SWARM_ENV_LIGHT_NODE_ENABLE); lne != "" {
   336  		lightnode, err := strconv.ParseBool(lne)
   337  		if err != nil {
   338  			utils.Fatalf("invalid environment variable %s: %v", SWARM_ENV_LIGHT_NODE_ENABLE, err)
   339  		}
   340  		currentConfig.LightNodeEnabled = lightnode
   341  	}
   342  
   343  	if swapapi := os.Getenv(SWARM_ENV_SWAP_API); swapapi != "" {
   344  		currentConfig.SwapAPI = swapapi
   345  	}
   346  
   347  	if currentConfig.SwapEnabled && currentConfig.SwapAPI == "" {
   348  		utils.Fatalf(SWARM_ERR_SWAP_SET_NO_API)
   349  	}
   350  
   351  	if ensapi := os.Getenv(SWARM_ENV_ENS_API); ensapi != "" {
   352  		currentConfig.EnsAPIs = strings.Split(ensapi, ",")
   353  	}
   354  
   355  	if ensaddr := os.Getenv(SWARM_ENV_ENS_ADDR); ensaddr != "" {
   356  		currentConfig.EnsRoot = common.HexToAddress(ensaddr)
   357  	}
   358  
   359  	if cors := os.Getenv(SWARM_ENV_CORS); cors != "" {
   360  		currentConfig.Cors = cors
   361  	}
   362  
   363  	return currentConfig
   364  }
   365  
   366  //dumpconfig是dumpconfig命令。
   367  //将默认配置写入stdout
   368  func dumpConfig(ctx *cli.Context) error {
   369  	cfg, err := buildConfig(ctx)
   370  	if err != nil {
   371  		utils.Fatalf(fmt.Sprintf("Uh oh - dumpconfig triggered an error %v", err))
   372  	}
   373  	comment := ""
   374  	out, err := tomlSettings.Marshal(&cfg)
   375  	if err != nil {
   376  		return err
   377  	}
   378  	io.WriteString(os.Stdout, comment)
   379  	os.Stdout.Write(out)
   380  	return nil
   381  }
   382  
   383  //验证配置参数
   384  func validateConfig(cfg *bzzapi.Config) (err error) {
   385  	for _, ensAPI := range cfg.EnsAPIs {
   386  		if ensAPI != "" {
   387  			if err := validateEnsAPIs(ensAPI); err != nil {
   388  				return fmt.Errorf("invalid format [tld:][contract-addr@]url for ENS API endpoint configuration %q: %v", ensAPI, err)
   389  			}
   390  		}
   391  	}
   392  	return nil
   393  }
   394  
   395  //验证ENSAPI配置参数
   396  func validateEnsAPIs(s string) (err error) {
   397  //
   398  	if strings.HasPrefix(s, "@") {
   399  		return errors.New("missing contract address")
   400  	}
   401  //丢失网址
   402  	if strings.HasSuffix(s, "@") {
   403  		return errors.New("missing url")
   404  	}
   405  //漏译
   406  	if strings.HasPrefix(s, ":") {
   407  		return errors.New("missing tld")
   408  	}
   409  //丢失网址
   410  	if strings.HasSuffix(s, ":") {
   411  		return errors.New("missing url")
   412  	}
   413  	return nil
   414  }
   415  
   416  //将配置打印为字符串
   417  func printConfig(config *bzzapi.Config) string {
   418  	out, err := tomlSettings.Marshal(&config)
   419  	if err != nil {
   420  		return fmt.Sprintf("Something is not right with the configuration: %v", err)
   421  	}
   422  	return string(out)
   423  }
   424