github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/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/config"
    33  	"github.com/ethereum/go-ethereum/contracts/release"
    34  	"github.com/ethereum/go-ethereum/eth"
    35  	"github.com/ethereum/go-ethereum/node"
    36  	"github.com/ethereum/go-ethereum/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(nodeFlags, rpcFlags...),
    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  type gethConfig struct {
    79  	Eth      eth.Config
    80  	Node     node.Config
    81  	Ethstats ethstatsConfig
    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)
   103  	cfg.HTTPModules = append(cfg.HTTPModules, "eth")
   104  	cfg.WSModules = append(cfg.WSModules, "eth")
   105  	cfg.IPCPath = "geth.ipc"
   106  	return cfg
   107  }
   108  
   109  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   110  	config.Config.GetConfig("REAPCHAIN_ENV", "SETUP_INFO")
   111  
   112  	// Load defaults.
   113  	cfg := gethConfig{
   114  		Eth:  eth.DefaultConfig,
   115  		Node: defaultNodeConfig(),
   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) //add local ip flag
   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  
   136  	return stack, cfg
   137  }
   138  
   139  func makeFullNode(ctx *cli.Context) *node.Node {
   140  	stack, cfg := makeConfigNode(ctx)
   141  	//important
   142  	utils.RegisterEthService(stack, &cfg.Eth) //jump to PoDC or istanbul
   143  
   144  	// Whisper must be explicitly enabled, but is auto-enabled in --dev mode.
   145  	shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name)
   146  	shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
   147  	if shhEnabled || shhAutoEnabled {
   148  		utils.RegisterShhService(stack) // --dev option  //stack에 넣는 작업
   149  	}
   150  
   151  	// Add the Ethereum Stats daemon if requested.
   152  	if cfg.Ethstats.URL != "" {
   153  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   154  	}
   155  
   156  	// Add the release oracle service so it boots along with node.
   157  	if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
   158  		config := release.Config{
   159  			Oracle: relOracle,
   160  			Major:  uint32(params.VersionMajor),
   161  			Minor:  uint32(params.VersionMinor),
   162  			Patch:  uint32(params.VersionPatch),
   163  		}
   164  		commit, _ := hex.DecodeString(gitCommit)
   165  		copy(config.Commit[:], commit)
   166  		return release.NewReleaseService(ctx, config)
   167  	}); err != nil {
   168  		utils.Fatalf("Failed to register the Geth release oracle service: %v", err)
   169  	}
   170  	return stack
   171  }
   172  
   173  // dumpConfig is the dumpconfig command.
   174  func dumpConfig(ctx *cli.Context) error {
   175  	_, cfg := makeConfigNode(ctx)
   176  	comment := ""
   177  
   178  	if cfg.Eth.Genesis != nil {
   179  		cfg.Eth.Genesis = nil
   180  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   181  	}
   182  
   183  	out, err := tomlSettings.Marshal(&cfg)
   184  	if err != nil {
   185  		return err
   186  	}
   187  	io.WriteString(os.Stdout, comment)
   188  	os.Stdout.Write(out)
   189  	return nil
   190  }