github.com/ConsenSys/Quorum@v20.10.0+incompatible/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  	"github.com/ethereum/go-ethereum/cmd/utils"
    29  	"github.com/ethereum/go-ethereum/dashboard"
    30  	"github.com/ethereum/go-ethereum/eth"
    31  	"github.com/ethereum/go-ethereum/log"
    32  	"github.com/ethereum/go-ethereum/node"
    33  	"github.com/ethereum/go-ethereum/params"
    34  	"github.com/ethereum/go-ethereum/private"
    35  	"github.com/ethereum/go-ethereum/private/engine"
    36  	whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
    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  type gethConfig struct {
    80  	Eth       eth.Config
    81  	Shh       whisper.Config
    82  	Node      node.Config
    83  	Ethstats  ethstatsConfig
    84  	Dashboard dashboard.Config
    85  }
    86  
    87  func loadConfig(file string, cfg *gethConfig) error {
    88  	f, err := os.Open(file)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	defer f.Close()
    93  
    94  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
    95  	// Add file name to errors that have a line number.
    96  	if _, ok := err.(*toml.LineError); ok {
    97  		err = errors.New(file + ", " + err.Error())
    98  	}
    99  	return err
   100  }
   101  
   102  func defaultNodeConfig() node.Config {
   103  	cfg := node.DefaultConfig
   104  	cfg.Name = clientIdentifier
   105  	cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
   106  	cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
   107  	cfg.WSModules = append(cfg.WSModules, "eth", "shh")
   108  	cfg.IPCPath = "geth.ipc"
   109  	return cfg
   110  }
   111  
   112  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   113  	// Load defaults.
   114  	cfg := gethConfig{
   115  		Eth:       eth.DefaultConfig,
   116  		Shh:       whisper.DefaultConfig,
   117  		Node:      defaultNodeConfig(),
   118  		Dashboard: dashboard.DefaultConfig,
   119  	}
   120  
   121  	// Load config file.
   122  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   123  		if err := loadConfig(file, &cfg); err != nil {
   124  			utils.Fatalf("%v", err)
   125  		}
   126  	}
   127  
   128  	// Apply flags.
   129  	utils.SetNodeConfig(ctx, &cfg.Node)
   130  	stack, err := node.New(&cfg.Node)
   131  	if err != nil {
   132  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   133  	}
   134  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   135  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   136  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   137  	}
   138  	utils.SetShhConfig(ctx, stack, &cfg.Shh)
   139  	utils.SetDashboardConfig(ctx, &cfg.Dashboard)
   140  
   141  	return stack, cfg
   142  }
   143  
   144  // enableWhisper returns true in case one of the whisper flags is set.
   145  func enableWhisper(ctx *cli.Context) bool {
   146  	for _, flag := range whisperFlags {
   147  		if ctx.GlobalIsSet(flag.GetName()) {
   148  			return true
   149  		}
   150  	}
   151  	return false
   152  }
   153  
   154  func makeFullNode(ctx *cli.Context) *node.Node {
   155  	stack, cfg := makeConfigNode(ctx)
   156  	if ctx.GlobalIsSet(utils.OverrideIstanbulFlag.Name) {
   157  		cfg.Eth.OverrideIstanbul = new(big.Int).SetUint64(ctx.GlobalUint64(utils.OverrideIstanbulFlag.Name))
   158  	}
   159  
   160  	ethChan := utils.RegisterEthService(stack, &cfg.Eth)
   161  
   162  	// plugin service must be after eth service so that eth service will be stopped gradually if any of the plugin
   163  	// fails to start
   164  	if cfg.Node.Plugins != nil {
   165  		utils.RegisterPluginService(stack, &cfg.Node, ctx.Bool(utils.PluginSkipVerifyFlag.Name), ctx.Bool(utils.PluginLocalVerifyFlag.Name), ctx.String(utils.PluginPublicKeyFlag.Name))
   166  	}
   167  
   168  	if cfg.Node.IsPermissionEnabled() {
   169  		utils.RegisterPermissionService(stack)
   170  	}
   171  
   172  	if ctx.GlobalBool(utils.RaftModeFlag.Name) {
   173  		utils.RegisterRaftService(stack, ctx, &cfg.Node, ethChan)
   174  	}
   175  
   176  	if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
   177  		utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
   178  	}
   179  
   180  	ipcPath := quorumGetPrivateTransactionManager()
   181  	if ipcPath != "" {
   182  		utils.RegisterExtensionService(stack, ethChan)
   183  	}
   184  
   185  	// Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
   186  	shhEnabled := enableWhisper(ctx)
   187  	shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
   188  	if shhEnabled || shhAutoEnabled {
   189  		if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
   190  			cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
   191  		}
   192  		if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
   193  			cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
   194  		}
   195  		if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
   196  			cfg.Shh.RestrictConnectionBetweenLightClients = true
   197  		}
   198  		utils.RegisterShhService(stack, &cfg.Shh)
   199  	}
   200  	// Configure GraphQL if requested
   201  	if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
   202  		utils.RegisterGraphQLService(stack, cfg.Node.GraphQLEndpoint(), cfg.Node.GraphQLCors, cfg.Node.GraphQLVirtualHosts, cfg.Node.HTTPTimeouts)
   203  	}
   204  	// Add the Ethereum Stats daemon if requested.
   205  	if cfg.Ethstats.URL != "" {
   206  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   207  	}
   208  	return stack
   209  }
   210  
   211  // dumpConfig is the dumpconfig command.
   212  func dumpConfig(ctx *cli.Context) error {
   213  	_, cfg := makeConfigNode(ctx)
   214  	comment := ""
   215  
   216  	if cfg.Eth.Genesis != nil {
   217  		cfg.Eth.Genesis = nil
   218  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   219  	}
   220  
   221  	out, err := tomlSettings.Marshal(&cfg)
   222  	if err != nil {
   223  		return err
   224  	}
   225  
   226  	dump := os.Stdout
   227  	if ctx.NArg() > 0 {
   228  		dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
   229  		if err != nil {
   230  			return err
   231  		}
   232  		defer dump.Close()
   233  	}
   234  	dump.WriteString(comment)
   235  	dump.Write(out)
   236  
   237  	return nil
   238  }
   239  
   240  // quorumValidateEthService checks quorum features that depend on the ethereum service
   241  func quorumValidateEthService(stack *node.Node, isRaft bool) {
   242  	var ethereum *eth.Ethereum
   243  
   244  	err := stack.Service(&ethereum)
   245  	if err != nil {
   246  		utils.Fatalf("Error retrieving Ethereum service: %v", err)
   247  	}
   248  
   249  	quorumValidateConsensus(ethereum, isRaft)
   250  
   251  	quorumValidatePrivacyEnhancements(ethereum)
   252  }
   253  
   254  // quorumValidateConsensus checks if a consensus was used. The node is killed if consensus was not used
   255  func quorumValidateConsensus(ethereum *eth.Ethereum, isRaft bool) {
   256  	if !isRaft && ethereum.BlockChain().Config().Istanbul == nil && ethereum.BlockChain().Config().Clique == nil {
   257  		utils.Fatalf("Consensus not specified. Exiting!!")
   258  	}
   259  }
   260  
   261  // quorumValidatePrivateTransactionManager returns whether the "PRIVATE_CONFIG"
   262  // environment variable is set
   263  func quorumValidatePrivateTransactionManager() bool {
   264  	return os.Getenv("PRIVATE_CONFIG") != ""
   265  }
   266  
   267  //
   268  func quorumGetPrivateTransactionManager() string {
   269  	cfgPath := os.Getenv("PRIVATE_CONFIG")
   270  	if cfgPath != "" && cfgPath != "ignore" {
   271  		return cfgPath
   272  	}
   273  	return ""
   274  }
   275  
   276  // quorumValidatePrivacyEnhancements checks if privacy enhancements are configured the transaction manager supports
   277  // the PrivacyEnhancements feature
   278  func quorumValidatePrivacyEnhancements(ethereum *eth.Ethereum) {
   279  	privacyEnhancementsBlock := ethereum.BlockChain().Config().PrivacyEnhancementsBlock
   280  	if privacyEnhancementsBlock != nil {
   281  		log.Info("Privacy enhancements is configured to be enabled from block ", "height", privacyEnhancementsBlock)
   282  		if !private.P.HasFeature(engine.PrivacyEnhancements) {
   283  			utils.Fatalf("Cannot start quorum with privacy enhancements enabled while the transaction manager does not support it")
   284  		}
   285  	}
   286  }