diff --git a/packages/core/src/lib/keep_alive_manager.ts b/packages/core/src/lib/keep_alive_manager.ts index 2b3b9e6c73..2bc86efdf6 100644 --- a/packages/core/src/lib/keep_alive_manager.ts +++ b/packages/core/src/lib/keep_alive_manager.ts @@ -2,7 +2,7 @@ import type { PeerId } from "@libp2p/interface/peer-id"; import type { PeerStore } from "@libp2p/interface/peer-store"; import type { IRelay, PeerIdStr } from "@waku/interfaces"; import type { KeepAliveOptions } from "@waku/interfaces"; -import { Logger, pubsubTopicToShardInfo } from "@waku/utils"; +import { Logger, pubsubTopicToSingleTopicShardInfo } from "@waku/utils"; import { utf8ToBytes } from "@waku/utils/bytes"; import type { PingService } from "libp2p/ping"; @@ -123,7 +123,7 @@ export class KeepAliveManager { if (!meshPeers.includes(peerIdStr)) continue; const encoder = createEncoder({ - pubsubTopicShardInfo: pubsubTopicToShardInfo(topic), + pubsubTopicShardInfo: pubsubTopicToSingleTopicShardInfo(topic), contentTopic: RelayPingContentTopic, ephemeral: true }); diff --git a/packages/interfaces/src/message.ts b/packages/interfaces/src/message.ts index aaf6ebba69..f3390ec212 100644 --- a/packages/interfaces/src/message.ts +++ b/packages/interfaces/src/message.ts @@ -43,7 +43,7 @@ export interface IMetaSetter { } export interface EncoderOptions { - pubsubTopic?: SingleTopicShardInfo | PubsubTopic; + pubsubTopicShardInfo?: SingleTopicShardInfo; /** The content topic to set on outgoing messages. */ contentTopic: string; /** diff --git a/packages/utils/src/common/sharding.ts b/packages/utils/src/common/sharding.ts index 2084cd8529..c23b248ef8 100644 --- a/packages/utils/src/common/sharding.ts +++ b/packages/utils/src/common/sharding.ts @@ -26,22 +26,16 @@ export const shardInfoToPubsubTopics = ( ); }; -export const pubsubTopicToShardInfo = ( - pubsubTopics: PubsubTopic[] -): ShardInfo => { - const indexList: number[] = []; - const cluster = parseInt(pubsubTopics[0].split("/")[4]); - for (const topic of pubsubTopics) { - const topicCluster = parseInt(topic.split("/")[4]); - if (topicCluster !== cluster) { - throw new Error( - "Pubsub topics must be from the same cluster to convert to shard info" - ); - } - const index = parseInt(topic.split("/")[5]); - indexList.push(index); - } - return { cluster, indexList }; +export const pubsubTopicToSingleTopicShardInfo = ( + pubsubTopics: PubsubTopic +): SingleTopicShardInfo => { + const parts = pubsubTopics.split("/"); + if (parts.length != 5) throw new Error("Invalid pubsub topic"); + + return { + cluster: parseInt(parts[3]), + index: parseInt(parts[4]) + }; }; export function ensurePubsubTopicIsConfigured(