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