fix: use correct shard index when creating encoder

This commit is contained in:
Arseniy Klempner 2024-03-04 18:40:08 -08:00 committed by Arseniy Klempner
parent 3b0f330b02
commit 9514653428
No known key found for this signature in database
GPG Key ID: 51653F18863BD24B
4 changed files with 30 additions and 7 deletions

View File

@ -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");
});
});

View File

@ -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 {

View File

@ -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
);

View File

@ -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]
};