github.com/humaniq/go-ethereum@v1.6.8-0.20171225131628-061223a13848/cmd/geth/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/ethereum/go-ethereum/cmd/utils"
    32  	"github.com/ethereum/go-ethereum/contracts/release"
    33  	"github.com/ethereum/go-ethereum/dashboard"
    34  	"github.com/ethereum/go-ethereum/eth"
    35  	"github.com/ethereum/go-ethereum/node"
    36  	"github.com/ethereum/go-ethereum/params"
    37  	whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
    38  	"github.com/naoina/toml"
    39  )
    40  
    41  var (
    42  	dumpConfigCommand = cli.Command{
    43  		Action:      utils.MigrateFlags(dumpConfig),
    44  		Name:        "dumpconfig",
    45  		Usage:       "Show configuration values",
    46  		ArgsUsage:   "",
    47  		Flags:       append(append(nodeFlags, rpcFlags...), whisperFlags...),
    48  		Category:    "MISCELLANEOUS COMMANDS",
    49  		Description: `The dumpconfig command shows configuration values.`,
    50  	}
    51  
    52  	configFileFlag = cli.StringFlag{
    53  		Name:  "config",
    54  		Usage: "TOML configuration file",
    55  	}
    56  )
    57  
    58  // These settings ensure that TOML keys use the same names as Go struct fields.
    59  var tomlSettings = toml.Config{
    60  	NormFieldName: func(rt reflect.Type, key string) string {
    61  		return key
    62  	},
    63  	FieldToKey: func(rt reflect.Type, field string) string {
    64  		return field
    65  	},
    66  	MissingField: func(rt reflect.Type, field string) error {
    67  		link := ""
    68  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    69  			link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
    70  		}
    71  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    72  	},
    73  }
    74  
    75  type ethstatsConfig struct {
    76  	URL string `toml:",omitempty"`
    77  }
    78  
    79  type gethConfig struct {
    80  	Eth       eth.Config
    81  	Shh       whisper.Config
    82  	Node      node.Config
    83  	Ethstats  ethstatsConfig
    84  	Dashboard dashboard.Config
    85  }
    86  
    87  func loadConfig(file string, cfg *gethConfig) error {
    88  	f, err := os.Open(file)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	defer f.Close()
    93  
    94  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
    95  	// Add file name to errors that have a line number.
    96  	if _, ok := err.(*toml.LineError); ok {
    97  		err = errors.New(file + ", " + err.Error())
    98  	}
    99  	return err
   100  }
   101  
   102  func defaultNodeConfig() node.Config {
   103  	cfg := node.DefaultConfig
   104  	cfg.Name = clientIdentifier
   105  	cfg.Version = params.VersionWithCommit(gitCommit)
   106  	cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
   107  	cfg.WSModules = append(cfg.WSModules, "eth", "shh")
   108  	cfg.IPCPath = "geth.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  		Dashboard: dashboard.DefaultConfig,
   119  	}
   120  
   121  	// Load config file.
   122  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   123  		if err := loadConfig(file, &cfg); err != nil {
   124  			utils.Fatalf("%v", err)
   125  		}
   126  	}
   127  
   128  	// Apply flags.
   129  	utils.SetNodeConfig(ctx, &cfg.Node)
   130  	stack, err := node.New(&cfg.Node)
   131  	if err != nil {
   132  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   133  	}
   134  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   135  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   136  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   137  	}
   138  
   139  	utils.SetShhConfig(ctx, stack, &cfg.Shh)
   140  	utils.SetDashboardConfig(ctx, &cfg.Dashboard)
   141  
   142  	return stack, cfg
   143  }
   144  
   145  // enableWhisper returns true in case one of the whisper flags is set.
   146  func enableWhisper(ctx *cli.Context) bool {
   147  	for _, flag := range whisperFlags {
   148  		if ctx.GlobalIsSet(flag.GetName()) {
   149  			return true
   150  		}
   151  	}
   152  	return false
   153  }
   154  
   155  func makeFullNode(ctx *cli.Context) *node.Node {
   156  	stack, cfg := makeConfigNode(ctx)
   157  
   158  	utils.RegisterEthService(stack, &cfg.Eth)
   159  
   160  	if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
   161  		utils.RegisterDashboardService(stack, &cfg.Dashboard)
   162  	}
   163  	// Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
   164  	shhEnabled := enableWhisper(ctx)
   165  	shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
   166  	if shhEnabled || shhAutoEnabled {
   167  		if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
   168  			cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
   169  		}
   170  		if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
   171  			cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
   172  		}
   173  		utils.RegisterShhService(stack, &cfg.Shh)
   174  	}
   175  
   176  	// Add the Ethereum Stats daemon if requested.
   177  	if cfg.Ethstats.URL != "" {
   178  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   179  	}
   180  
   181  	// Add the release oracle service so it boots along with node.
   182  	if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
   183  		config := release.Config{
   184  			Oracle: relOracle,
   185  			Major:  uint32(params.VersionMajor),
   186  			Minor:  uint32(params.VersionMinor),
   187  			Patch:  uint32(params.VersionPatch),
   188  		}
   189  		commit, _ := hex.DecodeString(gitCommit)
   190  		copy(config.Commit[:], commit)
   191  		return release.NewReleaseService(ctx, config)
   192  	}); err != nil {
   193  		utils.Fatalf("Failed to register the Geth release oracle service: %v", err)
   194  	}
   195  	return stack
   196  }
   197  
   198  // dumpConfig is the dumpconfig command.
   199  func dumpConfig(ctx *cli.Context) error {
   200  	_, cfg := makeConfigNode(ctx)
   201  	comment := ""
   202  
   203  	if cfg.Eth.Genesis != nil {
   204  		cfg.Eth.Genesis = nil
   205  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   206  	}
   207  
   208  	out, err := tomlSettings.Marshal(&cfg)
   209  	if err != nil {
   210  		return err
   211  	}
   212  	io.WriteString(os.Stdout, comment)
   213  	os.Stdout.Write(out)
   214  	return nil
   215  }