Skip to content

Commit

Permalink
fix: use correct shard index when creating encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
adklempner committed Apr 25, 2024
1 parent 3b0f330 commit 9514653
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
24 changes: 24 additions & 0 deletions packages/core/src/lib/message/version_0.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
2 changes: 1 addition & 1 deletion packages/interfaces/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/common/sharding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
9 changes: 4 additions & 5 deletions packages/utils/src/common/sharding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -232,7 +231,7 @@ export function determinePubsubTopic(
return pubsubTopicShardInfo;
} else {
return pubsubTopicShardInfo
? pubsubTopicShardInfo.shard
? pubsubTopicShardInfo.shard !== undefined
? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
: contentTopicToPubsubTopic(
contentTopic,
Expand Down Expand Up @@ -301,7 +300,7 @@ export const ensureShardingConfigured = (
shardingParams: { clusterId, application, version },
shardInfo: {
clusterId,
shards: [pubsubTopicToSingleShardInfo(pubsubTopic).shard]
shards: [pubsubTopicToSingleShardInfo(pubsubTopic).shard!]
},
pubsubTopics: [pubsubTopic]
};
Expand Down

0 comments on commit 9514653

Please sign in to comment.