Skip to content

Commit 7a18dc3

Browse files
committed
fix: allow autosharding nodes to get peers
1 parent 62725c1 commit 7a18dc3

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed

packages/tests/tests/getPeers.spec.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
5151
peerExchange: true,
5252
clusterId: shardInfo.clusterId,
5353
pubsubTopic: shardInfoToPubsubTopics(shardInfo),
54-
lightpush: true
54+
lightpush: true,
55+
relay: true
5556
});
5657

5758
const serviceNodeMa = await serviceNode1.getMultiaddrWithId();
@@ -77,12 +78,18 @@ describe("getConnectedPeersForProtocolAndShard", function () {
7778
shards: [1]
7879
};
7980

81+
const shardInfoServiceNode: ShardInfo = {
82+
clusterId: 1,
83+
shards: [2]
84+
};
85+
8086
await serviceNode1.start({
8187
discv5Discovery: true,
8288
peerExchange: true,
83-
clusterId: shardInfo.clusterId,
84-
pubsubTopic: shardInfoToPubsubTopics(shardInfo),
85-
lightpush: true
89+
clusterId: shardInfoServiceNode.clusterId,
90+
pubsubTopic: shardInfoToPubsubTopics(shardInfoServiceNode),
91+
lightpush: true,
92+
relay: true
8693
});
8794

8895
const serviceNodeMa = await serviceNode1.getMultiaddrWithId();
@@ -120,7 +127,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
120127
peerExchange: true,
121128
clusterId: shardInfo1.clusterId,
122129
pubsubTopic: shardInfoToPubsubTopics(shardInfo1),
123-
lightpush: true
130+
lightpush: true,
131+
relay: true
124132
});
125133

126134
// and another node in the same cluster cluster as our node
@@ -129,7 +137,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
129137
peerExchange: true,
130138
clusterId: shardInfo2.clusterId,
131139
pubsubTopic: shardInfoToPubsubTopics(shardInfo2),
132-
lightpush: true
140+
lightpush: true,
141+
relay: true
133142
});
134143

135144
const serviceNode1Ma = await serviceNode1.getMultiaddrWithId();
@@ -170,7 +179,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
170179
peerExchange: true,
171180
clusterId: shardInfo1.clusterId,
172181
pubsubTopic: shardInfoToPubsubTopics(shardInfo1),
173-
lightpush: true
182+
lightpush: true,
183+
relay: true
174184
});
175185

176186
// and another node in the same cluster cluster as our node
@@ -180,7 +190,8 @@ describe("getConnectedPeersForProtocolAndShard", function () {
180190
peerExchange: true,
181191
clusterId: shardInfo2.clusterId,
182192
pubsubTopic: shardInfoToPubsubTopics(shardInfo2),
183-
lightpush: true
193+
lightpush: true,
194+
relay: true
184195
});
185196

186197
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

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ShardingParams } from "@waku/interfaces";
44

55
import { bytesToUtf8 } from "../bytes/index.js";
66
import { decodeRelayShard } from "../common/relay_shard_codec.js";
7+
import { contentTopicToShardIndex } from "../index.js";
78

89
/**
910
* Returns a pseudo-random peer that supports the given protocol.
@@ -89,17 +90,29 @@ export async function getConnectedPeersForProtocolAndShard(
8990

9091
if (supportsProtocol) {
9192
if (shardInfo) {
92-
//TODO: support auto-sharding
93+
let shards;
9394
if (!("shards" in shardInfo)) {
94-
throw new Error(
95-
`Connections Manager only supports static sharding for now. Autosharding is not supported.`
95+
if (!("contentTopics" in shardInfo)) {
96+
throw new Error(
97+
"Missing configuration for static or auto sharding"
98+
);
99+
}
100+
shards = shardInfo.contentTopics.map((topic) =>
101+
contentTopicToShardIndex(topic)
96102
);
103+
} else {
104+
shards = shardInfo.shards;
97105
}
98106
const encodedPeerShardInfo = peer.metadata.get("shardInfo");
99107
const peerShardInfo =
100108
encodedPeerShardInfo && decodeRelayShard(encodedPeerShardInfo);
101109

102-
if (peerShardInfo && shardInfo.clusterId === peerShardInfo.clusterId) {
110+
if (
111+
peerShardInfo &&
112+
shardInfo.clusterId === peerShardInfo.clusterId &&
113+
(!("contentTopics" in shardInfo) ||
114+
shards.some((shard) => peerShardInfo.shards.includes(shard)))
115+
) {
103116
return peer;
104117
}
105118
} else {

0 commit comments

Comments
 (0)