1
+ import type { PeerId } from "@libp2p/interface/peer-id" ;
2
+ import type { PeerInfo } from "@libp2p/interface/peer-info" ;
1
3
import { CustomEvent } from "@libp2p/interfaces/events" ;
2
4
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory" ;
3
5
import { EPeersByDiscoveryEvents , LightNode , Tags } from "@waku/interfaces" ;
@@ -49,7 +51,13 @@ describe("ConnectionManager", function () {
49
51
} ) ;
50
52
51
53
waku . libp2p . dispatchEvent (
52
- new CustomEvent ( "peer" , { detail : await createSecp256k1PeerId ( ) } )
54
+ new CustomEvent < PeerInfo > ( "peer:discovery" , {
55
+ detail : {
56
+ id : peerIdBootstrap ,
57
+ multiaddrs : [ ] ,
58
+ protocols : [ ]
59
+ }
60
+ } )
53
61
) ;
54
62
55
63
expect ( await peerDiscoveryBootstrap ) . to . eq ( true ) ;
@@ -77,7 +85,13 @@ describe("ConnectionManager", function () {
77
85
} ) ;
78
86
79
87
waku . libp2p . dispatchEvent (
80
- new CustomEvent ( "peer" , { detail : peerIdPx } )
88
+ new CustomEvent < PeerInfo > ( "peer:discovery" , {
89
+ detail : {
90
+ id : peerIdPx ,
91
+ multiaddrs : [ ] ,
92
+ protocols : [ ]
93
+ }
94
+ } )
81
95
) ;
82
96
83
97
expect ( await peerDiscoveryPeerExchange ) . to . eq ( true ) ;
@@ -109,7 +123,7 @@ describe("ConnectionManager", function () {
109
123
} ) ;
110
124
111
125
waku . libp2p . dispatchEvent (
112
- new CustomEvent ( "peer:connect" , { detail : peerIdBootstrap } )
126
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdBootstrap } )
113
127
) ;
114
128
115
129
expect ( await peerConnectedBootstrap ) . to . eq ( true ) ;
@@ -136,7 +150,7 @@ describe("ConnectionManager", function () {
136
150
} ) ;
137
151
138
152
waku . libp2p . dispatchEvent (
139
- new CustomEvent ( "peer:connect" , { detail : peerIdPx } )
153
+ new CustomEvent < PeerId > ( "peer:connect" , { detail : peerIdPx } )
140
154
) ;
141
155
142
156
expect ( await peerConnectedPeerExchange ) . to . eq ( true ) ;
@@ -182,27 +196,34 @@ describe("ConnectionManager", function () {
182
196
attemptDialSpy . restore ( ) ;
183
197
} ) ;
184
198
185
- it ( "should be called on all `peer:discovery` events" , async function ( ) {
199
+ it ( "should be called at least once on all `peer:discovery` events" , async function ( ) {
186
200
this . timeout ( TEST_TIMEOUT ) ;
187
201
188
202
const totalPeerIds = 5 ;
189
203
for ( let i = 1 ; i <= totalPeerIds ; i ++ ) {
190
204
waku . libp2p . dispatchEvent (
191
- new CustomEvent ( "peer:discovery" , { detail : `peer-id-${ i } ` } )
205
+ new CustomEvent < PeerInfo > ( "peer:discovery" , {
206
+ detail : {
207
+ id : await createSecp256k1PeerId ( ) ,
208
+ multiaddrs : [ ] ,
209
+ protocols : [ ]
210
+ }
211
+ } )
192
212
) ;
193
213
}
194
214
195
- // add delay to allow async function calls within attemptDial to finish
196
215
await delay ( 100 ) ;
197
216
198
- expect ( attemptDialSpy . callCount ) . to . equal (
217
+ expect ( attemptDialSpy . callCount ) . to . be . greaterThanOrEqual (
199
218
totalPeerIds ,
200
- "attemptDial should be called once for each peer:discovery event"
219
+ "attemptDial should be called at least once for each peer:discovery event"
201
220
) ;
202
221
} ) ;
203
222
} ) ;
204
223
205
224
describe ( "dialPeer method" , function ( ) {
225
+ let peerStoreHasStub : SinonStub ;
226
+ let dialAttemptsForPeerHasStub : SinonStub ;
206
227
beforeEach ( function ( ) {
207
228
getConnectionsStub = sinon . stub (
208
229
( waku . connectionManager as any ) . libp2p ,
@@ -213,29 +234,44 @@ describe("ConnectionManager", function () {
213
234
"getTagNamesForPeer"
214
235
) ;
215
236
dialPeerStub = sinon . stub ( waku . connectionManager as any , "dialPeer" ) ;
237
+ peerStoreHasStub = sinon . stub ( waku . libp2p . peerStore , "has" ) ;
238
+ dialAttemptsForPeerHasStub = sinon . stub (
239
+ ( waku . connectionManager as any ) . dialAttemptsForPeer ,
240
+ "has"
241
+ ) ;
242
+
243
+ // simulate that the peer is not connected
244
+ getConnectionsStub . returns ( [ ] ) ;
245
+
246
+ // simulate that the peer is a bootstrap peer
247
+ getTagNamesForPeerStub . resolves ( [ Tags . BOOTSTRAP ] ) ;
248
+
249
+ // simulate that the peer is not in the peerStore
250
+ peerStoreHasStub . returns ( false ) ;
251
+
252
+ // simulate that the peer has not been dialed before
253
+ dialAttemptsForPeerHasStub . returns ( false ) ;
216
254
} ) ;
217
255
218
256
afterEach ( function ( ) {
219
257
dialPeerStub . restore ( ) ;
220
258
getTagNamesForPeerStub . restore ( ) ;
221
259
getConnectionsStub . restore ( ) ;
260
+ peerStoreHasStub . restore ( ) ;
261
+ dialAttemptsForPeerHasStub . restore ( ) ;
222
262
} ) ;
223
263
224
264
describe ( "For bootstrap peers" , function ( ) {
225
265
it ( "should be called for bootstrap peers" , async function ( ) {
226
266
this . timeout ( TEST_TIMEOUT ) ;
227
267
228
- // simulate that the peer is not connected
229
- getConnectionsStub . returns ( [ ] ) ;
230
-
231
- // simulate that the peer is a bootstrap peer
232
- getTagNamesForPeerStub . resolves ( [ Tags . BOOTSTRAP ] ) ;
233
-
234
268
const bootstrapPeer = await createSecp256k1PeerId ( ) ;
235
269
236
270
// emit a peer:discovery event
237
271
waku . libp2p . dispatchEvent (
238
- new CustomEvent ( "peer:discovery" , { detail : bootstrapPeer } )
272
+ new CustomEvent < PeerInfo > ( "peer:discovery" , {
273
+ detail : { id : bootstrapPeer , multiaddrs : [ ] , protocols : [ ] }
274
+ } )
239
275
) ;
240
276
241
277
// wait for the async function calls within attemptDial to finish
@@ -251,15 +287,15 @@ describe("ConnectionManager", function () {
251
287
it ( "should not be called more than DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED times for bootstrap peers" , async function ( ) {
252
288
this . timeout ( TEST_TIMEOUT ) ;
253
289
254
- // simulate that the peer is not connected
255
- getConnectionsStub . returns ( [ ] ) ;
256
-
257
- // simulate that the peer is a bootstrap peer
258
- getTagNamesForPeerStub . resolves ( [ Tags . BOOTSTRAP ] ) ;
259
-
260
290
// emit first peer:discovery event
261
291
waku . libp2p . dispatchEvent (
262
- new CustomEvent ( "peer:discovery" , { detail : "bootstrap-peer" } )
292
+ new CustomEvent < PeerInfo > ( "peer:discovery" , {
293
+ detail : {
294
+ id : await createSecp256k1PeerId ( ) ,
295
+ multiaddrs : [ ] ,
296
+ protocols : [ ]
297
+ }
298
+ } )
263
299
) ;
264
300
await delay ( 500 ) ;
265
301
@@ -271,8 +307,12 @@ describe("ConnectionManager", function () {
271
307
for ( let i = 1 ; i <= totalBootstrapPeers ; i ++ ) {
272
308
await delay ( 500 ) ;
273
309
waku . libp2p . dispatchEvent (
274
- new CustomEvent ( "peer:discovery" , {
275
- detail : await createSecp256k1PeerId ( )
310
+ new CustomEvent < PeerInfo > ( "peer:discovery" , {
311
+ detail : {
312
+ id : await createSecp256k1PeerId ( ) ,
313
+ multiaddrs : [ ] ,
314
+ protocols : [ ]
315
+ }
276
316
} )
277
317
) ;
278
318
}
@@ -289,17 +329,17 @@ describe("ConnectionManager", function () {
289
329
it ( "should be called for peers with PEER_EXCHANGE tags" , async function ( ) {
290
330
this . timeout ( TEST_TIMEOUT ) ;
291
331
292
- // simulate that the peer is not connected
293
- getConnectionsStub . returns ( [ ] ) ;
294
-
295
- // simulate that the peer has a PEER_EXCHANGE tag
296
- getTagNamesForPeerStub . resolves ( [ Tags . PEER_EXCHANGE ] ) ;
297
-
298
332
const pxPeer = await createSecp256k1PeerId ( ) ;
299
333
300
334
// emit a peer:discovery event
301
335
waku . libp2p . dispatchEvent (
302
- new CustomEvent ( "peer:discovery" , { detail : pxPeer } )
336
+ new CustomEvent < PeerInfo > ( "peer:discovery" , {
337
+ detail : {
338
+ id : pxPeer ,
339
+ multiaddrs : [ ] ,
340
+ protocols : [ ]
341
+ }
342
+ } )
303
343
) ;
304
344
305
345
// wait for the async function calls within attemptDial to finish
@@ -315,18 +355,16 @@ describe("ConnectionManager", function () {
315
355
it ( "should be called for every peer with PEER_EXCHANGE tags" , async function ( ) {
316
356
this . timeout ( TEST_TIMEOUT ) ;
317
357
318
- // simulate that the peer is not connected
319
- getConnectionsStub . returns ( [ ] ) ;
320
-
321
- // simulate that the peer has a PEER_EXCHANGE tag
322
- getTagNamesForPeerStub . resolves ( [ Tags . PEER_EXCHANGE ] ) ;
323
-
324
358
// emit multiple peer:discovery events
325
359
const totalPxPeers = 5 ;
326
360
for ( let i = 0 ; i < totalPxPeers ; i ++ ) {
327
361
waku . libp2p . dispatchEvent (
328
- new CustomEvent ( "peer:discovery" , {
329
- detail : await createSecp256k1PeerId ( )
362
+ new CustomEvent < PeerInfo > ( "peer:discovery" , {
363
+ detail : {
364
+ id : await createSecp256k1PeerId ( ) ,
365
+ multiaddrs : [ ] ,
366
+ protocols : [ ]
367
+ }
330
368
} )
331
369
) ;
332
370
await delay ( 500 ) ;
0 commit comments