@@ -4,6 +4,7 @@ import type { Peer } from "@libp2p/interface/peer-store";
4
4
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory" ;
5
5
import { LightPushCodec , waitForRemotePeer } from "@waku/core" ;
6
6
import {
7
+ ContentTopicInfo ,
7
8
createLightNode ,
8
9
Libp2pComponents ,
9
10
type LightNode ,
@@ -26,6 +27,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
26
27
let waku : LightNode ;
27
28
let serviceNode1 : NimGoNode ;
28
29
let serviceNode2 : NimGoNode ;
30
+ const contentTopic = "/test/2/waku-light-push/utf8" ;
29
31
30
32
this . beforeEach ( async function ( ) {
31
33
this . timeout ( 15000 ) ;
@@ -51,7 +53,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
51
53
peerExchange : true ,
52
54
clusterId : shardInfo . clusterId ,
53
55
pubsubTopic : shardInfoToPubsubTopics ( shardInfo ) ,
54
- lightpush : true
56
+ lightpush : true ,
57
+ relay : true
55
58
} ) ;
56
59
57
60
const serviceNodeMa = await serviceNode1 . getMultiaddrWithId ( ) ;
@@ -77,12 +80,18 @@ describe("getConnectedPeersForProtocolAndShard", function () {
77
80
shards : [ 1 ]
78
81
} ;
79
82
83
+ const shardInfoServiceNode : ShardInfo = {
84
+ clusterId : 1 ,
85
+ shards : [ 2 ]
86
+ } ;
87
+
80
88
await serviceNode1 . start ( {
81
89
discv5Discovery : true ,
82
90
peerExchange : true ,
83
- clusterId : shardInfo . clusterId ,
84
- pubsubTopic : shardInfoToPubsubTopics ( shardInfo ) ,
85
- lightpush : true
91
+ clusterId : shardInfoServiceNode . clusterId ,
92
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfoServiceNode ) ,
93
+ lightpush : true ,
94
+ relay : true
86
95
} ) ;
87
96
88
97
const serviceNodeMa = await serviceNode1 . getMultiaddrWithId ( ) ;
@@ -120,7 +129,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
120
129
peerExchange : true ,
121
130
clusterId : shardInfo1 . clusterId ,
122
131
pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 ) ,
123
- lightpush : true
132
+ lightpush : true ,
133
+ relay : true
124
134
} ) ;
125
135
126
136
// and another node in the same cluster cluster as our node
@@ -129,7 +139,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
129
139
peerExchange : true ,
130
140
clusterId : shardInfo2 . clusterId ,
131
141
pubsubTopic : shardInfoToPubsubTopics ( shardInfo2 ) ,
132
- lightpush : true
142
+ lightpush : true ,
143
+ relay : true
133
144
} ) ;
134
145
135
146
const serviceNode1Ma = await serviceNode1 . getMultiaddrWithId ( ) ;
@@ -170,7 +181,196 @@ describe("getConnectedPeersForProtocolAndShard", function () {
170
181
peerExchange : true ,
171
182
clusterId : shardInfo1 . clusterId ,
172
183
pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 ) ,
173
- lightpush : true
184
+ lightpush : true ,
185
+ relay : true
186
+ } ) ;
187
+
188
+ // and another node in the same cluster cluster as our node
189
+ const serviceNode2 = new NimGoNode ( makeLogFileName ( this ) + "2" ) ;
190
+ await serviceNode2 . start ( {
191
+ discv5Discovery : true ,
192
+ peerExchange : true ,
193
+ clusterId : shardInfo2 . clusterId ,
194
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo2 ) ,
195
+ lightpush : true ,
196
+ relay : true
197
+ } ) ;
198
+
199
+ const serviceNodeMa1 = await serviceNode1 . getMultiaddrWithId ( ) ;
200
+ const serviceNodeMa2 = await serviceNode2 . getMultiaddrWithId ( ) ;
201
+
202
+ waku = await createLightNode ( { shardInfo : shardInfo2 } ) ;
203
+ await waku . libp2p . dialProtocol ( serviceNodeMa1 , LightPushCodec ) ;
204
+ await waku . libp2p . dialProtocol ( serviceNodeMa2 , LightPushCodec ) ;
205
+ await waku . start ( ) ;
206
+ await waitForRemotePeer ( waku , [ Protocols . LightPush ] ) ;
207
+
208
+ const peers = await getConnectedPeersForProtocolAndShard (
209
+ waku . libp2p . getConnections ( ) ,
210
+ waku . libp2p . peerStore ,
211
+ waku . libp2p . getProtocols ( ) ,
212
+ shardInfo2
213
+ ) ;
214
+ expect ( peers . length ) . to . be . equal ( 1 ) ;
215
+ } ) ;
216
+
217
+ it ( "same cluster, same shard: nodes connect (autosharding)" , async function ( ) {
218
+ this . timeout ( 15000 ) ;
219
+
220
+ const shardInfo : ContentTopicInfo = {
221
+ clusterId : 1 ,
222
+ contentTopics : [ contentTopic ]
223
+ } ;
224
+
225
+ await serviceNode1 . start ( {
226
+ discv5Discovery : true ,
227
+ peerExchange : true ,
228
+ clusterId : shardInfo . clusterId ,
229
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo ) ,
230
+ lightpush : true ,
231
+ relay : true
232
+ } ) ;
233
+
234
+ const serviceNodeMa = await serviceNode1 . getMultiaddrWithId ( ) ;
235
+
236
+ waku = await createLightNode ( { shardInfo } ) ;
237
+ await waku . start ( ) ;
238
+ await waku . libp2p . dialProtocol ( serviceNodeMa , LightPushCodec ) ;
239
+ await waitForRemotePeer ( waku , [ Protocols . LightPush ] ) ;
240
+ const peers = await getConnectedPeersForProtocolAndShard (
241
+ waku . libp2p . getConnections ( ) ,
242
+ waku . libp2p . peerStore ,
243
+ waku . libp2p . getProtocols ( ) ,
244
+ shardInfo
245
+ ) ;
246
+ expect ( peers . length ) . to . be . greaterThan ( 0 ) ;
247
+ } ) ;
248
+
249
+ it ( "same cluster, different shard: nodes connect (autosharding)" , async function ( ) {
250
+ this . timeout ( 15000 ) ;
251
+
252
+ const shardInfo1 : ContentTopicInfo = {
253
+ clusterId : 1 ,
254
+ contentTopics : [ contentTopic ]
255
+ } ;
256
+
257
+ const shardInfo2 : ContentTopicInfo = {
258
+ clusterId : 1 ,
259
+ contentTopics : [ "/test/5/waku-light-push/utf8" ]
260
+ } ;
261
+
262
+ // Separate shard
263
+ await serviceNode1 . start ( {
264
+ discv5Discovery : true ,
265
+ peerExchange : true ,
266
+ clusterId : shardInfo1 . clusterId ,
267
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 ) ,
268
+ lightpush : true ,
269
+ relay : true
270
+ } ) ;
271
+
272
+ // Same shard
273
+ await serviceNode2 . start ( {
274
+ discv5Discovery : true ,
275
+ peerExchange : true ,
276
+ clusterId : shardInfo2 . clusterId ,
277
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo2 ) ,
278
+ lightpush : true ,
279
+ relay : true
280
+ } ) ;
281
+
282
+ const serviceNode1Ma = await serviceNode1 . getMultiaddrWithId ( ) ;
283
+ const serviceNode2Ma = await serviceNode2 . getMultiaddrWithId ( ) ;
284
+
285
+ waku = await createLightNode ( { shardInfo : shardInfo2 } ) ;
286
+ await waku . libp2p . dialProtocol ( serviceNode1Ma , LightPushCodec ) ;
287
+ await waku . libp2p . dialProtocol ( serviceNode2Ma , LightPushCodec ) ;
288
+
289
+ await waku . start ( ) ;
290
+ await waitForRemotePeer ( waku , [ Protocols . LightPush ] ) ;
291
+
292
+ const peers = await getConnectedPeersForProtocolAndShard (
293
+ waku . libp2p . getConnections ( ) ,
294
+ waku . libp2p . peerStore ,
295
+ waku . libp2p . getProtocols ( ) ,
296
+ shardInfo2
297
+ ) ;
298
+ expect ( peers . length ) . to . be . equal ( 1 ) ;
299
+ } ) ;
300
+
301
+ it ( "different cluster, same shard: nodes don't connect (autosharding)" , async function ( ) {
302
+ this . timeout ( 15000 ) ;
303
+
304
+ const shardInfo1 : ContentTopicInfo = {
305
+ clusterId : 1 ,
306
+ contentTopics : [ contentTopic ]
307
+ } ;
308
+
309
+ const shardInfo2 : ContentTopicInfo = {
310
+ clusterId : 2 ,
311
+ contentTopics : [ contentTopic ]
312
+ } ;
313
+
314
+ // we start one node in a separate cluster
315
+ await serviceNode1 . start ( {
316
+ discv5Discovery : true ,
317
+ peerExchange : true ,
318
+ clusterId : shardInfo1 . clusterId ,
319
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 ) ,
320
+ lightpush : true ,
321
+ relay : true
322
+ } ) ;
323
+
324
+ // and another node in the same cluster cluster as our node
325
+ await serviceNode2 . start ( {
326
+ discv5Discovery : true ,
327
+ peerExchange : true ,
328
+ clusterId : shardInfo2 . clusterId ,
329
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo2 ) ,
330
+ lightpush : true ,
331
+ relay : true
332
+ } ) ;
333
+
334
+ const serviceNode1Ma = await serviceNode1 . getMultiaddrWithId ( ) ;
335
+ const serviceNode2Ma = await serviceNode2 . getMultiaddrWithId ( ) ;
336
+
337
+ waku = await createLightNode ( { shardInfo : shardInfo2 } ) ;
338
+ await waku . libp2p . dialProtocol ( serviceNode1Ma , LightPushCodec ) ;
339
+ await waku . libp2p . dialProtocol ( serviceNode2Ma , LightPushCodec ) ;
340
+
341
+ await waku . start ( ) ;
342
+ await waitForRemotePeer ( waku , [ Protocols . LightPush ] ) ;
343
+
344
+ const peers = await getConnectedPeersForProtocolAndShard (
345
+ waku . libp2p . getConnections ( ) ,
346
+ waku . libp2p . peerStore ,
347
+ waku . libp2p . getProtocols ( ) ,
348
+ shardInfo2
349
+ ) ;
350
+ expect ( peers . length ) . to . be . equal ( 1 ) ;
351
+ } ) ;
352
+
353
+ it ( "different cluster, different shard: nodes don't connect (autosharding)" , async function ( ) {
354
+ this . timeout ( 15000 ) ;
355
+
356
+ const shardInfo1 : ContentTopicInfo = {
357
+ clusterId : 1 ,
358
+ contentTopics : [ contentTopic ]
359
+ } ;
360
+
361
+ const shardInfo2 : ContentTopicInfo = {
362
+ clusterId : 2 ,
363
+ contentTopics : [ "/test/5/waku-light-push/utf8" ]
364
+ } ;
365
+
366
+ // we start one node in a separate cluster
367
+ await serviceNode1 . start ( {
368
+ discv5Discovery : true ,
369
+ peerExchange : true ,
370
+ clusterId : shardInfo1 . clusterId ,
371
+ pubsubTopic : shardInfoToPubsubTopics ( shardInfo1 ) ,
372
+ lightpush : true ,
373
+ relay : true
174
374
} ) ;
175
375
176
376
// and another node in the same cluster cluster as our node
@@ -180,7 +380,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
180
380
peerExchange : true ,
181
381
clusterId : shardInfo2 . clusterId ,
182
382
pubsubTopic : shardInfoToPubsubTopics ( shardInfo2 ) ,
183
- lightpush : true
383
+ lightpush : true ,
384
+ relay : true
184
385
} ) ;
185
386
186
387
const serviceNodeMa1 = await serviceNode1 . getMultiaddrWithId ( ) ;
0 commit comments