11package p2p
22
33import (
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