@@ -4,15 +4,20 @@ import (
4
4
"context"
5
5
"crypto/ecdsa"
6
6
"fmt"
7
+ "math"
8
+ "net"
9
+ "strconv"
7
10
"testing"
8
11
"time"
9
12
10
13
gcrypto "github.com/ethereum/go-ethereum/crypto"
11
14
"github.com/ethereum/go-ethereum/p2p/enode"
15
+ "github.com/ethereum/go-ethereum/p2p/enr"
12
16
"github.com/multiformats/go-multiaddr"
13
17
"github.com/status-im/go-waku/tests"
14
18
"github.com/status-im/go-waku/waku/v2/utils"
15
19
"github.com/stretchr/testify/require"
20
+ "go.uber.org/zap"
16
21
17
22
"github.com/libp2p/go-libp2p"
18
23
libp2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
@@ -41,25 +46,87 @@ func createHost(t *testing.T) (host.Host, int, *ecdsa.PrivateKey) {
41
46
return host , port , privKey
42
47
}
43
48
49
+ func newLocalnode (priv * ecdsa.PrivateKey , ipAddr * net.TCPAddr , udpPort int , wakuFlags utils.WakuEnrBitfield , advertiseAddr * net.IP , log * zap.Logger ) (* enode.LocalNode , error ) {
50
+ db , err := enode .OpenDB ("" )
51
+ if err != nil {
52
+ return nil , err
53
+ }
54
+ localnode := enode .NewLocalNode (db , priv )
55
+ localnode .SetFallbackUDP (udpPort )
56
+ localnode .Set (enr .WithEntry (utils .WakuENRField , wakuFlags ))
57
+ localnode .SetFallbackIP (net.IP {127 , 0 , 0 , 1 })
58
+ localnode .SetStaticIP (ipAddr .IP )
59
+
60
+ if udpPort > 0 && udpPort <= math .MaxUint16 {
61
+ localnode .Set (enr .UDP (uint16 (udpPort ))) // lgtm [go/incorrect-integer-conversion]
62
+ } else {
63
+ log .Error ("setting udpPort" , zap .Int ("port" , udpPort ))
64
+ }
65
+
66
+ if ipAddr .Port > 0 && ipAddr .Port <= math .MaxUint16 {
67
+ localnode .Set (enr .TCP (uint16 (ipAddr .Port ))) // lgtm [go/incorrect-integer-conversion]
68
+ } else {
69
+ log .Error ("setting tcpPort" , zap .Int ("port" , ipAddr .Port ))
70
+ }
71
+
72
+ if advertiseAddr != nil {
73
+ localnode .SetStaticIP (* advertiseAddr )
74
+ }
75
+
76
+ return localnode , nil
77
+ }
78
+
79
+ func extractIP (addr multiaddr.Multiaddr ) (* net.TCPAddr , error ) {
80
+ ipStr , err := addr .ValueForProtocol (multiaddr .P_IP4 )
81
+ if err != nil {
82
+ return nil , err
83
+ }
84
+
85
+ portStr , err := addr .ValueForProtocol (multiaddr .P_TCP )
86
+ if err != nil {
87
+ return nil , err
88
+ }
89
+ port , err := strconv .Atoi (portStr )
90
+ if err != nil {
91
+ return nil , err
92
+ }
93
+ return & net.TCPAddr {
94
+ IP : net .ParseIP (ipStr ),
95
+ Port : port ,
96
+ }, nil
97
+ }
98
+
44
99
func TestDiscV5 (t * testing.T ) {
45
100
// Host1 <-> Host2 <-> Host3
46
101
102
+ // H1
47
103
host1 , _ , prvKey1 := createHost (t )
48
104
udpPort1 , err := tests .FindFreePort (t , "127.0.0.1" , 3 )
49
105
require .NoError (t , err )
50
- d1 , err := NewDiscoveryV5 (host1 , host1 .Addrs (), prvKey1 , utils .NewWakuEnrBitfield (true , true , true , true ), utils .Logger (), WithUDPPort (udpPort1 ))
106
+ ip1 , _ := extractIP (host1 .Addrs ()[0 ])
107
+ l1 , err := newLocalnode (prvKey1 , ip1 , udpPort1 , utils .NewWakuEnrBitfield (true , true , true , true ), nil , utils .Logger ())
108
+ require .NoError (t , err )
109
+ d1 , err := NewDiscoveryV5 (host1 , prvKey1 , l1 , utils .Logger (), WithUDPPort (udpPort1 ))
51
110
require .NoError (t , err )
52
111
112
+ // H2
53
113
host2 , _ , prvKey2 := createHost (t )
114
+ ip2 , _ := extractIP (host2 .Addrs ()[0 ])
54
115
udpPort2 , err := tests .FindFreePort (t , "127.0.0.1" , 3 )
55
116
require .NoError (t , err )
56
- d2 , err := NewDiscoveryV5 (host2 , host2 .Addrs (), prvKey2 , utils .NewWakuEnrBitfield (true , true , true , true ), utils .Logger (), WithUDPPort (udpPort2 ), WithBootnodes ([]* enode.Node {d1 .localnode .Node ()}))
117
+ l2 , err := newLocalnode (prvKey2 , ip2 , udpPort2 , utils .NewWakuEnrBitfield (true , true , true , true ), nil , utils .Logger ())
118
+ require .NoError (t , err )
119
+ d2 , err := NewDiscoveryV5 (host2 , prvKey2 , l2 , utils .Logger (), WithUDPPort (udpPort2 ), WithBootnodes ([]* enode.Node {d1 .localnode .Node ()}))
57
120
require .NoError (t , err )
58
121
122
+ // H3
59
123
host3 , _ , prvKey3 := createHost (t )
124
+ ip3 , _ := extractIP (host3 .Addrs ()[0 ])
60
125
udpPort3 , err := tests .FindFreePort (t , "127.0.0.1" , 3 )
61
126
require .NoError (t , err )
62
- d3 , err := NewDiscoveryV5 (host3 , host3 .Addrs (), prvKey3 , utils .NewWakuEnrBitfield (true , true , true , true ), utils .Logger (), WithUDPPort (udpPort3 ), WithBootnodes ([]* enode.Node {d2 .localnode .Node ()}))
127
+ l3 , err := newLocalnode (prvKey3 , ip3 , udpPort3 , utils .NewWakuEnrBitfield (true , true , true , true ), nil , utils .Logger ())
128
+ require .NoError (t , err )
129
+ d3 , err := NewDiscoveryV5 (host3 , prvKey3 , l3 , utils .Logger (), WithUDPPort (udpPort3 ), WithBootnodes ([]* enode.Node {d2 .localnode .Node ()}))
63
130
require .NoError (t , err )
64
131
65
132
defer d1 .Stop ()
@@ -75,7 +142,7 @@ func TestDiscV5(t *testing.T) {
75
142
err = d3 .Start ()
76
143
require .NoError (t , err )
77
144
78
- ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
145
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
79
146
defer cancel ()
80
147
81
148
peerChan , err := d3 .FindPeers (ctx , "" , discovery .Limit (2 ))
0 commit comments