github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/geth/config.go (about)

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