Skip to content

Commit fdb49ae

Browse files
committed
feat: add key file flags to p2p query
1 parent 9aeccac commit fdb49ae

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

cmd/p2p/query/query.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package query
22

33
import (
4+
"crypto/ecdsa"
45
"fmt"
6+
"net"
57

68
"github.com/ethereum/go-ethereum/eth/protocols/eth"
79
"github.com/rs/zerolog/log"
@@ -14,6 +16,12 @@ type (
1416
queryParams struct {
1517
StartBlock uint64
1618
Amount uint64
19+
Port int
20+
Addr net.IP
21+
KeyFile string
22+
PrivateKey string
23+
24+
privateKey *ecdsa.PrivateKey
1725
}
1826
)
1927

@@ -30,10 +38,16 @@ This command will initially establish a handshake and exchange status message
3038
from the peer. Then, it will query the node for block(s) given the start block
3139
and the amount of blocks to query and print the results.`,
3240
Args: cobra.MinimumNArgs(1),
33-
PreRunE: func(cmd *cobra.Command, args []string) error {
41+
PreRunE: func(cmd *cobra.Command, args []string) (err error) {
3442
if inputQueryParams.Amount < 1 {
3543
return fmt.Errorf("amount must be greater than 0")
3644
}
45+
46+
inputQueryParams.privateKey, err = p2p.ParsePrivateKey(inputQueryParams.KeyFile, inputQueryParams.PrivateKey)
47+
if err != nil {
48+
return err
49+
}
50+
3751
return nil
3852
},
3953
Run: func(cmd *cobra.Command, args []string) {
@@ -50,7 +64,13 @@ and the amount of blocks to query and print the results.`,
5064
amount uint64 = inputQueryParams.Amount
5165
)
5266

53-
conn, err := p2p.Dial(node, p2p.NewDialOpts())
67+
opts := p2p.DialOpts{
68+
Port: inputQueryParams.Port,
69+
Addr: inputQueryParams.Addr,
70+
PrivateKey: inputQueryParams.privateKey,
71+
}
72+
73+
conn, err := p2p.Dial(node, opts)
5474
if err != nil {
5575
log.Error().Err(err).Msg("Dial failed")
5676
return
@@ -106,6 +126,10 @@ func print(headers eth.BlockHeadersRequest) {
106126
func init() {
107127
QueryCmd.Flags().Uint64VarP(&inputQueryParams.StartBlock, "start-block", "s", 0, "Block number to start querying from")
108128
QueryCmd.Flags().Uint64VarP(&inputQueryParams.Amount, "amount", "a", 1, "Amount of blocks to query")
129+
QueryCmd.Flags().IntVarP(&inputQueryParams.Port, "port", "P", 30303, "Port for discovery protocol")
130+
QueryCmd.Flags().IPVarP(&inputQueryParams.Addr, "addr", "a", net.ParseIP("127.0.0.1"), "Address to bind discovery listener")
131+
QueryCmd.Flags().StringVarP(&inputQueryParams.KeyFile, "key-file", "k", "", "Private key file (cannot be set with --key)")
132+
QueryCmd.Flags().StringVar(&inputQueryParams.PrivateKey, "key", "", "Hex-encoded private key (cannot be set with --key-file)")
109133
if err := QueryCmd.MarkFlagRequired("start-block"); err != nil {
110134
log.Error().Err(err).Msg("Failed to mark start-block as required persistent flag")
111135
}

0 commit comments

Comments
 (0)