github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/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  	"io"
    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  	"github.com/ethereum/go-ethereum/mpcService"
    38  	"path/filepath"
    39  	"io/ioutil"
    40  	"encoding/json"
    41  	"github.com/ethereum/go-ethereum/log"
    42  )
    43  
    44  var (
    45  	dumpConfigCommand = cli.Command{
    46  		Action:      utils.MigrateFlags(dumpConfig),
    47  		Name:        "dumpconfig",
    48  		Usage:       "Show configuration values",
    49  		ArgsUsage:   "",
    50  		Flags:       append(append(nodeFlags, rpcFlags...), whisperFlags...),
    51  		Category:    "MISCELLANEOUS COMMANDS",
    52  		Description: `The dumpconfig command shows configuration values.`,
    53  	}
    54  
    55  	configFileFlag = cli.StringFlag{
    56  		Name:  "config",
    57  		Usage: "TOML configuration file",
    58  	}
    59  )
    60  
    61  // These settings ensure that TOML keys use the same names as Go struct fields.
    62  var tomlSettings = toml.Config{
    63  	NormFieldName: func(rt reflect.Type, key string) string {
    64  		return key
    65  	},
    66  	FieldToKey: func(rt reflect.Type, field string) string {
    67  		return field
    68  	},
    69  	MissingField: func(rt reflect.Type, field string) error {
    70  		link := ""
    71  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    72  			link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
    73  		}
    74  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    75  	},
    76  }
    77  
    78  type ethstatsConfig struct {
    79  	URL string `toml:",omitempty"`
    80  }
    81  
    82  type gethConfig struct {
    83  	Eth       eth.Config
    84  	Shh       whisper.Config
    85  	Node      node.Config
    86  	Ethstats  ethstatsConfig
    87  	Dashboard dashboard.Config
    88  	MpcConfig mpcService.MpcConfig
    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)
   110  	cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
   111  	cfg.WSModules = append(cfg.WSModules, "eth", "shh")
   112  	cfg.IPCPath = "geth.ipc"
   113  	return cfg
   114  }
   115  
   116  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   117  	// Load defaults.
   118  	cfg := gethConfig{
   119  		Eth:       eth.DefaultConfig,
   120  		Shh:       whisper.DefaultConfig,
   121  		Node:      defaultNodeConfig(),
   122  		Dashboard: dashboard.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  
   143  	utils.SetShhConfig(ctx, stack, &cfg.Shh)
   144  	utils.SetDashboardConfig(ctx, &cfg.Dashboard)
   145  	cfg.MpcConfig.DataDir = filepath.Join(cfg.Node.DataDir,"mpcKeyStore")
   146  	smspath := filepath.Join(cfg.Node.DataDir, "mpcService.json");
   147  	b, err := ioutil.ReadFile(smspath)
   148  	if err != nil {
   149  		log.Error("mpc Config Not Found","error",err)
   150  	}
   151  	errUnmarshal := json.Unmarshal(b, &cfg.MpcConfig)
   152  	if(errUnmarshal != nil) {
   153  		log.Error("Unmarshal error","errUnmarshal", errUnmarshal)
   154  	}
   155  
   156  	return stack, cfg
   157  }
   158  
   159  // enableWhisper returns true in case one of the whisper flags is set.
   160  func enableWhisper(ctx *cli.Context) bool {
   161  	for _, flag := range whisperFlags {
   162  		if ctx.GlobalIsSet(flag.GetName()) {
   163  			return true
   164  		}
   165  	}
   166  	return false
   167  }
   168  
   169  func makeFullNode(ctx *cli.Context) *node.Node {
   170  	stack, cfg := makeConfigNode(ctx)
   171  
   172  	utils.RegisterEthService(stack, &cfg.Eth)
   173  
   174  	if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
   175  		utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
   176  	}
   177  	// Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
   178  	shhEnabled := enableWhisper(ctx)
   179  	shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
   180  	if shhEnabled || shhAutoEnabled {
   181  		if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
   182  			cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
   183  		}
   184  		if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
   185  			cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
   186  		}
   187  		utils.RegisterShhService(stack, &cfg.Shh)
   188  	}
   189  
   190  	// Add the Ethereum Stats daemon if requested.
   191  	if cfg.Ethstats.URL != "" {
   192  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   193  	}
   194  	utils.RegisterMpcService(stack,&cfg.MpcConfig)
   195  	return stack
   196  }
   197  
   198  // dumpConfig is the dumpconfig command.
   199  func dumpConfig(ctx *cli.Context) error {
   200  	_, cfg := makeConfigNode(ctx)
   201  	comment := ""
   202  
   203  	if cfg.Eth.Genesis != nil {
   204  		cfg.Eth.Genesis = nil
   205  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   206  	}
   207  
   208  	out, err := tomlSettings.Marshal(&cfg)
   209  	if err != nil {
   210  		return err
   211  	}
   212  	io.WriteString(os.Stdout, comment)
   213  	os.Stdout.Write(out)
   214  	return nil
   215  }