diff --git a/packages/core/src/lib/connection_manager/shard_reader.spec.ts b/packages/core/src/lib/connection_manager/shard_reader.spec.ts index 8abb90265b..30bb19ac26 100644 --- a/packages/core/src/lib/connection_manager/shard_reader.spec.ts +++ b/packages/core/src/lib/connection_manager/shard_reader.spec.ts @@ -4,7 +4,7 @@ import { DEFAULT_NUM_SHARDS, NetworkConfig, PubsubTopic, - RelayShards + ShardInfo } from "@waku/interfaces"; import { contentTopicToShardIndex, encodeRelayShard } from "@waku/utils"; import { expect } from "chai"; @@ -39,7 +39,7 @@ describe("ShardReader", function () { numShardsInCluster: DEFAULT_NUM_SHARDS }; - const testRelayShards: RelayShards = { + const testShardInfo: ShardInfo = { clusterId: testClusterId, shards: [testShardIndex] }; @@ -98,7 +98,7 @@ describe("ShardReader", function () { describe("isPeerOnNetwork", function () { it("should return true when peer is on the same cluster", async function () { - const shardInfoBytes = encodeRelayShard(testRelayShards); + const shardInfoBytes = encodeRelayShard(testShardInfo); const mockPeer = { metadata: new Map([["shardInfo", shardInfoBytes]]) }; @@ -112,7 +112,7 @@ describe("ShardReader", function () { }); it("should return false when peer is on different cluster", async function () { - const differentClusterShardInfo: RelayShards = { + const differentClusterShardInfo: ShardInfo = { clusterId: 5, shards: [1, 2] }; @@ -129,7 +129,7 @@ describe("ShardReader", function () { }); it("should return true even if peer has no overlapping shards", async function () { - const noOverlapShardInfo: RelayShards = { + const noOverlapShardInfo: ShardInfo = { clusterId: testClusterId, shards: [testShardIndex + 100, testShardIndex + 200] // Use different shards }; @@ -168,7 +168,7 @@ describe("ShardReader", function () { describe("isPeerOnShard", function () { it("should return true when peer is on the specified shard", async function () { - const shardInfoBytes = encodeRelayShard(testRelayShards); + const shardInfoBytes = encodeRelayShard(testShardInfo); const mockPeer = { metadata: new Map([["shardInfo", shardInfoBytes]]) }; @@ -185,7 +185,7 @@ describe("ShardReader", function () { }); it("should return false when peer is on different cluster", async function () { - const shardInfoBytes = encodeRelayShard(testRelayShards); + const shardInfoBytes = encodeRelayShard(testShardInfo); const mockPeer = { metadata: new Map([["shardInfo", shardInfoBytes]]) }; @@ -202,7 +202,7 @@ describe("ShardReader", function () { }); it("should return false when peer is not on the specified shard", async function () { - const shardInfoBytes = encodeRelayShard(testRelayShards); + const shardInfoBytes = encodeRelayShard(testShardInfo); const mockPeer = { metadata: new Map([["shardInfo", shardInfoBytes]]) }; @@ -233,7 +233,7 @@ describe("ShardReader", function () { describe("isPeerOnTopic", function () { it("should return true when peer is on the pubsub topic shard", async function () { - const shardInfoBytes = encodeRelayShard(testRelayShards); + const shardInfoBytes = encodeRelayShard(testShardInfo); const mockPeer = { metadata: new Map([["shardInfo", shardInfoBytes]]) }; @@ -248,7 +248,7 @@ describe("ShardReader", function () { }); it("should return false when peer is not on the pubsub topic shard", async function () { - const shardInfoBytes = encodeRelayShard(testRelayShards); + const shardInfoBytes = encodeRelayShard(testShardInfo); const mockPeer = { metadata: new Map([["shardInfo", shardInfoBytes]]) }; @@ -263,7 +263,7 @@ describe("ShardReader", function () { }); it("should return false when pubsub topic parsing fails", async function () { - const shardInfoBytes = encodeRelayShard(testRelayShards); + const shardInfoBytes = encodeRelayShard(testShardInfo); const mockPeer = { metadata: new Map([["shardInfo", shardInfoBytes]]) }; diff --git a/packages/core/src/lib/connection_manager/shard_reader.ts b/packages/core/src/lib/connection_manager/shard_reader.ts index b5ae35a778..2dade0de99 100644 --- a/packages/core/src/lib/connection_manager/shard_reader.ts +++ b/packages/core/src/lib/connection_manager/shard_reader.ts @@ -3,8 +3,8 @@ import type { ClusterId, NetworkConfig, PubsubTopic, - RelayShards, - ShardId + ShardId, + ShardInfo } from "@waku/interfaces"; import { decodeRelayShard, @@ -96,7 +96,7 @@ export class ShardReader implements IShardReader { ); } - private async getRelayShards(id: PeerId): Promise { + private async getRelayShards(id: PeerId): Promise { try { const peer = await this.libp2p.peerStore.get(id); diff --git a/packages/core/src/lib/metadata/metadata.ts b/packages/core/src/lib/metadata/metadata.ts index 18d59b5790..ac4707e575 100644 --- a/packages/core/src/lib/metadata/metadata.ts +++ b/packages/core/src/lib/metadata/metadata.ts @@ -7,7 +7,7 @@ import { type MetadataQueryResult, type PeerIdStr, ProtocolError, - type RelayShards + type ShardInfo } from "@waku/interfaces"; import { proto_metadata } from "@waku/proto"; import { encodeRelayShard, Logger } from "@waku/utils"; @@ -25,7 +25,7 @@ export const MetadataCodec = "/vac/waku/metadata/1.0.0"; class Metadata implements IMetadata { private readonly streamManager: StreamManager; private readonly libp2pComponents: Libp2pComponents; - protected handshakesConfirmed: Map = new Map(); + protected handshakesConfirmed: Map = new Map(); public readonly multicodec = MetadataCodec; @@ -148,7 +148,7 @@ class Metadata implements IMetadata { }); const response = proto_metadata.WakuMetadataResponse.decode( bytes - ) as RelayShards; + ) as ShardInfo; if (!response) { log.error("Error decoding metadata response"); @@ -166,16 +166,16 @@ class Metadata implements IMetadata { private async savePeerShardInfo( peerId: PeerId, - relayShards: RelayShards + shardInfo: ShardInfo ): Promise { - // add or update the relayShards to peer store + // add or update the shardInfo to peer store await this.libp2pComponents.peerStore.merge(peerId, { metadata: { - shardInfo: encodeRelayShard(relayShards) + shardInfo: encodeRelayShard(shardInfo) } }); - this.handshakesConfirmed.set(peerId.toString(), relayShards); + this.handshakesConfirmed.set(peerId.toString(), shardInfo); } } diff --git a/packages/discovery/src/peer-exchange/waku_peer_exchange_discovery.ts b/packages/discovery/src/peer-exchange/waku_peer_exchange_discovery.ts index a9d46740f2..9087f12c15 100644 --- a/packages/discovery/src/peer-exchange/waku_peer_exchange_discovery.ts +++ b/packages/discovery/src/peer-exchange/waku_peer_exchange_discovery.ts @@ -10,7 +10,7 @@ import type { import { type Libp2pComponents, type PeerExchangeQueryResult, - type RelayShards, + ShardInfo, Tags } from "@waku/interfaces"; import { decodeRelayShard, encodeRelayShard, Logger } from "@waku/utils"; @@ -279,7 +279,7 @@ export class PeerExchangeDiscovery private async checkPeerInfoDiff( peerInfo: PeerInfo, - shardInfo?: RelayShards + shardInfo?: ShardInfo ): Promise<{ hasMultiaddrDiff: boolean; hasShardDiff: boolean }> { const { id: peerId } = peerInfo; const peer = await this.components.peerStore.get(peerId); diff --git a/packages/enr/src/enr.ts b/packages/enr/src/enr.ts index 71b2bcc0fb..77c80fc1c2 100644 --- a/packages/enr/src/enr.ts +++ b/packages/enr/src/enr.ts @@ -5,8 +5,8 @@ import type { ENRValue, IEnr, NodeId, - RelayShards, - SequenceNumber + SequenceNumber, + ShardInfo } from "@waku/interfaces"; import { Logger } from "@waku/utils"; @@ -64,7 +64,7 @@ export class ENR extends RawEnr implements IEnr { protocol: TransportProtocol | TransportProtocolPerIpVersion ) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this); - public get shardInfo(): RelayShards | undefined { + public get shardInfo(): ShardInfo | undefined { if (this.rs && this.rsv) { log.warn("ENR contains both `rs` and `rsv` fields."); } diff --git a/packages/enr/src/raw_enr.ts b/packages/enr/src/raw_enr.ts index 1b3ced089a..0629932f78 100644 --- a/packages/enr/src/raw_enr.ts +++ b/packages/enr/src/raw_enr.ts @@ -6,8 +6,8 @@ import { import type { ENRKey, ENRValue, - RelayShards, SequenceNumber, + ShardInfo, Waku2 } from "@waku/interfaces"; import { decodeRelayShard } from "@waku/utils"; @@ -52,13 +52,13 @@ export class RawEnr extends Map { } } - public get rs(): RelayShards | undefined { + public get rs(): ShardInfo | undefined { const rs = this.get("rs"); if (!rs) return undefined; return decodeRelayShard(rs); } - public get rsv(): RelayShards | undefined { + public get rsv(): ShardInfo | undefined { const rsv = this.get("rsv"); if (!rsv) return undefined; return decodeRelayShard(rsv); diff --git a/packages/interfaces/src/enr.ts b/packages/interfaces/src/enr.ts index 01d4bcb751..ec4b4ab54c 100644 --- a/packages/interfaces/src/enr.ts +++ b/packages/interfaces/src/enr.ts @@ -2,7 +2,7 @@ import type { PeerId } from "@libp2p/interface"; import type { PeerInfo } from "@libp2p/interface"; import type { Multiaddr } from "@multiformats/multiaddr"; -import { RelayShards } from "./sharding.js"; +import { ShardInfo } from "./sharding.js"; export type ENRKey = string; export type ENRValue = Uint8Array; @@ -36,7 +36,7 @@ export interface IEnr extends Map { multiaddrs?: Multiaddr[]; waku2?: Waku2; peerInfo: PeerInfo | undefined; - shardInfo?: RelayShards; + shardInfo?: ShardInfo; /** * @deprecated: use { @link IEnr.peerInfo } instead. diff --git a/packages/interfaces/src/metadata.ts b/packages/interfaces/src/metadata.ts index b9714d92f8..32ce59c2e6 100644 --- a/packages/interfaces/src/metadata.ts +++ b/packages/interfaces/src/metadata.ts @@ -1,9 +1,9 @@ import type { PeerId } from "@libp2p/interface"; import { ThisOrThat } from "./misc.js"; -import type { ClusterId, RelayShards } from "./sharding.js"; +import type { ClusterId, ShardInfo } from "./sharding.js"; -export type MetadataQueryResult = ThisOrThat<"shardInfo", RelayShards>; +export type MetadataQueryResult = ThisOrThat<"shardInfo", ShardInfo>; export interface IMetadata { readonly multicodec: string; diff --git a/packages/interfaces/src/sharding.ts b/packages/interfaces/src/sharding.ts index f204d47ecd..44689c5953 100644 --- a/packages/interfaces/src/sharding.ts +++ b/packages/interfaces/src/sharding.ts @@ -4,7 +4,7 @@ */ export type NetworkConfig = StaticSharding | AutoSharding; -export type RelayShards = { +export type ShardInfo = { clusterId: ClusterId; shards: ShardId[]; }; diff --git a/packages/sdk/src/peer_manager/peer_manager.spec.ts b/packages/sdk/src/peer_manager/peer_manager.spec.ts index f4eac85f81..8a4ce9ad84 100644 --- a/packages/sdk/src/peer_manager/peer_manager.spec.ts +++ b/packages/sdk/src/peer_manager/peer_manager.spec.ts @@ -1,9 +1,11 @@ import { PeerId } from "@libp2p/interface"; import { + ClusterId, CONNECTION_LOCKED_TAG, IConnectionManager, Libp2p, - Protocols + Protocols, + ShardId } from "@waku/interfaces"; import { expect } from "chai"; import sinon from "sinon"; @@ -81,7 +83,12 @@ describe("PeerManager", () => { pubsubTopics: [TEST_PUBSUB_TOPIC], getConnectedPeers: async () => peers, getPeers: async () => peers, - isPeerOnShard: async (_id: PeerId, _topic: string) => true + isPeerOnShard: async ( + _id: PeerId, + _clusterId: ClusterId, + _shardId: ShardId + ) => true, + isPeerOnTopic: async (_id: PeerId, _topic: string) => true } as unknown as IConnectionManager; peerManager = new PeerManager({ libp2p, diff --git a/packages/tests/src/constants.ts b/packages/tests/src/constants.ts index 6847f41dac..637cb76a4c 100644 --- a/packages/tests/src/constants.ts +++ b/packages/tests/src/constants.ts @@ -5,7 +5,7 @@ * @module */ -import { AutoSharding, RelayShards } from "@waku/interfaces"; +import { AutoSharding, ShardInfo } from "@waku/interfaces"; import { createRoutingInfo } from "@waku/utils"; export const NOISE_KEY_1 = new Uint8Array( @@ -91,7 +91,7 @@ export const DefaultTestNetworkConfig: AutoSharding = { clusterId: DefaultTestClusterId, numShardsInCluster: DefaultTestNumShardsInCluster }; -export const DefaultTestRelayShards: RelayShards = { +export const DefaultTestShardInfo: ShardInfo = { clusterId: DefaultTestClusterId, shards: [0] }; diff --git a/packages/tests/tests/peer-exchange/continuous_discovery.spec.ts b/packages/tests/tests/peer-exchange/continuous_discovery.spec.ts index e85ba125e0..9034ea9e4c 100644 --- a/packages/tests/tests/peer-exchange/continuous_discovery.spec.ts +++ b/packages/tests/tests/peer-exchange/continuous_discovery.spec.ts @@ -3,8 +3,8 @@ import { type PeerId } from "@libp2p/interface"; import { peerIdFromPrivateKey } from "@libp2p/peer-id"; import { multiaddr } from "@multiformats/multiaddr"; import { PeerExchangeDiscovery } from "@waku/discovery"; -import { IEnr, LightNode, RelayShards } from "@waku/interfaces"; -import { createLightNode } from "@waku/sdk"; +import { IEnr, LightNode } from "@waku/interfaces"; +import { createLightNode, ShardInfo } from "@waku/sdk"; import { decodeRelayShard } from "@waku/utils"; import { expect } from "chai"; import Sinon from "sinon"; @@ -15,7 +15,7 @@ describe("Peer Exchange Continuous Discovery", () => { let peerId: PeerId; let randomPeerId: PeerId; let waku: LightNode; - const relayShards: RelayShards = { + const shardInfo: ShardInfo = { clusterId: 2, shards: [1, 2] }; @@ -38,7 +38,7 @@ describe("Peer Exchange Continuous Discovery", () => { const newPeerInfo = { ENR: { peerId, - shardInfo: relayShards, + shardInfo, peerInfo: { multiaddrs: newMultiaddrs, id: peerId @@ -59,14 +59,14 @@ describe("Peer Exchange Continuous Discovery", () => { }); it("Should update shard info", async () => { - const newRelayShards: RelayShards = { + const newShardInfo: ShardInfo = { clusterId: 2, shards: [1, 2, 3] }; const newPeerInfo = { ENR: { peerId, - shardInfo: newRelayShards, + shardInfo: newShardInfo, peerInfo: { multiaddrs: multiaddrs, id: peerId @@ -86,7 +86,7 @@ describe("Peer Exchange Continuous Discovery", () => { ); const _shardInfo = decodeRelayShard(newPeer.metadata.get("shardInfo")!); - expect(_shardInfo).to.deep.equal(newRelayShards); + expect(_shardInfo).to.deep.equal(newShardInfo); }); async function discoverPeerOnce(): Promise { @@ -95,7 +95,7 @@ describe("Peer Exchange Continuous Discovery", () => { const enr: IEnr = { peerId, - shardInfo: relayShards, + shardInfo, peerInfo: { multiaddrs: multiaddrs, id: peerId @@ -122,6 +122,6 @@ describe("Peer Exchange Continuous Discovery", () => { multiaddrs[0].toString() ); const _shardInfo = decodeRelayShard(peer.metadata.get("shardInfo")!); - expect(_shardInfo).to.deep.equal(relayShards); + expect(_shardInfo).to.deep.equal(shardInfo); } }); diff --git a/packages/tests/tests/peer-exchange/index.spec.ts b/packages/tests/tests/peer-exchange/index.spec.ts index c0a3128363..e34b257895 100644 --- a/packages/tests/tests/peer-exchange/index.spec.ts +++ b/packages/tests/tests/peer-exchange/index.spec.ts @@ -12,7 +12,7 @@ import { beforeEachCustom, DefaultTestClusterId, DefaultTestNetworkConfig, - DefaultTestRelayShards, + DefaultTestShardInfo, makeLogFileName, ServiceNode, tearDownNodes @@ -33,14 +33,14 @@ describe("Peer Exchange", function () { nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2"); await nwaku1.start({ clusterId: DefaultTestClusterId, - shard: DefaultTestRelayShards.shards, + shard: DefaultTestShardInfo.shards, discv5Discovery: true, peerExchange: true, relay: true }); await nwaku2.start({ clusterId: DefaultTestClusterId, - shard: DefaultTestRelayShards.shards, + shard: DefaultTestShardInfo.shards, discv5Discovery: true, peerExchange: true, discv5BootstrapNode: (await nwaku1.info()).enrUri, @@ -120,7 +120,7 @@ describe("Peer Exchange", function () { nwaku3 = new ServiceNode(makeLogFileName(this) + "3"); await nwaku3.start({ clusterId: DefaultTestClusterId, - shard: DefaultTestRelayShards.shards, + shard: DefaultTestShardInfo.shards, discv5Discovery: true, peerExchange: true, discv5BootstrapNode: (await nwaku1.info()).enrUri, diff --git a/packages/utils/src/common/relay_shard_codec.ts b/packages/utils/src/common/relay_shard_codec.ts index 334673187f..91dea7b4ea 100644 --- a/packages/utils/src/common/relay_shard_codec.ts +++ b/packages/utils/src/common/relay_shard_codec.ts @@ -1,6 +1,6 @@ -import type { RelayShards } from "@waku/interfaces"; +import type { ShardInfo } from "@waku/interfaces"; -export const decodeRelayShard = (bytes: Uint8Array): RelayShards => { +export const decodeRelayShard = (bytes: Uint8Array): ShardInfo => { // explicitly converting to Uint8Array to avoid Buffer // https://github.com/libp2p/js-libp2p/issues/2146 bytes = new Uint8Array(bytes); @@ -33,8 +33,8 @@ export const decodeRelayShard = (bytes: Uint8Array): RelayShards => { return { clusterId, shards }; }; -export const encodeRelayShard = (relayShards: RelayShards): Uint8Array => { - const { clusterId, shards } = relayShards; +export const encodeRelayShard = (shardInfo: ShardInfo): Uint8Array => { + const { clusterId, shards } = shardInfo; const totalLength = shards.length >= 64 ? 130 : 3 + 2 * shards.length; const buffer = new ArrayBuffer(totalLength); const view = new DataView(buffer);