2017-04-14 11:29:00 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-04-12 17:27:23 +03:00
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
cli "gopkg.in/urfave/cli.v1"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2020-08-03 20:40:46 +03:00
|
|
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
2020-09-08 11:47:48 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-04-12 17:27:23 +03:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/naoina/toml"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dumpConfigCommand = cli.Command{
|
2017-05-03 15:39:07 +03:00
|
|
|
Action: utils.MigrateFlags(dumpConfig),
|
2017-04-12 17:27:23 +03:00
|
|
|
Name: "dumpconfig",
|
|
|
|
Usage: "Show configuration values",
|
|
|
|
ArgsUsage: "",
|
2017-06-21 11:49:14 +03:00
|
|
|
Flags: append(append(nodeFlags, rpcFlags...), whisperFlags...),
|
2017-04-12 17:27:23 +03:00
|
|
|
Category: "MISCELLANEOUS COMMANDS",
|
|
|
|
Description: `The dumpconfig command shows configuration values.`,
|
|
|
|
}
|
|
|
|
|
|
|
|
configFileFlag = cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "TOML configuration file",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// These settings ensure that TOML keys use the same names as Go struct fields.
|
|
|
|
var tomlSettings = toml.Config{
|
|
|
|
NormFieldName: func(rt reflect.Type, key string) string {
|
|
|
|
return key
|
|
|
|
},
|
|
|
|
FieldToKey: func(rt reflect.Type, field string) string {
|
|
|
|
return field
|
|
|
|
},
|
|
|
|
MissingField: func(rt reflect.Type, field string) error {
|
|
|
|
link := ""
|
|
|
|
if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
|
|
|
|
link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
|
|
|
|
}
|
|
|
|
return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type ethstatsConfig struct {
|
|
|
|
URL string `toml:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type gethConfig struct {
|
2019-11-14 12:04:16 +03:00
|
|
|
Eth eth.Config
|
|
|
|
Node node.Config
|
|
|
|
Ethstats ethstatsConfig
|
2017-04-12 17:27:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig(file string, cfg *gethConfig) error {
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
|
|
|
|
// Add file name to errors that have a line number.
|
|
|
|
if _, ok := err.(*toml.LineError); ok {
|
|
|
|
err = errors.New(file + ", " + err.Error())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultNodeConfig() node.Config {
|
|
|
|
cfg := node.DefaultConfig
|
|
|
|
cfg.Name = clientIdentifier
|
2019-05-08 16:44:28 +03:00
|
|
|
cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
|
2020-01-28 12:37:08 +03:00
|
|
|
cfg.HTTPModules = append(cfg.HTTPModules, "eth")
|
|
|
|
cfg.WSModules = append(cfg.WSModules, "eth")
|
2017-04-12 17:27:23 +03:00
|
|
|
cfg.IPCPath = "geth.ipc"
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:24:36 +03:00
|
|
|
// makeConfigNode loads geth configuration and creates a blank node instance.
|
2017-04-12 17:27:23 +03:00
|
|
|
func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
|
|
|
|
// Load defaults.
|
|
|
|
cfg := gethConfig{
|
2019-11-14 12:04:16 +03:00
|
|
|
Eth: eth.DefaultConfig,
|
|
|
|
Node: defaultNodeConfig(),
|
2017-04-12 17:27:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load config file.
|
|
|
|
if file := ctx.GlobalString(configFileFlag.Name); file != "" {
|
|
|
|
if err := loadConfig(file, &cfg); err != nil {
|
|
|
|
utils.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply flags.
|
|
|
|
utils.SetNodeConfig(ctx, &cfg.Node)
|
|
|
|
stack, err := node.New(&cfg.Node)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to create the protocol stack: %v", err)
|
|
|
|
}
|
|
|
|
utils.SetEthConfig(ctx, stack, &cfg.Eth)
|
|
|
|
if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
|
|
|
|
cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
|
|
|
|
}
|
2020-09-08 11:47:48 +03:00
|
|
|
utils.SetShhConfig(ctx, stack)
|
2017-06-13 12:49:07 +03:00
|
|
|
|
2017-04-12 17:27:23 +03:00
|
|
|
return stack, cfg
|
|
|
|
}
|
|
|
|
|
2017-06-13 12:49:07 +03:00
|
|
|
// enableWhisper returns true in case one of the whisper flags is set.
|
2020-09-08 11:47:48 +03:00
|
|
|
func checkWhisper(ctx *cli.Context) {
|
2017-06-21 11:49:14 +03:00
|
|
|
for _, flag := range whisperFlags {
|
2017-06-13 12:49:07 +03:00
|
|
|
if ctx.GlobalIsSet(flag.GetName()) {
|
2020-09-08 11:47:48 +03:00
|
|
|
log.Warn("deprecated whisper flag detected. Whisper has been moved to github.com/ethereum/whisper")
|
2017-06-13 12:49:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:24:36 +03:00
|
|
|
// makeFullNode loads geth configuration and creates the Ethereum backend.
|
2020-08-03 20:40:46 +03:00
|
|
|
func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
|
2017-04-12 17:27:23 +03:00
|
|
|
stack, cfg := makeConfigNode(ctx)
|
2020-08-03 20:40:46 +03:00
|
|
|
|
|
|
|
backend := utils.RegisterEthService(stack, &cfg.Eth)
|
2017-04-12 17:27:23 +03:00
|
|
|
|
2020-09-08 11:47:48 +03:00
|
|
|
checkWhisper(ctx)
|
2019-06-12 11:24:24 +03:00
|
|
|
// Configure GraphQL if requested
|
2019-01-21 17:38:13 +03:00
|
|
|
if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
|
2020-08-03 20:40:46 +03:00
|
|
|
utils.RegisterGraphQLService(stack, backend, cfg.Node)
|
2019-01-21 17:38:13 +03:00
|
|
|
}
|
2017-04-12 17:27:23 +03:00
|
|
|
// Add the Ethereum Stats daemon if requested.
|
|
|
|
if cfg.Ethstats.URL != "" {
|
2020-08-03 20:40:46 +03:00
|
|
|
utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL)
|
2017-04-12 17:27:23 +03:00
|
|
|
}
|
2020-08-03 20:40:46 +03:00
|
|
|
return stack, backend
|
2017-04-12 17:27:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// dumpConfig is the dumpconfig command.
|
|
|
|
func dumpConfig(ctx *cli.Context) error {
|
|
|
|
_, cfg := makeConfigNode(ctx)
|
|
|
|
comment := ""
|
|
|
|
|
|
|
|
if cfg.Eth.Genesis != nil {
|
|
|
|
cfg.Eth.Genesis = nil
|
|
|
|
comment += "# Note: this config doesn't contain the genesis block.\n\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := tomlSettings.Marshal(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-07 11:56:50 +03:00
|
|
|
|
|
|
|
dump := os.Stdout
|
|
|
|
if ctx.NArg() > 0 {
|
|
|
|
dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dump.Close()
|
|
|
|
}
|
|
|
|
dump.WriteString(comment)
|
|
|
|
dump.Write(out)
|
|
|
|
|
2017-04-12 17:27:23 +03:00
|
|
|
return nil
|
|
|
|
}
|