Skip to content

Commit 30d706c

Browse files
fjlkaralabe
authored andcommitted
cmd/geth: add --config file flag (ethereum#13875)
* p2p/discover, p2p/discv5: add marshaling methods to Node * p2p/netutil: make Netlist decodable from TOML * common/math: encode nil HexOrDecimal256 as 0x0 * cmd/geth: add --config file flag * cmd/geth: add missing license header * eth: prettify Config again, fix tests * eth: use gasprice.Config instead of duplicating its fields * eth/gasprice: hide nil default from dumpconfig output * cmd/geth: hide genesis block in dumpconfig output * node: make tests compile * console: fix tests * cmd/geth: make TOML keys look exactly like Go struct fields * p2p: use discovery by default This makes the zero Config slightly more useful. It also fixes package node tests because Node detects reuse of the datadir through the NodeDatabase. * cmd/geth: make ethstats URL settable through config file * cmd/faucet: fix configuration * cmd/geth: dedup attach tests * eth: add comment for DefaultConfig * eth: pass downloader.SyncMode in Config This removes the FastSync, LightSync flags in favour of a more general SyncMode flag. * cmd/utils: remove jitvm flags * cmd/utils: make mutually exclusive flag error prettier It now reads: Fatal: flags --dev, --testnet can't be used at the same time * p2p: fix typo * node: add DefaultConfig, use it for geth * mobile: add missing NoDiscovery option * cmd/utils: drop MakeNode This exposed a couple of places that needed to be updated to use node.DefaultConfig. * node: fix typo * eth: make fast sync the default mode * cmd/utils: remove IPCApiFlag (unused) * node: remove default IPC path Set it in the frontends instead. * cmd/geth: add --syncmode * cmd/utils: make --ipcdisable and --ipcpath mutually exclusive * cmd/utils: don't enable WS, HTTP when setting addr * cmd/utils: fix --identity
1 parent b57680b commit 30d706c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+6615
-648
lines changed

cmd/faucet/faucet.go

+19-20
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ import (
4141
"github.com/ethereum/go-ethereum/core"
4242
"github.com/ethereum/go-ethereum/core/types"
4343
"github.com/ethereum/go-ethereum/eth"
44+
"github.com/ethereum/go-ethereum/eth/downloader"
4445
"github.com/ethereum/go-ethereum/ethclient"
4546
"github.com/ethereum/go-ethereum/ethstats"
4647
"github.com/ethereum/go-ethereum/les"
4748
"github.com/ethereum/go-ethereum/log"
4849
"github.com/ethereum/go-ethereum/node"
50+
"github.com/ethereum/go-ethereum/p2p"
4951
"github.com/ethereum/go-ethereum/p2p/discover"
5052
"github.com/ethereum/go-ethereum/p2p/discv5"
5153
"github.com/ethereum/go-ethereum/p2p/nat"
@@ -175,32 +177,29 @@ type faucet struct {
175177
func newFaucet(genesis *core.Genesis, port int, enodes []*discv5.Node, network int, stats string, ks *keystore.KeyStore, index []byte) (*faucet, error) {
176178
// Assemble the raw devp2p protocol stack
177179
stack, err := node.New(&node.Config{
178-
Name: "geth",
179-
Version: params.Version,
180-
DataDir: filepath.Join(os.Getenv("HOME"), ".faucet"),
181-
NAT: nat.Any(),
182-
DiscoveryV5: true,
183-
ListenAddr: fmt.Sprintf(":%d", port),
184-
DiscoveryV5Addr: fmt.Sprintf(":%d", port+1),
185-
MaxPeers: 25,
186-
BootstrapNodesV5: enodes,
180+
Name: "geth",
181+
Version: params.Version,
182+
DataDir: filepath.Join(os.Getenv("HOME"), ".faucet"),
183+
P2P: p2p.Config{
184+
NAT: nat.Any(),
185+
NoDiscovery: true,
186+
DiscoveryV5: true,
187+
ListenAddr: fmt.Sprintf(":%d", port),
188+
DiscoveryV5Addr: fmt.Sprintf(":%d", port+1),
189+
MaxPeers: 25,
190+
BootstrapNodesV5: enodes,
191+
},
187192
})
188193
if err != nil {
189194
return nil, err
190195
}
191196
// Assemble the Ethereum light client protocol
192197
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
193-
return les.New(ctx, &eth.Config{
194-
LightMode: true,
195-
NetworkId: network,
196-
Genesis: genesis,
197-
GasPrice: big.NewInt(20 * params.Shannon),
198-
GpoBlocks: 10,
199-
GpoPercentile: 50,
200-
EthashCacheDir: "ethash",
201-
EthashCachesInMem: 2,
202-
EthashCachesOnDisk: 3,
203-
})
198+
cfg := eth.DefaultConfig
199+
cfg.SyncMode = downloader.LightSync
200+
cfg.NetworkId = network
201+
cfg.Genesis = genesis
202+
return les.New(ctx, &cfg)
204203
}); err != nil {
205204
return nil, err
206205
}

cmd/geth/accountcmd.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ nodes.
179179
)
180180

181181
func accountList(ctx *cli.Context) error {
182-
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
183-
182+
stack, _ := makeConfigNode(ctx)
184183
var index int
185184
for _, wallet := range stack.AccountManager().Wallets() {
186185
for _, account := range wallet.Accounts() {
@@ -278,7 +277,7 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
278277

279278
// accountCreate creates a new account into the keystore defined by the CLI flags.
280279
func accountCreate(ctx *cli.Context) error {
281-
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
280+
stack, _ := makeConfigNode(ctx)
282281
password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
283282

284283
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
@@ -296,7 +295,7 @@ func accountUpdate(ctx *cli.Context) error {
296295
if len(ctx.Args()) == 0 {
297296
utils.Fatalf("No accounts specified to update")
298297
}
299-
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
298+
stack, _ := makeConfigNode(ctx)
300299
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
301300

302301
account, oldPassword := unlockAccount(ctx, ks, ctx.Args().First(), 0, nil)
@@ -317,7 +316,7 @@ func importWallet(ctx *cli.Context) error {
317316
utils.Fatalf("Could not read wallet file: %v", err)
318317
}
319318

320-
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
319+
stack, _ := makeConfigNode(ctx)
321320
passphrase := getPassPhrase("", false, 0, utils.MakePasswordList(ctx))
322321

323322
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
@@ -338,7 +337,7 @@ func accountImport(ctx *cli.Context) error {
338337
if err != nil {
339338
utils.Fatalf("Failed to load the private key: %v", err)
340339
}
341-
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
340+
stack, _ := makeConfigNode(ctx)
342341
passphrase := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
343342

344343
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)

cmd/geth/chaincmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func exportChain(ctx *cli.Context) error {
244244
}
245245

246246
func removeDB(ctx *cli.Context) error {
247-
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
247+
stack, _ := makeConfigNode(ctx)
248248
dbdir := stack.ResolvePath(utils.ChainDbName(ctx))
249249
if !common.FileExist(dbdir) {
250250
fmt.Println(dbdir, "does not exist")

cmd/geth/config.go

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// Copyright 2015 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+
"encoding/hex"
22+
"errors"
23+
"fmt"
24+
"io"
25+
"os"
26+
"reflect"
27+
"unicode"
28+
29+
cli "gopkg.in/urfave/cli.v1"
30+
31+
"github.com/ethereum/go-ethereum/cmd/utils"
32+
"github.com/ethereum/go-ethereum/contracts/release"
33+
"github.com/ethereum/go-ethereum/eth"
34+
"github.com/ethereum/go-ethereum/node"
35+
"github.com/ethereum/go-ethereum/params"
36+
"github.com/naoina/toml"
37+
)
38+
39+
var (
40+
dumpConfigCommand = cli.Command{
41+
Action: dumpConfig,
42+
Name: "dumpconfig",
43+
Usage: "Show configuration values",
44+
ArgsUsage: "",
45+
Category: "MISCELLANEOUS COMMANDS",
46+
Description: `The dumpconfig command shows configuration values.`,
47+
}
48+
49+
configFileFlag = cli.StringFlag{
50+
Name: "config",
51+
Usage: "TOML configuration file",
52+
}
53+
)
54+
55+
// These settings ensure that TOML keys use the same names as Go struct fields.
56+
var tomlSettings = toml.Config{
57+
NormFieldName: func(rt reflect.Type, key string) string {
58+
return key
59+
},
60+
FieldToKey: func(rt reflect.Type, field string) string {
61+
return field
62+
},
63+
MissingField: func(rt reflect.Type, field string) error {
64+
link := ""
65+
if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
66+
link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
67+
}
68+
return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
69+
},
70+
}
71+
72+
type ethstatsConfig struct {
73+
URL string `toml:",omitempty"`
74+
}
75+
76+
type gethConfig struct {
77+
Eth eth.Config
78+
Node node.Config
79+
Ethstats ethstatsConfig
80+
}
81+
82+
func loadConfig(file string, cfg *gethConfig) error {
83+
f, err := os.Open(file)
84+
if err != nil {
85+
return err
86+
}
87+
defer f.Close()
88+
89+
err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
90+
// Add file name to errors that have a line number.
91+
if _, ok := err.(*toml.LineError); ok {
92+
err = errors.New(file + ", " + err.Error())
93+
}
94+
return err
95+
}
96+
97+
func defaultNodeConfig() node.Config {
98+
cfg := node.DefaultConfig
99+
cfg.Name = clientIdentifier
100+
cfg.Version = params.VersionWithCommit(gitCommit)
101+
cfg.HTTPModules = append(cfg.HTTPModules, "eth")
102+
cfg.WSModules = append(cfg.WSModules, "eth")
103+
cfg.IPCPath = "geth.ipc"
104+
return cfg
105+
}
106+
107+
func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
108+
// Load defaults.
109+
cfg := gethConfig{
110+
Eth: eth.DefaultConfig,
111+
Node: defaultNodeConfig(),
112+
}
113+
114+
// Load config file.
115+
if file := ctx.GlobalString(configFileFlag.Name); file != "" {
116+
if err := loadConfig(file, &cfg); err != nil {
117+
utils.Fatalf("%v", err)
118+
}
119+
}
120+
121+
// Apply flags.
122+
utils.SetNodeConfig(ctx, &cfg.Node)
123+
stack, err := node.New(&cfg.Node)
124+
if err != nil {
125+
utils.Fatalf("Failed to create the protocol stack: %v", err)
126+
}
127+
utils.SetEthConfig(ctx, stack, &cfg.Eth)
128+
if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
129+
cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
130+
}
131+
132+
return stack, cfg
133+
}
134+
135+
func makeFullNode(ctx *cli.Context) *node.Node {
136+
stack, cfg := makeConfigNode(ctx)
137+
138+
utils.RegisterEthService(stack, &cfg.Eth)
139+
140+
// Whisper must be explicitly enabled, but is auto-enabled in --dev mode.
141+
shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name)
142+
shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
143+
if shhEnabled || shhAutoEnabled {
144+
utils.RegisterShhService(stack)
145+
}
146+
147+
// Add the Ethereum Stats daemon if requested.
148+
if cfg.Ethstats.URL != "" {
149+
utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
150+
}
151+
152+
// Add the release oracle service so it boots along with node.
153+
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
154+
config := release.Config{
155+
Oracle: relOracle,
156+
Major: uint32(params.VersionMajor),
157+
Minor: uint32(params.VersionMinor),
158+
Patch: uint32(params.VersionPatch),
159+
}
160+
commit, _ := hex.DecodeString(gitCommit)
161+
copy(config.Commit[:], commit)
162+
return release.NewReleaseService(ctx, config)
163+
}); err != nil {
164+
utils.Fatalf("Failed to register the Geth release oracle service: %v", err)
165+
}
166+
return stack
167+
}
168+
169+
// dumpConfig is the dumpconfig command.
170+
func dumpConfig(ctx *cli.Context) error {
171+
_, cfg := makeConfigNode(ctx)
172+
comment := ""
173+
174+
if cfg.Eth.Genesis != nil {
175+
cfg.Eth.Genesis = nil
176+
comment += "# Note: this config doesn't contain the genesis block.\n\n"
177+
}
178+
179+
out, err := tomlSettings.Marshal(&cfg)
180+
if err != nil {
181+
return err
182+
}
183+
io.WriteString(os.Stdout, comment)
184+
os.Stdout.Write(out)
185+
return nil
186+
}

0 commit comments

Comments
 (0)