github.com/apegame/aac@v1.9.7/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  	"math/big"
    24  	"os"
    25  	"reflect"
    26  	"unicode"
    27  
    28  	cli "gopkg.in/urfave/cli.v1"
    29  
    30  	"github.com/ethereum/go-ethereum/cmd/utils"
    31  	"github.com/ethereum/go-ethereum/dashboard"
    32  	"github.com/ethereum/go-ethereum/eth"
    33  	"github.com/ethereum/go-ethereum/node"
    34  	"github.com/ethereum/go-ethereum/params"
    35  	whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
    36  	"github.com/naoina/toml"
    37  )
    38  
    39  var (
    40  	dumpConfigCommand = cli.Command{
    41  		Action:      utils.MigrateFlags(dumpConfig),
    42  		Name:        "dumpconfig",
    43  		Usage:       "Show configuration values",
    44  		ArgsUsage:   "",
    45  		Flags:       append(append(nodeFlags, rpcFlags...), whisperFlags...),
    46  		Category:    "MISCELLANEOUS COMMANDS",
    47  		Description: `The dumpconfig command shows configuration values.`,
    48  	}
    49  
    50  	configFileFlag = cli.StringFlag{
    51  		Name:  "config",
    52  		Usage: "TOML configuration file",
    53  	}
    54  )
    55  
    56  // These settings ensure that TOML keys use the same names as Go struct fields.
    57  var tomlSettings = toml.Config{
    58  	NormFieldName: func(rt reflect.Type, key string) string {
    59  		return key
    60  	},
    61  	FieldToKey: func(rt reflect.Type, field string) string {
    62  		return field
    63  	},
    64  	MissingField: func(rt reflect.Type, field string) error {
    65  		link := ""
    66  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    67  			link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
    68  		}
    69  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    70  	},
    71  }
    72  
    73  type ethstatsConfig struct {
    74  	URL string `toml:",omitempty"`
    75  }
    76  
    77  type gethConfig struct {
    78  	Eth       eth.Config
    79  	Shh       whisper.Config
    80  	Node      node.Config
    81  	Ethstats  ethstatsConfig
    82  	Dashboard dashboard.Config
    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, gitDate)
   104  	cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
   105  	cfg.WSModules = append(cfg.WSModules, "eth", "shh")
   106  	cfg.IPCPath = "geth.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  		Dashboard: dashboard.DefaultConfig,
   117  	}
   118  
   119  	// Load config file.
   120  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   121  		if err := loadConfig(file, &cfg); err != nil {
   122  			utils.Fatalf("%v", err)
   123  		}
   124  	}
   125  
   126  	// Apply flags.
   127  	utils.SetNodeConfig(ctx, &cfg.Node)
   128  	stack, err := node.New(&cfg.Node)
   129  	if err != nil {
   130  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   131  	}
   132  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   133  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   134  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   135  	}
   136  	utils.SetShhConfig(ctx, stack, &cfg.Shh)
   137  	utils.SetDashboardConfig(ctx, &cfg.Dashboard)
   138  
   139  	return stack, cfg
   140  }
   141  
   142  // enableWhisper returns true in case one of the whisper flags is set.
   143  func enableWhisper(ctx *cli.Context) bool {
   144  	for _, flag := range whisperFlags {
   145  		if ctx.GlobalIsSet(flag.GetName()) {
   146  			return true
   147  		}
   148  	}
   149  	return false
   150  }
   151  
   152  func makeFullNode(ctx *cli.Context) *node.Node {
   153  	stack, cfg := makeConfigNode(ctx)
   154  	if ctx.GlobalIsSet(utils.OverrideIstanbulFlag.Name) {
   155  		cfg.Eth.OverrideIstanbul = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideIstanbulFlag.Name))
   156  	}
   157  	utils.RegisterEthService(stack, &cfg.Eth)
   158  
   159  	if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
   160  		utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
   161  	}
   162  	// Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
   163  	shhEnabled := enableWhisper(ctx)
   164  	shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
   165  	if shhEnabled || shhAutoEnabled {
   166  		if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
   167  			cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
   168  		}
   169  		if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
   170  			cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
   171  		}
   172  		if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
   173  			cfg.Shh.RestrictConnectionBetweenLightClients = true
   174  		}
   175  		utils.RegisterShhService(stack, &cfg.Shh)
   176  	}
   177  	// Configure GraphQL if requested
   178  	if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
   179  		utils.RegisterGraphQLService(stack, cfg.Node.GraphQLEndpoint(), cfg.Node.GraphQLCors, cfg.Node.GraphQLVirtualHosts, cfg.Node.HTTPTimeouts)
   180  	}
   181  	// Add the Ethereum Stats daemon if requested.
   182  	if cfg.Ethstats.URL != "" {
   183  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   184  	}
   185  	return stack
   186  }
   187  
   188  // dumpConfig is the dumpconfig command.
   189  func dumpConfig(ctx *cli.Context) error {
   190  	_, cfg := makeConfigNode(ctx)
   191  	comment := ""
   192  
   193  	if cfg.Eth.Genesis != nil {
   194  		cfg.Eth.Genesis = nil
   195  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   196  	}
   197  
   198  	out, err := tomlSettings.Marshal(&cfg)
   199  	if err != nil {
   200  		return err
   201  	}
   202  
   203  	dump := os.Stdout
   204  	if ctx.NArg() > 0 {
   205  		dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
   206  		if err != nil {
   207  			return err
   208  		}
   209  		defer dump.Close()
   210  	}
   211  	dump.WriteString(comment)
   212  	dump.Write(out)
   213  
   214  	return nil
   215  }