Merge pull request #1885 from waku-org/fix/encoder-cluster-id

fix: use correct shard index when creating encoder
This commit is contained in:
Arseniy Klempner 2024-04-24 20:57:16 -07:00 committed by GitHub
commit 66081d6c95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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]
};