1
1
{.used .}
2
2
3
3
import
4
- std/ [options, sequtils, times],
4
+ std/ [options, sequtils, times, sugar ],
5
5
stew/ shims/ net as stewNet,
6
6
testutils/ unittests,
7
7
chronos,
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/ topics,
24
25
../../ waku/ waku_relay,
25
26
../../ waku/ waku_store,
26
27
../../ waku/ waku_filter,
@@ -128,7 +129,6 @@ procSuite "Peer Manager":
128
129
129
130
await node.stop ()
130
131
131
-
132
132
asyncTest " Peer manager keeps track of connections" :
133
133
# Create 2 nodes
134
134
let nodes = toSeq (0 ..< 2 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress .init (" 0.0.0.0" ), Port (0 )))
@@ -225,18 +225,36 @@ procSuite "Peer Manager":
225
225
let
226
226
database = SqliteDatabase .new (" :memory:" )[]
227
227
storage = WakuPeerStorage .new (database)[]
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
228
+ node1 = newTestWakuNode (
229
+ generateSecp256k1Key (),
230
+ ValidIpAddress .init (" 127.0.0.1" ),
231
+ Port (44048 ),
232
+ peerStorage = storage
233
+ )
234
+ node2 = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress .init (" 127.0.0.1" ), Port (34023 ))
235
+
236
+ node1.mountMetadata (0 ).expect (" Mounted Waku Metadata" )
237
+ node2.mountMetadata (0 ).expect (" Mounted Waku Metadata" )
231
238
232
239
await node1.start ()
233
240
await node2.start ()
234
241
235
242
await node1.mountRelay ()
236
243
await node2.mountRelay ()
244
+
245
+ let peerInfo2 = node2.switch.peerInfo
246
+ var remotePeerInfo2 = peerInfo2.toRemotePeerInfo ()
247
+ remotePeerInfo2.enr = some (node2.enr)
237
248
238
- require:
239
- (await node1.peerManager.connectRelay (peerInfo2.toRemotePeerInfo ())) == true
249
+ let is12Connected = await node1.peerManager.connectRelay (remotePeerInfo2)
250
+ assert is12Connected == true , " Node 1 and 2 not connected"
251
+
252
+ # When node use 0.0.0.0 and port 0
253
+ # After connecting the peer store is updated with the wrong address
254
+ check:
255
+ node1.peerManager.peerStore[AddressBook ][remotePeerInfo2.peerId] == remotePeerInfo2.addrs
256
+
257
+ # wait for the peer store update
240
258
await sleepAsync (chronos.milliseconds (500 ))
241
259
242
260
check:
@@ -246,24 +264,34 @@ procSuite "Peer Manager":
246
264
node1.peerManager.peerStore.connectedness (peerInfo2.peerId) == Connected
247
265
248
266
# Simulate restart by initialising a new node using the same storage
249
- let
250
- node3 = newTestWakuNode (generateSecp256k1Key (), ValidIpAddress .init (" 0.0.0.0" ), Port (0 ), peerStorage = storage)
267
+ let node3 = newTestWakuNode (
268
+ generateSecp256k1Key (),
269
+ ValidIpAddress .init (" 127.0.0.1" ),
270
+ Port (56037 ),
271
+ peerStorage = storage
272
+ )
273
+
274
+ node3.mountMetadata (0 ).expect (" Mounted Waku Metadata" )
251
275
252
276
await node3.start ()
277
+
253
278
check:
254
279
# Node2 has been loaded after "restart", but we have not yet reconnected
255
280
node3.peerManager.peerStore.peers ().len == 1
256
281
node3.peerManager.peerStore.peers ().anyIt (it.peerId == peerInfo2.peerId)
257
282
node3.peerManager.peerStore.connectedness (peerInfo2.peerId) == NotConnected
258
283
259
284
await node3.mountRelay ()
260
- await node3.peerManager.connectToRelayPeers ()
285
+
286
+ await node3.peerManager.manageRelayPeers ()
287
+
288
+ await sleepAsync (chronos.milliseconds (500 ))
261
289
262
290
check:
263
291
# Reconnected to node2 after "restart"
264
292
node3.peerManager.peerStore.peers ().len == 1
265
293
node3.peerManager.peerStore.peers ().anyIt (it.peerId == peerInfo2.peerId)
266
- node3.peerManager.peerStore.connectedness (peerInfo2.peerId) == Connected
294
+ node3.peerManager.peerStore.connectedness (peerInfo2.peerId) == Connected
267
295
268
296
await allFutures ([node1.stop (), node2.stop (), node3.stop ()])
269
297
@@ -297,9 +325,9 @@ procSuite "Peer Manager":
297
325
topics = @ [" /waku/2/rs/4/0" ],
298
326
)
299
327
300
- discard node1.mountMetadata (clusterId3)
301
- discard node2.mountMetadata (clusterId4)
302
- discard node3.mountMetadata (clusterId4)
328
+ node1.mountMetadata (clusterId3). expect ( " Mounted Waku Metadata " )
329
+ node2.mountMetadata (clusterId4). expect ( " Mounted Waku Metadata " )
330
+ node3.mountMetadata (clusterId4). expect ( " Mounted Waku Metadata " )
303
331
304
332
# Start nodes
305
333
await allFutures ([node1.start (), node2.start (), node3.start ()])
@@ -318,7 +346,6 @@ procSuite "Peer Manager":
318
346
conn2.isNone
319
347
conn3.isSome
320
348
321
-
322
349
# TODO: nwaku/issues/1377
323
350
xasyncTest " Peer manager support multiple protocol IDs when reconnecting to peers" :
324
351
let
@@ -380,19 +407,24 @@ procSuite "Peer Manager":
380
407
let nodes = toSeq (0 ..< 4 ).mapIt (newTestWakuNode (generateSecp256k1Key (), ValidIpAddress .init (" 0.0.0.0" ), Port (0 )))
381
408
382
409
# Start them
383
- await allFutures ( nodes.mapIt (it.start () ))
410
+ discard nodes.mapIt (it.mountMetadata ( 0 ))
384
411
await allFutures (nodes.mapIt (it.mountRelay ()))
412
+ await allFutures (nodes.mapIt (it.start ()))
385
413
386
414
# Get all peer infos
387
- let peerInfos = nodes.mapIt (it.switch.peerInfo.toRemotePeerInfo ())
415
+ let peerInfos = collect:
416
+ for i in 0 .. nodes.high:
417
+ let peerInfo = nodes[i].switch.peerInfo.toRemotePeerInfo ()
418
+ peerInfo.enr = some (nodes[i].enr)
419
+ peerInfo
388
420
389
421
# Add all peers (but self) to node 0
390
422
nodes[0 ].peerManager.addPeer (peerInfos[1 ])
391
423
nodes[0 ].peerManager.addPeer (peerInfos[2 ])
392
424
nodes[0 ].peerManager.addPeer (peerInfos[3 ])
393
425
394
426
# Connect to relay peers
395
- await nodes[0 ].peerManager.connectToRelayPeers ()
427
+ await nodes[0 ].peerManager.manageRelayPeers ()
396
428
397
429
check:
398
430
# Peerstore track all three peers
0 commit comments