github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/geth/config.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2017 Go Ethereum作者
    10  //此文件是Go以太坊的一部分。
    11  //
    12  //Go以太坊是免费软件:您可以重新发布和/或修改它
    13  //根据GNU通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊的分布希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU通用公共许可证了解更多详细信息。
    21  //
    22  //你应该已经收到一份GNU通用公共许可证的副本
    23  //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package main
    26  
    27  import (
    28  	"bufio"
    29  	"errors"
    30  	"fmt"
    31  	"io"
    32  	"os"
    33  	"reflect"
    34  	"unicode"
    35  
    36  	cli "gopkg.in/urfave/cli.v1"
    37  
    38  	"github.com/ethereum/go-ethereum/cmd/utils"
    39  	"github.com/ethereum/go-ethereum/dashboard"
    40  	"github.com/ethereum/go-ethereum/eth"
    41  	"github.com/ethereum/go-ethereum/node"
    42  	"github.com/ethereum/go-ethereum/params"
    43  	whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
    44  	"github.com/naoina/toml"
    45  )
    46  
    47  var (
    48  	dumpConfigCommand = cli.Command{
    49  		Action:      utils.MigrateFlags(dumpConfig),
    50  		Name:        "dumpconfig",
    51  		Usage:       "Show configuration values",
    52  		ArgsUsage:   "",
    53  		Flags:       append(append(nodeFlags, rpcFlags...), whisperFlags...),
    54  		Category:    "MISCELLANEOUS COMMANDS",
    55  		Description: `The dumpconfig command shows configuration values.`,
    56  	}
    57  
    58  	configFileFlag = cli.StringFlag{
    59  		Name:  "config",
    60  		Usage: "TOML configuration file",
    61  	}
    62  )
    63  
    64  //这些设置确保toml键使用与go struct字段相同的名称。
    65  var tomlSettings = toml.Config{
    66  	NormFieldName: func(rt reflect.Type, key string) string {
    67  		return key
    68  	},
    69  	FieldToKey: func(rt reflect.Type, field string) string {
    70  		return field
    71  	},
    72  	MissingField: func(rt reflect.Type, field string) error {
    73  		link := ""
    74  		if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
    75  link = fmt.Sprintf(", see https://godoc.org/%s%s,用于可用字段“,rt.pkgpath(),rt.name())
    76  		}
    77  		return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
    78  	},
    79  }
    80  
    81  type ethstatsConfig struct {
    82  	URL string `toml:",omitempty"`
    83  }
    84  
    85  type gethConfig struct {
    86  	Eth       eth.Config
    87  	Shh       whisper.Config
    88  	Node      node.Config
    89  	Ethstats  ethstatsConfig
    90  	Dashboard dashboard.Config
    91  }
    92  
    93  func loadConfig(file string, cfg *gethConfig) error {
    94  	f, err := os.Open(file)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	defer f.Close()
    99  
   100  	err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
   101  //将文件名添加到具有行号的错误中。
   102  	if _, ok := err.(*toml.LineError); ok {
   103  		err = errors.New(file + ", " + err.Error())
   104  	}
   105  	return err
   106  }
   107  
   108  func defaultNodeConfig() node.Config {
   109  	cfg := node.DefaultConfig
   110  	cfg.Name = clientIdentifier
   111  	cfg.Version = params.VersionWithCommit(gitCommit)
   112  	cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
   113  	cfg.WSModules = append(cfg.WSModules, "eth", "shh")
   114  	cfg.IPCPath = "geth.ipc"
   115  	return cfg
   116  }
   117  
   118  func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
   119  //加载默认值。
   120  	cfg := gethConfig{
   121  		Eth:       eth.DefaultConfig,
   122  		Shh:       whisper.DefaultConfig,
   123  		Node:      defaultNodeConfig(),
   124  		Dashboard: dashboard.DefaultConfig,
   125  	}
   126  
   127  //加载配置文件。
   128  	if file := ctx.GlobalString(configFileFlag.Name); file != "" {
   129  		if err := loadConfig(file, &cfg); err != nil {
   130  			utils.Fatalf("%v", err)
   131  		}
   132  	}
   133  
   134  //
   135  	utils.SetNodeConfig(ctx, &cfg.Node)
   136  	stack, err := node.New(&cfg.Node)
   137  	if err != nil {
   138  		utils.Fatalf("Failed to create the protocol stack: %v", err)
   139  	}
   140  	utils.SetEthConfig(ctx, stack, &cfg.Eth)
   141  	if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
   142  		cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
   143  	}
   144  
   145  	utils.SetShhConfig(ctx, stack, &cfg.Shh)
   146  	utils.SetDashboardConfig(ctx, &cfg.Dashboard)
   147  
   148  	return stack, cfg
   149  }
   150  
   151  //如果设置了其中一个耳语标志,则EnableShipper返回true。
   152  func enableWhisper(ctx *cli.Context) bool {
   153  	for _, flag := range whisperFlags {
   154  		if ctx.GlobalIsSet(flag.GetName()) {
   155  			return true
   156  		}
   157  	}
   158  	return false
   159  }
   160  
   161  func makeFullNode(ctx *cli.Context) *node.Node {
   162  	stack, cfg := makeConfigNode(ctx)
   163  
   164  	utils.RegisterEthService(stack, &cfg.Eth)
   165  
   166  	if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
   167  		utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
   168  	}
   169  //必须通过指定至少1个耳语标志或在DEV模式下显式启用耳语
   170  	shhEnabled := enableWhisper(ctx)
   171  	shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
   172  	if shhEnabled || shhAutoEnabled {
   173  		if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
   174  			cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
   175  		}
   176  		if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
   177  			cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
   178  		}
   179  		utils.RegisterShhService(stack, &cfg.Shh)
   180  	}
   181  
   182  //如果需要,添加ethereum stats守护进程。
   183  	if cfg.Ethstats.URL != "" {
   184  		utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
   185  	}
   186  	return stack
   187  }
   188  
   189  //dumpconfig是dumpconfig命令。
   190  func dumpConfig(ctx *cli.Context) error {
   191  	_, cfg := makeConfigNode(ctx)
   192  	comment := ""
   193  
   194  	if cfg.Eth.Genesis != nil {
   195  		cfg.Eth.Genesis = nil
   196  		comment += "# Note: this config doesn't contain the genesis block.\n\n"
   197  	}
   198  
   199  	out, err := tomlSettings.Marshal(&cfg)
   200  	if err != nil {
   201  		return err
   202  	}
   203  	io.WriteString(os.Stdout, comment)
   204  	os.Stdout.Write(out)
   205  	return nil
   206  }