1
1
{.used .}
2
2
3
3
import
4
- std/ [options, sequtils, times, sugar ],
4
+ std/ [options, sequtils, times],
5
5
stew/ shims/ net as stewNet,
6
6
testutils/ unittests,
7
7
chronos,
@@ -21,12 +21,10 @@ import
21
21
../../ waku/ node/ peer_manager/ peer_manager,
22
22
../../ waku/ node/ peer_manager/ peer_store/ waku_peer_storage,
23
23
../../ waku/ waku_node,
24
- ../../ waku/ waku_core,
25
- ../../ waku/ waku_enr/ capabilities,
26
- ../../ waku/ waku_relay/ protocol,
27
- ../../ waku/ waku_store/ common,
28
- ../../ waku/ waku_filter/ protocol,
29
- ../../ waku/ waku_lightpush/ common,
24
+ ../../ waku/ waku_relay,
25
+ ../../ waku/ waku_store,
26
+ ../../ waku/ waku_filter,
27
+ ../../ waku/ waku_lightpush,
30
28
../../ waku/ waku_peer_exchange,
31
29
../../ waku/ waku_metadata,
32
30
./ testlib/ common,
37
35
procSuite " Peer Manager" :
38
36
asyncTest " connectRelay() works" :
39
37
# Create 2 nodes
40
- let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 )))
38
+ let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 )))
41
39
await allFutures (nodes.mapIt (it.start ()))
42
40
43
41
let connOk = await nodes[0 ].peerManager.connectRelay (nodes[1 ].peerInfo.toRemotePeerInfo ())
@@ -50,7 +48,7 @@ procSuite "Peer Manager":
50
48
51
49
asyncTest " dialPeer() works" :
52
50
# Create 2 nodes
53
- let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 )))
51
+ let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 )))
54
52
55
53
await allFutures (nodes.mapIt (it.start ()))
56
54
await allFutures (nodes.mapIt (it.mountRelay ()))
@@ -78,7 +76,7 @@ procSuite "Peer Manager":
78
76
79
77
asyncTest " dialPeer() fails gracefully" :
80
78
# Create 2 nodes and start them
81
- let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 )))
79
+ let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 )))
82
80
await allFutures (nodes.mapIt (it.start ()))
83
81
await allFutures (nodes.mapIt (it.mountRelay ()))
84
82
@@ -101,7 +99,7 @@ procSuite "Peer Manager":
101
99
102
100
asyncTest " Adding, selecting and filtering peers work" :
103
101
let
104
- node = newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 ))
102
+ node = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 ))
105
103
106
104
# Create filter peer
107
105
filterLoc = MultiAddress .init (" /ip4/127.0.0.1/tcp/0" ).tryGet ()
@@ -130,9 +128,10 @@ procSuite "Peer Manager":
130
128
131
129
await node.stop ()
132
130
131
+
133
132
asyncTest " Peer manager keeps track of connections" :
134
133
# Create 2 nodes
135
- let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 )))
134
+ let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 )))
136
135
137
136
await allFutures (nodes.mapIt (it.start ()))
138
137
await allFutures (nodes.mapIt (it.mountRelay ()))
@@ -176,7 +175,7 @@ procSuite "Peer Manager":
176
175
177
176
asyncTest " Peer manager updates failed peers correctly" :
178
177
# Create 2 nodes
179
- let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 )))
178
+ let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 )))
180
179
181
180
await allFutures (nodes.mapIt (it.start ()))
182
181
await allFutures (nodes.mapIt (it.mountRelay ()))
@@ -226,34 +225,18 @@ procSuite "Peer Manager":
226
225
let
227
226
database = SqliteDatabase .new (" :memory:" )[]
228
227
storage = WakuPeerStorage .new (database)[]
229
- node1 = newTestWakuNode (
230
- generateSecp256k1Key (),
231
- parseIpAddress (" 127.0.0.1" ),
232
- Port (44048 ),
233
- peerStorage = storage
234
- )
235
- node2 = newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 127.0.0.1" ), Port (34023 ))
236
-
237
- node1.mountMetadata (0 ).expect (" Mounted Waku Metadata" )
238
- node2.mountMetadata (0 ).expect (" Mounted Waku Metadata" )
228
+ node1 = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress .init (" 0.0.0.0" ), Port (0 ), peerStorage = storage)
229
+ node2 = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress .init (" 0.0.0.0" ), Port (0 ))
230
+ peerInfo2 = node2.switch.peerInfo
239
231
240
232
await node1.start ()
241
233
await node2.start ()
242
234
243
235
await node1.mountRelay ()
244
236
await node2.mountRelay ()
245
-
246
- let peerInfo2 = node2.switch.peerInfo
247
- var remotePeerInfo2 = peerInfo2.toRemotePeerInfo ()
248
- remotePeerInfo2.enr = some (node2.enr)
249
-
250
- let is12Connected = await node1.peerManager.connectRelay (remotePeerInfo2)
251
- assert is12Connected == true , " Node 1 and 2 not connected"
252
237
253
- check:
254
- node1.peerManager.peerStore[AddressBook ][remotePeerInfo2.peerId] == remotePeerInfo2.addrs
255
-
256
- # wait for the peer store update
238
+ require:
239
+ (await node1.peerManager.connectRelay (peerInfo2.toRemotePeerInfo ())) == true
257
240
await sleepAsync (chronos.milliseconds (500 ))
258
241
259
242
check:
@@ -263,28 +246,18 @@ procSuite "Peer Manager":
263
246
node1.peerManager.peerStore.connectedness (peerInfo2.peerId) == Connected
264
247
265
248
# Simulate restart by initialising a new node using the same storage
266
- let node3 = newTestWakuNode (
267
- generateSecp256k1Key (),
268
- parseIpAddress (" 127.0.0.1" ),
269
- Port (56037 ),
270
- peerStorage = storage
271
- )
272
-
273
- node3.mountMetadata (0 ).expect (" Mounted Waku Metadata" )
249
+ let
250
+ node3 = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress .init (" 0.0.0.0" ), Port (0 ), peerStorage = storage)
274
251
275
252
await node3.start ()
276
-
277
253
check:
278
254
# Node2 has been loaded after "restart", but we have not yet reconnected
279
255
node3.peerManager.peerStore.peers ().len == 1
280
256
node3.peerManager.peerStore.peers ().anyIt (it.peerId == peerInfo2.peerId)
281
257
node3.peerManager.peerStore.connectedness (peerInfo2.peerId) == NotConnected
282
258
283
259
await node3.mountRelay ()
284
-
285
- await node3.peerManager.manageRelayPeers ()
286
-
287
- await sleepAsync (chronos.milliseconds (500 ))
260
+ await node3.peerManager.connectToRelayPeers ()
288
261
289
262
check:
290
263
# Reconnected to node2 after "restart"
@@ -302,7 +275,7 @@ procSuite "Peer Manager":
302
275
# different network
303
276
node1 = newTestWakuNode (
304
277
generateSecp256k1Key (),
305
- parseIpAddress (" 0.0.0.0" ),
278
+ ValidIpAddress . init (" 0.0.0.0" ),
306
279
Port (0 ),
307
280
clusterId = clusterId3,
308
281
topics = @ [" /waku/2/rs/3/0" ],
@@ -311,22 +284,22 @@ procSuite "Peer Manager":
311
284
# same network
312
285
node2 = newTestWakuNode (
313
286
generateSecp256k1Key (),
314
- parseIpAddress (" 0.0.0.0" ),
287
+ ValidIpAddress . init (" 0.0.0.0" ),
315
288
Port (0 ),
316
289
clusterId = clusterId4,
317
290
topics = @ [" /waku/2/rs/4/0" ],
318
291
)
319
292
node3 = newTestWakuNode (
320
293
generateSecp256k1Key (),
321
- parseIpAddress (" 0.0.0.0" ),
294
+ ValidIpAddress . init (" 0.0.0.0" ),
322
295
Port (0 ),
323
296
clusterId = clusterId4,
324
297
topics = @ [" /waku/2/rs/4/0" ],
325
298
)
326
299
327
- node1.mountMetadata (clusterId3). expect ( " Mounted Waku Metadata " )
328
- node2.mountMetadata (clusterId4). expect ( " Mounted Waku Metadata " )
329
- node3.mountMetadata (clusterId4). expect ( " Mounted Waku Metadata " )
300
+ discard node1.mountMetadata (clusterId3)
301
+ discard node2.mountMetadata (clusterId4)
302
+ discard node3.mountMetadata (clusterId4)
330
303
331
304
# Start nodes
332
305
await allFutures ([node1.start (), node2.start (), node3.start ()])
@@ -345,13 +318,14 @@ procSuite "Peer Manager":
345
318
conn2.isNone
346
319
conn3.isSome
347
320
321
+
348
322
# TODO: nwaku/issues/1377
349
323
xasyncTest " Peer manager support multiple protocol IDs when reconnecting to peers" :
350
324
let
351
325
database = SqliteDatabase .new (" :memory:" )[]
352
326
storage = WakuPeerStorage .new (database)[]
353
- node1 = newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 ), peerStorage = storage)
354
- node2 = newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 ))
327
+ node1 = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 ), peerStorage = storage)
328
+ node2 = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 ))
355
329
peerInfo2 = node2.switch.peerInfo
356
330
betaCodec = " /vac/waku/relay/2.0.0-beta2"
357
331
stableCodec = " /vac/waku/relay/2.0.0"
@@ -375,7 +349,7 @@ procSuite "Peer Manager":
375
349
376
350
# Simulate restart by initialising a new node using the same storage
377
351
let
378
- node3 = newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 ), peerStorage = storage)
352
+ node3 = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 ), peerStorage = storage)
379
353
380
354
await node3.mountRelay ()
381
355
node3.wakuRelay.codec = stableCodec
@@ -403,36 +377,22 @@ procSuite "Peer Manager":
403
377
404
378
asyncTest " Peer manager connects to all peers supporting a given protocol" :
405
379
# Create 4 nodes
406
- let nodes =
407
- toSeq (0 ..< 4 )
408
- .mapIt (
409
- newTestWakuNode (
410
- nodeKey = generateSecp256k1Key (),
411
- bindIp = parseIpAddress (" 0.0.0.0" ),
412
- bindPort = Port (0 ),
413
- wakuFlags = some (CapabilitiesBitfield .init (@ [Relay ]))
414
- )
415
- )
380
+ let nodes = toSeq (0 ..< 4 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress .init (" 0.0.0.0" ), Port (0 )))
416
381
417
382
# Start them
418
- discard nodes.mapIt (it.mountMetadata (0 ))
419
- await allFutures (nodes.mapIt (it.mountRelay ()))
420
383
await allFutures (nodes.mapIt (it.start ()))
384
+ await allFutures (nodes.mapIt (it.mountRelay ()))
421
385
422
386
# Get all peer infos
423
- let peerInfos = collect:
424
- for i in 0 .. nodes.high:
425
- let peerInfo = nodes[i].switch.peerInfo.toRemotePeerInfo ()
426
- peerInfo.enr = some (nodes[i].enr)
427
- peerInfo
387
+ let peerInfos = nodes.mapIt (it.switch.peerInfo.toRemotePeerInfo ())
428
388
429
389
# Add all peers (but self) to node 0
430
390
nodes[0 ].peerManager.addPeer (peerInfos[1 ])
431
391
nodes[0 ].peerManager.addPeer (peerInfos[2 ])
432
392
nodes[0 ].peerManager.addPeer (peerInfos[3 ])
433
393
434
394
# Connect to relay peers
435
- await nodes[0 ].peerManager.manageRelayPeers ()
395
+ await nodes[0 ].peerManager.connectToRelayPeers ()
436
396
437
397
check:
438
398
# Peerstore track all three peers
@@ -457,7 +417,7 @@ procSuite "Peer Manager":
457
417
458
418
asyncTest " Peer store keeps track of incoming connections" :
459
419
# Create 4 nodes
460
- let nodes = toSeq (0 ..< 4 ).mapIt (newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 )))
420
+ let nodes = toSeq (0 ..< 4 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 )))
461
421
462
422
# Start them
463
423
await allFutures (nodes.mapIt (it.start ()))
@@ -520,7 +480,7 @@ procSuite "Peer Manager":
520
480
let basePeerId = " 16Uiu2HAm7QGEZKujdSbbo1aaQyfDPQ6Bw3ybQnj6fruH5Dxwd7D"
521
481
522
482
let
523
- node = newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 ))
483
+ node = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 ))
524
484
peers = toSeq (1 .. 5 )
525
485
.mapIt (
526
486
parsePeerInfo (" /ip4/0.0.0.0/tcp/30300/p2p/" & basePeerId & $ it)
@@ -562,7 +522,7 @@ procSuite "Peer Manager":
562
522
563
523
asyncTest " connectedPeers() returns expected number of connections per protocol" :
564
524
# Create 4 nodes
565
- let nodes = toSeq (0 ..< 4 ).mapIt (newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 )))
525
+ let nodes = toSeq (0 ..< 4 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 )))
566
526
567
527
# Start them with relay + filter
568
528
await allFutures (nodes.mapIt (it.start ()))
@@ -613,7 +573,7 @@ procSuite "Peer Manager":
613
573
614
574
asyncTest " getNumStreams() returns expected number of connections per protocol" :
615
575
# Create 2 nodes
616
- let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 )))
576
+ let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 )))
617
577
618
578
# Start them with relay + filter
619
579
await allFutures (nodes.mapIt (it.start ()))
@@ -839,7 +799,7 @@ procSuite "Peer Manager":
839
799
840
800
asyncTest " colocationLimit is enforced by pruneConnsByIp()" :
841
801
# Create 5 nodes
842
- let nodes = toSeq (0 ..< 5 ).mapIt (newTestWakuNode (generateSecp256k1Key (), parseIpAddress (" 0.0.0.0" ), Port (0 )))
802
+ let nodes = toSeq (0 ..< 5 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress . init (" 0.0.0.0" ), Port (0 )))
843
803
844
804
# Start them with relay + filter
845
805
await allFutures (nodes.mapIt (it.start ()))
0 commit comments