diff --git a/packages/core/src/lib/message/version_0.ts b/packages/core/src/lib/message/version_0.ts index 4715ebe6e3..60748bb4e0 100644 --- a/packages/core/src/lib/message/version_0.ts +++ b/packages/core/src/lib/message/version_0.ts @@ -9,7 +9,7 @@ import type { IRoutingInfo } from "@waku/interfaces"; import { proto_message as proto } from "@waku/proto"; -import { isAutoShardingRoutingInfo, Logger } from "@waku/utils"; +import { Logger } from "@waku/utils"; const log = new Logger("message:version-0"); const OneMillion = BigInt(1_000_000); @@ -130,6 +130,8 @@ export class Encoder implements IEncoder { * format to be sent over the Waku network. The resulting encoder can then be * pass to { @link @waku/interfaces!ISender.send } to automatically encode outgoing * messages. + * + * Note that a routing info may be tied to a given content topic, this is not checked by the encoder. */ export function createEncoder({ contentTopic, @@ -137,10 +139,6 @@ export function createEncoder({ ephemeral, metaSetter }: EncoderOptions): Encoder { - if (isAutoShardingRoutingInfo(routingInfo)) { - if (routingInfo.contentTopic !== contentTopic) - throw "Routing Info must have the same content topic as the encoder"; - } return new Encoder(contentTopic, ephemeral, routingInfo, metaSetter); } @@ -198,15 +196,13 @@ export class Decoder implements IDecoder { * messages. * * @param contentTopic The resulting decoder will only decode messages with this content topic. - * @param routingInfo + * @param routingInfo Routing information such as cluster id and shard id on which the message is expected to be received. + * + * Note that a routing info may be tied to a given content topic, this is not checked by the encoder. */ export function createDecoder( contentTopic: string, routingInfo: IRoutingInfo ): Decoder { - if (isAutoShardingRoutingInfo(routingInfo)) { - if (routingInfo.contentTopic !== contentTopic) - throw "Routing Info must have the same content topic as the encoder"; - } return new Decoder(contentTopic, routingInfo); } diff --git a/packages/interfaces/src/sharding.ts b/packages/interfaces/src/sharding.ts index 3cbe86d6c7..f204d47ecd 100644 --- a/packages/interfaces/src/sharding.ts +++ b/packages/interfaces/src/sharding.ts @@ -22,31 +22,8 @@ export type ShardId = number; /** * Routing Information for a given message. */ -export interface IRoutingInfoAutoSharding { - pubsubTopic: string; +export interface IRoutingInfo { + clusterId: ClusterId; shardId: ShardId; - - // Is the network config really needed for exposure? - // we should probably aim to only expose the above + Cluster Id - networkConfig: AutoSharding; - - // This is actually a property of network config, should probably be removed - isAutoSharding: boolean; - isStaticSharding: boolean; - - // This is only needed for tests, to setup nwaku node - // might be a cleaner way to handle it - contentTopic: string; -} - -export interface IRoutingInfoStaticSharding { pubsubTopic: string; - shardId: ShardId; - networkConfig: StaticSharding; - isAutoSharding: boolean; - isStaticSharding: boolean; } - -export type IRoutingInfo = - | IRoutingInfoAutoSharding - | IRoutingInfoStaticSharding; diff --git a/packages/sdk/src/peer_manager/peer_manager.ts b/packages/sdk/src/peer_manager/peer_manager.ts index cb68f3da56..ebf8bc82c5 100644 --- a/packages/sdk/src/peer_manager/peer_manager.ts +++ b/packages/sdk/src/peer_manager/peer_manager.ts @@ -109,7 +109,7 @@ export class PeerManager { public async getPeers(params: GetPeersParams): Promise { log.info( `Getting peers for protocol: ${params.protocol}, ` + - `clusterId: ${params.routingInfo.networkConfig.clusterId},` + + `clusterId: ${params.routingInfo.clusterId},` + ` shard: ${params.routingInfo.shardId}` ); @@ -123,7 +123,7 @@ export class PeerManager { const isOnSameShard = await this.connectionManager.isPeerOnShard( peer.id, - params.routingInfo.networkConfig.clusterId, + params.routingInfo.clusterId, params.routingInfo.shardId ); if (!isOnSameShard) { diff --git a/packages/sdk/src/store/store.spec.ts b/packages/sdk/src/store/store.spec.ts index 983c1ddba1..83ccb08436 100644 --- a/packages/sdk/src/store/store.spec.ts +++ b/packages/sdk/src/store/store.spec.ts @@ -18,9 +18,7 @@ const TestNetworkingInfo = { clusterId: 0, numShardsInCluster: 8 }; const MockRoutingInfo: IRoutingInfo = { pubsubTopic: "/custom/topic", shardId: 1, - networkConfig: TestNetworkingInfo, - isAutoSharding: false, - isStaticSharding: false + clusterId: TestNetworkingInfo.clusterId }; describe("Store", () => { diff --git a/packages/tests/src/lib/index.ts b/packages/tests/src/lib/index.ts index 02b0b77fac..85de368b23 100644 --- a/packages/tests/src/lib/index.ts +++ b/packages/tests/src/lib/index.ts @@ -263,7 +263,7 @@ function applyDefaultArgs(routingInfo: RoutingInfo, args?: Args): Args { relay: true }; - defaultArgs.clusterId = routingInfo.networkConfig.clusterId; + defaultArgs.clusterId = routingInfo.clusterId; if (isAutoShardingRoutingInfo(routingInfo)) { defaultArgs.numShardsInNetwork = diff --git a/packages/tests/src/utils/nodes.ts b/packages/tests/src/utils/nodes.ts index 2275f5bd0c..26f29e27c3 100644 --- a/packages/tests/src/utils/nodes.ts +++ b/packages/tests/src/utils/nodes.ts @@ -75,9 +75,7 @@ export async function runMultipleNodes( if (customArgs?.shard) { const shards = customArgs?.shard ?? []; for (const s of shards) { - pubsubTopics.push( - formatPubsubTopic(routingInfo.networkConfig.clusterId, s) - ); + pubsubTopics.push(formatPubsubTopic(routingInfo.clusterId, s)); } } @@ -87,7 +85,7 @@ export async function runMultipleNodes( pubsubTopics.push( contentTopicToPubsubTopic( ct, - routingInfo.networkConfig.clusterId, + routingInfo.clusterId, routingInfo.networkConfig.numShardsInCluster ) ); diff --git a/packages/tests/tests/store/different_static_shards.spec.ts b/packages/tests/tests/store/different_static_shards.spec.ts index bb71b4a2d4..3c639364bc 100644 --- a/packages/tests/tests/store/different_static_shards.spec.ts +++ b/packages/tests/tests/store/different_static_shards.spec.ts @@ -1,7 +1,7 @@ import { createDecoder } from "@waku/core"; import { IMessage, LightNode, ShardId, StaticSharding } from "@waku/interfaces"; import { Protocols } from "@waku/sdk"; -import { createRoutingInfo } from "@waku/utils"; +import { createRoutingInfo, RoutingInfo } from "@waku/utils"; import { expect } from "chai"; import { @@ -155,13 +155,13 @@ describe("Waku Store, different static shards", function () { nwaku, totalMsgs, TestDecoderShardOne.contentTopic, - TestDecoderShardOne.routingInfo + TestDecoderShardOne.routingInfo as RoutingInfo ); await sendMessages( nwaku2, totalMsgs, TestDecoderShardTwo.contentTopic, - TestDecoderShardTwo.routingInfo + TestDecoderShardTwo.routingInfo as RoutingInfo ); await waku.dial(await nwaku.getMultiaddrWithId()); diff --git a/packages/tests/tests/store/message_hash.spec.ts b/packages/tests/tests/store/message_hash.spec.ts index d97077e747..906fc98ed8 100644 --- a/packages/tests/tests/store/message_hash.spec.ts +++ b/packages/tests/tests/store/message_hash.spec.ts @@ -1,5 +1,6 @@ import { messageHash } from "@waku/core"; import type { IDecodedMessage, LightNode } from "@waku/interfaces"; +import { RoutingInfo } from "@waku/utils"; import { expect } from "chai"; import { @@ -36,7 +37,7 @@ describe("Waku Store, message hash query", function () { nwaku, totalMsgs, TestDecoder.contentTopic, - TestDecoder.routingInfo, + TestDecoder.routingInfo as RoutingInfo, true ); diff --git a/packages/utils/src/common/sharding/routing_info.ts b/packages/utils/src/common/sharding/routing_info.ts index 1b804ac581..a51de7cfc3 100644 --- a/packages/utils/src/common/sharding/routing_info.ts +++ b/packages/utils/src/common/sharding/routing_info.ts @@ -1,8 +1,8 @@ import type { AutoSharding, + ClusterId, ContentTopic, - IRoutingInfoAutoSharding, - IRoutingInfoStaticSharding, + IRoutingInfo, NetworkConfig, PubsubTopic, ShardId, @@ -32,7 +32,7 @@ export abstract class BaseRoutingInfo { export class AutoShardingRoutingInfo extends BaseRoutingInfo - implements IRoutingInfoAutoSharding + implements IRoutingInfo { public static fromContentTopic( contentTopic: ContentTopic, @@ -68,6 +68,10 @@ export class AutoShardingRoutingInfo super(networkConfig, pubsubTopic, shardId); } + public get clusterId(): number { + return this.networkConfig.clusterId; + } + public get isAutoSharding(): boolean { return true; } @@ -79,7 +83,7 @@ export class AutoShardingRoutingInfo export class StaticShardingRoutingInfo extends BaseRoutingInfo - implements IRoutingInfoStaticSharding + implements IRoutingInfo { /** * Create Routing Info for static sharding network, using shard @@ -129,6 +133,10 @@ export class StaticShardingRoutingInfo super(networkConfig, pubsubTopic, shardId); } + public get clusterId(): ClusterId { + return this.networkConfig.clusterId; + } + public get isAutoSharding(): boolean { return false; }