Skip to content

Commit

Permalink
cmd/geth, cmd/devp2p: fix some cli parsing issues (ethereum#25234)
Browse files Browse the repository at this point in the history
* cmd/geth: add some missing argument count checks

* internal/flags: skip cmds with no action func in MigrateGlobalFlags

* internal/flags: add Merge

* cmd/devp2p: re-add listener config flags in discv4 commands
  • Loading branch information
fjl authored and blakehhuynh committed Oct 7, 2022
1 parent 583e9a5 commit 8da333f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
16 changes: 13 additions & 3 deletions cmd/devp2p/discv4cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ethereum/go-ethereum/cmd/devp2p/internal/v4test"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/flags"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/params"
Expand All @@ -49,32 +50,34 @@ var (
Usage: "Sends ping to a node",
Action: discv4Ping,
ArgsUsage: "<node>",
Flags: v4NodeFlags,
}
discv4RequestRecordCommand = &cli.Command{
Name: "requestenr",
Usage: "Requests a node record using EIP-868 enrRequest",
Action: discv4RequestRecord,
ArgsUsage: "<node>",
Flags: v4NodeFlags,
}
discv4ResolveCommand = &cli.Command{
Name: "resolve",
Usage: "Finds a node in the DHT",
Action: discv4Resolve,
ArgsUsage: "<node>",
Flags: []cli.Flag{bootnodesFlag},
Flags: v4NodeFlags,
}
discv4ResolveJSONCommand = &cli.Command{
Name: "resolve-json",
Usage: "Re-resolves nodes in a nodes.json file",
Action: discv4ResolveJSON,
Flags: []cli.Flag{bootnodesFlag},
Flags: v4NodeFlags,
ArgsUsage: "<nodes.json file>",
}
discv4CrawlCommand = &cli.Command{
Name: "crawl",
Usage: "Updates a nodes.json file with random nodes found in the DHT",
Action: discv4Crawl,
Flags: []cli.Flag{bootnodesFlag, crawlTimeoutFlag},
Flags: flags.Merge(v4NodeFlags, []cli.Flag{crawlTimeoutFlag}),
}
discv4TestCommand = &cli.Command{
Name: "test",
Expand Down Expand Up @@ -119,6 +122,13 @@ var (
}
)

var v4NodeFlags = []cli.Flag{
bootnodesFlag,
nodekeyFlag,
nodedbFlag,
listenAddrFlag,
}

func discv4Ping(ctx *cli.Context) error {
n := getNodeArg(ctx)
disc := startV4(ctx)
Expand Down
6 changes: 4 additions & 2 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ This command dumps out the state for a given block (or latest, if none provided)
// initGenesis will initialise the given JSON format genesis file and writes it as
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
func initGenesis(ctx *cli.Context) error {
// Make sure we have a valid genesis JSON
if ctx.Args().Len() != 1 {
utils.Fatalf("need genesis.json file as the only argument")
}
genesisPath := ctx.Args().First()
if len(genesisPath) == 0 {
utils.Fatalf("Must supply path to genesis JSON file")
utils.Fatalf("invalid path to genesis file")
}
file, err := os.Open(genesisPath)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/geth/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func localConsole(ctx *cli.Context) error {
// remoteConsole will connect to a remote geth instance, attaching a JavaScript
// console to it.
func remoteConsole(ctx *cli.Context) error {
if ctx.Args().Len() > 1 {
utils.Fatalf("invalid command-line: too many arguments")
}

endpoint := ctx.Args().First()
if endpoint == "" {
cfg := defaultNodeConfig()
Expand Down
13 changes: 13 additions & 0 deletions internal/flags/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func NewApp(gitCommit, gitDate, usage string) *cli.App {
return app
}

// Merge merges the given flag slices.
func Merge(groups ...[]cli.Flag) []cli.Flag {
var ret []cli.Flag
for _, group := range groups {
ret = append(ret, group...)
}
return ret
}

var migrationApplied = map[*cli.Command]struct{}{}

// MigrateGlobalFlags makes all global flag values available in the
Expand Down Expand Up @@ -70,6 +79,10 @@ func MigrateGlobalFlags(ctx *cli.Context) {

// This iterates over all commands and wraps their action function.
iterate(ctx.App.Commands, func(cmd *cli.Command) {
if cmd.Action == nil {
return
}

action := cmd.Action
cmd.Action = func(ctx *cli.Context) error {
doMigrateFlags(ctx)
Expand Down

0 comments on commit 8da333f

Please sign in to comment.