diff --git a/packages/core/src/lib/connection_manager/shard_reader.spec.ts b/packages/core/src/lib/connection_manager/shard_reader.spec.ts index 7f38c83190..8abb90265b 100644 --- a/packages/core/src/lib/connection_manager/shard_reader.spec.ts +++ b/packages/core/src/lib/connection_manager/shard_reader.spec.ts @@ -29,7 +29,10 @@ describe("ShardReader", function () { const testContentTopic = "/test/1/waku-light-push/utf8"; const testClusterId = 3; - const testShardIndex = contentTopicToShardIndex(testContentTopic); + const testShardIndex = contentTopicToShardIndex( + testContentTopic, + DEFAULT_NUM_SHARDS + ); const testNetworkConfig: AutoSharding = { clusterId: testClusterId, diff --git a/packages/tests/tests/filter/utils.ts b/packages/tests/tests/filter/utils.ts index a679f5337a..111ad5f420 100644 --- a/packages/tests/tests/filter/utils.ts +++ b/packages/tests/tests/filter/utils.ts @@ -10,10 +10,14 @@ import { utf8ToBytes } from "@waku/utils/bytes"; export const log = new Logger("test:filter"); export const TestContentTopic = "/test/1/waku-filter/default"; export const TestClusterId = 2; -export const TestShardIndex = contentTopicToShardIndex(TestContentTopic); +export const TestNumShardsInCluster = 8; +export const TestShardIndex = contentTopicToShardIndex( + TestContentTopic, + TestNumShardsInCluster +); export const TestNetworkConfig = { clusterId: TestClusterId, - numShardsInCluster: 8 + numShardsInCluster: TestNumShardsInCluster }; export const TestRoutingInfo = createRoutingInfo(TestNetworkConfig, { contentTopic: TestContentTopic diff --git a/packages/tests/tests/sharding/peer_management.spec.ts b/packages/tests/tests/sharding/peer_management.spec.ts index c0dec2e1fd..6d734828c9 100644 --- a/packages/tests/tests/sharding/peer_management.spec.ts +++ b/packages/tests/tests/sharding/peer_management.spec.ts @@ -200,7 +200,8 @@ describe("Static Sharding: Peer Management", function () { describe("Autosharding: Peer Management", function () { const ContentTopic = "/myapp/1/latest/proto"; const clusterId = 8; - const Shard = [contentTopicToShardIndex(ContentTopic)]; + const numShardsInCluster = 8; + const Shard = [contentTopicToShardIndex(ContentTopic, numShardsInCluster)]; describe("Peer Exchange", function () { let waku: LightNode; diff --git a/packages/utils/src/common/sharding/index.spec.ts b/packages/utils/src/common/sharding/index.spec.ts index 7e5056dbe5..28b5aeba2e 100644 --- a/packages/utils/src/common/sharding/index.spec.ts +++ b/packages/utils/src/common/sharding/index.spec.ts @@ -9,6 +9,9 @@ import { pubsubTopicToSingleShardInfo } from "./index.js"; +const ClusterId = 0; +const NumShardsInCluster = 8; + const testInvalidCases = ( contentTopics: string[], expectedError: string @@ -112,7 +115,9 @@ describe("contentTopicToShardIndex", () => { ]; contentTopicsWithExpectedShards.forEach(([topic, expectedShard]) => { it(`should correctly map ${topic} to shard index ${expectedShard}`, () => { - expect(contentTopicToShardIndex(topic)).to.eq(expectedShard); + expect(contentTopicToShardIndex(topic, NumShardsInCluster)).to.eq( + expectedShard + ); }); }); @@ -137,8 +142,8 @@ describe("contentTopicToShardIndex", () => { ["/waku/2/content/test.js", "/waku/2/users/proto"] ]; for (const [topic1, topic2] of contentTopics) { - expect(contentTopicToShardIndex(topic1)).to.eq( - contentTopicToShardIndex(topic2) + expect(contentTopicToShardIndex(topic1, NumShardsInCluster)).to.eq( + contentTopicToShardIndex(topic2, NumShardsInCluster) ); } }); @@ -147,9 +152,15 @@ describe("contentTopicToShardIndex", () => { describe("contentTopicsByPubsubTopic", () => { it("groups content topics by expected pubsub topic", () => { const contentTopics = ["/toychat/2/huilong/proto", "/myapp/1/latest/proto"]; - const grouped = contentTopicsByPubsubTopic(contentTopics); + const grouped = contentTopicsByPubsubTopic( + contentTopics, + ClusterId, + NumShardsInCluster + ); + for (const contentTopic of contentTopics) { const pubsubTopic = contentTopicToPubsubTopic(contentTopic, 0, 8); + expect(grouped.get(pubsubTopic)?.includes(contentTopic)).to.be.true; } }); @@ -159,7 +170,11 @@ describe("contentTopicsByPubsubTopic", () => { "/app/22/sometopic/someencoding", "/app/22/anothertopic/otherencoding" ]; - const grouped = contentTopicsByPubsubTopic(contentTopics); + const grouped = contentTopicsByPubsubTopic( + contentTopics, + ClusterId, + NumShardsInCluster + ); expect(grouped.size).to.eq(1); // Only one pubsub topic expected const pubsubTopic = contentTopicToPubsubTopic(contentTopics[0], 0, 8); expect(grouped.get(pubsubTopic)?.length).to.eq(2); // Both topics should be grouped under the same pubsub topic @@ -169,8 +184,16 @@ describe("contentTopicsByPubsubTopic", () => { const contentTopics = ["/app/22/sometopic/someencoding"]; const clusterId1 = 3; const clusterId2 = 2; - const grouped1 = contentTopicsByPubsubTopic(contentTopics, clusterId1); - const grouped2 = contentTopicsByPubsubTopic(contentTopics, clusterId2); + const grouped1 = contentTopicsByPubsubTopic( + contentTopics, + clusterId1, + NumShardsInCluster + ); + const grouped2 = contentTopicsByPubsubTopic( + contentTopics, + clusterId2, + NumShardsInCluster + ); const pubsubTopic1 = contentTopicToPubsubTopic( contentTopics[0], clusterId1, @@ -221,7 +244,13 @@ describe("contentTopicsByPubsubTopic", () => { it("throws an error for improperly formatted content topics", () => { const invalidContentTopics = ["/invalid/format"]; - expect(() => contentTopicsByPubsubTopic(invalidContentTopics)).to.throw(); + expect(() => + contentTopicsByPubsubTopic( + invalidContentTopics, + ClusterId, + NumShardsInCluster + ) + ).to.throw(); }); }); diff --git a/packages/utils/src/common/sharding/index.ts b/packages/utils/src/common/sharding/index.ts index e48522ac22..495ea42c6e 100644 --- a/packages/utils/src/common/sharding/index.ts +++ b/packages/utils/src/common/sharding/index.ts @@ -2,7 +2,6 @@ import { sha256 } from "@noble/hashes/sha256"; import { type ClusterId, ContentTopic, - DEFAULT_CLUSTER_ID, PubsubTopic, type ShardId } from "@waku/interfaces"; @@ -116,7 +115,7 @@ export function ensureValidContentTopic( */ export function contentTopicToShardIndex( contentTopic: ContentTopic, - numShardsInCluster: number = 8 + numShardsInCluster: number ): number { const { application, version } = ensureValidContentTopic(contentTopic); const digest = sha256( @@ -145,8 +144,8 @@ export function contentTopicToPubsubTopic( */ export function contentTopicsByPubsubTopic( contentTopics: ContentTopic[], - clusterId: number = DEFAULT_CLUSTER_ID, - networkShards: number = 8 + clusterId: number, + networkShards: number ): Map> { const groupedContentTopics = new Map(); for (const contentTopic of contentTopics) {