github.com/devkononov/go-func@v0.0.0-20190722084534-f14392d369a7/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  	"errors"
    22  	"fmt"
    23  	"os"
    24  	"reflect"
    25  	"unicode"
    26  
    27  	cli "gopkg.in/urfave/cli.v1"
    28  
    29  	"github.com/ethereum/go-ethereum/cmd/utils"
    30  	"github.com/ethereum/go-ethereum/dashboard"
    31  	"github.com/ethereum/go-ethereum/eth"
    32  	"github.com/ethereum/go-ethereum/node"
    33  	"github.com/ethereum/go-ethereum/params"
    34  	whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
    35  	"github.com/naoina/toml"
    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 ethstatsConfig struct {
    73  	URL string `toml:",omitempty"`
    74  }
    75  
    76  type gethConfig struct {
    77  	Eth       eth.Config
    78  	Shh       whisper.Config
    79  	Node      node.Config
    80  	Ethstats  ethstatsConfig
    81  	Dashboard dashboard.Config
    82  }
    83  
    84  func loadConfig(file string, cfg *gethConfig) error {
    85  	f, err := os.Open(file)
    86  	if err != nil {
    87  		return err
    88  	}
    89  	defer f.Close()
    90  
    91  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
    92  	// Add file name to errors that have a line number.
    93  	if _, ok := err.(*toml.LineError); ok {
    94  		err = errors.New(file + ", " + err.Error())
    95  	}
    96  	return err
    97  }
    98  
    99  func defaultNodeConfig() node.Config {
   100  	cfg := node.DefaultConfig
   101  	cfg.Name = clientIdentifier
   102  	cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
   103  	cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
   104  	cfg.WSModules = append(cfg.WSModules, "eth", "shh")
   105  	cfg.IPCPath = "geth.ipc"
   106  	return cfg
   107  }
   108  
   109  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   110  	// Load defaults.
   111  	cfg := gethConfig{
   112  		Eth:       eth.DefaultConfig,
   113  		Shh:       whisper.DefaultConfig,
   114  		Node:      defaultNodeConfig(),
   115  		Dashboard: dashboard.DefaultConfig,
   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  	utils.SetShhConfig(ctx, stack, &cfg.Shh)
   136  	utils.SetDashboardConfig(ctx, &cfg.Dashboard)
   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  	utils.RegisterEthService(stack, &cfg.Eth)
   154  
   155  	if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
   156  		utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
   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.DeveloperFlag.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  		if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
   169  			cfg.Shh.RestrictConnectionBetweenLightClients = true
   170  		}
   171  		utils.RegisterShhService(stack, &cfg.Shh)
   172  	}
   173  	// Configure GraphQL if requested
   174  	if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
   175  		utils.RegisterGraphQLService(stack, cfg.Node.GraphQLEndpoint(), cfg.Node.GraphQLCors, cfg.Node.GraphQLVirtualHosts, cfg.Node.HTTPTimeouts)
   176  	}
   177  	// Add the Ethereum Stats daemon if requested.
   178  	if cfg.Ethstats.URL != "" {
   179  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   180  	}
   181  	return stack
   182  }
   183  
   184  // dumpConfig is the dumpconfig command.
   185  func dumpConfig(ctx *cli.Context) error {
   186  	_, cfg := makeConfigNode(ctx)
   187  	comment := ""
   188  
   189  	if cfg.Eth.Genesis != nil {
   190  		cfg.Eth.Genesis = nil
   191  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   192  	}
   193  
   194  	out, err := tomlSettings.Marshal(&cfg)
   195  	if err != nil {
   196  		return err
   197  	}
   198  
   199  	dump := os.Stdout
   200  	if ctx.NArg() > 0 {
   201  		dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
   202  		if err != nil {
   203  			return err
   204  		}
   205  		defer dump.Close()
   206  	}
   207  	dump.WriteString(comment)
   208  	dump.Write(out)
   209  
   210  	return nil
   211  }