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