github.com/snowblossomcoin/go-ethereum@v1.9.25/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/eth"
    31  	"github.com/ethereum/go-ethereum/internal/ethapi"
    32  	"github.com/ethereum/go-ethereum/log"
    33  	"github.com/ethereum/go-ethereum/node"
    34  	"github.com/ethereum/go-ethereum/params"
    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  // whisper has been deprecated, but clients out there might still have [Shh]
    77  // in their config, which will crash. Cut them some slack by keeping the
    78  // config, and displaying a message that those config switches are ineffectual.
    79  // To be removed circa Q1 2021 -- @gballet.
    80  type whisperDeprecatedConfig struct {
    81  	MaxMessageSize                        uint32  `toml:",omitempty"`
    82  	MinimumAcceptedPOW                    float64 `toml:",omitempty"`
    83  	RestrictConnectionBetweenLightClients bool    `toml:",omitempty"`
    84  }
    85  
    86  type gethConfig struct {
    87  	Eth      eth.Config
    88  	Shh      whisperDeprecatedConfig
    89  	Node     node.Config
    90  	Ethstats ethstatsConfig
    91  }
    92  
    93  func loadConfig(file string, cfg *gethConfig) error {
    94  	f, err := os.Open(file)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	defer f.Close()
    99  
   100  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
   101  	// Add file name to errors that have a line number.
   102  	if _, ok := err.(*toml.LineError); ok {
   103  		err = errors.New(file + ", " + err.Error())
   104  	}
   105  	return err
   106  }
   107  
   108  func defaultNodeConfig() node.Config {
   109  	cfg := node.DefaultConfig
   110  	cfg.Name = clientIdentifier
   111  	cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
   112  	cfg.HTTPModules = append(cfg.HTTPModules, "eth")
   113  	cfg.WSModules = append(cfg.WSModules, "eth")
   114  	cfg.IPCPath = "geth.ipc"
   115  	return cfg
   116  }
   117  
   118  // makeConfigNode loads geth configuration and creates a blank node instance.
   119  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   120  	// Load defaults.
   121  	cfg := gethConfig{
   122  		Eth:  eth.DefaultConfig,
   123  		Node: defaultNodeConfig(),
   124  	}
   125  
   126  	// Load config file.
   127  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   128  		if err := loadConfig(file, &cfg); err != nil {
   129  			utils.Fatalf("%v", err)
   130  		}
   131  
   132  		if cfg.Shh != (whisperDeprecatedConfig{}) {
   133  			log.Warn("Deprecated whisper config detected. Whisper has been moved to github.com/ethereum/whisper")
   134  		}
   135  	}
   136  
   137  	// Apply flags.
   138  	utils.SetNodeConfig(ctx, &cfg.Node)
   139  	stack, err := node.New(&cfg.Node)
   140  	if err != nil {
   141  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   142  	}
   143  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   144  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   145  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   146  	}
   147  	utils.SetShhConfig(ctx, stack)
   148  
   149  	return stack, cfg
   150  }
   151  
   152  // enableWhisper returns true in case one of the whisper flags is set.
   153  func checkWhisper(ctx *cli.Context) {
   154  	for _, flag := range whisperFlags {
   155  		if ctx.GlobalIsSet(flag.GetName()) {
   156  			log.Warn("deprecated whisper flag detected. Whisper has been moved to github.com/ethereum/whisper")
   157  		}
   158  	}
   159  }
   160  
   161  // makeFullNode loads geth configuration and creates the Ethereum backend.
   162  func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
   163  	stack, cfg := makeConfigNode(ctx)
   164  
   165  	backend := utils.RegisterEthService(stack, &cfg.Eth)
   166  
   167  	checkWhisper(ctx)
   168  	// Configure GraphQL if requested
   169  	if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
   170  		utils.RegisterGraphQLService(stack, backend, cfg.Node)
   171  	}
   172  	// Add the Ethereum Stats daemon if requested.
   173  	if cfg.Ethstats.URL != "" {
   174  		utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL)
   175  	}
   176  	return stack, backend
   177  }
   178  
   179  // dumpConfig is the dumpconfig command.
   180  func dumpConfig(ctx *cli.Context) error {
   181  	_, cfg := makeConfigNode(ctx)
   182  	comment := ""
   183  
   184  	if cfg.Eth.Genesis != nil {
   185  		cfg.Eth.Genesis = nil
   186  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   187  	}
   188  
   189  	out, err := tomlSettings.Marshal(&cfg)
   190  	if err != nil {
   191  		return err
   192  	}
   193  
   194  	dump := os.Stdout
   195  	if ctx.NArg() > 0 {
   196  		dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
   197  		if err != nil {
   198  			return err
   199  		}
   200  		defer dump.Close()
   201  	}
   202  	dump.WriteString(comment)
   203  	dump.Write(out)
   204  
   205  	return nil
   206  }