github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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  	"github.com/kisexp/xdchain/cmd/utils"
    28  	"github.com/kisexp/xdchain/common/http"
    29  	"github.com/kisexp/xdchain/eth"
    30  	"github.com/kisexp/xdchain/extension/privacyExtension"
    31  	"github.com/kisexp/xdchain/internal/ethapi"
    32  	"github.com/kisexp/xdchain/log"
    33  	"github.com/kisexp/xdchain/node"
    34  	"github.com/kisexp/xdchain/params"
    35  	"github.com/kisexp/xdchain/private"
    36  	"github.com/kisexp/xdchain/private/engine"
    37  	"github.com/naoina/toml"
    38  	"gopkg.in/urfave/cli.v1"
    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(append(nodeFlags, rpcFlags...), whisperFlags...),
    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  		link := ""
    68  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    69  			link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
    70  		}
    71  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    72  	},
    73  }
    74  
    75  type ethstatsConfig struct {
    76  	URL string `toml:",omitempty"`
    77  }
    78  
    79  // whisper has been deprecated, but clients out there might still have [Shh]
    80  // in their config, which will crash. Cut them some slack by keeping the
    81  // config, and displaying a message that those config switches are ineffectual.
    82  // To be removed circa Q1 2021 -- @gballet.
    83  type whisperDeprecatedConfig struct {
    84  	MaxMessageSize                        uint32  `toml:",omitempty"`
    85  	MinimumAcceptedPOW                    float64 `toml:",omitempty"`
    86  	RestrictConnectionBetweenLightClients bool    `toml:",omitempty"`
    87  }
    88  
    89  type gethConfig struct {
    90  	Eth      eth.Config
    91  	Shh      whisperDeprecatedConfig
    92  	Node     node.Config
    93  	Ethstats ethstatsConfig
    94  }
    95  
    96  func loadConfig(file string, cfg *gethConfig) error {
    97  	f, err := os.Open(file)
    98  	if err != nil {
    99  		return err
   100  	}
   101  	defer f.Close()
   102  
   103  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
   104  	// Add file name to errors that have a line number.
   105  	if _, ok := err.(*toml.LineError); ok {
   106  		err = errors.New(file + ", " + err.Error())
   107  	}
   108  	return err
   109  }
   110  
   111  func defaultNodeConfig() node.Config {
   112  	cfg := node.DefaultConfig
   113  	cfg.Name = clientIdentifier
   114  	cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
   115  	cfg.HTTPModules = append(cfg.HTTPModules, "eth")
   116  	cfg.WSModules = append(cfg.WSModules, "eth")
   117  	cfg.IPCPath = "geth.ipc"
   118  	return cfg
   119  }
   120  
   121  // makeConfigNode loads geth configuration and creates a blank node instance.
   122  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   123  	// Quorum: Must occur before setQuorumConfig, as it needs an initialised PTM to be enabled
   124  	// 		   Extension Service and Multitenancy feature validation also depend on PTM availability
   125  	if err := quorumInitialisePrivacy(ctx); err != nil {
   126  		utils.Fatalf("Error initialising Private Transaction Manager: %s", err.Error())
   127  	}
   128  
   129  	// Load defaults.
   130  	cfg := gethConfig{
   131  		Eth:  eth.DefaultConfig,
   132  		Node: defaultNodeConfig(),
   133  	}
   134  
   135  	// Load config file.
   136  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   137  		if err := loadConfig(file, &cfg); err != nil {
   138  			utils.Fatalf("%v", err)
   139  		}
   140  
   141  		if cfg.Shh != (whisperDeprecatedConfig{}) {
   142  			log.Warn("Deprecated whisper config detected. Whisper has been moved to github.com/ethereum/whisper")
   143  		}
   144  	}
   145  
   146  	// Apply flags.
   147  	utils.SetNodeConfig(ctx, &cfg.Node)
   148  	stack, err := node.New(&cfg.Node)
   149  	if err != nil {
   150  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   151  	}
   152  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   153  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   154  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   155  	}
   156  	utils.SetShhConfig(ctx, stack)
   157  
   158  	return stack, cfg
   159  }
   160  
   161  // enableWhisper returns true in case one of the whisper flags is set.
   162  func checkWhisper(ctx *cli.Context) {
   163  	for _, flag := range whisperFlags {
   164  		if ctx.GlobalIsSet(flag.GetName()) {
   165  			log.Warn("deprecated whisper flag detected. Whisper has been moved to github.com/ethereum/whisper")
   166  		}
   167  	}
   168  }
   169  
   170  // makeFullNode loads geth configuration and creates the Ethereum backend.
   171  func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
   172  	stack, cfg := makeConfigNode(ctx)
   173  
   174  	//Must occur before registering the extension service, as it needs an initialised PTM to be enabled
   175  	if err := quorumInitialisePrivacy(ctx); err != nil {
   176  		utils.Fatalf("Error initialising Private Transaction Manager: %s", err.Error())
   177  	}
   178  
   179  	// Quorum - returning `ethService` too for the Raft and extension service
   180  	backend, ethService := utils.RegisterEthService(stack, &cfg.Eth)
   181  
   182  	// Quorum
   183  	// plugin service must be after eth service so that eth service will be stopped gradually if any of the plugin
   184  	// fails to start
   185  	if cfg.Node.Plugins != nil {
   186  		utils.RegisterPluginService(stack, &cfg.Node, ctx.Bool(utils.PluginSkipVerifyFlag.Name), ctx.Bool(utils.PluginLocalVerifyFlag.Name), ctx.String(utils.PluginPublicKeyFlag.Name))
   187  	}
   188  
   189  	if cfg.Node.IsPermissionEnabled() {
   190  		utils.RegisterPermissionService(stack, ctx.Bool(utils.RaftDNSEnabledFlag.Name))
   191  	}
   192  
   193  	if ctx.GlobalBool(utils.RaftModeFlag.Name) {
   194  		utils.RegisterRaftService(stack, ctx, &cfg.Node, ethService)
   195  	}
   196  
   197  	if private.IsQuorumPrivacyEnabled() {
   198  		utils.RegisterExtensionService(stack, ethService)
   199  	}
   200  	// End Quorum
   201  
   202  	checkWhisper(ctx)
   203  	// Configure GraphQL if requested
   204  	if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
   205  		utils.RegisterGraphQLService(stack, backend, cfg.Node)
   206  	}
   207  	// Add the Ethereum Stats daemon if requested.
   208  	if cfg.Ethstats.URL != "" {
   209  		utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL)
   210  	}
   211  	return stack, backend
   212  }
   213  
   214  // dumpConfig is the dumpconfig command.
   215  func dumpConfig(ctx *cli.Context) error {
   216  	_, cfg := makeConfigNode(ctx)
   217  	comment := ""
   218  
   219  	if cfg.Eth.Genesis != nil {
   220  		cfg.Eth.Genesis = nil
   221  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   222  	}
   223  
   224  	out, err := tomlSettings.Marshal(&cfg)
   225  	if err != nil {
   226  		return err
   227  	}
   228  
   229  	dump := os.Stdout
   230  	if ctx.NArg() > 0 {
   231  		dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
   232  		if err != nil {
   233  			return err
   234  		}
   235  		defer dump.Close()
   236  	}
   237  	dump.WriteString(comment)
   238  	dump.Write(out)
   239  
   240  	return nil
   241  }
   242  
   243  // quorumValidateEthService checks quorum features that depend on the ethereum service
   244  func quorumValidateEthService(stack *node.Node, isRaft bool) {
   245  	var ethereum *eth.Ethereum
   246  
   247  	err := stack.Lifecycle(&ethereum)
   248  	if err != nil {
   249  		utils.Fatalf("Error retrieving Ethereum service: %v", err)
   250  	}
   251  
   252  	quorumValidateConsensus(ethereum, isRaft)
   253  
   254  	quorumValidatePrivacyEnhancements(ethereum)
   255  }
   256  
   257  // quorumValidateConsensus checks if a consensus was used. The node is killed if consensus was not used
   258  func quorumValidateConsensus(ethereum *eth.Ethereum, isRaft bool) {
   259  	if !isRaft && ethereum.BlockChain().Config().Istanbul == nil && ethereum.BlockChain().Config().Clique == nil {
   260  		utils.Fatalf("Consensus not specified. Exiting!!")
   261  	}
   262  }
   263  
   264  // quorumValidatePrivacyEnhancements checks if privacy enhancements are configured the transaction manager supports
   265  // the PrivacyEnhancements feature
   266  func quorumValidatePrivacyEnhancements(ethereum *eth.Ethereum) {
   267  	privacyEnhancementsBlock := ethereum.BlockChain().Config().PrivacyEnhancementsBlock
   268  	if privacyEnhancementsBlock != nil {
   269  		log.Info("Privacy enhancements is configured to be enabled from block ", "height", privacyEnhancementsBlock)
   270  		if !private.P.HasFeature(engine.PrivacyEnhancements) {
   271  			utils.Fatalf("Cannot start quorum with privacy enhancements enabled while the transaction manager does not support it")
   272  		}
   273  	}
   274  }
   275  
   276  // configure and set up quorum transaction privacy
   277  func quorumInitialisePrivacy(ctx *cli.Context) error {
   278  	cfg, err := QuorumSetupPrivacyConfiguration(ctx)
   279  	if err != nil {
   280  		return err
   281  	}
   282  	err = private.InitialiseConnection(cfg)
   283  	if err != nil {
   284  		return err
   285  	}
   286  	privacyExtension.Init()
   287  
   288  	return nil
   289  }
   290  
   291  // Get private transaction manager configuration
   292  func QuorumSetupPrivacyConfiguration(ctx *cli.Context) (http.Config, error) {
   293  	// get default configuration
   294  	cfg, err := private.GetLegacyEnvironmentConfig()
   295  	if err != nil {
   296  		return http.Config{}, err
   297  	}
   298  
   299  	// override the config with command line parameters
   300  	if ctx.GlobalIsSet(utils.QuorumPTMUnixSocketFlag.Name) {
   301  		cfg.SetSocket(ctx.GlobalString(utils.QuorumPTMUnixSocketFlag.Name))
   302  	}
   303  	if ctx.GlobalIsSet(utils.QuorumPTMUrlFlag.Name) {
   304  		cfg.SetHttpUrl(ctx.GlobalString(utils.QuorumPTMUrlFlag.Name))
   305  	}
   306  	if ctx.GlobalIsSet(utils.QuorumPTMTimeoutFlag.Name) {
   307  		cfg.SetTimeout(ctx.GlobalUint(utils.QuorumPTMTimeoutFlag.Name))
   308  	}
   309  	if ctx.GlobalIsSet(utils.QuorumPTMDialTimeoutFlag.Name) {
   310  		cfg.SetDialTimeout(ctx.GlobalUint(utils.QuorumPTMDialTimeoutFlag.Name))
   311  	}
   312  	if ctx.GlobalIsSet(utils.QuorumPTMHttpIdleTimeoutFlag.Name) {
   313  		cfg.SetHttpIdleConnTimeout(ctx.GlobalUint(utils.QuorumPTMHttpIdleTimeoutFlag.Name))
   314  	}
   315  	if ctx.GlobalIsSet(utils.QuorumPTMHttpWriteBufferSizeFlag.Name) {
   316  		cfg.SetHttpWriteBufferSize(ctx.GlobalInt(utils.QuorumPTMHttpWriteBufferSizeFlag.Name))
   317  	}
   318  	if ctx.GlobalIsSet(utils.QuorumPTMHttpReadBufferSizeFlag.Name) {
   319  		cfg.SetHttpReadBufferSize(ctx.GlobalInt(utils.QuorumPTMHttpReadBufferSizeFlag.Name))
   320  	}
   321  	if ctx.GlobalIsSet(utils.QuorumPTMTlsModeFlag.Name) {
   322  		cfg.SetTlsMode(ctx.GlobalString(utils.QuorumPTMTlsModeFlag.Name))
   323  	}
   324  	if ctx.GlobalIsSet(utils.QuorumPTMTlsRootCaFlag.Name) {
   325  		cfg.SetTlsRootCA(ctx.GlobalString(utils.QuorumPTMTlsRootCaFlag.Name))
   326  	}
   327  	if ctx.GlobalIsSet(utils.QuorumPTMTlsClientCertFlag.Name) {
   328  		cfg.SetTlsClientCert(ctx.GlobalString(utils.QuorumPTMTlsClientCertFlag.Name))
   329  	}
   330  	if ctx.GlobalIsSet(utils.QuorumPTMTlsClientKeyFlag.Name) {
   331  		cfg.SetTlsClientKey(ctx.GlobalString(utils.QuorumPTMTlsClientKeyFlag.Name))
   332  	}
   333  	if ctx.GlobalIsSet(utils.QuorumPTMTlsInsecureSkipVerify.Name) {
   334  		cfg.SetTlsInsecureSkipVerify(ctx.Bool(utils.QuorumPTMTlsInsecureSkipVerify.Name))
   335  	}
   336  
   337  	if err = cfg.Validate(); err != nil {
   338  		return cfg, err
   339  	}
   340  	return cfg, nil
   341  }