github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/cmd/ginb/config.go (about)

     1  // Copyright 2017 The inb-go Authors
     2  // This file is part of inb-go.
     3  //
     4  // inb-go 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  // inb-go 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 inb-go. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"bufio"
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  	"math/big"
    25  	"os"
    26  	"reflect"
    27  	"unicode"
    28  
    29  	cli "gopkg.in/urfave/cli.v1"
    30  
    31  	"github.com/insight-chain/inb-go/cmd/utils"
    32  	"github.com/insight-chain/inb-go/dashboard"
    33  	"github.com/insight-chain/inb-go/eth"
    34  	"github.com/insight-chain/inb-go/node"
    35  	"github.com/insight-chain/inb-go/params"
    36  	whisper "github.com/insight-chain/inb-go/whisper/whisperv6"
    37  	"github.com/naoina/toml"
    38  )
    39  
    40  var (
    41  	dumpConfigCommand = cli.Command{
    42  		Action:      utils.MigrateFlags(dumpConfig),
    43  		Name:        "dumpconfig",
    44  		Usage:       "Show configuration values",
    45  		ArgsUsage:   "",
    46  		Flags:       append(append(nodeFlags, rpcFlags...), whisperFlags...),
    47  		Category:    "MISCELLANEOUS COMMANDS",
    48  		Description: `The dumpconfig command shows configuration values.`,
    49  	}
    50  
    51  	configFileFlag = cli.StringFlag{
    52  		Name:  "config",
    53  		Usage: "TOML configuration file",
    54  	}
    55  )
    56  
    57  // These settings ensure that TOML keys use the same names as Go struct fields.
    58  var tomlSettings = toml.Config{
    59  	NormFieldName: func(rt reflect.Type, key string) string {
    60  		return key
    61  	},
    62  	FieldToKey: func(rt reflect.Type, field string) string {
    63  		return field
    64  	},
    65  	MissingField: func(rt reflect.Type, field string) error {
    66  		link := ""
    67  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    68  			link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
    69  		}
    70  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    71  	},
    72  }
    73  
    74  type ethstatsConfig struct {
    75  	URL string `toml:",omitempty"`
    76  }
    77  
    78  type gethConfig struct {
    79  	Eth       eth.Config
    80  	Shh       whisper.Config
    81  	Node      node.Config
    82  	Ethstats  ethstatsConfig
    83  	Dashboard dashboard.Config
    84  }
    85  
    86  func loadConfig(file string, cfg *gethConfig) error {
    87  	f, err := os.Open(file)
    88  	if err != nil {
    89  		return err
    90  	}
    91  	defer f.Close()
    92  
    93  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
    94  	// Add file name to errors that have a line number.
    95  	if _, ok := err.(*toml.LineError); ok {
    96  		err = errors.New(file + ", " + err.Error())
    97  	}
    98  	return err
    99  }
   100  
   101  func defaultNodeConfig() node.Config {
   102  	cfg := node.DefaultConfig
   103  	cfg.Name = clientIdentifier
   104  	cfg.Version = params.VersionWithCommit(gitCommit)
   105  	cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
   106  	cfg.WSModules = append(cfg.WSModules, "eth", "shh")
   107  	cfg.IPCPath = "ginb.ipc"
   108  	return cfg
   109  }
   110  
   111  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   112  	// Load defaults.
   113  	cfg := gethConfig{
   114  		Eth:       eth.DefaultConfig,
   115  		Shh:       whisper.DefaultConfig,
   116  		Node:      defaultNodeConfig(),
   117  		Dashboard: dashboard.DefaultConfig,
   118  	}
   119  
   120  	// Load config file.
   121  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   122  		if err := loadConfig(file, &cfg); err != nil {
   123  			utils.Fatalf("%v", err)
   124  		}
   125  	}
   126  
   127  	// Apply flags.
   128  	utils.SetNodeConfig(ctx, &cfg.Node)
   129  	stack, err := node.New(&cfg.Node)
   130  	if err != nil {
   131  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   132  	}
   133  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   134  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   135  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   136  	}
   137  
   138  	utils.SetShhConfig(ctx, stack, &cfg.Shh)
   139  	utils.SetDashboardConfig(ctx, &cfg.Dashboard)
   140  
   141  	return stack, cfg
   142  }
   143  
   144  // enableWhisper returns true in case one of the whisper flags is set.
   145  func enableWhisper(ctx *cli.Context) bool {
   146  	for _, flag := range whisperFlags {
   147  		if ctx.GlobalIsSet(flag.GetName()) {
   148  			return true
   149  		}
   150  	}
   151  	return false
   152  }
   153  
   154  func makeFullNode(ctx *cli.Context) *node.Node {
   155  	stack, cfg := makeConfigNode(ctx)
   156  	if ctx.GlobalIsSet(utils.ConstantinopleOverrideFlag.Name) {
   157  		cfg.Eth.ConstantinopleOverride = new(big.Int).SetUint64(ctx.GlobalUint64(utils.ConstantinopleOverrideFlag.Name))
   158  	}
   159  	utils.RegisterEthService(stack, &cfg.Eth)
   160  
   161  	if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
   162  		utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
   163  	}
   164  	// Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
   165  	shhEnabled := enableWhisper(ctx)
   166  	shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
   167  	if shhEnabled || shhAutoEnabled {
   168  		if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
   169  			cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
   170  		}
   171  		if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
   172  			cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
   173  		}
   174  		if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
   175  			cfg.Shh.RestrictConnectionBetweenLightClients = true
   176  		}
   177  		utils.RegisterShhService(stack, &cfg.Shh)
   178  	}
   179  
   180  	// Add the Ethereum Stats daemon if requested.
   181  	if cfg.Ethstats.URL != "" {
   182  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   183  	}
   184  	return stack
   185  }
   186  
   187  // dumpConfig is the dumpconfig command.
   188  func dumpConfig(ctx *cli.Context) error {
   189  	_, cfg := makeConfigNode(ctx)
   190  	comment := ""
   191  
   192  	if cfg.Eth.Genesis != nil {
   193  		cfg.Eth.Genesis = nil
   194  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   195  	}
   196  
   197  	out, err := tomlSettings.Marshal(&cfg)
   198  	if err != nil {
   199  		return err
   200  	}
   201  	io.WriteString(os.Stdout, comment)
   202  	os.Stdout.Write(out)
   203  	return nil
   204  }