github.com/etherbanking/go-etherbanking@v1.7.1-0.20181009210156-cf649bca5aba/cmd/gebc/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/etherbanking/go-etherbanking/cmd/utils"
    32  	"github.com/etherbanking/go-etherbanking/contracts/release"
    33  	"github.com/etherbanking/go-etherbanking/eth"
    34  	"github.com/etherbanking/go-etherbanking/node"
    35  	"github.com/etherbanking/go-etherbanking/params"
    36  	whisper "github.com/etherbanking/go-etherbanking/whisper/whisperv5"
    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  }
    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.WSModules = append(cfg.WSModules, "eth", "shh")
   106  	cfg.IPCPath = "gebc.ipc"
   107  	return cfg
   108  }
   109  
   110  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   111  	// Load defaults.
   112  	cfg := gethConfig{
   113  		Eth:  eth.DefaultConfig,
   114  		Shh:  whisper.DefaultConfig,
   115  		Node: defaultNodeConfig(),
   116  	}
   117  
   118  	// Load config file.
   119  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   120  		if err := loadConfig(file, &cfg); err != nil {
   121  			utils.Fatalf("%v", err)
   122  		}
   123  	}
   124  
   125  	// Apply flags.
   126  	utils.SetNodeConfig(ctx, &cfg.Node)
   127  	stack, err := node.New(&cfg.Node)
   128  	if err != nil {
   129  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   130  	}
   131  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   132  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   133  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   134  	}
   135  
   136  	utils.SetShhConfig(ctx, stack, &cfg.Shh)
   137  
   138  	return stack, cfg
   139  }
   140  
   141  // enableWhisper returns true in case one of the whisper flags is set.
   142  func enableWhisper(ctx *cli.Context) bool {
   143  	for _, flag := range whisperFlags {
   144  		if ctx.GlobalIsSet(flag.GetName()) {
   145  			return true
   146  		}
   147  	}
   148  	return false
   149  }
   150  
   151  func makeFullNode(ctx *cli.Context) *node.Node {
   152  	stack, cfg := makeConfigNode(ctx)
   153  
   154  	utils.RegisterEthService(stack, &cfg.Eth)
   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.DevModeFlag.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 Ethereum Stats daemon if requested.
   170  	if cfg.Ethstats.URL != "" {
   171  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   172  	}
   173  
   174  	// Add the release oracle service so it boots along with node.
   175  	if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
   176  		config := release.Config{
   177  			Oracle: relOracle,
   178  			Major:  uint32(params.VersionMajor),
   179  			Minor:  uint32(params.VersionMinor),
   180  			Patch:  uint32(params.VersionPatch),
   181  		}
   182  		commit, _ := hex.DecodeString(gitCommit)
   183  		copy(config.Commit[:], commit)
   184  		return release.NewReleaseService(ctx, config)
   185  	}); err != nil {
   186  		utils.Fatalf("Failed to register the Gebc release oracle service: %v", err)
   187  	}
   188  	return stack
   189  }
   190  
   191  // dumpConfig is the dumpconfig command.
   192  func dumpConfig(ctx *cli.Context) error {
   193  	_, cfg := makeConfigNode(ctx)
   194  	comment := ""
   195  
   196  	if cfg.Eth.Genesis != nil {
   197  		cfg.Eth.Genesis = nil
   198  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   199  	}
   200  
   201  	out, err := tomlSettings.Marshal(&cfg)
   202  	if err != nil {
   203  		return err
   204  	}
   205  	io.WriteString(os.Stdout, comment)
   206  	os.Stdout.Write(out)
   207  	return nil
   208  }