github.com/XinFinOrg/xdcchain@v1.1.0/cmd/XDC/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  	"bufio"
    21  	"errors"
    22  	"fmt"
    23  	"math/big"
    24  	"os"
    25  	"reflect"
    26  	"strings"
    27  	"unicode"
    28  
    29  	cli "gopkg.in/urfave/cli.v1"
    30  
    31  	"github.com/ethereum/go-ethereum/cmd/utils"
    32  	"github.com/ethereum/go-ethereum/common"
    33  	"github.com/ethereum/go-ethereum/dashboard"
    34  	"github.com/ethereum/go-ethereum/eth"
    35  	"github.com/ethereum/go-ethereum/internal/debug"
    36  	"github.com/ethereum/go-ethereum/log"
    37  	"github.com/ethereum/go-ethereum/node"
    38  	"github.com/ethereum/go-ethereum/params"
    39  	whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
    40  	"github.com/naoina/toml"
    41  )
    42  
    43  var (
    44  	dumpConfigCommand = cli.Command{
    45  		Action:      utils.MigrateFlags(dumpConfig),
    46  		Name:        "dumpconfig",
    47  		Usage:       "Show configuration values",
    48  		ArgsUsage:   "",
    49  		Flags:       append(append(nodeFlags, rpcFlags...), whisperFlags...),
    50  		Category:    "MISCELLANEOUS COMMANDS",
    51  		Description: `The dumpconfig command shows configuration values.`,
    52  	}
    53  
    54  	configFileFlag = cli.StringFlag{
    55  		Name:  "config",
    56  		Usage: "TOML configuration file",
    57  	}
    58  )
    59  
    60  // These settings ensure that TOML keys use the same names as Go struct fields.
    61  var tomlSettings = toml.Config{
    62  	NormFieldName: func(rt reflect.Type, key string) string {
    63  		return key
    64  	},
    65  	FieldToKey: func(rt reflect.Type, field string) string {
    66  		return field
    67  	},
    68  	MissingField: func(rt reflect.Type, field string) error {
    69  		link := ""
    70  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    71  			link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
    72  		}
    73  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    74  	},
    75  }
    76  
    77  type ethstatsConfig struct {
    78  	URL string
    79  }
    80  
    81  type account struct {
    82  	Unlocks   []string
    83  	Passwords []string
    84  }
    85  
    86  type Bootnodes struct {
    87  	Mainnet []string
    88  	Testnet []string
    89  }
    90  
    91  type XDCConfig struct {
    92  	Eth         eth.Config
    93  	Shh         whisper.Config
    94  	Node        node.Config
    95  	Ethstats    ethstatsConfig
    96  	Dashboard   dashboard.Config
    97  	Account     account
    98  	StakeEnable bool
    99  	Bootnodes   Bootnodes
   100  	Verbosity   int
   101  	NAT         string
   102  }
   103  
   104  func loadConfig(file string, cfg *XDCConfig) error {
   105  	f, err := os.Open(file)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	defer f.Close()
   110  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
   111  	// Add file name to errors that have a line number.
   112  	if _, ok := err.(*toml.LineError); ok {
   113  		err = errors.New(file + ", " + err.Error())
   114  	}
   115  	return err
   116  }
   117  
   118  func defaultNodeConfig() node.Config {
   119  	cfg := node.DefaultConfig
   120  	cfg.Name = clientIdentifier
   121  	cfg.Version = params.VersionWithCommit(gitCommit)
   122  	cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
   123  	cfg.WSModules = append(cfg.WSModules, "eth", "shh")
   124  	cfg.IPCPath = "XDC.ipc"
   125  	return cfg
   126  }
   127  
   128  func makeConfigNode(ctx *cli.Context) (*node.Node, XDCConfig) {
   129  	// Load defaults.
   130  	cfg := XDCConfig{
   131  		Eth:         eth.DefaultConfig,
   132  		Shh:         whisper.DefaultConfig,
   133  		Node:        defaultNodeConfig(),
   134  		Dashboard:   dashboard.DefaultConfig,
   135  		StakeEnable: true,
   136  		Verbosity:   3,
   137  		NAT:         "",
   138  	}
   139  	// Load config file.
   140  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   141  		if err := loadConfig(file, &cfg); err != nil {
   142  			utils.Fatalf("%v", err)
   143  		}
   144  	}
   145  	if ctx.GlobalIsSet(utils.StakingEnabledFlag.Name) {
   146  		cfg.StakeEnable = ctx.GlobalBool(utils.StakingEnabledFlag.Name)
   147  	}
   148  	if !ctx.GlobalIsSet(debug.VerbosityFlag.Name) {
   149  		debug.Glogger.Verbosity(log.Lvl(cfg.Verbosity))
   150  	}
   151  
   152  	if !ctx.GlobalIsSet(utils.NATFlag.Name) && cfg.NAT != "" {
   153  		ctx.Set(utils.NATFlag.Name, cfg.NAT)
   154  	}
   155  
   156  	// Check testnet is enable.
   157  	if ctx.GlobalBool(utils.XDCTestnetFlag.Name) {
   158  		common.IsTestnet = true
   159  	}
   160  
   161  	// Check rollback hash exist.
   162  	if rollbackHash := ctx.GlobalString(utils.RollbackFlag.Name); rollbackHash != "" {
   163  		common.RollbackHash = common.HexToHash(rollbackHash)
   164  	}
   165  
   166  	// Check GasPrice
   167  	common.MinGasPrice = common.DefaultMinGasPrice
   168  	if ctx.GlobalIsSet(utils.MinerGasPriceFlag.Name) {
   169  		if gasPrice := int64(ctx.GlobalInt(utils.MinerGasPriceFlag.Name)); gasPrice > common.DefaultMinGasPrice {
   170  			common.MinGasPrice = gasPrice
   171  		}
   172  	}
   173  
   174  	// read passwords from environment
   175  	passwords := []string{}
   176  	for _, env := range cfg.Account.Passwords {
   177  		if trimmed := strings.TrimSpace(env); trimmed != "" {
   178  			value := os.Getenv(trimmed)
   179  			for _, info := range strings.Split(value, ",") {
   180  				if trimmed2 := strings.TrimSpace(info); trimmed2 != "" {
   181  					passwords = append(passwords, trimmed2)
   182  				}
   183  			}
   184  		}
   185  	}
   186  	cfg.Account.Passwords = passwords
   187  
   188  	// Apply flags.
   189  	utils.SetNodeConfig(ctx, &cfg.Node)
   190  	stack, err := node.New(&cfg.Node)
   191  	if err != nil {
   192  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   193  	}
   194  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   195  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   196  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   197  	}
   198  
   199  	utils.SetShhConfig(ctx, stack, &cfg.Shh)
   200  	utils.SetDashboardConfig(ctx, &cfg.Dashboard)
   201  
   202  	return stack, cfg
   203  }
   204  
   205  func applyValues(values []string, params *[]string) {
   206  	data := []string{}
   207  	for _, value := range values {
   208  		if trimmed := strings.TrimSpace(value); trimmed != "" {
   209  			data = append(data, trimmed)
   210  		}
   211  	}
   212  	if len(data) > 0 {
   213  		*params = data
   214  	}
   215  
   216  }
   217  
   218  // enableWhisper returns true in case one of the whisper flags is set.
   219  func enableWhisper(ctx *cli.Context) bool {
   220  	for _, flag := range whisperFlags {
   221  		if ctx.GlobalIsSet(flag.GetName()) {
   222  			return true
   223  		}
   224  	}
   225  	return false
   226  }
   227  
   228  func makeFullNode(ctx *cli.Context) (*node.Node, XDCConfig) {
   229  	stack, cfg := makeConfigNode(ctx)
   230  	if ctx.GlobalIsSet(utils.ConstantinopleOverrideFlag.Name) {
   231  		cfg.Eth.ConstantinopleOverride = new(big.Int).SetUint64(ctx.GlobalUint64(utils.ConstantinopleOverrideFlag.Name))
   232  	}
   233  	utils.RegisterEthService(stack, &cfg.Eth)
   234  
   235  	if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
   236  		utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
   237  	}
   238  	// Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
   239  	shhEnabled := enableWhisper(ctx)
   240  	shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
   241  	if shhEnabled || shhAutoEnabled {
   242  		if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
   243  			cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
   244  		}
   245  		if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
   246  			cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
   247  		}
   248  		if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
   249  			cfg.Shh.RestrictConnectionBetweenLightClients = true
   250  		}
   251  		utils.RegisterShhService(stack, &cfg.Shh)
   252  	}
   253  
   254  	// Add the Ethereum Stats daemon if requested.
   255  	if cfg.Ethstats.URL != "" {
   256  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   257  	}
   258  	return stack, cfg
   259  }
   260  
   261  // dumpConfig is the dumpconfig command.
   262  func dumpConfig(ctx *cli.Context) error {
   263  	_, cfg := makeConfigNode(ctx)
   264  	comment := ""
   265  
   266  	if cfg.Eth.Genesis != nil {
   267  		cfg.Eth.Genesis = nil
   268  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   269  	}
   270  
   271  	out, err := tomlSettings.Marshal(&cfg)
   272  	if err != nil {
   273  		return err
   274  	}
   275  
   276  	dump := os.Stdout
   277  	if ctx.NArg() > 0 {
   278  		dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
   279  		if err != nil {
   280  			return err
   281  		}
   282  		defer dump.Close()
   283  	}
   284  	dump.WriteString(comment)
   285  	dump.Write(out)
   286  
   287  	return nil
   288  }