Skip to content

Commit

Permalink
fix: sharding utils for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Nov 27, 2023
1 parent 7fd07b2 commit 85500d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe("Waku Light Push : Multiple PubsubTopics", function () {
let nwaku: NimGoNode;
let nwaku2: NimGoNode;
let messageCollector: MessageCollector;

const customPubsubTopic1 = singleTopicShardInfoToPubsubTopic({
cluster: 3,
index: 1
Expand Down
16 changes: 9 additions & 7 deletions packages/utils/src/common/sharding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import { concat, utf8ToBytes } from "../bytes/index.js";
export const singleTopicShardInfoToPubsubTopic = (
shardInfo: SingleTopicShardInfo
): PubsubTopic => {
if (!shardInfo.cluster || !shardInfo.index) throw new Error("Invalid shard");
if (shardInfo.cluster === undefined || shardInfo.index === undefined)
throw new Error("Invalid shard");

return `/waku/2/rs/${shardInfo.cluster}/${shardInfo.index}`;
};

export const shardInfoToPubsubTopics = (
shardInfo: ShardInfo
): PubsubTopic[] => {
if (!shardInfo.cluster || !shardInfo.indexList)
if (shardInfo.cluster === undefined || shardInfo.indexList === undefined)
throw new Error("Invalid shard");

return shardInfo.indexList.map(
Expand All @@ -30,12 +31,13 @@ export const pubsubTopicToSingleTopicShardInfo = (
pubsubTopics: PubsubTopic
): SingleTopicShardInfo => {
const parts = pubsubTopics.split("/");
if (parts.length != 5) throw new Error("Invalid pubsub topic");
if (parts.length != 6) throw new Error("Invalid pubsub topic");

return {
cluster: parseInt(parts[3]),
index: parseInt(parts[4])
};
const cluster = parseInt(parts[4]);
const index = parseInt(parts[5]);
if (isNaN(cluster) || isNaN(index)) throw new Error("Invalid pubsub topic");

return { cluster, index };
};

export function ensurePubsubTopicIsConfigured(
Expand Down

0 comments on commit 85500d3

Please sign in to comment.