gitee.com/liu-zhao234568/cntest@v1.0.0/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  	"gopkg.in/urfave/cli.v1"
    29  
    30  	"gitee.com/liu-zhao234568/cntest/cmd/utils"
    31  	"gitee.com/liu-zhao234568/cntest/eth/catalyst"
    32  	"gitee.com/liu-zhao234568/cntest/eth/ethconfig"
    33  	"gitee.com/liu-zhao234568/cntest/internal/ethapi"
    34  	"gitee.com/liu-zhao234568/cntest/log"
    35  	"gitee.com/liu-zhao234568/cntest/metrics"
    36  	"gitee.com/liu-zhao234568/cntest/node"
    37  	"gitee.com/liu-zhao234568/cntest/params"
    38  	"github.com/naoina/toml"
    39  )
    40  
    41  var (
    42  	dumpConfigCommand = cli.Command{
    43  		Action:      utils.MigrateFlags(dumpConfig),
    44  		Name:        "dumpconfig",
    45  		Usage:       "Show configuration values",
    46  		ArgsUsage:   "",
    47  		Flags:       append(nodeFlags, rpcFlags...),
    48  		Category:    "MISCELLANEOUS COMMANDS",
    49  		Description: `The dumpconfig command shows configuration values.`,
    50  	}
    51  
    52  	configFileFlag = cli.StringFlag{
    53  		Name:  "config",
    54  		Usage: "TOML configuration file",
    55  	}
    56  )
    57  
    58  // These settings ensure that TOML keys use the same names as Go struct fields.
    59  var tomlSettings = toml.Config{
    60  	NormFieldName: func(rt reflect.Type, key string) string {
    61  		return key
    62  	},
    63  	FieldToKey: func(rt reflect.Type, field string) string {
    64  		return field
    65  	},
    66  	MissingField: func(rt reflect.Type, field string) error {
    67  		id := fmt.Sprintf("%s.%s", rt.String(), field)
    68  		if deprecated(id) {
    69  			log.Warn("Config field is deprecated and won't have an effect", "name", id)
    70  			return nil
    71  		}
    72  		var link string
    73  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    74  			link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
    75  		}
    76  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    77  	},
    78  }
    79  
    80  type ethstatsConfig struct {
    81  	URL string `toml:",omitempty"`
    82  }
    83  
    84  type gethConfig struct {
    85  	Eth      ethconfig.Config
    86  	Node     node.Config
    87  	Ethstats ethstatsConfig
    88  	Metrics  metrics.Config
    89  }
    90  
    91  func loadConfig(file string, cfg *gethConfig) error {
    92  	f, err := os.Open(file)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	defer f.Close()
    97  
    98  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
    99  	// Add file name to errors that have a line number.
   100  	if _, ok := err.(*toml.LineError); ok {
   101  		err = errors.New(file + ", " + err.Error())
   102  	}
   103  	return err
   104  }
   105  
   106  func defaultNodeConfig() node.Config {
   107  	cfg := node.DefaultConfig
   108  	cfg.Name = clientIdentifier
   109  	cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
   110  	cfg.HTTPModules = append(cfg.HTTPModules, "eth")
   111  	cfg.WSModules = append(cfg.WSModules, "eth")
   112  	cfg.IPCPath = "geth.ipc"
   113  	return cfg
   114  }
   115  
   116  // makeConfigNode loads geth configuration and creates a blank node instance.
   117  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   118  	// Load defaults.
   119  	cfg := gethConfig{
   120  		Eth:     ethconfig.Defaults,
   121  		Node:    defaultNodeConfig(),
   122  		Metrics: metrics.DefaultConfig,
   123  	}
   124  
   125  	// Load config file.
   126  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   127  		if err := loadConfig(file, &cfg); err != nil {
   128  			utils.Fatalf("%v", err)
   129  		}
   130  	}
   131  
   132  	// Apply flags.
   133  	utils.SetNodeConfig(ctx, &cfg.Node)
   134  	stack, err := node.New(&cfg.Node)
   135  	if err != nil {
   136  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   137  	}
   138  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   139  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   140  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   141  	}
   142  	applyMetricConfig(ctx, &cfg)
   143  
   144  	return stack, cfg
   145  }
   146  
   147  // makeFullNode loads geth configuration and creates the Ethereum backend.
   148  func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
   149  	stack, cfg := makeConfigNode(ctx)
   150  	if ctx.GlobalIsSet(utils.OverrideLondonFlag.Name) {
   151  		cfg.Eth.OverrideLondon = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideLondonFlag.Name))
   152  	}
   153  	backend, eth := utils.RegisterEthService(stack, &cfg.Eth)
   154  
   155  	// Configure catalyst.
   156  	if ctx.GlobalBool(utils.CatalystFlag.Name) {
   157  		if eth == nil {
   158  			utils.Fatalf("Catalyst does not work in light client mode.")
   159  		}
   160  		if err := catalyst.Register(stack, eth); err != nil {
   161  			utils.Fatalf("%v", err)
   162  		}
   163  	}
   164  
   165  	// Configure GraphQL if requested
   166  	if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
   167  		utils.RegisterGraphQLService(stack, backend, cfg.Node)
   168  	}
   169  	// Add the Ethereum Stats daemon if requested.
   170  	if cfg.Ethstats.URL != "" {
   171  		utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL)
   172  	}
   173  	return stack, backend
   174  }
   175  
   176  // dumpConfig is the dumpconfig command.
   177  func dumpConfig(ctx *cli.Context) error {
   178  	_, cfg := makeConfigNode(ctx)
   179  	comment := ""
   180  
   181  	if cfg.Eth.Genesis != nil {
   182  		cfg.Eth.Genesis = nil
   183  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   184  	}
   185  
   186  	out, err := tomlSettings.Marshal(&cfg)
   187  	if err != nil {
   188  		return err
   189  	}
   190  
   191  	dump := os.Stdout
   192  	if ctx.NArg() > 0 {
   193  		dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
   194  		if err != nil {
   195  			return err
   196  		}
   197  		defer dump.Close()
   198  	}
   199  	dump.WriteString(comment)
   200  	dump.Write(out)
   201  
   202  	return nil
   203  }
   204  
   205  func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) {
   206  	if ctx.GlobalIsSet(utils.MetricsEnabledFlag.Name) {
   207  		cfg.Metrics.Enabled = ctx.GlobalBool(utils.MetricsEnabledFlag.Name)
   208  	}
   209  	if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) {
   210  		cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name)
   211  	}
   212  	if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) {
   213  		cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name)
   214  	}
   215  	if ctx.GlobalIsSet(utils.MetricsPortFlag.Name) {
   216  		cfg.Metrics.Port = ctx.GlobalInt(utils.MetricsPortFlag.Name)
   217  	}
   218  	if ctx.GlobalIsSet(utils.MetricsEnableInfluxDBFlag.Name) {
   219  		cfg.Metrics.EnableInfluxDB = ctx.GlobalBool(utils.MetricsEnableInfluxDBFlag.Name)
   220  	}
   221  	if ctx.GlobalIsSet(utils.MetricsInfluxDBEndpointFlag.Name) {
   222  		cfg.Metrics.InfluxDBEndpoint = ctx.GlobalString(utils.MetricsInfluxDBEndpointFlag.Name)
   223  	}
   224  	if ctx.GlobalIsSet(utils.MetricsInfluxDBDatabaseFlag.Name) {
   225  		cfg.Metrics.InfluxDBDatabase = ctx.GlobalString(utils.MetricsInfluxDBDatabaseFlag.Name)
   226  	}
   227  	if ctx.GlobalIsSet(utils.MetricsInfluxDBUsernameFlag.Name) {
   228  		cfg.Metrics.InfluxDBUsername = ctx.GlobalString(utils.MetricsInfluxDBUsernameFlag.Name)
   229  	}
   230  	if ctx.GlobalIsSet(utils.MetricsInfluxDBPasswordFlag.Name) {
   231  		cfg.Metrics.InfluxDBPassword = ctx.GlobalString(utils.MetricsInfluxDBPasswordFlag.Name)
   232  	}
   233  	if ctx.GlobalIsSet(utils.MetricsInfluxDBTagsFlag.Name) {
   234  		cfg.Metrics.InfluxDBTags = ctx.GlobalString(utils.MetricsInfluxDBTagsFlag.Name)
   235  	}
   236  }
   237  
   238  func deprecated(field string) bool {
   239  	switch field {
   240  	case "ethconfig.Config.EVMInterpreter":
   241  		return true
   242  	case "ethconfig.Config.EWASMInterpreter":
   243  		return true
   244  	default:
   245  		return false
   246  	}
   247  }