Skip to content

Commit 4dd451b

Browse files
authored
fix: allow autosharding nodes to get peers (#1785)
1 parent 941b02b commit 4dd451b

File tree

4 files changed

+219
-26
lines changed

4 files changed

+219
-26
lines changed

packages/tests/tests/getPeers.spec.ts

+209-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Peer } from "@libp2p/interface/peer-store";
44
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
55
import { LightPushCodec, waitForRemotePeer } from "@waku/core";
66
import {
7+
ContentTopicInfo,
78
createLightNode,
89
Libp2pComponents,
910
type LightNode,
@@ -26,6 +27,7 @@ describe("getConnectedPeersForProtocolAndShard", function () {
2627
let waku: LightNode;
2728
let serviceNode1: NimGoNode;
2829
let serviceNode2: NimGoNode;
30+
const contentTopic = "/test/2/waku-light-push/utf8";
2931

3032
this.beforeEach(async function () {
3133
this.timeout(15000);
@@ -51,7 +53,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
5153
peerExchange: true,
5254
clusterId: shardInfo.clusterId,
5355
pubsubTopic: shardInfoToPubsubTopics(shardInfo),
54-
lightpush: true
56+
lightpush: true,
57+
relay: true
5558
});
5659

5760
const serviceNodeMa = await serviceNode1.getMultiaddrWithId();
@@ -77,12 +80,18 @@ describe("getConnectedPeersForProtocolAndShard", function () {
7780
shards: [1]
7881
};
7982

83+
const shardInfoServiceNode: ShardInfo = {
84+
clusterId: 1,
85+
shards: [2]
86+
};
87+
8088
await serviceNode1.start({
8189
discv5Discovery: true,
8290
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
8695
});
8796

8897
const serviceNodeMa = await serviceNode1.getMultiaddrWithId();
@@ -120,7 +129,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
120129
peerExchange: true,
121130
clusterId: shardInfo1.clusterId,
122131
pubsubTopic: shardInfoToPubsubTopics(shardInfo1),
123-
lightpush: true
132+
lightpush: true,
133+
relay: true
124134
});
125135

126136
// and another node in the same cluster cluster as our node
@@ -129,7 +139,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
129139
peerExchange: true,
130140
clusterId: shardInfo2.clusterId,
131141
pubsubTopic: shardInfoToPubsubTopics(shardInfo2),
132-
lightpush: true
142+
lightpush: true,
143+
relay: true
133144
});
134145

135146
const serviceNode1Ma = await serviceNode1.getMultiaddrWithId();
@@ -170,7 +181,196 @@ describe("getConnectedPeersForProtocolAndShard", function () {
170181
peerExchange: true,
171182
clusterId: shardInfo1.clusterId,
172183
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
174374
});
175375

176376
// and another node in the same cluster cluster as our node
@@ -180,7 +380,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
180380
peerExchange: true,
181381
clusterId: shardInfo2.clusterId,
182382
pubsubTopic: shardInfoToPubsubTopics(shardInfo2),
183-
lightpush: true
383+
lightpush: true,
384+
relay: true
184385
});
185386

186387
const serviceNodeMa1 = await serviceNode1.getMultiaddrWithId();

packages/tests/tests/light-push/multiple_pubsub.node.spec.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () {
290290
filter: true,
291291
lightpush: true,
292292
relay: true,
293-
pubsubTopic: [autoshardingPubsubTopic2]
293+
pubsubTopic: [autoshardingPubsubTopic2],
294+
clusterId: shardInfo.clusterId
294295
});
295296
await nwaku2.ensureSubscriptionsAutosharding([customContentTopic2]);
296297
await waku.dial(await nwaku2.getMultiaddrWithId());
@@ -353,10 +354,6 @@ describe("Waku Light Push (named sharding): Multiple PubsubTopics", function ()
353354
customContentTopic2,
354355
clusterId
355356
);
356-
const contentTopicInfo: ContentTopicInfo = {
357-
clusterId,
358-
contentTopics: [customContentTopic1, customContentTopic2]
359-
};
360357
const customEncoder1 = createEncoder({
361358
contentTopic: customContentTopic1,
362359
pubsubTopicShardInfo: {
@@ -372,11 +369,10 @@ describe("Waku Light Push (named sharding): Multiple PubsubTopics", function ()
372369

373370
this.beforeEach(async function () {
374371
this.timeout(15000);
375-
[nwaku, waku] = await runNodes(
376-
this,
377-
[autoshardingPubsubTopic1, autoshardingPubsubTopic2],
378-
contentTopicInfo
379-
);
372+
[nwaku, waku] = await runNodes(this, [
373+
autoshardingPubsubTopic1,
374+
autoshardingPubsubTopic2
375+
]);
380376
messageCollector = new MessageCollector(nwaku);
381377
nimPeerId = await nwaku.getPeerId();
382378
});

packages/tests/tests/store/multiple_pubsub.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ describe("Waku Store (Autosharding), custom pubsub topic", function () {
214214
await nwaku.start({
215215
store: true,
216216
pubsubTopic: [autoshardingPubsubTopic1, autoshardingPubsubTopic2],
217-
relay: true
217+
relay: true,
218+
clusterId
218219
});
219220
await nwaku.ensureSubscriptionsAutosharding([
220221
customContentTopic1,
@@ -287,7 +288,8 @@ describe("Waku Store (Autosharding), custom pubsub topic", function () {
287288
await nwaku2.start({
288289
store: true,
289290
pubsubTopic: [autoshardingPubsubTopic2],
290-
relay: true
291+
relay: true,
292+
clusterId
291293
});
292294
await nwaku2.ensureSubscriptionsAutosharding([customContentTopic2]);
293295

packages/utils/src/libp2p/index.ts

-6
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ export async function getConnectedPeersForProtocolAndShard(
8989

9090
if (supportsProtocol) {
9191
if (shardInfo) {
92-
//TODO: support auto-sharding
93-
if (!("shards" in shardInfo)) {
94-
throw new Error(
95-
`Connections Manager only supports static sharding for now. Autosharding is not supported.`
96-
);
97-
}
9892
const encodedPeerShardInfo = peer.metadata.get("shardInfo");
9993
const peerShardInfo =
10094
encodedPeerShardInfo && decodeRelayShard(encodedPeerShardInfo);

0 commit comments

Comments
 (0)