11package query
22
33import (
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
3038from the peer. Then, it will query the node for block(s) given the start block
3139and 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 )
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
@@ -104,8 +124,14 @@ func print(headers eth.BlockHeadersRequest) {
104124}
105125
106126func init () {
107- QueryCmd .Flags ().Uint64VarP (& inputQueryParams .StartBlock , "start-block" , "s" , 0 , "block number to start querying from" )
108- QueryCmd .Flags ().Uint64VarP (& inputQueryParams .Amount , "amount" , "a" , 1 , "amount of blocks to query" )
127+ f := QueryCmd .Flags ()
128+ f .Uint64VarP (& inputQueryParams .StartBlock , "start-block" , "s" , 0 , "block number to start querying from" )
129+ f .Uint64VarP (& inputQueryParams .Amount , "amount" , "a" , 1 , "amount of blocks to query" )
130+ f .IntVarP (& inputQueryParams .Port , "port" , "P" , 30303 , "port for discovery protocol" )
131+ f .IPVar (& inputQueryParams .Addr , "addr" , net .ParseIP ("127.0.0.1" ), "address to bind discovery listener" )
132+ f .StringVarP (& inputQueryParams .KeyFile , "key-file" , "k" , "" , "private key file (cannot be set with --key)" )
133+ f .StringVar (& inputQueryParams .PrivateKey , "key" , "" , "hex-encoded private key (cannot be set with --key-file)" )
134+ QueryCmd .MarkFlagsMutuallyExclusive ("key-file" , "key" )
109135 if err := QueryCmd .MarkFlagRequired ("start-block" ); err != nil {
110136 log .Error ().Err (err ).Msg ("Failed to mark start-block as required flag" )
111137 }
0 commit comments