Skip to content

Commit 3bfcc3e

Browse files
committed
feat: add DialOpts
1 parent a358828 commit 3bfcc3e

File tree

4 files changed

+55
-30
lines changed

4 files changed

+55
-30
lines changed

cmd/p2p/crawl/crawl_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (c *crawler) updateNode(n *enode.Node) int {
181181
c.mu.Unlock()
182182
}()
183183

184-
conn, dialErr := p2p.Dial(n)
184+
conn, dialErr := p2p.Dial(n, p2p.NewDialOpts())
185185
if err = dialErr; err != nil {
186186
log.Error().Err(err).Msg("Dial failed")
187187
return nodeDialErr

cmd/p2p/ping/ping.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ping
22

33
import (
44
"crypto/ecdsa"
5+
"net"
56
"sync"
67
"time"
78

@@ -18,10 +19,11 @@ type (
1819
OutputFile string
1920
NodesFile string
2021
Listen bool
21-
Port uint
22-
Addr bool
22+
Port int
23+
Addr net.IP
2324
KeyFile string
2425
PrivateKey string
26+
EnableWit bool
2527

2628
privateKey *ecdsa.PrivateKey
2729
}
@@ -96,7 +98,14 @@ can see other messages the peer sends (e.g. blocks, transactions, etc.).`,
9698
status *p2p.Status
9799
)
98100

99-
conn, err := p2p.Dial(node)
101+
opts := p2p.DialOpts{
102+
EnableWit: inputPingParams.EnableWit,
103+
Port: int(inputPingParams.Port),
104+
Addr: inputPingParams.Addr,
105+
PrivateKey: inputPingParams.privateKey,
106+
}
107+
108+
conn, err := p2p.Dial(node, opts)
100109
if err != nil {
101110
log.Error().Err(err).Msg("Dial failed")
102111
} else {
@@ -150,7 +159,9 @@ argument is an enode/enr, not a nodes file.`)
150159
PingCmd.Flags().BoolVarP(&inputPingParams.Listen, "listen", "l", true,
151160
`Keep the connection open and listen to the peer. This only works if the first
152161
argument is an enode/enr, not a nodes file.`)
153-
PingCmd.Flags().UintVarP(&inputPingParams.Port, "port", "P", 30303, "The port to listen on")
162+
PingCmd.Flags().BoolVarP(&inputPingParams.EnableWit, "wit", "w", false, "Whether to enable the wit/1 capability")
163+
PingCmd.Flags().IntVarP(&inputPingParams.Port, "port", "P", 30303, "Port for discovery protocol")
164+
PingCmd.Flags().IPVarP(&inputPingParams.Addr, "addr", "a", net.ParseIP("127.0.0.1"), "Address to bind discovery listener")
154165
PingCmd.Flags().StringVarP(&inputPingParams.KeyFile, "key-file", "k", "", "Private key file (cannot be set with --key)")
155166
PingCmd.Flags().StringVar(&inputPingParams.PrivateKey, "key", "", "Hex-encoded private key (cannot be set with --key-file)")
156167
PingCmd.MarkFlagsMutuallyExclusive("key-file", "key")

cmd/p2p/query/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ and the amount of blocks to query and print the results.`,
5050
amount uint64 = inputQueryParams.Amount
5151
)
5252

53-
conn, err := p2p.Dial(node)
53+
conn, err := p2p.Dial(node, p2p.NewDialOpts())
5454
if err != nil {
5555
log.Error().Err(err).Msg("Dial failed")
5656
return

p2p/rlpx.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package p2p
22

33
import (
4+
"crypto/ecdsa"
45
"fmt"
56
"math/rand"
67
"net"
@@ -21,39 +22,54 @@ var (
2122
timeout = 20 * time.Second
2223
)
2324

25+
type DialOpts struct {
26+
EnableWit bool
27+
Port int
28+
Addr net.IP
29+
PrivateKey *ecdsa.PrivateKey
30+
}
31+
32+
func NewDialOpts() DialOpts {
33+
privateKey, err := crypto.GenerateKey()
34+
if err != nil {
35+
log.Error().Err(err).Msg("Failed to generate private key")
36+
}
37+
38+
return DialOpts{
39+
EnableWit: false,
40+
Port: 30303,
41+
Addr: net.ParseIP("127.0.0.1"),
42+
PrivateKey: privateKey,
43+
}
44+
}
45+
2446
// Dial attempts to Dial the given node and perform a handshake,
2547
// returning the created Conn if successful.
26-
func Dial(n *enode.Node) (*rlpxConn, error) {
48+
func Dial(n *enode.Node, opts DialOpts) (*rlpxConn, error) {
2749
fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", n.IP(), n.TCP()))
2850
if err != nil {
2951
return nil, err
3052
}
3153

32-
conn := rlpxConn{
33-
Conn: rlpx.NewConn(fd, n.Pubkey()),
34-
node: n,
35-
logger: log.With().Str("peer", n.URLv4()).Logger(),
36-
caps: []p2p.Cap{
37-
{Name: "eth", Version: 66},
38-
{Name: "eth", Version: 67},
39-
{Name: "eth", Version: 68},
40-
{Name: "wit", Version: 1},
41-
},
54+
caps := []p2p.Cap{
55+
{Name: "eth", Version: 66},
56+
{Name: "eth", Version: 67},
57+
{Name: "eth", Version: 68},
4258
}
4359

44-
if conn.ourKey, err = crypto.LoadECDSA("witness.key"); err != nil {
45-
log.Warn().Msg("witness.key not found generating key")
46-
if conn.ourKey, err = crypto.GenerateKey(); err != nil {
47-
return nil, err
48-
}
60+
if opts.EnableWit {
61+
caps = append(caps, p2p.Cap{Name: "wit", Version: 1})
4962
}
5063

51-
if err = crypto.SaveECDSA("witness.key", conn.ourKey); err != nil {
52-
return nil, err
64+
conn := rlpxConn{
65+
Conn: rlpx.NewConn(fd, n.Pubkey()),
66+
node: n,
67+
logger: log.With().Str("peer", n.URLv4()).Logger(),
68+
caps: caps,
69+
ourKey: opts.PrivateKey,
5370
}
5471

55-
ip := net.ParseIP("127.0.0.1")
56-
v4 := enode.NewV4(&conn.ourKey.PublicKey, ip, 30303, 30303)
72+
v4 := enode.NewV4(&conn.ourKey.PublicKey, opts.Addr, opts.Port, opts.Port)
5773

5874
log.Info().Any("enode", v4.String()).Send()
5975

@@ -230,6 +246,7 @@ func (c *rlpxConn) ReadAndServe(count *MessageCount) error {
230246
}
231247

232248
req := GetWitnessPacket{
249+
RequestId: rand.Uint64(),
233250
GetWitnessRequest: &GetWitnessRequest{
234251
WitnessPages: []WitnessPageRequest{
235252
{
@@ -238,9 +255,7 @@ func (c *rlpxConn) ReadAndServe(count *MessageCount) error {
238255
},
239256
},
240257
},
241-
RequestId: uint64(time.Now().Unix()),
242258
}
243-
log.Trace().Any("request", req).Msg("Writing GetWitnessPacket request")
244259
if err := c.Write(req); err != nil {
245260
log.Error().Err(err).Msg("Failed to write GetWitnessPacket request")
246261
}
@@ -250,6 +265,7 @@ func (c *rlpxConn) ReadAndServe(count *MessageCount) error {
250265
c.logger.Trace().Str("hash", msg.Block.Hash().Hex()).Msg("Received NewBlock")
251266

252267
req := GetWitnessPacket{
268+
RequestId: rand.Uint64(),
253269
GetWitnessRequest: &GetWitnessRequest{
254270
WitnessPages: []WitnessPageRequest{
255271
{
@@ -258,9 +274,7 @@ func (c *rlpxConn) ReadAndServe(count *MessageCount) error {
258274
},
259275
},
260276
},
261-
RequestId: uint64(time.Now().Unix()),
262277
}
263-
log.Trace().Any("request", req).Msg("Writing GetWitnessPacket request")
264278
if err := c.Write(req); err != nil {
265279
log.Error().Err(err).Msg("Failed to write GetWitnessPacket request")
266280
}

0 commit comments

Comments
 (0)