Simplify IRouting interface

This commit is contained in:
fryorcraken 2025-07-19 14:45:06 +10:00
parent d04130feb6
commit 3200b19a02
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
9 changed files with 31 additions and 53 deletions

View File

@ -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<IDecodedMessage> {
* 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);
}

View File

@ -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;

View File

@ -109,7 +109,7 @@ export class PeerManager {
public async getPeers(params: GetPeersParams): Promise<PeerId[]> {
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) {

View File

@ -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", () => {

View File

@ -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 =

View File

@ -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
)
);

View File

@ -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());

View File

@ -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
);

View File

@ -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;
}