Skip to content

Commit

Permalink
SingleShardInfo: use a new interface instead of omitting and rename n…
Browse files Browse the repository at this point in the history
…amespace
  • Loading branch information
danisharora099 committed Nov 28, 2023
1 parent f42d848 commit 5dd508c
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 36 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/lib/filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
PeerIdStr,
ProtocolCreateOptions,
PubsubTopic,
SingleTopicShardInfo,
SingleShardInfo,
Unsubscribe
} from "@waku/interfaces";
import { WakuMessage } from "@waku/proto";
Expand Down Expand Up @@ -291,7 +291,7 @@ class Filter extends BaseProtocol implements IReceiver {
}

async createSubscription(
pubsubTopicShardInfo?: SingleTopicShardInfo
pubsubTopicShardInfo?: SingleShardInfo
): Promise<Subscription> {
const pubsubTopic = pubsubTopicShardInfo
? singleTopicShardInfoToPubsubTopic(pubsubTopicShardInfo)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/message/version_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
IProtoMessage,
IRateLimitProof,
PubsubTopic,
SingleTopicShardInfo
SingleShardInfo
} from "@waku/interfaces";
import { proto_message as proto } from "@waku/proto";
import { Logger, singleTopicShardInfoToPubsubTopic } from "@waku/utils";
Expand Down Expand Up @@ -190,7 +190,7 @@ export class Decoder implements IDecoder<DecodedMessage> {
*/
export function createDecoder(
contentTopic: string,
pubsubTopicShardInfo?: SingleTopicShardInfo
pubsubTopicShardInfo?: SingleShardInfo
): Decoder {
return new Decoder(
pubsubTopicShardInfo?.index
Expand Down
8 changes: 2 additions & 6 deletions packages/interfaces/src/filter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { PeerId } from "@libp2p/interface/peer-id";

import type {
IDecodedMessage,
IDecoder,
SingleTopicShardInfo
} from "./message.js";
import type { IDecodedMessage, IDecoder, SingleShardInfo } from "./message.js";
import type { ContentTopic } from "./misc.js";
import type { Callback, IBaseProtocol } from "./protocols.js";
import type { IReceiver } from "./receiver.js";
Expand All @@ -29,7 +25,7 @@ export interface IFilterSubscription {
export type IFilter = IReceiver &
IBaseProtocol & {
createSubscription(
pubsubTopicShardInfo?: SingleTopicShardInfo,
pubsubTopicShardInfo?: SingleShardInfo,
peerId?: PeerId
): Promise<IFilterSubscription>;
};
6 changes: 3 additions & 3 deletions packages/interfaces/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ShardInfo } from "./enr.js";
import type { PubsubTopic } from "./misc.js";

export interface SingleTopicShardInfo extends Omit<ShardInfo, "indexList"> {
export interface SingleShardInfo {
cluster: number;
index: number;
}

Expand Down Expand Up @@ -43,7 +43,7 @@ export interface IMetaSetter {
}

export interface EncoderOptions {
pubsubTopicShardInfo?: SingleTopicShardInfo;
pubsubTopicShardInfo?: SingleShardInfo;
/** The content topic to set on outgoing messages. */
contentTopic: string;
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/message-encryption/src/ecies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
IMetaSetter,
IProtoMessage,
PubsubTopic,
SingleTopicShardInfo
SingleShardInfo
} from "@waku/interfaces";
import { WakuMessage } from "@waku/proto";
import { Logger, singleTopicShardInfoToPubsubTopic } from "@waku/utils";
Expand Down Expand Up @@ -197,7 +197,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
export function createDecoder(
contentTopic: string,
privateKey: Uint8Array,
pubsubTopicShardInfo?: SingleTopicShardInfo
pubsubTopicShardInfo?: SingleShardInfo
): Decoder {
return new Decoder(
pubsubTopicShardInfo?.index
Expand Down
4 changes: 2 additions & 2 deletions packages/message-encryption/src/symmetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
IMetaSetter,
IProtoMessage,
PubsubTopic,
SingleTopicShardInfo
SingleShardInfo
} from "@waku/interfaces";
import { WakuMessage } from "@waku/proto";
import { Logger, singleTopicShardInfoToPubsubTopic } from "@waku/utils";
Expand Down Expand Up @@ -197,7 +197,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
export function createDecoder(
contentTopic: string,
symKey: Uint8Array,
pubsubTopicShardInfo?: SingleTopicShardInfo
pubsubTopicShardInfo?: SingleShardInfo
): Decoder {
return new Decoder(
pubsubTopicShardInfo?.index
Expand Down
6 changes: 3 additions & 3 deletions packages/tests/tests/filter/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
IFilterSubscription,
LightNode,
ShardInfo,
SingleTopicShardInfo
SingleShardInfo
} from "@waku/interfaces";
import { Protocols } from "@waku/interfaces";
import {
Expand Down Expand Up @@ -40,8 +40,8 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () {
index: 2
});
const shardInfo: ShardInfo = { cluster: 3, indexList: [1, 2] };
const singleTopicShardInfo1: SingleTopicShardInfo = { cluster: 3, index: 1 };
const singleTopicShardInfo2: SingleTopicShardInfo = { cluster: 3, index: 2 };
const singleTopicShardInfo1: SingleShardInfo = { cluster: 3, index: 1 };
const singleTopicShardInfo2: SingleShardInfo = { cluster: 3, index: 2 };
const customContentTopic1 = "/test/2/waku-filter";
const customContentTopic2 = "/test/3/waku-filter";
const customEncoder1 = createEncoder({
Expand Down
6 changes: 3 additions & 3 deletions packages/tests/tests/light-push/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Protocols,
SendResult,
ShardInfo,
SingleTopicShardInfo
SingleShardInfo
} from "@waku/interfaces";
import { singleTopicShardInfoToPubsubTopic } from "@waku/utils";
import { utf8ToBytes } from "@waku/utils/bytes";
Expand Down Expand Up @@ -35,8 +35,8 @@ describe("Waku Light Push : Multiple PubsubTopics", function () {
index: 2
});
const shardInfo: ShardInfo = { cluster: 3, indexList: [1, 2] };
const singleTopicShardInfo1: SingleTopicShardInfo = { cluster: 3, index: 1 };
const singleTopicShardInfo2: SingleTopicShardInfo = { cluster: 3, index: 2 };
const singleTopicShardInfo1: SingleShardInfo = { cluster: 3, index: 1 };
const singleTopicShardInfo2: SingleShardInfo = { cluster: 3, index: 2 };
const customContentTopic1 = "/test/2/waku-light-push/utf8";
const customContentTopic2 = "/test/3/waku-light-push/utf8";
const customEncoder1 = createEncoder({
Expand Down
6 changes: 3 additions & 3 deletions packages/tests/tests/relay/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
DecodedMessage,
waitForRemotePeer
} from "@waku/core";
import { RelayNode, ShardInfo, SingleTopicShardInfo } from "@waku/interfaces";
import { RelayNode, ShardInfo, SingleShardInfo } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces";
import { createRelayNode } from "@waku/sdk";
import { singleTopicShardInfoToPubsubTopic } from "@waku/utils";
Expand Down Expand Up @@ -35,14 +35,14 @@ describe("Waku Relay, multiple pubsub topics", function () {
index: 2
});
const shardInfo1: ShardInfo = { cluster: 3, indexList: [1] };
const singleTopicShardInfo1: SingleTopicShardInfo = {
const singleTopicShardInfo1: SingleShardInfo = {
cluster: 3,
index: 1
};
const customContentTopic1 = "/test/2/waku-relay/utf8";
const customContentTopic2 = "/test/3/waku-relay/utf8";
const shardInfo2: ShardInfo = { cluster: 3, indexList: [2] };
const singleTopicShardInfo2: SingleTopicShardInfo = {
const singleTopicShardInfo2: SingleShardInfo = {
cluster: 3,
index: 2
};
Expand Down
6 changes: 3 additions & 3 deletions packages/tests/tests/sharding/running_nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LightNode, ShardInfo, SingleTopicShardInfo } from "@waku/interfaces";
import { LightNode, ShardInfo, SingleShardInfo } from "@waku/interfaces";
import { createEncoder, createLightNode, utf8ToBytes } from "@waku/sdk";
import { singleTopicShardInfoToPubsubTopic } from "@waku/utils";
import { expect } from "chai";
Expand All @@ -17,8 +17,8 @@ const PubsubTopic2 = singleTopicShardInfoToPubsubTopic({
});
const shardInfoFirstShard: ShardInfo = { cluster: 0, indexList: [2] };
const shardInfoBothShards: ShardInfo = { cluster: 0, indexList: [2, 3] };
const singleTopicShardInfo1: SingleTopicShardInfo = { cluster: 0, index: 2 };
const singleTopicShardInfo2: SingleTopicShardInfo = { cluster: 0, index: 3 };
const singleTopicShardInfo1: SingleShardInfo = { cluster: 0, index: 2 };
const singleTopicShardInfo2: SingleShardInfo = { cluster: 0, index: 3 };
const ContentTopic = "/waku/2/content/test.js";

describe("Static Sharding: Running Nodes", () => {
Expand Down
10 changes: 3 additions & 7 deletions packages/utils/src/common/sharding.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { sha256 } from "@noble/hashes/sha256";
import type {
PubsubTopic,
ShardInfo,
SingleTopicShardInfo
} from "@waku/interfaces";
import type { PubsubTopic, ShardInfo, SingleShardInfo } from "@waku/interfaces";

import { concat, utf8ToBytes } from "../bytes/index.js";

export const singleTopicShardInfoToPubsubTopic = (
shardInfo: SingleTopicShardInfo
shardInfo: SingleShardInfo
): PubsubTopic => {
if (shardInfo.cluster === undefined || shardInfo.index === undefined)
throw new Error("Invalid shard");
Expand All @@ -29,7 +25,7 @@ export const shardInfoToPubsubTopics = (

export const pubsubTopicToSingleTopicShardInfo = (
pubsubTopics: PubsubTopic
): SingleTopicShardInfo => {
): SingleShardInfo => {
const parts = pubsubTopics.split("/");
if (parts.length != 6) throw new Error("Invalid pubsub topic");

Expand Down

0 comments on commit 5dd508c

Please sign in to comment.