diff --git a/packages/core/src/lib/message/version_0.spec.ts b/packages/core/src/lib/message/version_0.spec.ts index 4ee67ff165..7498c15f80 100644 --- a/packages/core/src/lib/message/version_0.spec.ts +++ b/packages/core/src/lib/message/version_0.spec.ts @@ -137,3 +137,27 @@ describe("Ensures content topic is defined", () => { expect(wrapper).to.throw("Content topic must be specified"); }); }); + +describe("Sets sharding configuration correctly", () => { + it("uses static shard pubsub topic instead of autosharding when set", async () => { + // Create an encoder setup to use autosharding + const ContentTopic = "/waku/2/content/test.js"; + const autoshardingEncoder = createEncoder({ + pubsubTopicShardInfo: { clusterId: 0 }, + contentTopic: ContentTopic + }); + + // When autosharding is enabled, we expect the shard index to be 1 + expect(autoshardingEncoder.pubsubTopic).to.be.eq("/waku/2/rs/0/1"); + + // Create an encoder setup to use static sharding with the same content topic + const singleShardInfo = { clusterId: 0, shard: 0 }; + const staticshardingEncoder = createEncoder({ + contentTopic: ContentTopic, + pubsubTopicShardInfo: singleShardInfo + }); + + // When static sharding is enabled, we expect the shard index to be 0 + expect(staticshardingEncoder.pubsubTopic).to.be.eq("/waku/2/rs/0/0"); + }); +}); diff --git a/packages/interfaces/src/message.ts b/packages/interfaces/src/message.ts index 1e48030288..1c8348239e 100644 --- a/packages/interfaces/src/message.ts +++ b/packages/interfaces/src/message.ts @@ -5,7 +5,7 @@ export interface SingleShardInfo { /** * Specifying this field indicates to the encoder/decoder that static sharding must be used. */ - shard: number; + shard?: number; } export interface IRateLimitProof { diff --git a/packages/utils/src/common/sharding.spec.ts b/packages/utils/src/common/sharding.spec.ts index 7ae9f2bc18..cc5ea223ca 100644 --- a/packages/utils/src/common/sharding.spec.ts +++ b/packages/utils/src/common/sharding.spec.ts @@ -409,7 +409,7 @@ describe("determinePubsubTopic", () => { it("should process correctly when SingleShardInfo has no clusterId but has a shard", () => { const info = { shard: 0 }; - const expectedTopic = `/waku/2/rs/${DEFAULT_CLUSTER_ID}/6`; + const expectedTopic = `/waku/2/rs/${DEFAULT_CLUSTER_ID}/0`; expect(determinePubsubTopic(contentTopic, info as any)).to.equal( expectedTopic ); diff --git a/packages/utils/src/common/sharding.ts b/packages/utils/src/common/sharding.ts index 6d9a98f540..79be1e6d36 100644 --- a/packages/utils/src/common/sharding.ts +++ b/packages/utils/src/common/sharding.ts @@ -13,10 +13,9 @@ import { concat, utf8ToBytes } from "../bytes/index.js"; export const singleShardInfoToPubsubTopic = ( shardInfo: SingleShardInfo ): PubsubTopic => { - if (shardInfo.clusterId === undefined || shardInfo.shard === undefined) - throw new Error("Invalid shard"); + if (shardInfo.shard === undefined) throw new Error("Invalid shard"); - return `/waku/2/rs/${shardInfo.clusterId}/${shardInfo.shard}`; + return `/waku/2/rs/${shardInfo.clusterId ?? DEFAULT_CLUSTER_ID}/${shardInfo.shard}`; }; export const singleShardInfosToShardInfo = ( @@ -232,7 +231,7 @@ export function determinePubsubTopic( return pubsubTopicShardInfo; } else { return pubsubTopicShardInfo - ? pubsubTopicShardInfo.shard + ? pubsubTopicShardInfo.shard !== undefined ? singleShardInfoToPubsubTopic(pubsubTopicShardInfo) : contentTopicToPubsubTopic( contentTopic, @@ -301,7 +300,7 @@ export const ensureShardingConfigured = ( shardingParams: { clusterId, application, version }, shardInfo: { clusterId, - shards: [pubsubTopicToSingleShardInfo(pubsubTopic).shard] + shards: [pubsubTopicToSingleShardInfo(pubsubTopic).shard!] }, pubsubTopics: [pubsubTopic] };