github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/cmd/gwan/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  	"encoding/hex"
    22  	"errors"
    23  	"fmt"
    24  	"io"
    25  	"os"
    26  	"reflect"
    27  	"unicode"
    28  
    29  	cli "gopkg.in/urfave/cli.v1"
    30  
    31  	"github.com/naoina/toml"
    32  	"github.com/wanchain/go-wanchain/cmd/utils"
    33  	"github.com/wanchain/go-wanchain/contracts/release"
    34  	"github.com/wanchain/go-wanchain/eth"
    35  	"github.com/wanchain/go-wanchain/node"
    36  	"github.com/wanchain/go-wanchain/params"
    37  	whisper "github.com/wanchain/go-wanchain/whisper/whisperv5"
    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  }
    84  
    85  func loadConfig(file string, cfg *gethConfig) error {
    86  	f, err := os.Open(file)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	defer f.Close()
    91  
    92  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
    93  	// Add file name to errors that have a line number.
    94  	if _, ok := err.(*toml.LineError); ok {
    95  		err = errors.New(file + ", " + err.Error())
    96  	}
    97  	return err
    98  }
    99  
   100  func defaultNodeConfig() node.Config {
   101  	cfg := node.DefaultConfig
   102  	cfg.Name = clientIdentifier
   103  	cfg.Version = params.VersionWithCommit(gitCommit)
   104  	cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
   105  	cfg.HTTPModules = append(cfg.HTTPModules, "wan", "shh")
   106  	cfg.WSModules = append(cfg.WSModules, "eth", "shh")
   107  	cfg.WSModules = append(cfg.WSModules, "wan", "shh")
   108  	cfg.IPCPath = "gwan.ipc"
   109  	return cfg
   110  }
   111  
   112  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   113  	// Load defaults.
   114  	cfg := gethConfig{
   115  		Eth:  eth.DefaultConfig,
   116  		Shh:  whisper.DefaultConfig,
   117  		Node: defaultNodeConfig(),
   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  
   140  	return stack, cfg
   141  }
   142  
   143  // enableWhisper returns true in case one of the whisper flags is set.
   144  func enableWhisper(ctx *cli.Context) bool {
   145  	for _, flag := range whisperFlags {
   146  		if ctx.GlobalIsSet(flag.GetName()) {
   147  			return true
   148  		}
   149  	}
   150  	return false
   151  }
   152  
   153  func makeFullNode(ctx *cli.Context) *node.Node {
   154  	stack, cfg := makeConfigNode(ctx)
   155  
   156  	utils.RegisterEthService(stack, &cfg.Eth)
   157  
   158  	// Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
   159  	shhEnabled := enableWhisper(ctx)
   160  	shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
   161  	if shhEnabled || shhAutoEnabled {
   162  		if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
   163  			cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
   164  		}
   165  		if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
   166  			cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
   167  		}
   168  		utils.RegisterShhService(stack, &cfg.Shh)
   169  	}
   170  
   171  	// Add the Ethereum Stats daemon if requested.
   172  	if cfg.Ethstats.URL != "" {
   173  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   174  	}
   175  
   176  	// Add the release oracle service so it boots along with node.
   177  	if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
   178  		config := release.Config{
   179  			Oracle: relOracle,
   180  			Major:  uint32(params.VersionMajor),
   181  			Minor:  uint32(params.VersionMinor),
   182  			Patch:  uint32(params.VersionPatch),
   183  		}
   184  		commit, _ := hex.DecodeString(gitCommit)
   185  		copy(config.Commit[:], commit)
   186  		return release.NewReleaseService(ctx, config)
   187  	}); err != nil {
   188  		utils.Fatalf("Failed to register the Geth release oracle service: %v", err)
   189  	}
   190  	return stack
   191  }
   192  
   193  // dumpConfig is the dumpconfig command.
   194  func dumpConfig(ctx *cli.Context) error {
   195  	_, cfg := makeConfigNode(ctx)
   196  	comment := ""
   197  
   198  	if cfg.Eth.Genesis != nil {
   199  		cfg.Eth.Genesis = nil
   200  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   201  	}
   202  
   203  	out, err := tomlSettings.Marshal(&cfg)
   204  	if err != nil {
   205  		return err
   206  	}
   207  	io.WriteString(os.Stdout, comment)
   208  	os.Stdout.Write(out)
   209  	return nil
   210  }