From 4dbb1b6552687e697f28cf203e7edf0bf0608e6b Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 1 Oct 2024 21:51:26 +0200 Subject: [PATCH 01/28] fix comment of default number of peers --- packages/interfaces/src/protocols.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index 5b5e9ea919..ddd864659e 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -102,7 +102,7 @@ export type ProtocolCreateOptions = { * This is used by: * - Light Push to send messages, * - Filter to retrieve messages. - * Defaults to 3. + * Defaults to 2. */ numPeersToUse?: number; /** From 315d82fc6f98663d7d0be8439bad9119843fbbf8 Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 1 Oct 2024 21:51:58 +0200 Subject: [PATCH 02/28] export default number of peers from base protocol sdk --- packages/sdk/src/protocols/base_protocol.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk/src/protocols/base_protocol.ts b/packages/sdk/src/protocols/base_protocol.ts index 84a883eb0a..8435c930d3 100644 --- a/packages/sdk/src/protocols/base_protocol.ts +++ b/packages/sdk/src/protocols/base_protocol.ts @@ -14,7 +14,7 @@ interface Options { } const RENEW_TIME_LOCK_DURATION = 30 * 1000; -const DEFAULT_NUM_PEERS_TO_USE = 2; +export const DEFAULT_NUM_PEERS_TO_USE = 2; const DEFAULT_MAINTAIN_PEERS_INTERVAL = 30_000; export class BaseProtocolSDK implements IBaseProtocolSDK { From 23ded77323f4b943232599d3689f07b4c668fac2 Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 1 Oct 2024 21:56:27 +0200 Subject: [PATCH 03/28] rename to light_push, move class to separate file --- packages/sdk/src/index.ts | 2 +- packages/sdk/src/protocols/light_push/index.ts | 1 + .../protocols/{lightpush/index.ts => light_push/light_push.ts} | 0 packages/sdk/src/waku.ts | 2 +- 4 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 packages/sdk/src/protocols/light_push/index.ts rename packages/sdk/src/protocols/{lightpush/index.ts => light_push/light_push.ts} (100%) diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 2cbcfce937..16355050d4 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -14,7 +14,7 @@ export { defaultLibp2p, createLibp2pAndUpdateOptions } from "./create/index.js"; -export { wakuLightPush } from "./protocols/lightpush/index.js"; +export { wakuLightPush } from "./protocols/light_push/index.js"; export { wakuFilter } from "./protocols/filter/index.js"; export { wakuStore } from "./protocols/store/index.js"; diff --git a/packages/sdk/src/protocols/light_push/index.ts b/packages/sdk/src/protocols/light_push/index.ts new file mode 100644 index 0000000000..57c03d3296 --- /dev/null +++ b/packages/sdk/src/protocols/light_push/index.ts @@ -0,0 +1 @@ +export { wakuLightPush } from "./light_push.js"; diff --git a/packages/sdk/src/protocols/lightpush/index.ts b/packages/sdk/src/protocols/light_push/light_push.ts similarity index 100% rename from packages/sdk/src/protocols/lightpush/index.ts rename to packages/sdk/src/protocols/light_push/light_push.ts diff --git a/packages/sdk/src/waku.ts b/packages/sdk/src/waku.ts index a5314a99f8..ebd01f3485 100644 --- a/packages/sdk/src/waku.ts +++ b/packages/sdk/src/waku.ts @@ -17,7 +17,7 @@ import { Protocols } from "@waku/interfaces"; import { Logger } from "@waku/utils"; import { wakuFilter } from "./protocols/filter/index.js"; -import { wakuLightPush } from "./protocols/lightpush/index.js"; +import { wakuLightPush } from "./protocols/light_push/index.js"; import { wakuStore } from "./protocols/store/index.js"; import { ReliabilityMonitorManager } from "./reliability_monitor/index.js"; From 240b1b6aa5b6d1ac8a120fba2132363340850d5c Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 1 Oct 2024 22:24:41 +0200 Subject: [PATCH 04/28] move waitForRemotePeer to sdk package --- package-lock.json | 4 ++-- packages/core/package.json | 1 - packages/core/src/index.ts | 2 -- packages/sdk/package.json | 3 ++- packages/sdk/src/index.ts | 4 +++- packages/{core/src/lib => sdk/src}/wait_for_remote_peer.ts | 3 +-- packages/tests/src/lib/runNodes.ts | 3 +-- packages/tests/src/utils/nodes.ts | 3 +-- 8 files changed, 10 insertions(+), 13 deletions(-) rename packages/{core/src/lib => sdk/src}/wait_for_remote_peer.ts (96%) diff --git a/package-lock.json b/package-lock.json index 6ac618e40c..66344a70e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39089,7 +39089,6 @@ "it-all": "^3.0.4", "it-length-prefixed": "^9.0.4", "it-pipe": "^3.0.1", - "p-event": "^6.0.1", "uint8arraylist": "^2.4.3", "uuid": "^9.0.0" }, @@ -39410,7 +39409,8 @@ "@waku/message-hash": "0.1.16", "@waku/proto": "^0.0.8", "@waku/utils": "0.0.20", - "libp2p": "^1.8.1" + "libp2p": "^1.8.1", + "p-event": "^6.0.1" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.7", diff --git a/packages/core/package.json b/packages/core/package.json index 5039fb9e5c..48e2e8eb67 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -77,7 +77,6 @@ "it-all": "^3.0.4", "it-length-prefixed": "^9.0.4", "it-pipe": "^3.0.1", - "p-event": "^6.0.1", "uint8arraylist": "^2.4.3", "uuid": "^9.0.0" }, diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 3368021ead..37ef189df1 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -15,8 +15,6 @@ export { LightPushCodec, LightPushCore } from "./lib/light_push/index.js"; export * as waku_store from "./lib/store/index.js"; export { StoreCore } from "./lib/store/index.js"; -export { waitForRemotePeer } from "./lib/wait_for_remote_peer.js"; - export { ConnectionManager } from "./lib/connection_manager.js"; export { getHealthManager } from "./lib/health_manager.js"; diff --git a/packages/sdk/package.json b/packages/sdk/package.json index e2a6c6dc0b..c828b6afa4 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -70,7 +70,8 @@ "@waku/proto": "^0.0.8", "@waku/utils": "0.0.20", "@waku/message-hash": "0.1.16", - "libp2p": "^1.8.1" + "libp2p": "^1.8.1", + "p-event": "^6.0.1" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.7", diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 16355050d4..79e2140449 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -1,4 +1,4 @@ -export { waitForRemotePeer, createEncoder, createDecoder } from "@waku/core"; +export { createEncoder, createDecoder } from "@waku/core"; export { DecodedMessage, Decoder, @@ -18,6 +18,8 @@ export { wakuLightPush } from "./protocols/light_push/index.js"; export { wakuFilter } from "./protocols/filter/index.js"; export { wakuStore } from "./protocols/store/index.js"; +export { waitForRemotePeer } from "./wait_for_remote_peer.js"; + export * as waku from "@waku/core"; export * as utils from "@waku/utils"; export * from "@waku/interfaces"; diff --git a/packages/core/src/lib/wait_for_remote_peer.ts b/packages/sdk/src/wait_for_remote_peer.ts similarity index 96% rename from packages/core/src/lib/wait_for_remote_peer.ts rename to packages/sdk/src/wait_for_remote_peer.ts index 809264a648..e2953b0eea 100644 --- a/packages/core/src/lib/wait_for_remote_peer.ts +++ b/packages/sdk/src/wait_for_remote_peer.ts @@ -10,7 +10,6 @@ import { Logger } from "@waku/utils"; import { pEvent } from "p-event"; const log = new Logger("wait-for-remote-peer"); -//TODO: move this function within the Waku class: https://github.com/waku-org/js-waku/issues/1761 /** * Wait for a remote peer to be ready given the passed protocols. * Must be used after attempting to connect to nodes, using @@ -85,7 +84,6 @@ export async function waitForRemotePeer( } } -//TODO: move this function within protocol SDK class: https://github.com/waku-org/js-waku/issues/1761 /** * Wait for a peer with the given protocol to be connected. * If sharding is enabled on the node, it will also wait for the peer to be confirmed by the metadata service. @@ -110,6 +108,7 @@ async function waitForConnectedPeer( ); return; } catch (e) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED") log.error( `Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}` diff --git a/packages/tests/src/lib/runNodes.ts b/packages/tests/src/lib/runNodes.ts index 56af7bd7a0..82893f4f97 100644 --- a/packages/tests/src/lib/runNodes.ts +++ b/packages/tests/src/lib/runNodes.ts @@ -1,11 +1,10 @@ -import { waitForRemotePeer } from "@waku/core"; import { NetworkConfig, ProtocolCreateOptions, Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; -import { createLightNode, WakuNode } from "@waku/sdk"; +import { createLightNode, waitForRemotePeer, WakuNode } from "@waku/sdk"; import { derivePubsubTopicsFromNetworkConfig, Logger, diff --git a/packages/tests/src/utils/nodes.ts b/packages/tests/src/utils/nodes.ts index 2cdef056aa..dbf75e456f 100644 --- a/packages/tests/src/utils/nodes.ts +++ b/packages/tests/src/utils/nodes.ts @@ -1,4 +1,3 @@ -import { waitForRemotePeer } from "@waku/core"; import { DefaultNetworkConfig, LightNode, @@ -7,7 +6,7 @@ import { Protocols, Waku } from "@waku/interfaces"; -import { createLightNode } from "@waku/sdk"; +import { createLightNode, waitForRemotePeer } from "@waku/sdk"; import { derivePubsubTopicsFromNetworkConfig, isDefined } from "@waku/utils"; import { Context } from "mocha"; import pRetry from "p-retry"; From ad56eae44557aa1a782f5d67876662e6f2590ccf Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 1 Oct 2024 22:36:26 +0200 Subject: [PATCH 05/28] add todo to move waitForGossipSubPeerInMesh into @waku/relay --- packages/sdk/src/wait_for_remote_peer.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/sdk/src/wait_for_remote_peer.ts b/packages/sdk/src/wait_for_remote_peer.ts index e2953b0eea..82ed78a4c0 100644 --- a/packages/sdk/src/wait_for_remote_peer.ts +++ b/packages/sdk/src/wait_for_remote_peer.ts @@ -149,6 +149,7 @@ async function waitForConnectedPeer( }); } +// TODO: move to @waku/relay and use in `node.connect()` API https://github.com/waku-org/js-waku/issues/1761 /** * Wait for at least one peer with the given protocol to be connected and in the gossipsub * mesh for all pubsubTopics. From 7580eeb110e927e1ddc582e47d9ce37d45a5e866 Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 1 Oct 2024 23:52:29 +0200 Subject: [PATCH 06/28] clean up waitForRemotePeer, split metadata await from event and optimise, decouple from protocol implementations --- packages/core/src/index.ts | 2 +- packages/sdk/src/wait_for_remote_peer.ts | 168 +++++++++++++---------- 2 files changed, 94 insertions(+), 76 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 37ef189df1..9b4acf2eae 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -13,7 +13,7 @@ export * as waku_light_push from "./lib/light_push/index.js"; export { LightPushCodec, LightPushCore } from "./lib/light_push/index.js"; export * as waku_store from "./lib/store/index.js"; -export { StoreCore } from "./lib/store/index.js"; +export { StoreCore, StoreCodec } from "./lib/store/index.js"; export { ConnectionManager } from "./lib/connection_manager.js"; diff --git a/packages/sdk/src/wait_for_remote_peer.ts b/packages/sdk/src/wait_for_remote_peer.ts index 82ed78a4c0..6d78383fed 100644 --- a/packages/sdk/src/wait_for_remote_peer.ts +++ b/packages/sdk/src/wait_for_remote_peer.ts @@ -1,13 +1,10 @@ import type { IdentifyResult } from "@libp2p/interface"; -import type { - IBaseProtocolCore, - IMetadata, - IRelay, - Waku -} from "@waku/interfaces"; +import { FilterCodecs, LightPushCodec, StoreCodec } from "@waku/core"; +import type { IRelay, Libp2p, Waku } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { Logger } from "@waku/utils"; import { pEvent } from "p-event"; + const log = new Logger("wait-for-remote-peer"); /** @@ -35,42 +32,48 @@ export async function waitForRemotePeer( timeoutMs?: number ): Promise { protocols = protocols ?? getEnabledProtocols(waku); + const connections = waku.libp2p.getConnections(); + + if (!waku.isStarted()) { + throw Error("Waku node is not started"); + } - if (!waku.isStarted()) return Promise.reject("Waku node is not started"); + if (connections.length > 0) { + const success = await waitForMetadata(waku.libp2p); + + if (success) { + return; + } + } const promises = []; if (protocols.includes(Protocols.Relay)) { - if (!waku.relay) - throw new Error("Cannot wait for Relay peer: protocol not mounted"); + if (!waku.relay) { + throw Error("Cannot wait for Relay peer: protocol not mounted"); + } promises.push(waitForGossipSubPeerInMesh(waku.relay)); } if (protocols.includes(Protocols.Store)) { - if (!waku.store) - throw new Error("Cannot wait for Store peer: protocol not mounted"); - promises.push( - waitForConnectedPeer(waku.store.protocol, waku.libp2p.services.metadata) - ); + if (!waku.store) { + throw Error("Cannot wait for Store peer: protocol not mounted"); + } + promises.push(waitForConnectedPeer(StoreCodec, waku.libp2p)); } if (protocols.includes(Protocols.LightPush)) { - if (!waku.lightPush) - throw new Error("Cannot wait for LightPush peer: protocol not mounted"); - promises.push( - waitForConnectedPeer( - waku.lightPush.protocol, - waku.libp2p.services.metadata - ) - ); + if (!waku.lightPush) { + throw Error("Cannot wait for LightPush peer: protocol not mounted"); + } + promises.push(waitForConnectedPeer(LightPushCodec, waku.libp2p)); } if (protocols.includes(Protocols.Filter)) { - if (!waku.filter) + if (!waku.filter) { throw new Error("Cannot wait for Filter peer: protocol not mounted"); - promises.push( - waitForConnectedPeer(waku.filter.protocol, waku.libp2p.services.metadata) - ); + } + promises.push(waitForConnectedPeer(FilterCodecs.SUBSCRIBE, waku.libp2p)); } if (timeoutMs) { @@ -84,71 +87,86 @@ export async function waitForRemotePeer( } } +type EventListener = (_: CustomEvent) => void; + /** * Wait for a peer with the given protocol to be connected. * If sharding is enabled on the node, it will also wait for the peer to be confirmed by the metadata service. */ async function waitForConnectedPeer( - protocol: IBaseProtocolCore, - metadataService?: IMetadata + codec: string, + libp2p: Libp2p ): Promise { - const codec = protocol.multicodec; - const peers = await protocol.connectedPeers(); + log.info(`Waiting for ${codec} peer.`); - if (peers.length) { - if (!metadataService) { - log.info(`${codec} peer found: `, peers[0].id.toString()); - return; - } + await new Promise((resolve) => { + const cb = (async (evt: CustomEvent): Promise => { + if (evt.detail?.protocols?.includes(codec)) { + const metadataService = libp2p.services.metadata; - // once a peer is connected, we need to confirm the metadata handshake with at least one of those peers if sharding is enabled - try { - await Promise.any( - peers.map((peer) => metadataService.confirmOrAttemptHandshake(peer.id)) - ); - return; - } catch (e) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED") - log.error( - `Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}` - ); - - log.error(`Error waiting for handshake confirmation: ${e}`); - } - } + if (!metadataService) { + libp2p.removeEventListener("peer:identify", cb); + resolve(); + return; + } - log.info(`Waiting for ${codec} peer`); + try { + await metadataService.confirmOrAttemptHandshake(evt.detail.peerId); - // else we'll just wait for the next peer to connect - await new Promise((resolve) => { - const cb = (evt: CustomEvent): void => { - if (evt.detail?.protocols?.includes(codec)) { - if (metadataService) { - metadataService - .confirmOrAttemptHandshake(evt.detail.peerId) - .then(() => { - protocol.removeLibp2pEventListener("peer:identify", cb); - resolve(); - }) - .catch((e) => { - if (e.code === "ERR_CONNECTION_BEING_CLOSED") - log.error( - `Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}` - ); - - log.error(`Error waiting for handshake confirmation: ${e}`); - }); - } else { - protocol.removeLibp2pEventListener("peer:identify", cb); + libp2p.removeEventListener("peer:identify", cb); resolve(); + } catch (e) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED") { + log.error( + "Connection closed. Some peers can be on different shard." + ); + } + + log.error(`Error waiting for metadata: ${e}`); } } - }; - protocol.addLibp2pEventListener("peer:identify", cb); + }) as EventListener; + + libp2p.addEventListener("peer:identify", cb); }); } +/** + * Waits for the metadata from the remote peer. + */ +async function waitForMetadata(libp2p: Libp2p): Promise { + const connections = libp2p.getConnections(); + const metadataService = libp2p.services.metadata; + + if (!connections.length || !metadataService) { + log.info( + `Skipping waitForMetadata due to missing connections:${connections.length} or metadataService:${!!metadataService}` + ); + return false; + } + + try { + // confirm at least with one connected peer + await Promise.any( + connections + .map((c) => c.remotePeer) + .map((peer) => metadataService.confirmOrAttemptHandshake(peer)) + ); + + return true; + } catch (e) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED") { + log.error("Connection closed. Some peers can be on different shard."); + } + + log.error(`Error waiting for metadata: ${e}`); + } + + return false; +} + // TODO: move to @waku/relay and use in `node.connect()` API https://github.com/waku-org/js-waku/issues/1761 /** * Wait for at least one peer with the given protocol to be connected and in the gossipsub From 800eae20ffc1f95af769ff14f09d2f9239f2d2d3 Mon Sep 17 00:00:00 2001 From: Sasha Date: Wed, 2 Oct 2024 00:12:33 +0200 Subject: [PATCH 07/28] simplify and rename ILightPush interface --- packages/interfaces/src/light_push.ts | 5 ++--- packages/interfaces/src/waku.ts | 6 +++--- packages/sdk/src/protocols/light_push/light_push.ts | 6 +++--- packages/sdk/src/waku.ts | 4 ++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/interfaces/src/light_push.ts b/packages/interfaces/src/light_push.ts index 26bc8bacdc..b9350c6b6b 100644 --- a/packages/interfaces/src/light_push.ts +++ b/packages/interfaces/src/light_push.ts @@ -1,5 +1,4 @@ -import { IBaseProtocolCore, IBaseProtocolSDK } from "./protocols.js"; +import { IBaseProtocolCore } from "./protocols.js"; import type { ISender } from "./sender.js"; -export type ILightPushSDK = ISender & - IBaseProtocolSDK & { protocol: IBaseProtocolCore }; +export type ILightPush = ISender & { protocol: IBaseProtocolCore }; diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index c5659c4a92..482c2cb19f 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -5,7 +5,7 @@ import { IConnectionManager } from "./connection_manager.js"; import type { IFilterSDK } from "./filter.js"; import { IHealthManager } from "./health_manager.js"; import type { Libp2p } from "./libp2p.js"; -import type { ILightPushSDK } from "./light_push.js"; +import type { ILightPush } from "./light_push.js"; import { Protocols } from "./protocols.js"; import type { IRelay } from "./relay.js"; import type { IStoreSDK } from "./store.js"; @@ -15,7 +15,7 @@ export interface Waku { relay?: IRelay; store?: IStoreSDK; filter?: IFilterSDK; - lightPush?: ILightPushSDK; + lightPush?: ILightPush; connectionManager: IConnectionManager; @@ -36,7 +36,7 @@ export interface LightNode extends Waku { relay: undefined; store: IStoreSDK; filter: IFilterSDK; - lightPush: ILightPushSDK; + lightPush: ILightPush; } export interface RelayNode extends Waku { diff --git a/packages/sdk/src/protocols/light_push/light_push.ts b/packages/sdk/src/protocols/light_push/light_push.ts index f3be55d54a..dc50e655a4 100644 --- a/packages/sdk/src/protocols/light_push/light_push.ts +++ b/packages/sdk/src/protocols/light_push/light_push.ts @@ -3,7 +3,7 @@ import { ConnectionManager, LightPushCore } from "@waku/core"; import { Failure, type IEncoder, - ILightPushSDK, + ILightPush, type IMessage, type Libp2p, type ProtocolCreateOptions, @@ -19,7 +19,7 @@ import { BaseProtocolSDK } from "../base_protocol.js"; const log = new Logger("sdk:light-push"); -class LightPushSDK extends BaseProtocolSDK implements ILightPushSDK { +class LightPushSDK extends BaseProtocolSDK implements ILightPush { public readonly protocol: LightPushCore; private readonly reliabilityMonitor: SenderReliabilityMonitor; @@ -131,6 +131,6 @@ class LightPushSDK extends BaseProtocolSDK implements ILightPushSDK { export function wakuLightPush( connectionManager: ConnectionManager, init: Partial = {} -): (libp2p: Libp2p) => ILightPushSDK { +): (libp2p: Libp2p) => ILightPush { return (libp2p: Libp2p) => new LightPushSDK(connectionManager, libp2p, init); } diff --git a/packages/sdk/src/waku.ts b/packages/sdk/src/waku.ts index ebd01f3485..be656ad796 100644 --- a/packages/sdk/src/waku.ts +++ b/packages/sdk/src/waku.ts @@ -5,7 +5,7 @@ import { ConnectionManager, getHealthManager } from "@waku/core"; import type { IFilterSDK, IHealthManager, - ILightPushSDK, + ILightPush, IRelay, IStoreSDK, Libp2p, @@ -64,7 +64,7 @@ export class WakuNode implements Waku { public relay?: IRelay; public store?: IStoreSDK; public filter?: IFilterSDK; - public lightPush?: ILightPushSDK; + public lightPush?: ILightPush; public connectionManager: ConnectionManager; public readonly health: IHealthManager; From 6bcf2836867016a4458cafff4eb32343038c8a8f Mon Sep 17 00:00:00 2001 From: Sasha Date: Wed, 2 Oct 2024 21:43:44 +0200 Subject: [PATCH 08/28] use only connected peers in light push based on connections instead of peer renewal mechanism --- .../src/protocols/light_push/light_push.ts | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/sdk/src/protocols/light_push/light_push.ts b/packages/sdk/src/protocols/light_push/light_push.ts index dc50e655a4..044a55568a 100644 --- a/packages/sdk/src/protocols/light_push/light_push.ts +++ b/packages/sdk/src/protocols/light_push/light_push.ts @@ -1,5 +1,5 @@ -import type { PeerId } from "@libp2p/interface"; -import { ConnectionManager, LightPushCore } from "@waku/core"; +import type { Peer, PeerId } from "@libp2p/interface"; +import { ConnectionManager, LightPushCodec, LightPushCore } from "@waku/core"; import { Failure, type IEncoder, @@ -19,14 +19,14 @@ import { BaseProtocolSDK } from "../base_protocol.js"; const log = new Logger("sdk:light-push"); -class LightPushSDK extends BaseProtocolSDK implements ILightPush { +class LightPush extends BaseProtocolSDK implements ILightPush { public readonly protocol: LightPushCore; private readonly reliabilityMonitor: SenderReliabilityMonitor; public constructor( connectionManager: ConnectionManager, - libp2p: Libp2p, + private libp2p: Libp2p, options?: ProtocolCreateOptions ) { super( @@ -49,11 +49,6 @@ class LightPushSDK extends BaseProtocolSDK implements ILightPush { message: IMessage, _options?: ProtocolUseOptions ): Promise { - const options = { - autoRetry: true, - ..._options - } as ProtocolUseOptions; - const successes: PeerId[] = []; const failures: Failure[] = []; @@ -72,8 +67,8 @@ class LightPushSDK extends BaseProtocolSDK implements ILightPush { }; } - const hasPeers = await this.hasPeers(options); - if (!hasPeers) { + const peers = await this.getConnectedPeers(); + if (peers.length === 0) { return { successes, failures: [ @@ -84,7 +79,7 @@ class LightPushSDK extends BaseProtocolSDK implements ILightPush { }; } - const sendPromises = this.connectedPeers.map((peer) => + const sendPromises = peers.map((peer) => this.protocol.send(encoder, message, peer) ); @@ -126,11 +121,38 @@ class LightPushSDK extends BaseProtocolSDK implements ILightPush { failures }; } + + private async getConnectedPeers(): Promise { + const peerIDs = this.libp2p + .getConnections() + .filter((c) => c.status === "open") + .sort((left, right) => right.timeline.open - left.timeline.open) + .map((c) => c.remotePeer); + + if (peerIDs.length === 0) { + return []; + } + + const peers = await Promise.all( + peerIDs.map(async (id) => { + try { + return await this.libp2p.peerStore.get(id); + } catch (e) { + return null; + } + }) + ); + + return peers + .filter((p) => !!p) + .filter((p) => (p as Peer).protocols.includes(LightPushCodec)) + .slice(0, this.numPeersToUse) as Peer[]; + } } export function wakuLightPush( connectionManager: ConnectionManager, init: Partial = {} ): (libp2p: Libp2p) => ILightPush { - return (libp2p: Libp2p) => new LightPushSDK(connectionManager, libp2p, init); + return (libp2p: Libp2p) => new LightPush(connectionManager, libp2p, init); } From 9742d6887d9eb35d0349ec864e4b0b5d8ee2ee19 Mon Sep 17 00:00:00 2001 From: Sasha Date: Wed, 2 Oct 2024 22:07:45 +0200 Subject: [PATCH 09/28] improve readability of result processing in light push --- .../src/protocols/light_push/light_push.ts | 59 +++++++++---------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/packages/sdk/src/protocols/light_push/light_push.ts b/packages/sdk/src/protocols/light_push/light_push.ts index 044a55568a..b75c97dd13 100644 --- a/packages/sdk/src/protocols/light_push/light_push.ts +++ b/packages/sdk/src/protocols/light_push/light_push.ts @@ -58,12 +58,12 @@ class LightPush extends BaseProtocolSDK implements ILightPush { } catch (error) { log.error("Failed to send waku light push: pubsub topic not configured"); return { + successes, failures: [ { error: ProtocolError.TOPIC_NOT_CONFIGURED } - ], - successes: [] + ] }; } @@ -79,40 +79,37 @@ class LightPush extends BaseProtocolSDK implements ILightPush { }; } - const sendPromises = peers.map((peer) => - this.protocol.send(encoder, message, peer) + const results = await Promise.allSettled( + peers.map((peer) => this.protocol.send(encoder, message, peer)) ); - const results = await Promise.allSettled(sendPromises); - for (const result of results) { - if (result.status === "fulfilled") { - const { failure, success } = result.value; - if (success) { - successes.push(success); - } - if (failure) { - failures.push(failure); - if (failure.peerId) { - const peer = this.connectedPeers.find((connectedPeer) => - connectedPeer.id.equals(failure.peerId) - ); - if (peer) { - log.info(` - Failed to send message to peer ${failure.peerId}. - Retrying the message with the same peer in the background. - If this fails, the peer will be renewed. - `); - void this.reliabilityMonitor.attemptRetriesOrRenew( - failure.peerId, - () => this.protocol.send(encoder, message, peer) - ); - } - } - } - } else { + if (result.status !== "fulfilled") { log.error("Failed unexpectedly while sending:", result.reason); failures.push({ error: ProtocolError.GENERIC_FAIL }); + continue; + } + + const { failure, success } = result.value; + + if (success) { + successes.push(success); + continue; + } + + if (failure) { + failures.push(failure); + + const connectedPeer = this.connectedPeers.find((connectedPeer) => + connectedPeer.id.equals(failure.peerId) + ); + + if (connectedPeer) { + void this.reliabilityMonitor.attemptRetriesOrRenew( + connectedPeer.id, + () => this.protocol.send(encoder, message, connectedPeer) + ); + } } } From 0f0990596de28d98172ef716d99d9f59215dfa3b Mon Sep 17 00:00:00 2001 From: Sasha Date: Wed, 2 Oct 2024 22:49:26 +0200 Subject: [PATCH 10/28] fix check & update tests --- packages/interfaces/src/light_push.ts | 5 +++-- packages/tests/tests/enr.node.spec.ts | 2 +- packages/tests/tests/ephemeral.node.spec.ts | 9 ++------- packages/tests/tests/filter/push.node.spec.ts | 3 +-- .../filter/single_node/multiple_pubsub.node.spec.ts | 3 ++- .../tests/tests/filter/single_node/push.node.spec.ts | 3 +-- .../tests/filter/single_node/subscribe.node.spec.ts | 4 ++-- packages/tests/tests/filter/utils.ts | 4 ++-- packages/tests/tests/getPeers.spec.ts | 5 +++-- .../light-push/single_node/multiple_pubsub.node.spec.ts | 3 ++- packages/tests/tests/relay/interop.node.spec.ts | 3 ++- packages/tests/tests/relay/multiple_pubsub.node.spec.ts | 8 ++------ packages/tests/tests/relay/utils.ts | 3 ++- packages/tests/tests/store/index.node.spec.ts | 3 ++- packages/tests/tests/store/multiple_pubsub.spec.ts | 4 ++-- packages/tests/tests/wait_for_remote_peer.node.spec.ts | 3 +-- packages/tests/tests/waku.node.spec.ts | 5 +++-- 17 files changed, 33 insertions(+), 37 deletions(-) diff --git a/packages/interfaces/src/light_push.ts b/packages/interfaces/src/light_push.ts index b9350c6b6b..32b29048e6 100644 --- a/packages/interfaces/src/light_push.ts +++ b/packages/interfaces/src/light_push.ts @@ -1,4 +1,5 @@ -import { IBaseProtocolCore } from "./protocols.js"; +import { IBaseProtocolCore, IBaseProtocolSDK } from "./protocols.js"; import type { ISender } from "./sender.js"; -export type ILightPush = ISender & { protocol: IBaseProtocolCore }; +export type ILightPush = ISender & + IBaseProtocolSDK & { protocol: IBaseProtocolCore }; diff --git a/packages/tests/tests/enr.node.spec.ts b/packages/tests/tests/enr.node.spec.ts index 18ca31a04e..6a02e70fb9 100644 --- a/packages/tests/tests/enr.node.spec.ts +++ b/packages/tests/tests/enr.node.spec.ts @@ -1,8 +1,8 @@ -import { waitForRemotePeer } from "@waku/core"; import { EnrDecoder } from "@waku/enr"; import type { RelayNode } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; +import { waitForRemotePeer } from "@waku/sdk"; import { expect } from "chai"; import { diff --git a/packages/tests/tests/ephemeral.node.spec.ts b/packages/tests/tests/ephemeral.node.spec.ts index c9a68a110f..25ad308d8e 100644 --- a/packages/tests/tests/ephemeral.node.spec.ts +++ b/packages/tests/tests/ephemeral.node.spec.ts @@ -1,9 +1,4 @@ -import { - createDecoder, - createEncoder, - DecodedMessage, - waitForRemotePeer -} from "@waku/core"; +import { createDecoder, createEncoder, DecodedMessage } from "@waku/core"; import { Protocols } from "@waku/interfaces"; import type { LightNode } from "@waku/interfaces"; import { @@ -19,7 +14,7 @@ import { createDecoder as createSymDecoder, createEncoder as createSymEncoder } from "@waku/message-encryption/symmetric"; -import { createLightNode } from "@waku/sdk"; +import { createLightNode, waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, Logger } from "@waku/utils"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/filter/push.node.spec.ts b/packages/tests/tests/filter/push.node.spec.ts index 5d69c73948..bbd49be6c2 100644 --- a/packages/tests/tests/filter/push.node.spec.ts +++ b/packages/tests/tests/filter/push.node.spec.ts @@ -1,6 +1,5 @@ -import { waitForRemotePeer } from "@waku/core"; import { LightNode, Protocols } from "@waku/interfaces"; -import { utf8ToBytes } from "@waku/sdk"; +import { utf8ToBytes, waitForRemotePeer } from "@waku/sdk"; import { expect } from "chai"; import { diff --git a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts index 75e12b1919..60aeeebfe9 100644 --- a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts @@ -1,4 +1,4 @@ -import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core"; +import { createDecoder, createEncoder } from "@waku/core"; import type { ContentTopicInfo, LightNode, @@ -6,6 +6,7 @@ import type { SingleShardInfo } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; +import { waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, contentTopicToShardIndex, diff --git a/packages/tests/tests/filter/single_node/push.node.spec.ts b/packages/tests/tests/filter/single_node/push.node.spec.ts index 79fd020ae3..baf074b2c2 100644 --- a/packages/tests/tests/filter/single_node/push.node.spec.ts +++ b/packages/tests/tests/filter/single_node/push.node.spec.ts @@ -1,6 +1,5 @@ -import { waitForRemotePeer } from "@waku/core"; import { LightNode, Protocols } from "@waku/interfaces"; -import { utf8ToBytes } from "@waku/sdk"; +import { utf8ToBytes, waitForRemotePeer } from "@waku/sdk"; import { expect } from "chai"; import { diff --git a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts index e75f81f94d..cf2aef1303 100644 --- a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts +++ b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts @@ -1,4 +1,4 @@ -import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core"; +import { createDecoder, createEncoder } from "@waku/core"; import { LightNode, Protocols } from "@waku/interfaces"; import { ecies, @@ -7,7 +7,7 @@ import { getPublicKey, symmetric } from "@waku/message-encryption"; -import { utf8ToBytes } from "@waku/sdk"; +import { utf8ToBytes, waitForRemotePeer } from "@waku/sdk"; import { expect } from "chai"; import type { Context } from "mocha"; diff --git a/packages/tests/tests/filter/utils.ts b/packages/tests/tests/filter/utils.ts index 2bcbd88125..1e3165af24 100644 --- a/packages/tests/tests/filter/utils.ts +++ b/packages/tests/tests/filter/utils.ts @@ -1,4 +1,4 @@ -import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core"; +import { createDecoder, createEncoder } from "@waku/core"; import { DefaultNetworkConfig, ISubscriptionSDK, @@ -8,7 +8,7 @@ import { Protocols, Waku } from "@waku/interfaces"; -import { createLightNode } from "@waku/sdk"; +import { createLightNode, waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, derivePubsubTopicsFromNetworkConfig, diff --git a/packages/tests/tests/getPeers.spec.ts b/packages/tests/tests/getPeers.spec.ts index 252340a5bf..52127bb54b 100644 --- a/packages/tests/tests/getPeers.spec.ts +++ b/packages/tests/tests/getPeers.spec.ts @@ -1,6 +1,6 @@ import type { Connection, Peer, PeerStore } from "@libp2p/interface"; import { createSecp256k1PeerId } from "@libp2p/peer-id-factory"; -import { LightPushCodec, waitForRemotePeer } from "@waku/core"; +import { LightPushCodec } from "@waku/core"; import { ContentTopicInfo, createLightNode, @@ -9,7 +9,8 @@ import { Protocols, ShardInfo, Tags, - utf8ToBytes + utf8ToBytes, + waitForRemotePeer } from "@waku/sdk"; import { encodeRelayShard, diff --git a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts index 4fabf1919f..f36a614aaf 100644 --- a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts @@ -1,5 +1,5 @@ import type { PeerId } from "@libp2p/interface"; -import { createEncoder, waitForRemotePeer } from "@waku/core"; +import { createEncoder } from "@waku/core"; import { ContentTopicInfo, LightNode, @@ -8,6 +8,7 @@ import { ShardInfo, SingleShardInfo } from "@waku/interfaces"; +import { waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, contentTopicToShardIndex, diff --git a/packages/tests/tests/relay/interop.node.spec.ts b/packages/tests/tests/relay/interop.node.spec.ts index b9cc3eeb70..3c05bebfa0 100644 --- a/packages/tests/tests/relay/interop.node.spec.ts +++ b/packages/tests/tests/relay/interop.node.spec.ts @@ -1,7 +1,8 @@ import type { PeerId } from "@libp2p/interface"; -import { DecodedMessage, waitForRemotePeer } from "@waku/core"; +import { DecodedMessage } from "@waku/core"; import { Protocols, RelayNode } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; +import { waitForRemotePeer } from "@waku/sdk"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts index 91915f76a0..6434153f17 100644 --- a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts @@ -1,9 +1,4 @@ -import { - createDecoder, - createEncoder, - DecodedMessage, - waitForRemotePeer -} from "@waku/core"; +import { createDecoder, createEncoder, DecodedMessage } from "@waku/core"; import { ContentTopicInfo, RelayNode, @@ -12,6 +7,7 @@ import { } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; +import { waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, pubsubTopicToSingleShardInfo, diff --git a/packages/tests/tests/relay/utils.ts b/packages/tests/tests/relay/utils.ts index 090d6221e6..f0141e3481 100644 --- a/packages/tests/tests/relay/utils.ts +++ b/packages/tests/tests/relay/utils.ts @@ -1,4 +1,4 @@ -import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core"; +import { createDecoder, createEncoder } from "@waku/core"; import { NetworkConfig, Protocols, @@ -6,6 +6,7 @@ import { ShardInfo } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; +import { waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, Logger } from "@waku/utils"; import { Context } from "mocha"; diff --git a/packages/tests/tests/store/index.node.spec.ts b/packages/tests/tests/store/index.node.spec.ts index e34864d604..e5ce92d7a0 100644 --- a/packages/tests/tests/store/index.node.spec.ts +++ b/packages/tests/tests/store/index.node.spec.ts @@ -1,4 +1,4 @@ -import { createDecoder, DecodedMessage, waitForRemotePeer } from "@waku/core"; +import { createDecoder, DecodedMessage } from "@waku/core"; import type { IMessage, LightNode } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { @@ -14,6 +14,7 @@ import { createDecoder as createSymDecoder, createEncoder as createSymEncoder } from "@waku/message-encryption/symmetric"; +import { waitForRemotePeer } from "@waku/sdk"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { equals } from "uint8arrays/equals"; diff --git a/packages/tests/tests/store/multiple_pubsub.spec.ts b/packages/tests/tests/store/multiple_pubsub.spec.ts index 2a98793cb2..de2b346b7b 100644 --- a/packages/tests/tests/store/multiple_pubsub.spec.ts +++ b/packages/tests/tests/store/multiple_pubsub.spec.ts @@ -1,6 +1,6 @@ -import { createDecoder, waitForRemotePeer } from "@waku/core"; +import { createDecoder } from "@waku/core"; import type { ContentTopicInfo, IMessage, LightNode } from "@waku/interfaces"; -import { createLightNode, Protocols } from "@waku/sdk"; +import { createLightNode, Protocols, waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, pubsubTopicToSingleShardInfo diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 90393bf2f3..71c89eda0f 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -1,8 +1,7 @@ -import { waitForRemotePeer } from "@waku/core"; import type { LightNode, RelayNode } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; -import { createLightNode } from "@waku/sdk"; +import { createLightNode, waitForRemotePeer } from "@waku/sdk"; import { expect } from "chai"; import { diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 3881971e1e..250835f16b 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -1,6 +1,6 @@ import { bootstrap } from "@libp2p/bootstrap"; import type { PeerId } from "@libp2p/interface"; -import { DecodedMessage, waitForRemotePeer } from "@waku/core"; +import { DecodedMessage } from "@waku/core"; import type { LightNode, RelayNode, Waku } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { generateSymmetricKey } from "@waku/message-encryption"; @@ -12,7 +12,8 @@ import { createRelayNode } from "@waku/relay"; import { createLightNode, createEncoder as createPlainEncoder, - DefaultUserAgent + DefaultUserAgent, + waitForRemotePeer } from "@waku/sdk"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; From 3dda8ad177e15e24df53a77f218c6ffbc43e98c8 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 3 Oct 2024 22:13:08 +0200 Subject: [PATCH 11/28] address tests, add new test cases, fix racing condition in StreamManager --- .../lib/stream_manager/stream_manager.spec.ts | 29 +++++++- .../src/lib/stream_manager/stream_manager.ts | 27 ++++++-- packages/sdk/src/protocols/base_protocol.ts | 2 +- .../src/protocols/light_push/light_push.ts | 2 + packages/sdk/src/wait_for_remote_peer.ts | 2 +- .../tests/filter/peer_management.spec.ts | 3 - .../tests/tests/health-manager/node.spec.ts | 28 +++++++- .../tests/light-push/peer_management.spec.ts | 69 +++++++------------ 8 files changed, 104 insertions(+), 58 deletions(-) diff --git a/packages/core/src/lib/stream_manager/stream_manager.spec.ts b/packages/core/src/lib/stream_manager/stream_manager.spec.ts index 6706a5ff9f..e66b199d2e 100644 --- a/packages/core/src/lib/stream_manager/stream_manager.spec.ts +++ b/packages/core/src/lib/stream_manager/stream_manager.spec.ts @@ -86,6 +86,32 @@ describe("StreamManager", () => { } }); + it("should return different streams if requested simultaniously", async () => { + const con1 = createMockConnection(); + con1.streams = [createMockStream({ id: "1", protocol: MULTICODEC })]; + + const newStreamSpy = sinon.spy(async (_protocol, _options) => + createMockStream({ + id: "2", + protocol: MULTICODEC, + writeStatus: "writable" + }) + ); + + con1.newStream = newStreamSpy; + streamManager["getConnections"] = (_peerId: PeerId | undefined) => [con1]; + + const [stream1, stream2] = await Promise.all([ + streamManager.getStream(mockPeer), + streamManager.getStream(mockPeer) + ]); + + const expected = ["1", "2"].toString(); + const actual = [stream1.id, stream2.id].sort().toString(); + + expect(actual).to.be.eq(expected); + }); + it("peer:update - should do nothing if another protocol hit", async () => { const scheduleNewStreamSpy = sinon.spy(); streamManager["scheduleNewStream"] = scheduleNewStreamSpy; @@ -156,6 +182,7 @@ function createMockStream(options: MockStreamOptions): Stream { return { id: options.id, protocol: options.protocol, - writeStatus: options.writeStatus || "ready" + writeStatus: options.writeStatus || "ready", + metadata: {} } as Stream; } diff --git a/packages/core/src/lib/stream_manager/stream_manager.ts b/packages/core/src/lib/stream_manager/stream_manager.ts index 6b2799a706..e1da56453d 100644 --- a/packages/core/src/lib/stream_manager/stream_manager.ts +++ b/packages/core/src/lib/stream_manager/stream_manager.ts @@ -4,6 +4,8 @@ import { Logger } from "@waku/utils"; import { selectOpenConnection } from "./utils.js"; +const STREAM_LOCK_KEY = "consumed"; + export class StreamManager { private readonly log: Logger; @@ -29,16 +31,20 @@ export class StreamManager { await scheduledStream; } - const stream = this.getOpenStreamForCodec(peer.id); + let stream = this.getOpenStreamForCodec(peer.id); if (stream) { this.log.info( `Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}` ); + this.lockStream(peer.id.toString(), stream); return stream; } - return this.createStream(peer); + stream = await this.createStream(peer); + this.lockStream(peer.id.toString(), stream); + + return stream; } private async createStream(peer: Peer, retries = 0): Promise { @@ -142,13 +148,26 @@ export class StreamManager { (s) => s.protocol === this.multicodec ); + if (!stream) { + return; + } + const isStreamUnusable = ["done", "closed", "closing"].includes( - stream?.writeStatus || "" + stream.writeStatus || "" ); - if (isStreamUnusable) { + if (isStreamUnusable || this.isStreamLocked(stream)) { return; } return stream; } + + private lockStream(peerId: string, stream: Stream): void { + this.log.info(`Locking stream for peerId:${peerId}\tstreamId:${stream.id}`); + stream.metadata[STREAM_LOCK_KEY] = true; + } + + private isStreamLocked(stream: Stream): boolean { + return !!stream.metadata[STREAM_LOCK_KEY]; + } } diff --git a/packages/sdk/src/protocols/base_protocol.ts b/packages/sdk/src/protocols/base_protocol.ts index 8435c930d3..944ad497c2 100644 --- a/packages/sdk/src/protocols/base_protocol.ts +++ b/packages/sdk/src/protocols/base_protocol.ts @@ -18,7 +18,7 @@ export const DEFAULT_NUM_PEERS_TO_USE = 2; const DEFAULT_MAINTAIN_PEERS_INTERVAL = 30_000; export class BaseProtocolSDK implements IBaseProtocolSDK { - private healthManager: IHealthManager; + protected healthManager: IHealthManager; public readonly numPeersToUse: number; private peers: Peer[] = []; private maintainPeersIntervalId: ReturnType< diff --git a/packages/sdk/src/protocols/light_push/light_push.ts b/packages/sdk/src/protocols/light_push/light_push.ts index b75c97dd13..395cd95f5e 100644 --- a/packages/sdk/src/protocols/light_push/light_push.ts +++ b/packages/sdk/src/protocols/light_push/light_push.ts @@ -113,6 +113,8 @@ class LightPush extends BaseProtocolSDK implements ILightPush { } } + this.healthManager.updateProtocolHealth(LightPushCodec, successes.length); + return { successes, failures diff --git a/packages/sdk/src/wait_for_remote_peer.ts b/packages/sdk/src/wait_for_remote_peer.ts index 6d78383fed..44bb1d63fb 100644 --- a/packages/sdk/src/wait_for_remote_peer.ts +++ b/packages/sdk/src/wait_for_remote_peer.ts @@ -38,7 +38,7 @@ export async function waitForRemotePeer( throw Error("Waku node is not started"); } - if (connections.length > 0) { + if (connections.length > 0 && !protocols.includes(Protocols.Relay)) { const success = await waitForMetadata(waku.libp2p); if (success) { diff --git a/packages/tests/tests/filter/peer_management.spec.ts b/packages/tests/tests/filter/peer_management.spec.ts index 13e7baba05..31a6c07fad 100644 --- a/packages/tests/tests/filter/peer_management.spec.ts +++ b/packages/tests/tests/filter/peer_management.spec.ts @@ -275,8 +275,5 @@ describe("Waku Filter: Peer Management: E2E", function () { await sendMessage(); expect(waku.filter.connectedPeers.length).to.equal(2); - expect( - waku.filter.connectedPeers.map((p) => p.id.toString()) - ).to.not.include(nodeWithouDiscoveryPeerIdStr); }); }); diff --git a/packages/tests/tests/health-manager/node.spec.ts b/packages/tests/tests/health-manager/node.spec.ts index b0edc41cb5..5627adab37 100644 --- a/packages/tests/tests/health-manager/node.spec.ts +++ b/packages/tests/tests/health-manager/node.spec.ts @@ -1,4 +1,4 @@ -import { HealthStatus, LightNode, Protocols } from "@waku/interfaces"; +import { HealthStatus, LightNode, Protocols, Waku } from "@waku/interfaces"; import { createLightNode } from "@waku/sdk"; import { shardInfoToPubsubTopics } from "@waku/utils"; import { expect } from "chai"; @@ -34,8 +34,7 @@ describe("Node Health Status Matrix Tests", function () { peerCounts.forEach((lightPushPeers) => { peerCounts.forEach((filterPeers) => { - const expectedHealth = getExpectedNodeHealth(lightPushPeers, filterPeers); - it(`LightPush: ${lightPushPeers} peers, Filter: ${filterPeers} peers - Expected: ${expectedHealth}`, async function () { + it(`LightPush: ${lightPushPeers} peers, Filter: ${filterPeers} peers`, async function () { this.timeout(10_000); [waku, serviceNodes] = await setupTestEnvironment( @@ -59,6 +58,10 @@ describe("Node Health Status Matrix Tests", function () { ); const filterHealth = waku.health.getProtocolStatus(Protocols.Filter); + lightPushPeers = await getPeerCounBasedOnConnections( + waku, + waku.lightPush.protocol.multicodec + ); expect(lightPushHealth?.status).to.equal( getExpectedProtocolStatus(lightPushPeers) ); @@ -66,6 +69,10 @@ describe("Node Health Status Matrix Tests", function () { getExpectedProtocolStatus(filterPeers) ); + const expectedHealth = getExpectedNodeHealth( + lightPushPeers, + filterPeers + ); const nodeHealth = waku.health.getHealthStatus(); expect(nodeHealth).to.equal(expectedHealth); }); @@ -79,6 +86,21 @@ function getExpectedProtocolStatus(peerCount: number): HealthStatus { return HealthStatus.SufficientlyHealthy; } +async function getPeerCounBasedOnConnections( + waku: Waku, + codec: string +): Promise { + const peerIDs = waku.libp2p + .getConnections() + .map((c) => c.remotePeer.toString()); + + const peers = await waku.libp2p.peerStore.all(); + + return peers + .filter((peer) => peerIDs.includes(peer.id.toString())) + .filter((peer) => peer.protocols.includes(codec)).length; +} + function getExpectedNodeHealth( lightPushPeers: number, filterPeers: number diff --git a/packages/tests/tests/light-push/peer_management.spec.ts b/packages/tests/tests/light-push/peer_management.spec.ts index a275f4e970..776d503cb3 100644 --- a/packages/tests/tests/light-push/peer_management.spec.ts +++ b/packages/tests/tests/light-push/peer_management.spec.ts @@ -1,6 +1,5 @@ import { LightNode } from "@waku/interfaces"; import { createEncoder, utf8ToBytes } from "@waku/sdk"; -import { delay } from "@waku/utils"; import { expect } from "chai"; import { describe } from "mocha"; @@ -15,7 +14,7 @@ import { } from "../../src/index.js"; import { TestContentTopic } from "../filter/utils.js"; -describe("Waku Light Push: Peer Management: E2E", function () { +describe("Waku Light Push: Connection Management: E2E", function () { this.timeout(15000); let waku: LightNode; let serviceNodes: ServiceNodesFleet; @@ -39,64 +38,44 @@ describe("Waku Light Push: Peer Management: E2E", function () { contentTopic: TestContentTopic }); - it("Number of peers are maintained correctly", async function () { + it("should push to needed amount of connections", async function () { const { successes, failures } = await waku.lightPush.send(encoder, { payload: utf8ToBytes("Hello_World") }); - expect(successes.length).to.be.greaterThan(0); expect(successes.length).to.be.equal(waku.lightPush.numPeersToUse); - - if (failures) { - expect(failures.length).to.equal(0); - } + expect(failures?.length || 0).to.equal(0); }); - it("Failed peers are renewed", async function () { - // send a lightpush request -- should have all successes - const response1 = await waku.lightPush.send(encoder, { - payload: utf8ToBytes("Hello_World") - }); - - expect(response1.successes.length).to.be.equal( - waku.lightPush.numPeersToUse + it("should push to available amount of connection if less than required", async function () { + const connections = waku.libp2p.getConnections(); + await Promise.all( + connections + .slice(0, connections.length - 1) + .map((c) => waku.connectionManager.dropConnection(c.remotePeer)) ); - if (response1.failures) { - expect(response1.failures.length).to.equal(0); - } - // disconnect from one peer to force a failure - const peerToDisconnect = response1.successes[0]; - await waku.connectionManager.dropConnection(peerToDisconnect); - - // send another lightpush request -- should have all successes except the one that was disconnected - const response2 = await waku.lightPush.send(encoder, { + const { successes, failures } = await waku.lightPush.send(encoder, { payload: utf8ToBytes("Hello_World") }); - // check that the peer that was disconnected is not in the new successes - expect(response2.successes).to.not.include(peerToDisconnect); - expect(response2.failures).to.have.length(1); - expect(response2.failures?.[0].peerId).to.equal(peerToDisconnect); + expect(successes.length).to.be.equal(1); + expect(failures?.length || 0).to.equal(0); + }); - // send another lightpush request - // reattempts to send should be triggerred - // then renewal should happen - // so one failure should exist - const response3 = await waku.lightPush.send(encoder, { + it("should fail to send if no connections available", async function () { + const connections = waku.libp2p.getConnections(); + await Promise.all( + connections.map((c) => + waku.connectionManager.dropConnection(c.remotePeer) + ) + ); + + const { successes, failures } = await waku.lightPush.send(encoder, { payload: utf8ToBytes("Hello_World") }); - // wait for reattempts to finish as they are async and not awaited - await delay(500); - - // doing -1 because the peer that was disconnected is not in the successes - expect(response3.successes.length).to.be.equal( - waku.lightPush.numPeersToUse - 1 - ); - // and exists in failure instead - expect(response3.failures).to.have.length(1); - - expect(response3.successes).to.not.include(peerToDisconnect); + expect(successes.length).to.be.equal(0); + expect(failures?.length).to.equal(1); }); }); From 06498f8c373411e20014e80d41388d0f478348fe Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 00:11:42 +0200 Subject: [PATCH 12/28] use libp2p.getPeers --- packages/sdk/src/protocols/light_push/light_push.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/sdk/src/protocols/light_push/light_push.ts b/packages/sdk/src/protocols/light_push/light_push.ts index 395cd95f5e..7007ff28d5 100644 --- a/packages/sdk/src/protocols/light_push/light_push.ts +++ b/packages/sdk/src/protocols/light_push/light_push.ts @@ -122,11 +122,7 @@ class LightPush extends BaseProtocolSDK implements ILightPush { } private async getConnectedPeers(): Promise { - const peerIDs = this.libp2p - .getConnections() - .filter((c) => c.status === "open") - .sort((left, right) => right.timeline.open - left.timeline.open) - .map((c) => c.remotePeer); + const peerIDs = this.libp2p.getPeers(); if (peerIDs.length === 0) { return []; From eaee6351d96c0ddbfdf89580f43bb638d9fbfb17 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 01:38:47 +0200 Subject: [PATCH 13/28] feat: confirm metadata and protocols needed in waitForRemotePeer --- packages/sdk/src/wait_for_remote_peer.ts | 73 ++++++++++++++++++------ 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/packages/sdk/src/wait_for_remote_peer.ts b/packages/sdk/src/wait_for_remote_peer.ts index 44bb1d63fb..9126bd7314 100644 --- a/packages/sdk/src/wait_for_remote_peer.ts +++ b/packages/sdk/src/wait_for_remote_peer.ts @@ -39,7 +39,7 @@ export async function waitForRemotePeer( } if (connections.length > 0 && !protocols.includes(Protocols.Relay)) { - const success = await waitForMetadata(waku.libp2p); + const success = await waitForMetadata(waku); if (success) { return; @@ -135,33 +135,52 @@ async function waitForConnectedPeer( /** * Waits for the metadata from the remote peer. */ -async function waitForMetadata(libp2p: Libp2p): Promise { - const connections = libp2p.getConnections(); - const metadataService = libp2p.services.metadata; +async function waitForMetadata(waku: Waku): Promise { + const connectedPeers = waku.libp2p.getPeers(); + const metadataService = waku.libp2p.services.metadata; + const enabledCodes = getEnabledCodecs(waku); - if (!connections.length || !metadataService) { + if (!connectedPeers.length || !metadataService) { log.info( - `Skipping waitForMetadata due to missing connections:${connections.length} or metadataService:${!!metadataService}` + `Skipping waitForMetadata due to missing connections:${connectedPeers.length} or metadataService:${!!metadataService}` ); return false; } - try { - // confirm at least with one connected peer - await Promise.any( - connections - .map((c) => c.remotePeer) - .map((peer) => metadataService.confirmOrAttemptHandshake(peer)) + for (const peerId of connectedPeers) { + const confirmedAllCodecs = Array.from(enabledCodes.values()).every( + (v) => v ); - return true; - } catch (e) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED") { - log.error("Connection closed. Some peers can be on different shard."); + if (confirmedAllCodecs) { + return true; } - log.error(`Error waiting for metadata: ${e}`); + try { + const peer = await waku.libp2p.peerStore.get(peerId); + const hasSomeCodes = peer.protocols.some((c) => enabledCodes.has(c)); + + if (hasSomeCodes) { + const response = + await metadataService.confirmOrAttemptHandshake(peerId); + + if (!response.error) { + peer.protocols.forEach((c) => { + if (enabledCodes.has(c)) { + enabledCodes.set(c, true); + } + }); + } + } + } catch (e) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED") { + log.error("Connection closed. Some peers can be on different shard."); + } + + log.error(`Error while iterating through peers: ${e}`); + continue; + } } return false; @@ -216,3 +235,21 @@ function getEnabledProtocols(waku: Waku): Protocols[] { return protocols; } + +function getEnabledCodecs(waku: Waku): Map { + const codecs: Map = new Map(); + + if (waku.filter) { + codecs.set(FilterCodecs.SUBSCRIBE, false); + } + + if (waku.store) { + codecs.set(StoreCodec, false); + } + + if (waku.lightPush) { + codecs.set(LightPushCodec, false); + } + + return codecs; +} From 21056fd7fe1cc9b6c81ae349d609ac758432ebdb Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 14:59:09 +0200 Subject: [PATCH 14/28] rely on passed protocols and fallback to mounted --- packages/sdk/src/wait_for_remote_peer.ts | 32 +++++++++++++----------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/sdk/src/wait_for_remote_peer.ts b/packages/sdk/src/wait_for_remote_peer.ts index 9126bd7314..a7bae966b4 100644 --- a/packages/sdk/src/wait_for_remote_peer.ts +++ b/packages/sdk/src/wait_for_remote_peer.ts @@ -31,7 +31,8 @@ export async function waitForRemotePeer( protocols?: Protocols[], timeoutMs?: number ): Promise { - protocols = protocols ?? getEnabledProtocols(waku); + // if no protocols or empty array passed - try to derive from mounted + protocols = protocols?.length ? protocols : getEnabledProtocols(waku); const connections = waku.libp2p.getConnections(); if (!waku.isStarted()) { @@ -39,7 +40,7 @@ export async function waitForRemotePeer( } if (connections.length > 0 && !protocols.includes(Protocols.Relay)) { - const success = await waitForMetadata(waku); + const success = await waitForMetadata(waku, protocols); if (success) { return; @@ -135,10 +136,13 @@ async function waitForConnectedPeer( /** * Waits for the metadata from the remote peer. */ -async function waitForMetadata(waku: Waku): Promise { +async function waitForMetadata( + waku: Waku, + protocols: Protocols[] +): Promise { const connectedPeers = waku.libp2p.getPeers(); const metadataService = waku.libp2p.services.metadata; - const enabledCodes = getEnabledCodecs(waku); + const enabledCodes = mapProtocolsToCodecs(protocols); if (!connectedPeers.length || !metadataService) { log.info( @@ -236,19 +240,19 @@ function getEnabledProtocols(waku: Waku): Protocols[] { return protocols; } -function getEnabledCodecs(waku: Waku): Map { +function mapProtocolsToCodecs(protocols: Protocols[]): Map { const codecs: Map = new Map(); - if (waku.filter) { - codecs.set(FilterCodecs.SUBSCRIBE, false); - } - - if (waku.store) { - codecs.set(StoreCodec, false); - } + const protocolToCodec: Record = { + [Protocols.Filter]: FilterCodecs.SUBSCRIBE, + [Protocols.LightPush]: LightPushCodec, + [Protocols.Store]: StoreCodec + }; - if (waku.lightPush) { - codecs.set(LightPushCodec, false); + for (const protocol of protocols) { + if (protocolToCodec[protocol]) { + codecs.set(protocolToCodec[protocol], false); + } } return codecs; From ac212c166ba494a35c0fc1083e6dedd06e62143a Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 15:38:52 +0200 Subject: [PATCH 15/28] add I prefix to Waku interface --- packages/interfaces/src/waku.ts | 6 +++--- packages/sdk/src/wait_for_remote_peer.ts | 8 ++++---- packages/sdk/src/waku.ts | 6 +++--- packages/tests/src/utils/nodes.ts | 6 +++--- packages/tests/src/utils/teardown.ts | 4 ++-- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index c22258e0f5..82024568c4 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -10,7 +10,7 @@ import { Protocols } from "./protocols.js"; import type { IRelay } from "./relay.js"; import type { IStore } from "./store.js"; -export interface Waku { +export interface IWaku { libp2p: Libp2p; relay?: IRelay; store?: IStore; @@ -32,14 +32,14 @@ export interface Waku { health: IHealthManager; } -export interface LightNode extends Waku { +export interface LightNode extends IWaku { relay: undefined; store: IStore; filter: IFilter; lightPush: ILightPush; } -export interface RelayNode extends Waku { +export interface RelayNode extends IWaku { relay: IRelay; store: undefined; filter: undefined; diff --git a/packages/sdk/src/wait_for_remote_peer.ts b/packages/sdk/src/wait_for_remote_peer.ts index a7bae966b4..ee8727a10d 100644 --- a/packages/sdk/src/wait_for_remote_peer.ts +++ b/packages/sdk/src/wait_for_remote_peer.ts @@ -1,6 +1,6 @@ import type { IdentifyResult } from "@libp2p/interface"; import { FilterCodecs, LightPushCodec, StoreCodec } from "@waku/core"; -import type { IRelay, Libp2p, Waku } from "@waku/interfaces"; +import type { IRelay, IWaku, Libp2p } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { Logger } from "@waku/utils"; import { pEvent } from "p-event"; @@ -27,7 +27,7 @@ const log = new Logger("wait-for-remote-peer"); * @default Wait for remote peers with protocols enabled locally and no time out is applied. */ export async function waitForRemotePeer( - waku: Waku, + waku: IWaku, protocols?: Protocols[], timeoutMs?: number ): Promise { @@ -137,7 +137,7 @@ async function waitForConnectedPeer( * Waits for the metadata from the remote peer. */ async function waitForMetadata( - waku: Waku, + waku: IWaku, protocols: Protocols[] ): Promise { const connectedPeers = waku.libp2p.getPeers(); @@ -218,7 +218,7 @@ async function rejectOnTimeout( await Promise.race([promise, awaitTimeout(timeoutMs, rejectReason)]); } -function getEnabledProtocols(waku: Waku): Protocols[] { +function getEnabledProtocols(waku: IWaku): Protocols[] { const protocols = []; if (waku.relay) { diff --git a/packages/sdk/src/waku.ts b/packages/sdk/src/waku.ts index 88dddea5df..3911c2c7c4 100644 --- a/packages/sdk/src/waku.ts +++ b/packages/sdk/src/waku.ts @@ -8,10 +8,10 @@ import type { ILightPush, IRelay, IStore, + IWaku, Libp2p, ProtocolCreateOptions, - PubsubTopic, - Waku + PubsubTopic } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { Logger } from "@waku/utils"; @@ -59,7 +59,7 @@ type ProtocolsEnabled = { store?: boolean; }; -export class WakuNode implements Waku { +export class WakuNode implements IWaku { public libp2p: Libp2p; public relay?: IRelay; public store?: IStore; diff --git a/packages/tests/src/utils/nodes.ts b/packages/tests/src/utils/nodes.ts index dbf75e456f..6d7f1db219 100644 --- a/packages/tests/src/utils/nodes.ts +++ b/packages/tests/src/utils/nodes.ts @@ -1,10 +1,10 @@ import { DefaultNetworkConfig, + IWaku, LightNode, NetworkConfig, ProtocolCreateOptions, - Protocols, - Waku + Protocols } from "@waku/interfaces"; import { createLightNode, waitForRemotePeer } from "@waku/sdk"; import { derivePubsubTopicsFromNetworkConfig, isDefined } from "@waku/utils"; @@ -79,7 +79,7 @@ export async function runMultipleNodes( export async function teardownNodesWithRedundancy( serviceNodes: ServiceNodesFleet, - wakuNodes: Waku | Waku[] + wakuNodes: IWaku | IWaku[] ): Promise { const wNodes = Array.isArray(wakuNodes) ? wakuNodes : [wakuNodes]; diff --git a/packages/tests/src/utils/teardown.ts b/packages/tests/src/utils/teardown.ts index 5bdee8e448..86257ceb41 100644 --- a/packages/tests/src/utils/teardown.ts +++ b/packages/tests/src/utils/teardown.ts @@ -1,4 +1,4 @@ -import { Waku } from "@waku/interfaces"; +import { IWaku } from "@waku/interfaces"; import { Logger } from "@waku/utils"; import pRetry from "p-retry"; @@ -8,7 +8,7 @@ const log = new Logger("test:teardown"); export async function tearDownNodes( nwakuNodes: ServiceNode | ServiceNode[], - wakuNodes: Waku | Waku[] + wakuNodes: IWaku | IWaku[] ): Promise { const nNodes = Array.isArray(nwakuNodes) ? nwakuNodes : [nwakuNodes]; const wNodes = Array.isArray(wakuNodes) ? wakuNodes : [wakuNodes]; From 4b94686d4f017d7136bb8a9c62d6e1642399cb86 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 15:40:47 +0200 Subject: [PATCH 16/28] implement waku.connect method --- packages/interfaces/src/waku.ts | 5 +++-- packages/sdk/src/waku.ts | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index 82024568c4..b71beba7ac 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -17,6 +17,7 @@ export interface IWaku { filter?: IFilter; lightPush?: ILightPush; + health: IHealthManager; connectionManager: IConnectionManager; dial(peer: PeerId | MultiaddrInput, protocols?: Protocols[]): Promise; @@ -25,11 +26,11 @@ export interface IWaku { stop(): Promise; + connect(): Promise; + isStarted(): boolean; isConnected(): boolean; - - health: IHealthManager; } export interface LightNode extends IWaku { diff --git a/packages/sdk/src/waku.ts b/packages/sdk/src/waku.ts index 3911c2c7c4..c470269958 100644 --- a/packages/sdk/src/waku.ts +++ b/packages/sdk/src/waku.ts @@ -20,6 +20,7 @@ import { wakuFilter } from "./protocols/filter/index.js"; import { wakuLightPush } from "./protocols/light_push/index.js"; import { wakuStore } from "./protocols/store/index.js"; import { ReliabilityMonitorManager } from "./reliability_monitor/index.js"; +import { waitForRemotePeer } from "./wait_for_remote_peer.js"; export const DefaultPingKeepAliveValueSecs = 5 * 60; export const DefaultRelayKeepAliveValueSecs = 5 * 60; @@ -201,6 +202,13 @@ export class WakuNode implements IWaku { await this.libp2p.stop(); } + public async connect( + protocols?: Protocols[], + timeoutMs?: number + ): Promise { + return waitForRemotePeer(this, protocols, timeoutMs); + } + public isStarted(): boolean { return this.libp2p.status == "started"; } From 690e630a154364a432b58b9e3b64e70de0092da6 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 16:22:59 +0200 Subject: [PATCH 17/28] add docs to IWaku interface --- packages/interfaces/src/waku.ts | 72 ++++++++++++++++++++++++++++++++- packages/sdk/src/waku.ts | 6 --- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index b71beba7ac..43daf0ed84 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -20,16 +20,86 @@ export interface IWaku { health: IHealthManager; connectionManager: IConnectionManager; + /** + * Dials to the provided peer + * + * @param {PeerId | MultiaddrInput} peer information to use for dialing + * @param {Protocols[]} [protocols] array of Waku protocols to be used for dialing. If no provided - will be derived from mounted protocols. + * + * @returns {Promise} `Promise` that will resolve to a `Stream` to a dialed peer + * + * @example + * ```typescript + * await waku.dial(remotePeerId, [Protocols.LightPush]); + * + * waku.isConnected() === true; + * ``` + */ dial(peer: PeerId | MultiaddrInput, protocols?: Protocols[]): Promise; + /** + * Starts all services and components related to functionality of Waku node. + * + * @returns {Promise} `Promise` that will resolve when started. + * + * @example + * ```typescript + * await waku.start(); + * + * waku.isStarted() === true; + * ``` + */ start(): Promise; + /** + * Stops all recurring processes and services that are needed for functionality of Waku node. + * + * @returns {Promise} `Promise` that resolves when stopped. + * + * @example + * ```typescript + * await waku.stop(); + * + * waku.isStarted === false; + * ``` + */ stop(): Promise; - connect(): Promise; + /** + * Resolves when Waku successfully gains connection to a remote peers that fits provided requirements. + * Must be used after attempting to connect to nodes, using {@link IWaku.dial} or + * if was bootstrapped by using {@link IPeerExchange} or {@link DnsDiscoveryComponents}. + * + * @param {Protocols[]} [protocols] Protocols that need to be enabled by remote peers + * @param {number} [timeoutMs] Timeout value in milliseconds after which promise rejects + * + * @returns {Promise} `Promise` that **resolves** if all desired protocols are fulfilled by + * at least one remote peer, **rejects** if the timeoutMs is reached + * @throws If passing a protocol that is not mounted or Waku node is not started + * + * @example + * ```typescript + * try { + * // let's wait for at least one LightPush node and timeout in 1 second + * await waku.connect([Protocols.LightPush], 1000); + * } catch(e) { + * waku.isConnected() === false; + * console.error("Failed to connect due to", e); + * } + * + * waku.isConnected() === true; + * ``` + */ + connect(protocols?: Protocols[], timeoutMs?: number): Promise; + /** + * @returns {boolean} `true` if the node was started and `false` otherwise + */ isStarted(): boolean; + /** + * @returns {boolean} `true` if the node has working connection and `false` otherwise + */ isConnected(): boolean; } diff --git a/packages/sdk/src/waku.ts b/packages/sdk/src/waku.ts index c470269958..e1497f6662 100644 --- a/packages/sdk/src/waku.ts +++ b/packages/sdk/src/waku.ts @@ -127,12 +127,6 @@ export class WakuNode implements IWaku { ); } - /** - * Dials to the provided peer. - * - * @param peer The peer to dial - * @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols - */ public async dial( peer: PeerId | MultiaddrInput, protocols?: Protocols[] From ba78ed2321bf218ffdf2feed63944daa56d1b65d Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 17:00:16 +0200 Subject: [PATCH 18/28] remove export and usage of waitForRemotePeer --- packages/sdk/src/create/create.ts | 2 +- packages/sdk/src/create/libp2p.ts | 2 +- packages/sdk/src/index.ts | 4 +--- packages/sdk/src/waku/index.ts | 1 + .../src/{ => waku}/wait_for_remote_peer.ts | 0 packages/sdk/src/{ => waku}/waku.ts | 9 +++++---- packages/tests/src/lib/runNodes.ts | 4 ++-- packages/tests/src/utils/nodes.ts | 5 ++--- packages/tests/tests/enr.node.spec.ts | 7 +++---- packages/tests/tests/ephemeral.node.spec.ts | 8 ++++---- packages/tests/tests/filter/push.node.spec.ts | 6 +++--- .../single_node/multiple_pubsub.node.spec.ts | 7 +++---- .../filter/single_node/push.node.spec.ts | 6 +++--- .../filter/single_node/subscribe.node.spec.ts | 2 +- packages/tests/tests/filter/utils.ts | 10 +++++----- packages/tests/tests/getPeers.spec.ts | 19 +++++++++---------- .../single_node/multiple_pubsub.node.spec.ts | 7 +++---- .../tests/tests/relay/interop.node.spec.ts | 3 +-- .../tests/sharding/auto_sharding.spec.ts | 15 +++++---------- .../tests/sharding/static_sharding.spec.ts | 15 +++++---------- packages/tests/tests/store/index.node.spec.ts | 3 +-- .../tests/tests/store/multiple_pubsub.spec.ts | 8 ++++---- packages/tests/tests/store/utils.ts | 2 +- .../tests/wait_for_remote_peer.node.spec.ts | 8 ++++---- packages/tests/tests/waku.node.spec.ts | 7 +++---- 25 files changed, 71 insertions(+), 89 deletions(-) create mode 100644 packages/sdk/src/waku/index.ts rename packages/sdk/src/{ => waku}/wait_for_remote_peer.ts (100%) rename packages/sdk/src/{ => waku}/waku.ts (96%) diff --git a/packages/sdk/src/create/create.ts b/packages/sdk/src/create/create.ts index 4e0b3706c5..155fee3403 100644 --- a/packages/sdk/src/create/create.ts +++ b/packages/sdk/src/create/create.ts @@ -1,6 +1,6 @@ import { type LightNode } from "@waku/interfaces"; -import { CreateWakuNodeOptions, WakuNode } from "../waku.js"; +import { CreateWakuNodeOptions, WakuNode } from "../waku/index.js"; import { createLibp2pAndUpdateOptions } from "./libp2p.js"; diff --git a/packages/sdk/src/create/libp2p.ts b/packages/sdk/src/create/libp2p.ts index bbeb7a3c0a..81ff9e7cfe 100644 --- a/packages/sdk/src/create/libp2p.ts +++ b/packages/sdk/src/create/libp2p.ts @@ -21,7 +21,7 @@ import { CreateWakuNodeOptions, DefaultPingMaxInboundStreams, DefaultUserAgent -} from "../waku.js"; +} from "../waku/index.js"; import { defaultPeerDiscoveries } from "./discovery.js"; diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 79e2140449..4bf636e6b1 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -7,7 +7,7 @@ export { export { utf8ToBytes, bytesToUtf8 } from "@waku/utils/bytes"; -export * from "./waku.js"; +export * from "./waku/index.js"; export { createLightNode, @@ -18,8 +18,6 @@ export { wakuLightPush } from "./protocols/light_push/index.js"; export { wakuFilter } from "./protocols/filter/index.js"; export { wakuStore } from "./protocols/store/index.js"; -export { waitForRemotePeer } from "./wait_for_remote_peer.js"; - export * as waku from "@waku/core"; export * as utils from "@waku/utils"; export * from "@waku/interfaces"; diff --git a/packages/sdk/src/waku/index.ts b/packages/sdk/src/waku/index.ts new file mode 100644 index 0000000000..8718e26a4c --- /dev/null +++ b/packages/sdk/src/waku/index.ts @@ -0,0 +1 @@ +export * from "./waku.js"; diff --git a/packages/sdk/src/wait_for_remote_peer.ts b/packages/sdk/src/waku/wait_for_remote_peer.ts similarity index 100% rename from packages/sdk/src/wait_for_remote_peer.ts rename to packages/sdk/src/waku/wait_for_remote_peer.ts diff --git a/packages/sdk/src/waku.ts b/packages/sdk/src/waku/waku.ts similarity index 96% rename from packages/sdk/src/waku.ts rename to packages/sdk/src/waku/waku.ts index e1497f6662..9801e082bb 100644 --- a/packages/sdk/src/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -16,10 +16,11 @@ import type { import { Protocols } from "@waku/interfaces"; import { Logger } from "@waku/utils"; -import { wakuFilter } from "./protocols/filter/index.js"; -import { wakuLightPush } from "./protocols/light_push/index.js"; -import { wakuStore } from "./protocols/store/index.js"; -import { ReliabilityMonitorManager } from "./reliability_monitor/index.js"; +import { wakuFilter } from "../protocols/filter/index.js"; +import { wakuLightPush } from "../protocols/light_push/index.js"; +import { wakuStore } from "../protocols/store/index.js"; +import { ReliabilityMonitorManager } from "../reliability_monitor/index.js"; + import { waitForRemotePeer } from "./wait_for_remote_peer.js"; export const DefaultPingKeepAliveValueSecs = 5 * 60; diff --git a/packages/tests/src/lib/runNodes.ts b/packages/tests/src/lib/runNodes.ts index 82893f4f97..5b9197cdd9 100644 --- a/packages/tests/src/lib/runNodes.ts +++ b/packages/tests/src/lib/runNodes.ts @@ -4,7 +4,7 @@ import { Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; -import { createLightNode, waitForRemotePeer, WakuNode } from "@waku/sdk"; +import { createLightNode, WakuNode } from "@waku/sdk"; import { derivePubsubTopicsFromNetworkConfig, Logger, @@ -63,7 +63,7 @@ export async function runNodes( if (waku) { await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, protocols); + await waku.connect(protocols); await nwaku.ensureSubscriptions(pubsubTopics); return [nwaku, waku as T]; } else { diff --git a/packages/tests/src/utils/nodes.ts b/packages/tests/src/utils/nodes.ts index 6d7f1db219..c9ab503257 100644 --- a/packages/tests/src/utils/nodes.ts +++ b/packages/tests/src/utils/nodes.ts @@ -6,7 +6,7 @@ import { ProtocolCreateOptions, Protocols } from "@waku/interfaces"; -import { createLightNode, waitForRemotePeer } from "@waku/sdk"; +import { createLightNode } from "@waku/sdk"; import { derivePubsubTopicsFromNetworkConfig, isDefined } from "@waku/utils"; import { Context } from "mocha"; import pRetry from "p-retry"; @@ -52,8 +52,7 @@ export async function runMultipleNodes( for (const node of serviceNodes.nodes) { await waku.dial(await node.getMultiaddrWithId()); - await waitForRemotePeer( - waku, + await waku.connect( [ !customArgs?.filter ? undefined : Protocols.Filter, !customArgs?.lightpush ? undefined : Protocols.LightPush diff --git a/packages/tests/tests/enr.node.spec.ts b/packages/tests/tests/enr.node.spec.ts index 6a02e70fb9..2cad6280fc 100644 --- a/packages/tests/tests/enr.node.spec.ts +++ b/packages/tests/tests/enr.node.spec.ts @@ -2,7 +2,6 @@ import { EnrDecoder } from "@waku/enr"; import type { RelayNode } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; -import { waitForRemotePeer } from "@waku/sdk"; import { expect } from "chai"; import { @@ -41,7 +40,7 @@ describe("ENR Interop: ServiceNode", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waitForRemotePeer(waku, [Protocols.Relay]); + await waku.connect([Protocols.Relay]); const nwakuInfo = await nwaku.info(); const nimPeerId = await nwaku.getPeerId(); @@ -75,7 +74,7 @@ describe("ENR Interop: ServiceNode", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waitForRemotePeer(waku, [Protocols.Relay]); + await waku.connect([Protocols.Relay]); const nwakuInfo = await nwaku.info(); const nimPeerId = await nwaku.getPeerId(); @@ -110,7 +109,7 @@ describe("ENR Interop: ServiceNode", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waitForRemotePeer(waku, [Protocols.Relay]); + await waku.connect([Protocols.Relay]); const nwakuInfo = await nwaku.info(); const nimPeerId = await nwaku.getPeerId(); diff --git a/packages/tests/tests/ephemeral.node.spec.ts b/packages/tests/tests/ephemeral.node.spec.ts index 25ad308d8e..5d4a80ed11 100644 --- a/packages/tests/tests/ephemeral.node.spec.ts +++ b/packages/tests/tests/ephemeral.node.spec.ts @@ -14,7 +14,7 @@ import { createDecoder as createSymDecoder, createEncoder as createSymEncoder } from "@waku/message-encryption/symmetric"; -import { createLightNode, waitForRemotePeer } from "@waku/sdk"; +import { createLightNode } from "@waku/sdk"; import { contentTopicToPubsubTopic, Logger } from "@waku/utils"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; @@ -110,7 +110,7 @@ describe("Waku Message Ephemeral field", function () { await waku.start(); await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [ + await waku.connect([ Protocols.Filter, Protocols.LightPush, Protocols.Store @@ -161,7 +161,7 @@ describe("Waku Message Ephemeral field", function () { log.info("Waku nodes connected to nwaku"); - await waitForRemotePeer(waku1, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); log.info("Sending messages using light push"); await Promise.all([ @@ -170,7 +170,7 @@ describe("Waku Message Ephemeral field", function () { waku1.lightPush.send(ClearEncoder, clearMsg) ]); - await waitForRemotePeer(waku2, [Protocols.Store]); + await waku2.connect([Protocols.Store]); const messages: DecodedMessage[] = []; diff --git a/packages/tests/tests/filter/push.node.spec.ts b/packages/tests/tests/filter/push.node.spec.ts index bbd49be6c2..6ac26e530a 100644 --- a/packages/tests/tests/filter/push.node.spec.ts +++ b/packages/tests/tests/filter/push.node.spec.ts @@ -1,5 +1,5 @@ import { LightNode, Protocols } from "@waku/interfaces"; -import { utf8ToBytes, waitForRemotePeer } from "@waku/sdk"; +import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { @@ -233,7 +233,7 @@ const runTests = (strictCheckNodes: boolean): void => { // Redo the connection and create a new subscription for (const node of this.serviceNodes) { await waku.dial(await node.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); + await waku.connect([Protocols.Filter, Protocols.LightPush]); } await waku.filter.subscribe( @@ -273,7 +273,7 @@ const runTests = (strictCheckNodes: boolean): void => { // Restart nwaku node await teardownNodesWithRedundancy(serviceNodes, []); await serviceNodes.start(); - await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); + await waku.connect([Protocols.Filter, Protocols.LightPush]); await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M2") }); diff --git a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts index 60aeeebfe9..b4efef0755 100644 --- a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts @@ -6,7 +6,6 @@ import type { SingleShardInfo } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; -import { waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, contentTopicToShardIndex, @@ -117,7 +116,7 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () { clusterId: 3 }); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); + await waku.connect([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptions([customPubsubTopic2]); @@ -270,7 +269,7 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () { contentTopic: [customContentTopic2] }); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); + await waku.connect([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptionsAutosharding([customContentTopic2]); @@ -409,7 +408,7 @@ describe("Waku Filter V2 (Named sharding): Multiple PubsubTopics", function () { clusterId: 3 }); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); + await waku.connect([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptions([customPubsubTopic2]); diff --git a/packages/tests/tests/filter/single_node/push.node.spec.ts b/packages/tests/tests/filter/single_node/push.node.spec.ts index baf074b2c2..ae8ea12e8e 100644 --- a/packages/tests/tests/filter/single_node/push.node.spec.ts +++ b/packages/tests/tests/filter/single_node/push.node.spec.ts @@ -1,5 +1,5 @@ import { LightNode, Protocols } from "@waku/interfaces"; -import { utf8ToBytes, waitForRemotePeer } from "@waku/sdk"; +import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { @@ -215,7 +215,7 @@ describe("Waku Filter V2: FilterPush", function () { // Redo the connection and create a new subscription await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); + await waku.connect([Protocols.Filter, Protocols.LightPush]); await waku.filter.subscribe([TestDecoder], messageCollector.callback); @@ -242,7 +242,7 @@ describe("Waku Filter V2: FilterPush", function () { // Restart nwaku node await tearDownNodes(nwaku, []); await nwaku.start(); - await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); + await waku.connect([Protocols.Filter, Protocols.LightPush]); await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M2") }); diff --git a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts index cf2aef1303..4218b299b3 100644 --- a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts +++ b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts @@ -445,7 +445,7 @@ describe("Waku Filter V2: Subscribe: Single Service Node", function () { // Set up and start a new nwaku node [nwaku2, waku2] = await runNodes(ctx, TestShardInfo); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); + await waku.connect([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptions([TestPubsubTopic]); // Send a message using the new subscription diff --git a/packages/tests/tests/filter/utils.ts b/packages/tests/tests/filter/utils.ts index e0d02767a0..78a6e9db86 100644 --- a/packages/tests/tests/filter/utils.ts +++ b/packages/tests/tests/filter/utils.ts @@ -2,13 +2,13 @@ import { createDecoder, createEncoder } from "@waku/core"; import { DefaultNetworkConfig, ISubscription, + IWaku, LightNode, NetworkConfig, ProtocolCreateOptions, - Protocols, - Waku + Protocols } from "@waku/interfaces"; -import { createLightNode, waitForRemotePeer } from "@waku/sdk"; +import { createLightNode } from "@waku/sdk"; import { contentTopicToPubsubTopic, derivePubsubTopicsFromNetworkConfig, @@ -107,7 +107,7 @@ export async function runMultipleNodes( for (const node of serviceNodes.nodes) { await waku.dial(await node.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); + await waku.connect([Protocols.Filter, Protocols.LightPush]); await node.ensureSubscriptions(pubsubTopics); const wakuConnections = waku.libp2p.getConnections(); @@ -126,7 +126,7 @@ export async function runMultipleNodes( export async function teardownNodesWithRedundancy( serviceNodes: ServiceNodesFleet, - wakuNodes: Waku | Waku[] + wakuNodes: IWaku | IWaku[] ): Promise { const wNodes = Array.isArray(wakuNodes) ? wakuNodes : [wakuNodes]; diff --git a/packages/tests/tests/getPeers.spec.ts b/packages/tests/tests/getPeers.spec.ts index 52127bb54b..30d6e0a014 100644 --- a/packages/tests/tests/getPeers.spec.ts +++ b/packages/tests/tests/getPeers.spec.ts @@ -9,8 +9,7 @@ import { Protocols, ShardInfo, Tags, - utf8ToBytes, - waitForRemotePeer + utf8ToBytes } from "@waku/sdk"; import { encodeRelayShard, @@ -70,7 +69,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { waku = await createLightNode({ networkConfig: shardInfo }); await waku.start(); await waku.libp2p.dialProtocol(serviceNodeMa, LightPushCodec); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), waku.libp2p.peerStore, @@ -120,7 +119,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode1Ma, LightPushCodec); await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -173,7 +172,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -226,7 +225,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await delay(500); await waku.libp2p.dialProtocol(serviceNodeMa2, LightPushCodec); await waku.start(); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -260,7 +259,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { waku = await createLightNode({ networkConfig: shardInfo }); await waku.start(); await waku.libp2p.dialProtocol(serviceNodeMa, LightPushCodec); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), waku.libp2p.peerStore, @@ -313,7 +312,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -367,7 +366,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -421,7 +420,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await delay(500); await waku.libp2p.dialProtocol(serviceNodeMa2, LightPushCodec); await waku.start(); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), diff --git a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts index f36a614aaf..fddd604a75 100644 --- a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts @@ -8,7 +8,6 @@ import { ShardInfo, SingleShardInfo } from "@waku/interfaces"; -import { waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, contentTopicToShardIndex, @@ -133,7 +132,7 @@ describe("Waku Light Push : Multiple PubsubTopics", function () { singleShardInfoToPubsubTopic(singleShardInfo2) ]); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); @@ -275,7 +274,7 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () { }); await nwaku2.ensureSubscriptionsAutosharding([customContentTopic2]); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); @@ -426,7 +425,7 @@ describe("Waku Light Push (named sharding): Multiple PubsubTopics", function () await nwaku2.ensureSubscriptions([autoshardingPubsubTopic2]); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); diff --git a/packages/tests/tests/relay/interop.node.spec.ts b/packages/tests/tests/relay/interop.node.spec.ts index 3c05bebfa0..4de59329fe 100644 --- a/packages/tests/tests/relay/interop.node.spec.ts +++ b/packages/tests/tests/relay/interop.node.spec.ts @@ -2,7 +2,6 @@ import type { PeerId } from "@libp2p/interface"; import { DecodedMessage } from "@waku/core"; import { Protocols, RelayNode } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; -import { waitForRemotePeer } from "@waku/sdk"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; @@ -109,7 +108,7 @@ describe("Waku Relay, Interop", function () { const nwakuMultiaddr = await nwaku.getMultiaddrWithId(); await waku2.dial(nwakuMultiaddr); - await waitForRemotePeer(waku2, [Protocols.Relay]); + await waku2.connect([Protocols.Relay]); await delay(2000); // Check that the two JS peers are NOT directly connected diff --git a/packages/tests/tests/sharding/auto_sharding.spec.ts b/packages/tests/tests/sharding/auto_sharding.spec.ts index 91e58979b7..275e81e63e 100644 --- a/packages/tests/tests/sharding/auto_sharding.spec.ts +++ b/packages/tests/tests/sharding/auto_sharding.spec.ts @@ -1,10 +1,5 @@ import { LightNode, ProtocolError, Protocols } from "@waku/interfaces"; -import { - createEncoder, - createLightNode, - utf8ToBytes, - waitForRemotePeer -} from "@waku/sdk"; +import { createEncoder, createLightNode, utf8ToBytes } from "@waku/sdk"; import { contentTopicToPubsubTopic, contentTopicToShardIndex @@ -61,7 +56,7 @@ describe("Autosharding: Running Nodes", function () { } }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -103,7 +98,7 @@ describe("Autosharding: Running Nodes", function () { } }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -160,7 +155,7 @@ describe("Autosharding: Running Nodes", function () { }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -223,7 +218,7 @@ describe("Autosharding: Running Nodes", function () { } }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const encoder1 = createEncoder({ contentTopic: ContentTopic, diff --git a/packages/tests/tests/sharding/static_sharding.spec.ts b/packages/tests/tests/sharding/static_sharding.spec.ts index 47cca240a5..b2612bf71a 100644 --- a/packages/tests/tests/sharding/static_sharding.spec.ts +++ b/packages/tests/tests/sharding/static_sharding.spec.ts @@ -5,12 +5,7 @@ import { ShardInfo, SingleShardInfo } from "@waku/interfaces"; -import { - createEncoder, - createLightNode, - utf8ToBytes, - waitForRemotePeer -} from "@waku/sdk"; +import { createEncoder, createLightNode, utf8ToBytes } from "@waku/sdk"; import { shardInfoToPubsubTopics, singleShardInfosToShardInfo, @@ -62,7 +57,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfo }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -102,7 +97,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfo }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -151,7 +146,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfo }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -217,7 +212,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfoBothShards }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.LightPush]); + await waku.connect([Protocols.LightPush]); const encoder1 = createEncoder({ contentTopic: ContentTopic, diff --git a/packages/tests/tests/store/index.node.spec.ts b/packages/tests/tests/store/index.node.spec.ts index e5ce92d7a0..467da95554 100644 --- a/packages/tests/tests/store/index.node.spec.ts +++ b/packages/tests/tests/store/index.node.spec.ts @@ -14,7 +14,6 @@ import { createDecoder as createSymDecoder, createEncoder as createSymEncoder } from "@waku/message-encryption/symmetric"; -import { waitForRemotePeer } from "@waku/sdk"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { equals } from "uint8arrays/equals"; @@ -292,7 +291,7 @@ describe("Waku Store, general", function () { waku.lightPush.send(TestEncoder, clearMsg) ]); - await waitForRemotePeer(waku2, [Protocols.Store]); + await waku2.connect([Protocols.Store]); const messages: DecodedMessage[] = []; log.info("Retrieve messages from store"); diff --git a/packages/tests/tests/store/multiple_pubsub.spec.ts b/packages/tests/tests/store/multiple_pubsub.spec.ts index de2b346b7b..ed109d71cd 100644 --- a/packages/tests/tests/store/multiple_pubsub.spec.ts +++ b/packages/tests/tests/store/multiple_pubsub.spec.ts @@ -1,6 +1,6 @@ import { createDecoder } from "@waku/core"; import type { ContentTopicInfo, IMessage, LightNode } from "@waku/interfaces"; -import { createLightNode, Protocols, waitForRemotePeer } from "@waku/sdk"; +import { createLightNode, Protocols } from "@waku/sdk"; import { contentTopicToPubsubTopic, pubsubTopicToSingleShardInfo @@ -130,7 +130,7 @@ describe("Waku Store, custom pubsub topic", function () { ); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await waku.connect([Protocols.Store]); let customMessages: IMessage[] = []; let testMessages: IMessage[] = []; @@ -263,7 +263,7 @@ describe("Waku Store (Autosharding), custom pubsub topic", function () { await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await waku.connect([Protocols.Store]); let customMessages: IMessage[] = []; let testMessages: IMessage[] = []; @@ -389,7 +389,7 @@ describe("Waku Store (named sharding), custom pubsub topic", function () { ); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await waku.connect([Protocols.Store]); let customMessages: IMessage[] = []; let testMessages: IMessage[] = []; diff --git a/packages/tests/tests/store/utils.ts b/packages/tests/tests/store/utils.ts index c4d1a1811d..beb6b5d16b 100644 --- a/packages/tests/tests/store/utils.ts +++ b/packages/tests/tests/store/utils.ts @@ -111,7 +111,7 @@ export async function startAndConnectLightNode( }); await waku.start(); await waku.dial(await instance.getMultiaddrWithId()); - await waitForRemotePeer(waku, [Protocols.Store]); + await waku.connect([Protocols.Store]); const wakuConnections = waku.libp2p.getConnections(); diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 71c89eda0f..3898e9ef69 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -111,7 +111,7 @@ describe("Wait for remote peer", function () { await waku2.start(); await waku2.dial(multiAddrWithId); await delay(1000); - await waitForRemotePeer(waku2, [Protocols.Store]); + await waku2.connect([Protocols.Store]); const peers = (await waku2.store.protocol.connectedPeers()).map((peer) => peer.id.toString() @@ -170,7 +170,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waitForRemotePeer(waku2, [Protocols.LightPush]); + await waku2.connect([Protocols.LightPush]); const peers = (await waku2.lightPush.protocol.connectedPeers()).map( (peer) => peer.id.toString() @@ -199,7 +199,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waitForRemotePeer(waku2, [Protocols.Filter]); + await waku2.connect([Protocols.Filter]); const peers = (await waku2.filter.protocol.connectedPeers()).map((peer) => peer.id.toString() @@ -228,7 +228,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waitForRemotePeer(waku2, [ + await waku2.connect([ Protocols.Filter, Protocols.Store, Protocols.LightPush diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 250835f16b..93d4588214 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -12,8 +12,7 @@ import { createRelayNode } from "@waku/relay"; import { createLightNode, createEncoder as createPlainEncoder, - DefaultUserAgent, - waitForRemotePeer + DefaultUserAgent } from "@waku/sdk"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; @@ -59,7 +58,7 @@ describe("Waku Dial [node only]", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waitForRemotePeer(waku, [ + await waku.connect([ Protocols.Store, Protocols.Filter, Protocols.LightPush @@ -268,7 +267,7 @@ describe("User Agent", function () { multiaddrs: waku2.libp2p.getMultiaddrs() }); await waku1.dial(waku2.libp2p.peerId); - await waitForRemotePeer(waku1); + await waku1.connect(); const [waku1PeerInfo, waku2PeerInfo] = await Promise.all([ waku2.libp2p.peerStore.get(waku1.libp2p.peerId), From 9267c7073018950737c76ad89ddb7cb49984a847 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 17:17:05 +0200 Subject: [PATCH 19/28] move wait for remote peer related to Realy out of @waku/sdk --- package-lock.json | 6 ++--- packages/interfaces/src/relay.ts | 1 + packages/relay/package.json | 3 ++- packages/relay/src/relay.ts | 17 ++++++++++++++ packages/sdk/package.json | 3 +-- packages/sdk/src/waku/wait_for_remote_peer.ts | 22 ++----------------- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index 66344a70e7..9f3150809d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39370,7 +39370,8 @@ "@waku/utils": "0.0.20", "chai": "^4.3.10", "debug": "^4.3.4", - "fast-check": "^3.19.0" + "fast-check": "^3.19.0", + "p-event": "^6.0.1" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.7", @@ -39409,8 +39410,7 @@ "@waku/message-hash": "0.1.16", "@waku/proto": "^0.0.8", "@waku/utils": "0.0.20", - "libp2p": "^1.8.1", - "p-event": "^6.0.1" + "libp2p": "^1.8.1" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.7", diff --git a/packages/interfaces/src/relay.ts b/packages/interfaces/src/relay.ts index a212337ac1..103d5bc9cc 100644 --- a/packages/interfaces/src/relay.ts +++ b/packages/interfaces/src/relay.ts @@ -16,6 +16,7 @@ export interface IRelayAPI { readonly pubsubTopics: Set; readonly gossipSub: GossipSub; start: () => Promise; + connect: () => Promise; getMeshPeers: (topic?: TopicStr) => PeerIdStr[]; } diff --git a/packages/relay/package.json b/packages/relay/package.json index 2f8088487d..1d25d68454 100644 --- a/packages/relay/package.json +++ b/packages/relay/package.json @@ -58,7 +58,8 @@ "@waku/utils": "0.0.20", "chai": "^4.3.10", "debug": "^4.3.4", - "fast-check": "^3.19.0" + "fast-check": "^3.19.0", + "p-event": "^6.0.1" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.7", diff --git a/packages/relay/src/relay.ts b/packages/relay/src/relay.ts index 6724ae344c..421f767656 100644 --- a/packages/relay/src/relay.ts +++ b/packages/relay/src/relay.ts @@ -26,6 +26,7 @@ import { import { isWireSizeUnderCap, toAsyncIterator } from "@waku/utils"; import { pushOrInitMapSet } from "@waku/utils"; import { Logger } from "@waku/utils"; +import { pEvent } from "p-event"; import { RelayCodecs } from "./constants.js"; import { messageValidator } from "./message_validator.js"; @@ -94,6 +95,22 @@ class Relay implements IRelay { this.subscribeToAllTopics(); } + /** + * Wait for at least one peer with the given protocol to be connected and in the gossipsub + * mesh for all pubsubTopics. + */ + public async connect(): Promise { + let peers = this.getMeshPeers(); + const pubsubTopics = this.pubsubTopics; + + for (const topic of pubsubTopics) { + while (peers.length == 0) { + await pEvent(this.gossipSub, "gossipsub:heartbeat"); + peers = this.getMeshPeers(topic); + } + } + } + /** * Send Waku message. */ diff --git a/packages/sdk/package.json b/packages/sdk/package.json index c828b6afa4..e2a6c6dc0b 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -70,8 +70,7 @@ "@waku/proto": "^0.0.8", "@waku/utils": "0.0.20", "@waku/message-hash": "0.1.16", - "libp2p": "^1.8.1", - "p-event": "^6.0.1" + "libp2p": "^1.8.1" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.7", diff --git a/packages/sdk/src/waku/wait_for_remote_peer.ts b/packages/sdk/src/waku/wait_for_remote_peer.ts index ee8727a10d..20c058e632 100644 --- a/packages/sdk/src/waku/wait_for_remote_peer.ts +++ b/packages/sdk/src/waku/wait_for_remote_peer.ts @@ -1,9 +1,8 @@ import type { IdentifyResult } from "@libp2p/interface"; import { FilterCodecs, LightPushCodec, StoreCodec } from "@waku/core"; -import type { IRelay, IWaku, Libp2p } from "@waku/interfaces"; +import type { IWaku, Libp2p } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { Logger } from "@waku/utils"; -import { pEvent } from "p-event"; const log = new Logger("wait-for-remote-peer"); @@ -53,7 +52,7 @@ export async function waitForRemotePeer( if (!waku.relay) { throw Error("Cannot wait for Relay peer: protocol not mounted"); } - promises.push(waitForGossipSubPeerInMesh(waku.relay)); + promises.push(waku.relay.connect()); } if (protocols.includes(Protocols.Store)) { @@ -190,23 +189,6 @@ async function waitForMetadata( return false; } -// TODO: move to @waku/relay and use in `node.connect()` API https://github.com/waku-org/js-waku/issues/1761 -/** - * Wait for at least one peer with the given protocol to be connected and in the gossipsub - * mesh for all pubsubTopics. - */ -async function waitForGossipSubPeerInMesh(waku: IRelay): Promise { - let peers = waku.getMeshPeers(); - const pubsubTopics = waku.pubsubTopics; - - for (const topic of pubsubTopics) { - while (peers.length == 0) { - await pEvent(waku.gossipSub, "gossipsub:heartbeat"); - peers = waku.getMeshPeers(topic); - } - } -} - const awaitTimeout = (ms: number, rejectReason: string): Promise => new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms)); From 440f9089f25c1df50a0a718a9bb6c6faf6ca4c28 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 17:31:36 +0200 Subject: [PATCH 20/28] change tests to use new API --- .../filter/single_node/subscribe.node.spec.ts | 2 +- .../tests/relay/multiple_pubsub.node.spec.ts | 33 +++++++++---------- packages/tests/tests/relay/utils.ts | 3 +- packages/tests/tests/store/utils.ts | 2 +- .../tests/wait_for_remote_peer.node.spec.ts | 8 ++--- packages/tests/tests/waku.node.spec.ts | 4 +-- 6 files changed, 25 insertions(+), 27 deletions(-) diff --git a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts index 4218b299b3..d0932f5854 100644 --- a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts +++ b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts @@ -7,7 +7,7 @@ import { getPublicKey, symmetric } from "@waku/message-encryption"; -import { utf8ToBytes, waitForRemotePeer } from "@waku/sdk"; +import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import type { Context } from "mocha"; diff --git a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts index 6434153f17..234168381c 100644 --- a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts @@ -7,7 +7,6 @@ import { } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; -import { waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, pubsubTopicToSingleShardInfo, @@ -115,9 +114,9 @@ describe("Waku Relay, multiple pubsub topics", function () { ]); await Promise.all([ - waitForRemotePeer(waku1, [Protocols.Relay]), - waitForRemotePeer(waku2, [Protocols.Relay]), - waitForRemotePeer(waku3, [Protocols.Relay]) + waku1.connect([Protocols.Relay]), + waku2.connect([Protocols.Relay]), + waku2.connect([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -222,9 +221,9 @@ describe("Waku Relay, multiple pubsub topics", function () { ]); await Promise.all([ - waitForRemotePeer(waku1, [Protocols.Relay]), - waitForRemotePeer(waku2, [Protocols.Relay]), - waitForRemotePeer(waku3, [Protocols.Relay]) + waku1.connect([Protocols.Relay]), + waku2.connect([Protocols.Relay]), + waku3.connect([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -290,8 +289,8 @@ describe("Waku Relay, multiple pubsub topics", function () { ]); await Promise.all([ - waitForRemotePeer(waku1, [Protocols.Relay]), - waitForRemotePeer(waku2, [Protocols.Relay]) + waku1.connect([Protocols.Relay]), + waku2.connect([Protocols.Relay]) ]); const messageText = "Communicating using a custom pubsub topic"; @@ -420,9 +419,9 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { ]); await Promise.all([ - waitForRemotePeer(waku1, [Protocols.Relay]), - waitForRemotePeer(waku2, [Protocols.Relay]), - waitForRemotePeer(waku3, [Protocols.Relay]) + waku1.connect([Protocols.Relay]), + waku2.connect([Protocols.Relay]), + waku3.connect([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -536,9 +535,9 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { ]); await Promise.all([ - waitForRemotePeer(waku1, [Protocols.Relay]), - waitForRemotePeer(waku2, [Protocols.Relay]), - waitForRemotePeer(waku3, [Protocols.Relay]) + waku1.connect([Protocols.Relay]), + waku2.connect([Protocols.Relay]), + waku3.connect([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -631,8 +630,8 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { ]); await Promise.all([ - waitForRemotePeer(waku1, [Protocols.Relay]), - waitForRemotePeer(waku2, [Protocols.Relay]) + waku1.connect([Protocols.Relay]), + waku2.connect([Protocols.Relay]) ]); const messageText = "Communicating using a custom pubsub topic"; diff --git a/packages/tests/tests/relay/utils.ts b/packages/tests/tests/relay/utils.ts index f0141e3481..6cd3c9065b 100644 --- a/packages/tests/tests/relay/utils.ts +++ b/packages/tests/tests/relay/utils.ts @@ -6,7 +6,6 @@ import { ShardInfo } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; -import { waitForRemotePeer } from "@waku/sdk"; import { contentTopicToPubsubTopic, Logger } from "@waku/utils"; import { Context } from "mocha"; @@ -46,7 +45,7 @@ export async function waitForAllRemotePeers( ): Promise { log.info("Wait for mutual pubsub subscription"); await Promise.all( - nodes.map((node): Promise => waitForRemotePeer(node, RELAY_PROTOCOLS)) + nodes.map((node): Promise => node.connect(RELAY_PROTOCOLS)) ); } diff --git a/packages/tests/tests/store/utils.ts b/packages/tests/tests/store/utils.ts index beb6b5d16b..739be103b0 100644 --- a/packages/tests/tests/store/utils.ts +++ b/packages/tests/tests/store/utils.ts @@ -11,7 +11,7 @@ import { ShardInfo, type SingleShardInfo } from "@waku/interfaces"; -import { createLightNode, waitForRemotePeer } from "@waku/sdk"; +import { createLightNode } from "@waku/sdk"; import { Logger, singleShardInfoToPubsubTopic } from "@waku/utils"; import { expect } from "chai"; import { Context } from "mocha"; diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 3898e9ef69..4fb74db336 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -1,7 +1,7 @@ import type { LightNode, RelayNode } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; -import { createLightNode, waitForRemotePeer } from "@waku/sdk"; +import { createLightNode, waku } from "@waku/sdk"; import { expect } from "chai"; import { @@ -60,7 +60,7 @@ describe("Wait for remote peer", function () { }); await waku1.start(); - const waitPromise = waitForRemotePeer(waku1, [Protocols.Relay]); + const waitPromise = waku1.connect([Protocols.Relay]); await delay(1000); await waku1.dial(multiAddrWithId); await waitPromise; @@ -80,7 +80,7 @@ describe("Wait for remote peer", function () { }) .then((waku1) => waku1.start().then(() => waku1)) .then((waku1) => { - waitForRemotePeer(waku1, [Protocols.Relay], 200).then( + waku1.connect([Protocols.Relay], 200).then( () => { throw "Promise expected to reject on time out"; }, @@ -138,7 +138,7 @@ describe("Wait for remote peer", function () { networkConfig: DefaultTestShardInfo }); await waku2.start(); - const waitPromise = waitForRemotePeer(waku2, [Protocols.Store], 2000); + const waitPromise = waku2.connect([Protocols.Store], 2000); await delay(1000); await waku2.dial(multiAddrWithId); await waitPromise; diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 93d4588214..6ab793bbdc 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -189,8 +189,8 @@ describe("Decryption Keys", function () { await waku1.dial(waku2.libp2p.peerId); await Promise.all([ - waitForRemotePeer(waku1, [Protocols.Relay]), - waitForRemotePeer(waku2, [Protocols.Relay]) + waku1.connect([Protocols.Relay]), + waku1.connect([Protocols.Relay]) ]); }); From 4f194ab63e430830015a1cb61e6607dce4db4db5 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 17:37:11 +0200 Subject: [PATCH 21/28] fix linting --- packages/tests/tests/health-manager/node.spec.ts | 4 ++-- packages/tests/tests/multiaddr.node.spec.ts | 4 ++-- packages/tests/tests/wait_for_remote_peer.node.spec.ts | 2 +- packages/tests/tests/waku.node.spec.ts | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/tests/tests/health-manager/node.spec.ts b/packages/tests/tests/health-manager/node.spec.ts index 5627adab37..f29e4d98b9 100644 --- a/packages/tests/tests/health-manager/node.spec.ts +++ b/packages/tests/tests/health-manager/node.spec.ts @@ -1,4 +1,4 @@ -import { HealthStatus, LightNode, Protocols, Waku } from "@waku/interfaces"; +import { HealthStatus, IWaku, LightNode, Protocols } from "@waku/interfaces"; import { createLightNode } from "@waku/sdk"; import { shardInfoToPubsubTopics } from "@waku/utils"; import { expect } from "chai"; @@ -87,7 +87,7 @@ function getExpectedProtocolStatus(peerCount: number): HealthStatus { } async function getPeerCounBasedOnConnections( - waku: Waku, + waku: IWaku, codec: string ): Promise { const peerIDs = waku.libp2p diff --git a/packages/tests/tests/multiaddr.node.spec.ts b/packages/tests/tests/multiaddr.node.spec.ts index b996d5b88e..b2e71fc310 100644 --- a/packages/tests/tests/multiaddr.node.spec.ts +++ b/packages/tests/tests/multiaddr.node.spec.ts @@ -3,7 +3,7 @@ import type { PeerInfo } from "@libp2p/interface"; import { CustomEvent } from "@libp2p/interface"; import { multiaddr } from "@multiformats/multiaddr"; import type { Multiaddr } from "@multiformats/multiaddr"; -import type { Waku } from "@waku/interfaces"; +import type { IWaku } from "@waku/interfaces"; import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; import Sinon, { SinonSpy, SinonStub } from "sinon"; @@ -18,7 +18,7 @@ import { } from "../src/index.js"; describe("multiaddr: dialing", function () { - let waku: Waku; + let waku: IWaku; let nwaku: ServiceNode; let dialPeerSpy: SinonSpy; let isPeerTopicConfigured: SinonStub; diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 4fb74db336..7c4d035b14 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -1,7 +1,7 @@ import type { LightNode, RelayNode } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { createRelayNode } from "@waku/relay"; -import { createLightNode, waku } from "@waku/sdk"; +import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; import { diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 6ab793bbdc..7f5c1a2374 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -1,7 +1,7 @@ import { bootstrap } from "@libp2p/bootstrap"; import type { PeerId } from "@libp2p/interface"; import { DecodedMessage } from "@waku/core"; -import type { LightNode, RelayNode, Waku } from "@waku/interfaces"; +import type { IWaku, LightNode, RelayNode } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { generateSymmetricKey } from "@waku/message-encryption"; import { @@ -238,8 +238,8 @@ describe("Decryption Keys", function () { }); describe("User Agent", function () { - let waku1: Waku; - let waku2: Waku; + let waku1: IWaku; + let waku2: IWaku; afterEachCustom(this, async () => { await tearDownNodes([], [waku1, waku2]); From cd6aceeb207c306db7eaa0e0955300dea4951c44 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 20:17:27 +0200 Subject: [PATCH 22/28] update size limit --- .size-limit.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.size-limit.cjs b/.size-limit.cjs index 7d08b86697..2eaee24aef 100644 --- a/.size-limit.cjs +++ b/.size-limit.cjs @@ -9,7 +9,7 @@ module.exports = [ path: ["packages/sdk/bundle/index.js", "packages/core/bundle/index.js"], import: { "packages/sdk/bundle/index.js": - "{ createLightNode, waitForRemotePeer, createEncoder, createDecoder, bytesToUtf8, utf8ToBytes, Decoder, Encoder, DecodedMessage, WakuNode }", + "{ createLightNode, createEncoder, createDecoder, bytesToUtf8, utf8ToBytes, Decoder, Encoder, DecodedMessage, WakuNode }", }, }, { From 64b8f3a26d252244b2008904decdc0054b7f31af Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 21:41:15 +0200 Subject: [PATCH 23/28] rename .connect to .waitForPeer --- packages/interfaces/src/relay.ts | 2 +- packages/interfaces/src/waku.ts | 4 +-- packages/relay/src/relay.ts | 2 +- packages/sdk/src/waku/wait_for_remote_peer.ts | 2 +- packages/sdk/src/waku/waku.ts | 2 +- packages/tests/src/lib/runNodes.ts | 2 +- packages/tests/src/utils/nodes.ts | 2 +- packages/tests/tests/enr.node.spec.ts | 6 ++-- packages/tests/tests/ephemeral.node.spec.ts | 6 ++-- packages/tests/tests/filter/push.node.spec.ts | 4 +-- .../single_node/multiple_pubsub.node.spec.ts | 6 ++-- .../filter/single_node/push.node.spec.ts | 4 +-- .../filter/single_node/subscribe.node.spec.ts | 2 +- packages/tests/tests/filter/utils.ts | 2 +- packages/tests/tests/getPeers.spec.ts | 16 +++++----- .../single_node/multiple_pubsub.node.spec.ts | 6 ++-- .../tests/tests/relay/interop.node.spec.ts | 2 +- .../tests/relay/multiple_pubsub.node.spec.ts | 32 +++++++++---------- packages/tests/tests/relay/utils.ts | 2 +- .../tests/sharding/auto_sharding.spec.ts | 8 ++--- .../tests/sharding/static_sharding.spec.ts | 8 ++--- packages/tests/tests/store/index.node.spec.ts | 2 +- .../tests/tests/store/multiple_pubsub.spec.ts | 6 ++-- packages/tests/tests/store/utils.ts | 2 +- .../tests/wait_for_remote_peer.node.spec.ts | 14 ++++---- packages/tests/tests/waku.node.spec.ts | 8 ++--- 26 files changed, 76 insertions(+), 76 deletions(-) diff --git a/packages/interfaces/src/relay.ts b/packages/interfaces/src/relay.ts index 103d5bc9cc..126022ee2a 100644 --- a/packages/interfaces/src/relay.ts +++ b/packages/interfaces/src/relay.ts @@ -16,7 +16,7 @@ export interface IRelayAPI { readonly pubsubTopics: Set; readonly gossipSub: GossipSub; start: () => Promise; - connect: () => Promise; + waitForPeer: () => Promise; getMeshPeers: (topic?: TopicStr) => PeerIdStr[]; } diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index 43daf0ed84..3602046f01 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -81,7 +81,7 @@ export interface IWaku { * ```typescript * try { * // let's wait for at least one LightPush node and timeout in 1 second - * await waku.connect([Protocols.LightPush], 1000); + * await waku.waitForPeer([Protocols.LightPush], 1000); * } catch(e) { * waku.isConnected() === false; * console.error("Failed to connect due to", e); @@ -90,7 +90,7 @@ export interface IWaku { * waku.isConnected() === true; * ``` */ - connect(protocols?: Protocols[], timeoutMs?: number): Promise; + waitForPeer(protocols?: Protocols[], timeoutMs?: number): Promise; /** * @returns {boolean} `true` if the node was started and `false` otherwise diff --git a/packages/relay/src/relay.ts b/packages/relay/src/relay.ts index 421f767656..0fabc76dd9 100644 --- a/packages/relay/src/relay.ts +++ b/packages/relay/src/relay.ts @@ -99,7 +99,7 @@ class Relay implements IRelay { * Wait for at least one peer with the given protocol to be connected and in the gossipsub * mesh for all pubsubTopics. */ - public async connect(): Promise { + public async waitForPeer(): Promise { let peers = this.getMeshPeers(); const pubsubTopics = this.pubsubTopics; diff --git a/packages/sdk/src/waku/wait_for_remote_peer.ts b/packages/sdk/src/waku/wait_for_remote_peer.ts index 20c058e632..2defb85d9c 100644 --- a/packages/sdk/src/waku/wait_for_remote_peer.ts +++ b/packages/sdk/src/waku/wait_for_remote_peer.ts @@ -52,7 +52,7 @@ export async function waitForRemotePeer( if (!waku.relay) { throw Error("Cannot wait for Relay peer: protocol not mounted"); } - promises.push(waku.relay.connect()); + promises.push(waku.relay.waitForPeer()); } if (protocols.includes(Protocols.Store)) { diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index 9801e082bb..319b23fad6 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -197,7 +197,7 @@ export class WakuNode implements IWaku { await this.libp2p.stop(); } - public async connect( + public async waitForPeer( protocols?: Protocols[], timeoutMs?: number ): Promise { diff --git a/packages/tests/src/lib/runNodes.ts b/packages/tests/src/lib/runNodes.ts index 5b9197cdd9..6f7b1775e1 100644 --- a/packages/tests/src/lib/runNodes.ts +++ b/packages/tests/src/lib/runNodes.ts @@ -63,7 +63,7 @@ export async function runNodes( if (waku) { await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect(protocols); + await waku.waitForPeer(protocols); await nwaku.ensureSubscriptions(pubsubTopics); return [nwaku, waku as T]; } else { diff --git a/packages/tests/src/utils/nodes.ts b/packages/tests/src/utils/nodes.ts index c9ab503257..87eb2d7a17 100644 --- a/packages/tests/src/utils/nodes.ts +++ b/packages/tests/src/utils/nodes.ts @@ -52,7 +52,7 @@ export async function runMultipleNodes( for (const node of serviceNodes.nodes) { await waku.dial(await node.getMultiaddrWithId()); - await waku.connect( + await waku.waitForPeer( [ !customArgs?.filter ? undefined : Protocols.Filter, !customArgs?.lightpush ? undefined : Protocols.LightPush diff --git a/packages/tests/tests/enr.node.spec.ts b/packages/tests/tests/enr.node.spec.ts index 2cad6280fc..33d9e30344 100644 --- a/packages/tests/tests/enr.node.spec.ts +++ b/packages/tests/tests/enr.node.spec.ts @@ -40,7 +40,7 @@ describe("ENR Interop: ServiceNode", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waku.connect([Protocols.Relay]); + await waku.waitForPeer([Protocols.Relay]); const nwakuInfo = await nwaku.info(); const nimPeerId = await nwaku.getPeerId(); @@ -74,7 +74,7 @@ describe("ENR Interop: ServiceNode", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waku.connect([Protocols.Relay]); + await waku.waitForPeer([Protocols.Relay]); const nwakuInfo = await nwaku.info(); const nimPeerId = await nwaku.getPeerId(); @@ -109,7 +109,7 @@ describe("ENR Interop: ServiceNode", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waku.connect([Protocols.Relay]); + await waku.waitForPeer([Protocols.Relay]); const nwakuInfo = await nwaku.info(); const nimPeerId = await nwaku.getPeerId(); diff --git a/packages/tests/tests/ephemeral.node.spec.ts b/packages/tests/tests/ephemeral.node.spec.ts index 5d4a80ed11..49519b5690 100644 --- a/packages/tests/tests/ephemeral.node.spec.ts +++ b/packages/tests/tests/ephemeral.node.spec.ts @@ -110,7 +110,7 @@ describe("Waku Message Ephemeral field", function () { await waku.start(); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([ + await waku.waitForPeer([ Protocols.Filter, Protocols.LightPush, Protocols.Store @@ -161,7 +161,7 @@ describe("Waku Message Ephemeral field", function () { log.info("Waku nodes connected to nwaku"); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); log.info("Sending messages using light push"); await Promise.all([ @@ -170,7 +170,7 @@ describe("Waku Message Ephemeral field", function () { waku1.lightPush.send(ClearEncoder, clearMsg) ]); - await waku2.connect([Protocols.Store]); + await waku2.waitForPeer([Protocols.Store]); const messages: DecodedMessage[] = []; diff --git a/packages/tests/tests/filter/push.node.spec.ts b/packages/tests/tests/filter/push.node.spec.ts index 6ac26e530a..ba0e73569f 100644 --- a/packages/tests/tests/filter/push.node.spec.ts +++ b/packages/tests/tests/filter/push.node.spec.ts @@ -233,7 +233,7 @@ const runTests = (strictCheckNodes: boolean): void => { // Redo the connection and create a new subscription for (const node of this.serviceNodes) { await waku.dial(await node.getMultiaddrWithId()); - await waku.connect([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); } await waku.filter.subscribe( @@ -273,7 +273,7 @@ const runTests = (strictCheckNodes: boolean): void => { // Restart nwaku node await teardownNodesWithRedundancy(serviceNodes, []); await serviceNodes.start(); - await waku.connect([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M2") }); diff --git a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts index b4efef0755..9e05ba4bfd 100644 --- a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts @@ -116,7 +116,7 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () { clusterId: 3 }); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptions([customPubsubTopic2]); @@ -269,7 +269,7 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () { contentTopic: [customContentTopic2] }); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptionsAutosharding([customContentTopic2]); @@ -408,7 +408,7 @@ describe("Waku Filter V2 (Named sharding): Multiple PubsubTopics", function () { clusterId: 3 }); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptions([customPubsubTopic2]); diff --git a/packages/tests/tests/filter/single_node/push.node.spec.ts b/packages/tests/tests/filter/single_node/push.node.spec.ts index ae8ea12e8e..1ccb1c6bde 100644 --- a/packages/tests/tests/filter/single_node/push.node.spec.ts +++ b/packages/tests/tests/filter/single_node/push.node.spec.ts @@ -215,7 +215,7 @@ describe("Waku Filter V2: FilterPush", function () { // Redo the connection and create a new subscription await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); await waku.filter.subscribe([TestDecoder], messageCollector.callback); @@ -242,7 +242,7 @@ describe("Waku Filter V2: FilterPush", function () { // Restart nwaku node await tearDownNodes(nwaku, []); await nwaku.start(); - await waku.connect([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M2") }); diff --git a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts index d0932f5854..f628579107 100644 --- a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts +++ b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts @@ -445,7 +445,7 @@ describe("Waku Filter V2: Subscribe: Single Service Node", function () { // Set up and start a new nwaku node [nwaku2, waku2] = await runNodes(ctx, TestShardInfo); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptions([TestPubsubTopic]); // Send a message using the new subscription diff --git a/packages/tests/tests/filter/utils.ts b/packages/tests/tests/filter/utils.ts index 78a6e9db86..e65540700c 100644 --- a/packages/tests/tests/filter/utils.ts +++ b/packages/tests/tests/filter/utils.ts @@ -107,7 +107,7 @@ export async function runMultipleNodes( for (const node of serviceNodes.nodes) { await waku.dial(await node.getMultiaddrWithId()); - await waku.connect([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); await node.ensureSubscriptions(pubsubTopics); const wakuConnections = waku.libp2p.getConnections(); diff --git a/packages/tests/tests/getPeers.spec.ts b/packages/tests/tests/getPeers.spec.ts index 30d6e0a014..017cc76fa3 100644 --- a/packages/tests/tests/getPeers.spec.ts +++ b/packages/tests/tests/getPeers.spec.ts @@ -69,7 +69,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { waku = await createLightNode({ networkConfig: shardInfo }); await waku.start(); await waku.libp2p.dialProtocol(serviceNodeMa, LightPushCodec); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), waku.libp2p.peerStore, @@ -119,7 +119,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode1Ma, LightPushCodec); await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -172,7 +172,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -225,7 +225,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await delay(500); await waku.libp2p.dialProtocol(serviceNodeMa2, LightPushCodec); await waku.start(); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -259,7 +259,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { waku = await createLightNode({ networkConfig: shardInfo }); await waku.start(); await waku.libp2p.dialProtocol(serviceNodeMa, LightPushCodec); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), waku.libp2p.peerStore, @@ -312,7 +312,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -366,7 +366,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -420,7 +420,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await delay(500); await waku.libp2p.dialProtocol(serviceNodeMa2, LightPushCodec); await waku.start(); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), diff --git a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts index fddd604a75..c6d1715b61 100644 --- a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts @@ -132,7 +132,7 @@ describe("Waku Light Push : Multiple PubsubTopics", function () { singleShardInfoToPubsubTopic(singleShardInfo2) ]); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); @@ -274,7 +274,7 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () { }); await nwaku2.ensureSubscriptionsAutosharding([customContentTopic2]); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); @@ -425,7 +425,7 @@ describe("Waku Light Push (named sharding): Multiple PubsubTopics", function () await nwaku2.ensureSubscriptions([autoshardingPubsubTopic2]); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); diff --git a/packages/tests/tests/relay/interop.node.spec.ts b/packages/tests/tests/relay/interop.node.spec.ts index 4de59329fe..727cfd3785 100644 --- a/packages/tests/tests/relay/interop.node.spec.ts +++ b/packages/tests/tests/relay/interop.node.spec.ts @@ -108,7 +108,7 @@ describe("Waku Relay, Interop", function () { const nwakuMultiaddr = await nwaku.getMultiaddrWithId(); await waku2.dial(nwakuMultiaddr); - await waku2.connect([Protocols.Relay]); + await waku2.waitForPeer([Protocols.Relay]); await delay(2000); // Check that the two JS peers are NOT directly connected diff --git a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts index 234168381c..6b6af4761b 100644 --- a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts @@ -114,9 +114,9 @@ describe("Waku Relay, multiple pubsub topics", function () { ]); await Promise.all([ - waku1.connect([Protocols.Relay]), - waku2.connect([Protocols.Relay]), - waku2.connect([Protocols.Relay]) + waku1.waitForPeer([Protocols.Relay]), + waku2.waitForPeer([Protocols.Relay]), + waku2.waitForPeer([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -221,9 +221,9 @@ describe("Waku Relay, multiple pubsub topics", function () { ]); await Promise.all([ - waku1.connect([Protocols.Relay]), - waku2.connect([Protocols.Relay]), - waku3.connect([Protocols.Relay]) + waku1.waitForPeer([Protocols.Relay]), + waku2.waitForPeer([Protocols.Relay]), + waku3.waitForPeer([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -289,8 +289,8 @@ describe("Waku Relay, multiple pubsub topics", function () { ]); await Promise.all([ - waku1.connect([Protocols.Relay]), - waku2.connect([Protocols.Relay]) + waku1.waitForPeer([Protocols.Relay]), + waku2.waitForPeer([Protocols.Relay]) ]); const messageText = "Communicating using a custom pubsub topic"; @@ -419,9 +419,9 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { ]); await Promise.all([ - waku1.connect([Protocols.Relay]), - waku2.connect([Protocols.Relay]), - waku3.connect([Protocols.Relay]) + waku1.waitForPeer([Protocols.Relay]), + waku2.waitForPeer([Protocols.Relay]), + waku3.waitForPeer([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -535,9 +535,9 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { ]); await Promise.all([ - waku1.connect([Protocols.Relay]), - waku2.connect([Protocols.Relay]), - waku3.connect([Protocols.Relay]) + waku1.waitForPeer([Protocols.Relay]), + waku2.waitForPeer([Protocols.Relay]), + waku3.waitForPeer([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -630,8 +630,8 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { ]); await Promise.all([ - waku1.connect([Protocols.Relay]), - waku2.connect([Protocols.Relay]) + waku1.waitForPeer([Protocols.Relay]), + waku2.waitForPeer([Protocols.Relay]) ]); const messageText = "Communicating using a custom pubsub topic"; diff --git a/packages/tests/tests/relay/utils.ts b/packages/tests/tests/relay/utils.ts index 6cd3c9065b..2c1c866ed1 100644 --- a/packages/tests/tests/relay/utils.ts +++ b/packages/tests/tests/relay/utils.ts @@ -45,7 +45,7 @@ export async function waitForAllRemotePeers( ): Promise { log.info("Wait for mutual pubsub subscription"); await Promise.all( - nodes.map((node): Promise => node.connect(RELAY_PROTOCOLS)) + nodes.map((node): Promise => node.waitForPeer(RELAY_PROTOCOLS)) ); } diff --git a/packages/tests/tests/sharding/auto_sharding.spec.ts b/packages/tests/tests/sharding/auto_sharding.spec.ts index 275e81e63e..38d9847ad5 100644 --- a/packages/tests/tests/sharding/auto_sharding.spec.ts +++ b/packages/tests/tests/sharding/auto_sharding.spec.ts @@ -56,7 +56,7 @@ describe("Autosharding: Running Nodes", function () { } }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -98,7 +98,7 @@ describe("Autosharding: Running Nodes", function () { } }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -155,7 +155,7 @@ describe("Autosharding: Running Nodes", function () { }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -218,7 +218,7 @@ describe("Autosharding: Running Nodes", function () { } }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const encoder1 = createEncoder({ contentTopic: ContentTopic, diff --git a/packages/tests/tests/sharding/static_sharding.spec.ts b/packages/tests/tests/sharding/static_sharding.spec.ts index b2612bf71a..472ba5fdd1 100644 --- a/packages/tests/tests/sharding/static_sharding.spec.ts +++ b/packages/tests/tests/sharding/static_sharding.spec.ts @@ -57,7 +57,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfo }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -97,7 +97,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfo }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -146,7 +146,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfo }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -212,7 +212,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfoBothShards }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.connect([Protocols.LightPush]); + await waku.waitForPeer([Protocols.LightPush]); const encoder1 = createEncoder({ contentTopic: ContentTopic, diff --git a/packages/tests/tests/store/index.node.spec.ts b/packages/tests/tests/store/index.node.spec.ts index 467da95554..07843462b6 100644 --- a/packages/tests/tests/store/index.node.spec.ts +++ b/packages/tests/tests/store/index.node.spec.ts @@ -291,7 +291,7 @@ describe("Waku Store, general", function () { waku.lightPush.send(TestEncoder, clearMsg) ]); - await waku2.connect([Protocols.Store]); + await waku2.waitForPeer([Protocols.Store]); const messages: DecodedMessage[] = []; log.info("Retrieve messages from store"); diff --git a/packages/tests/tests/store/multiple_pubsub.spec.ts b/packages/tests/tests/store/multiple_pubsub.spec.ts index ed109d71cd..4cd4b1cd73 100644 --- a/packages/tests/tests/store/multiple_pubsub.spec.ts +++ b/packages/tests/tests/store/multiple_pubsub.spec.ts @@ -130,7 +130,7 @@ describe("Waku Store, custom pubsub topic", function () { ); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.Store]); + await waku.waitForPeer([Protocols.Store]); let customMessages: IMessage[] = []; let testMessages: IMessage[] = []; @@ -263,7 +263,7 @@ describe("Waku Store (Autosharding), custom pubsub topic", function () { await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.Store]); + await waku.waitForPeer([Protocols.Store]); let customMessages: IMessage[] = []; let testMessages: IMessage[] = []; @@ -389,7 +389,7 @@ describe("Waku Store (named sharding), custom pubsub topic", function () { ); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.connect([Protocols.Store]); + await waku.waitForPeer([Protocols.Store]); let customMessages: IMessage[] = []; let testMessages: IMessage[] = []; diff --git a/packages/tests/tests/store/utils.ts b/packages/tests/tests/store/utils.ts index 739be103b0..b6fa62cee6 100644 --- a/packages/tests/tests/store/utils.ts +++ b/packages/tests/tests/store/utils.ts @@ -111,7 +111,7 @@ export async function startAndConnectLightNode( }); await waku.start(); await waku.dial(await instance.getMultiaddrWithId()); - await waku.connect([Protocols.Store]); + await waku.waitForPeer([Protocols.Store]); const wakuConnections = waku.libp2p.getConnections(); diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 7c4d035b14..5eba758e97 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -60,7 +60,7 @@ describe("Wait for remote peer", function () { }); await waku1.start(); - const waitPromise = waku1.connect([Protocols.Relay]); + const waitPromise = waku1.waitForPeer([Protocols.Relay]); await delay(1000); await waku1.dial(multiAddrWithId); await waitPromise; @@ -80,7 +80,7 @@ describe("Wait for remote peer", function () { }) .then((waku1) => waku1.start().then(() => waku1)) .then((waku1) => { - waku1.connect([Protocols.Relay], 200).then( + waku1.waitForPeer([Protocols.Relay], 200).then( () => { throw "Promise expected to reject on time out"; }, @@ -111,7 +111,7 @@ describe("Wait for remote peer", function () { await waku2.start(); await waku2.dial(multiAddrWithId); await delay(1000); - await waku2.connect([Protocols.Store]); + await waku2.waitForPeer([Protocols.Store]); const peers = (await waku2.store.protocol.connectedPeers()).map((peer) => peer.id.toString() @@ -138,7 +138,7 @@ describe("Wait for remote peer", function () { networkConfig: DefaultTestShardInfo }); await waku2.start(); - const waitPromise = waku2.connect([Protocols.Store], 2000); + const waitPromise = waku2.waitForPeer([Protocols.Store], 2000); await delay(1000); await waku2.dial(multiAddrWithId); await waitPromise; @@ -170,7 +170,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waku2.connect([Protocols.LightPush]); + await waku2.waitForPeer([Protocols.LightPush]); const peers = (await waku2.lightPush.protocol.connectedPeers()).map( (peer) => peer.id.toString() @@ -199,7 +199,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waku2.connect([Protocols.Filter]); + await waku2.waitForPeer([Protocols.Filter]); const peers = (await waku2.filter.protocol.connectedPeers()).map((peer) => peer.id.toString() @@ -228,7 +228,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waku2.connect([ + await waku2.waitForPeer([ Protocols.Filter, Protocols.Store, Protocols.LightPush diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 7f5c1a2374..93b78cb04b 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -58,7 +58,7 @@ describe("Waku Dial [node only]", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waku.connect([ + await waku.waitForPeer([ Protocols.Store, Protocols.Filter, Protocols.LightPush @@ -189,8 +189,8 @@ describe("Decryption Keys", function () { await waku1.dial(waku2.libp2p.peerId); await Promise.all([ - waku1.connect([Protocols.Relay]), - waku1.connect([Protocols.Relay]) + waku1.waitForPeer([Protocols.Relay]), + waku1.waitForPeer([Protocols.Relay]) ]); }); @@ -267,7 +267,7 @@ describe("User Agent", function () { multiaddrs: waku2.libp2p.getMultiaddrs() }); await waku1.dial(waku2.libp2p.peerId); - await waku1.connect(); + await waku1.waitForPeer(); const [waku1PeerInfo, waku2PeerInfo] = await Promise.all([ waku2.libp2p.peerStore.get(waku1.libp2p.peerId), From d1e2ba1f1ae6f72c1263a882eda9917f33dacf74 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 4 Oct 2024 22:06:25 +0200 Subject: [PATCH 24/28] export waitForRemotePeer and mark as deprecated --- packages/sdk/src/waku/index.ts | 1 + packages/sdk/src/waku/wait_for_remote_peer.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/packages/sdk/src/waku/index.ts b/packages/sdk/src/waku/index.ts index 8718e26a4c..484b5def3e 100644 --- a/packages/sdk/src/waku/index.ts +++ b/packages/sdk/src/waku/index.ts @@ -1 +1,2 @@ export * from "./waku.js"; +export { waitForRemotePeer } from "./wait_for_remote_peer.js"; diff --git a/packages/sdk/src/waku/wait_for_remote_peer.ts b/packages/sdk/src/waku/wait_for_remote_peer.ts index 2defb85d9c..570fde801c 100644 --- a/packages/sdk/src/waku/wait_for_remote_peer.ts +++ b/packages/sdk/src/waku/wait_for_remote_peer.ts @@ -7,6 +7,8 @@ import { Logger } from "@waku/utils"; const log = new Logger("wait-for-remote-peer"); /** + * @deprecated Since @waku/sdk 0.29.0. Will be removed from 0.31.0 + * * Wait for a remote peer to be ready given the passed protocols. * Must be used after attempting to connect to nodes, using * {@link @waku/sdk!WakuNode.dial} or a bootstrap method with From 8937a5617836f4472bd215aad7d0fc4f61ab6658 Mon Sep 17 00:00:00 2001 From: Sasha <118575614+weboko@users.noreply.github.com> Date: Tue, 8 Oct 2024 13:13:44 +0200 Subject: [PATCH 25/28] feat: add mocha tests to @waku/sdk and cover waitForRemotePeer (#2163) * feat: add mocha tests to @waku/sdk and cover waitForRemotePeer * add waitForRemote UTs * remove junk --- package-lock.json | 7 +- packages/sdk/.mocha.reporters.json | 6 + packages/sdk/.mocharc.cjs | 26 ++ packages/sdk/package.json | 8 + .../sdk/src/waku/wait_for_remote_peer.spec.ts | 399 ++++++++++++++++++ packages/sdk/src/waku/wait_for_remote_peer.ts | 6 +- 6 files changed, 448 insertions(+), 4 deletions(-) create mode 100644 packages/sdk/.mocha.reporters.json create mode 100644 packages/sdk/.mocharc.cjs create mode 100644 packages/sdk/src/waku/wait_for_remote_peer.spec.ts diff --git a/package-lock.json b/package-lock.json index 9f3150809d..6203ae1818 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39417,11 +39417,16 @@ "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-replace": "^5.0.5", + "@types/chai": "^4.3.11", + "@types/mocha": "^10.0.6", "@waku/build-utils": "*", + "chai": "^4.3.10", "cspell": "^8.6.1", "interface-datastore": "^8.2.10", + "mocha": "^10.3.0", "npm-run-all": "^4.1.5", - "rollup": "^4.12.0" + "rollup": "^4.12.0", + "sinon": "^18.0.0" }, "engines": { "node": ">=20" diff --git a/packages/sdk/.mocha.reporters.json b/packages/sdk/.mocha.reporters.json new file mode 100644 index 0000000000..8c00e441fc --- /dev/null +++ b/packages/sdk/.mocha.reporters.json @@ -0,0 +1,6 @@ +{ + "reporterEnabled": "spec, allure-mocha", + "allureMochaReporter": { + "outputDir": "allure-results" + } +} diff --git a/packages/sdk/.mocharc.cjs b/packages/sdk/.mocharc.cjs new file mode 100644 index 0000000000..dc72692443 --- /dev/null +++ b/packages/sdk/.mocharc.cjs @@ -0,0 +1,26 @@ +const config = { + extension: ['ts'], + spec: 'src/**/*.spec.ts', + require: ['ts-node/register', 'isomorphic-fetch'], + loader: 'ts-node/esm', + nodeOptions: [ + 'experimental-specifier-resolution=node', + 'loader=ts-node/esm' + ], + exit: true +}; + +if (process.env.CI) { + console.log("Running tests in parallel"); + config.parallel = true; + config.jobs = 6; + console.log("Activating allure reporting"); + config.reporter = 'mocha-multi-reporters'; + config.reporterOptions = { + configFile: '.mocha.reporters.json' + }; +} else { + console.log("Running tests serially. To enable parallel execution update mocha config"); +} + +module.exports = config; diff --git a/packages/sdk/package.json b/packages/sdk/package.json index e2a6c6dc0b..437f14fa09 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -50,6 +50,9 @@ "check:lint": "eslint src *.js", "check:spelling": "cspell \"{README.md,src/**/*.ts}\"", "check:tsc": "tsc -p tsconfig.dev.json", + "test": "NODE_ENV=test run-s test:*", + "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha", + "watch:test": "mocha --watch", "prepublish": "npm run build", "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build" }, @@ -73,11 +76,16 @@ "libp2p": "^1.8.1" }, "devDependencies": { + "@types/mocha": "^10.0.6", + "@types/chai": "^4.3.11", "@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-replace": "^5.0.5", "@waku/build-utils": "*", + "mocha": "^10.3.0", + "sinon": "^18.0.0", + "chai": "^4.3.10", "cspell": "^8.6.1", "interface-datastore": "^8.2.10", "npm-run-all": "^4.1.5", diff --git a/packages/sdk/src/waku/wait_for_remote_peer.spec.ts b/packages/sdk/src/waku/wait_for_remote_peer.spec.ts new file mode 100644 index 0000000000..9f52552f3f --- /dev/null +++ b/packages/sdk/src/waku/wait_for_remote_peer.spec.ts @@ -0,0 +1,399 @@ +import type { Connection, Peer, PeerStore } from "@libp2p/interface"; +import { FilterCodecs, LightPushCodec, StoreCodec } from "@waku/core"; +import { IRelay, Protocols } from "@waku/interfaces"; +import { expect } from "chai"; +import sinon from "sinon"; + +import { waitForRemotePeer } from "./wait_for_remote_peer.js"; +import { WakuNode } from "./waku.js"; + +describe("waitForRemotePeer", () => { + let eventTarget = new EventTarget(); + + beforeEach(() => { + eventTarget = new EventTarget(); + }); + + it("should reject if WakuNode is not started", async () => { + const wakuMock = mockWakuNode({ + connections: [{}] + }); + + let err: Error | undefined = undefined; + try { + await waitForRemotePeer(wakuMock); + } catch (e) { + err = e as Error; + } + + expect(err).not.to.be.undefined; + expect(err!.message).to.be.eq("Waku node is not started"); + }); + + it("should reject if Relay is requested but not mounted", async () => { + const wakuMock = mockWakuNode({ isStarted: true }); + wakuMock.relay = undefined; + + let err: Error | undefined = undefined; + try { + await waitForRemotePeer(wakuMock, [Protocols.Relay]); + } catch (e) { + err = e as Error; + } + + expect(err).not.to.be.undefined; + expect(err!.message).to.be.eq( + "Cannot wait for Relay peer: protocol not mounted" + ); + }); + + it("should reject if LightPush is requested but not mounted", async () => { + const wakuMock = mockWakuNode({ isStarted: true }); + wakuMock.lightPush = undefined; + + let err: Error | undefined = undefined; + try { + await waitForRemotePeer(wakuMock, [Protocols.LightPush]); + } catch (e) { + err = e as Error; + } + + expect(err).not.to.be.undefined; + expect(err!.message).to.be.eq( + "Cannot wait for LightPush peer: protocol not mounted" + ); + }); + + it("should reject if Store is requested but not mounted", async () => { + const wakuMock = mockWakuNode({ isStarted: true }); + wakuMock.store = undefined; + + let err: Error | undefined = undefined; + try { + await waitForRemotePeer(wakuMock, [Protocols.Store]); + } catch (e) { + err = e as Error; + } + + expect(err).not.to.be.undefined; + expect(err!.message).to.be.eq( + "Cannot wait for Store peer: protocol not mounted" + ); + }); + + it("should reject if Filter is requested but not mounted", async () => { + const wakuMock = mockWakuNode({ isStarted: true }); + wakuMock.filter = undefined; + + let err: Error | undefined = undefined; + try { + await waitForRemotePeer(wakuMock, [Protocols.Filter]); + } catch (e) { + err = e as Error; + } + + expect(err).not.to.be.undefined; + expect(err!.message).to.be.eq( + "Cannot wait for Filter peer: protocol not mounted" + ); + }); + + it("should reject on timeout if it was set", async () => { + const addEventListenerSpy = sinon.spy(eventTarget.addEventListener); + eventTarget.addEventListener = addEventListenerSpy; + + const wakuMock = mockWakuNode({ isStarted: true, eventTarget }); + + // let's wait for LightPush in that test + wakuMock.lightPush = {} as any; + + let err: Error | undefined = undefined; + try { + await waitForRemotePeer(wakuMock, [Protocols.LightPush], 10); + } catch (e) { + err = e as Error; + } + + expect(addEventListenerSpy.calledOnceWith("peer:identify")).to.be.true; + + expect(err).not.to.be.undefined; + expect(err!.message).to.be.eq("Timed out waiting for a remote peer."); + }); + + it("should check connected peers if present and suitable", async () => { + const addEventListenerSpy = sinon.spy(eventTarget.addEventListener); + eventTarget.addEventListener = addEventListenerSpy; + + const wakuNode = mockWakuNode({ + isStarted: true, + eventTarget, + connections: [{}], + peers: [ + mockPeer("1", []), + mockPeer("1.1", ["random"]), + mockPeer("2", ["random", LightPushCodec]) + ], + metadataService: mockMetadataService("resolve") + }); + + let err: Error | undefined; + try { + await waitForRemotePeer(wakuNode, [Protocols.LightPush]); + } catch (e) { + err = e as Error; + } + + expect(err).to.be.undefined; + expect(addEventListenerSpy.notCalled).to.be.true; + }); + + it("should wait for LightPush peer to be connected", async () => { + const addEventListenerSpy = sinon.spy( + (_type: string, _cb: (e: any) => void) => { + _cb({ detail: { protocols: [LightPushCodec] } }); + } + ); + eventTarget.addEventListener = addEventListenerSpy; + + // check without metadata service + let wakuNode = mockWakuNode({ + isStarted: true, + eventTarget, + connections: [{}], + peers: [ + mockPeer("1", []), + mockPeer("1.1", ["random"]), + mockPeer("2", ["random"]) + ] + }); + + let err: Error | undefined; + try { + await waitForRemotePeer(wakuNode, [Protocols.LightPush]); + } catch (e) { + err = e as Error; + } + + expect(addEventListenerSpy.calledOnceWith("peer:identify")).to.be.true; + expect(err).to.be.undefined; + + // check with metadata serivice + wakuNode = mockWakuNode({ + isStarted: true, + eventTarget, + connections: [{}], + peers: [ + mockPeer("1", []), + mockPeer("1.1", ["random"]), + mockPeer("2", ["random"]) + ], + metadataService: mockMetadataService("resolve") + }); + + try { + await waitForRemotePeer(wakuNode, [Protocols.LightPush]); + } catch (e) { + err = e as Error; + } + + expect(addEventListenerSpy.calledTwice).to.be.true; + expect(addEventListenerSpy.lastCall.calledWith("peer:identify")).to.be.true; + expect(err).to.be.undefined; + }); + + it("should wait for Filter peer to be connected", async () => { + const addEventListenerSpy = sinon.spy( + (_type: string, _cb: (e: any) => void) => { + _cb({ detail: { protocols: [FilterCodecs.SUBSCRIBE] } }); + } + ); + eventTarget.addEventListener = addEventListenerSpy; + + // check without metadata service + let wakuNode = mockWakuNode({ + isStarted: true, + eventTarget, + connections: [{}], + peers: [ + mockPeer("1", []), + mockPeer("1.1", ["random"]), + mockPeer("2", ["random"]) + ] + }); + + let err: Error | undefined; + try { + await waitForRemotePeer(wakuNode, [Protocols.Filter]); + } catch (e) { + err = e as Error; + } + + expect(addEventListenerSpy.calledOnceWith("peer:identify")).to.be.true; + expect(err).to.be.undefined; + + // check with metadata serivice + wakuNode = mockWakuNode({ + isStarted: true, + eventTarget, + connections: [{}], + peers: [ + mockPeer("1", []), + mockPeer("1.1", ["random"]), + mockPeer("2", ["random"]) + ], + metadataService: mockMetadataService("resolve") + }); + + try { + await waitForRemotePeer(wakuNode, [Protocols.Filter]); + } catch (e) { + err = e as Error; + } + + expect(addEventListenerSpy.calledTwice).to.be.true; + expect(addEventListenerSpy.lastCall.calledWith("peer:identify")).to.be.true; + expect(err).to.be.undefined; + }); + + it("should wait for Store peer to be connected", async () => { + const addEventListenerSpy = sinon.spy( + (_type: string, _cb: (e: any) => void) => { + _cb({ detail: { protocols: [StoreCodec] } }); + } + ); + eventTarget.addEventListener = addEventListenerSpy; + + // check without metadata service + let wakuNode = mockWakuNode({ + isStarted: true, + eventTarget, + connections: [{}], + peers: [ + mockPeer("1", []), + mockPeer("1.1", ["random"]), + mockPeer("2", ["random"]) + ] + }); + + let err: Error | undefined; + try { + await waitForRemotePeer(wakuNode, [Protocols.Store]); + } catch (e) { + err = e as Error; + } + + expect(addEventListenerSpy.calledOnceWith("peer:identify")).to.be.true; + expect(err).to.be.undefined; + + // check with metadata serivice + wakuNode = mockWakuNode({ + isStarted: true, + eventTarget, + connections: [{}], + peers: [ + mockPeer("1", []), + mockPeer("1.1", ["random"]), + mockPeer("2", ["random"]) + ], + metadataService: mockMetadataService("resolve") + }); + + try { + await waitForRemotePeer(wakuNode, [Protocols.Store]); + } catch (e) { + err = e as Error; + } + + expect(addEventListenerSpy.calledTwice).to.be.true; + expect(addEventListenerSpy.lastCall.calledWith("peer:identify")).to.be.true; + expect(err).to.be.undefined; + }); + + it("should call waitForPeer on Relay", async () => { + const waitForRelaySpy = sinon.spy(); + const wakuNode = mockWakuNode({ + isStarted: true, + eventTarget, + connections: [{}], + peers: [ + mockPeer("1", []), + mockPeer("1.1", ["random"]), + mockPeer("2", ["random"]) + ] + }); + wakuNode.relay = { waitForPeer: waitForRelaySpy } as unknown as IRelay; + + let err: Error | undefined; + try { + await waitForRemotePeer(wakuNode, [Protocols.Relay]); + } catch (e) { + err = e as Error; + } + + expect(waitForRelaySpy.calledOnceWith()).to.be.true; + expect(err).to.be.undefined; + }); +}); + +type MockWakuOptions = { + isStarted?: boolean; + connections?: any[]; + peers?: Peer[]; + metadataService?: any; + eventTarget?: EventTarget; +}; + +function mockWakuNode(options: MockWakuOptions = {}): WakuNode { + return { + filter: {}, + lightPush: {}, + relay: {}, + store: {}, + libp2p: { + peerStore: mockPeerStore(options?.peers || []), + services: { + metadata: options.metadataService + }, + addEventListener: options?.eventTarget + ? options.eventTarget.addEventListener.bind(options.eventTarget) + : undefined, + removeEventListener: options?.eventTarget + ? options.eventTarget.removeEventListener.bind(options.eventTarget) + : undefined, + getConnections() { + return (options.connections || []) as Connection[]; + }, + getPeers() { + return (options?.peers || []).map((p) => p.id); + } + }, + isStarted() { + return options?.isStarted || false; + } + } as WakuNode; +} + +function mockMetadataService(mode: "resolve" | "reject"): any { + return { + confirmOrAttemptHandshake: () => { + return Promise.resolve( + mode === "resolve" ? { error: null } : { error: {} } + ); + } + }; +} + +function mockPeerStore(peers: any[]): PeerStore { + return { + get(peerId) { + return Promise.resolve(peers.find((p) => p.id === peerId)); + } + } as PeerStore; +} + +function mockPeer(id: string, protocols: string[]): Peer { + return { + id, + protocols + } as unknown as Peer; +} diff --git a/packages/sdk/src/waku/wait_for_remote_peer.ts b/packages/sdk/src/waku/wait_for_remote_peer.ts index ce956d0c44..52506cfa8c 100644 --- a/packages/sdk/src/waku/wait_for_remote_peer.ts +++ b/packages/sdk/src/waku/wait_for_remote_peer.ts @@ -32,8 +32,6 @@ export async function waitForRemotePeer( protocols?: Protocols[], timeoutMs?: number ): Promise { - // if no protocols or empty array passed - try to derive from mounted - protocols = protocols?.length ? protocols : getEnabledProtocols(waku); // if no protocols or empty array passed - try to derive from mounted protocols = protocols?.length ? protocols : getEnabledProtocols(waku); const connections = waku.libp2p.getConnections(); @@ -194,7 +192,9 @@ async function waitForMetadata( } const awaitTimeout = (ms: number, rejectReason: string): Promise => - new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms)); + new Promise((_resolve, reject) => + setTimeout(() => reject(Error(rejectReason)), ms) + ); async function rejectOnTimeout( promise: Promise, From 027a1d15daf7d464ecc7e55ad25f55987d75c77d Mon Sep 17 00:00:00 2001 From: Sasha <118575614+weboko@users.noreply.github.com> Date: Tue, 8 Oct 2024 13:14:05 +0200 Subject: [PATCH 26/28] feat: expose peerId and protocols from WakuNode (#2166) * chore: expose peerId and protocols from WakuNode * remove unused method * move to private method --- packages/interfaces/src/waku.ts | 20 ++++++++++++++++++++ packages/sdk/src/waku/waku.ts | 32 +++++++++++++------------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index 3602046f01..ca49e489f8 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -20,6 +20,26 @@ export interface IWaku { health: IHealthManager; connectionManager: IConnectionManager; + /** + * Returns a unique identifier for a node on the network. + * + * @example + * ```typescript + * console.log(waku.peerId); // 12D3KooWNmk9yXHfHJ4rUduRqD1TCTHkNFMPF9WP2dqWpZDL4aUb + * ``` + */ + peerId: PeerId; + + /** + * Returns a list of supported protocols. + * + * @example + * ```typescript + * console.log(waku.protocols); // ['/ipfs/id/1.0.0', '/ipfs/ping/1.0.0', '/vac/waku/filter-push/2.0.0-beta1', '/vac/waku/metadata/1.0.0'] + * ``` + */ + protocols: string[]; + /** * Dials to the provided peer * diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index 319b23fad6..414abefbea 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -128,12 +128,20 @@ export class WakuNode implements IWaku { ); } + public get peerId(): PeerId { + return this.libp2p.peerId; + } + + public get protocols(): string[] { + return this.libp2p.getProtocols(); + } + public async dial( peer: PeerId | MultiaddrInput, protocols?: Protocols[] ): Promise { const _protocols = protocols ?? []; - const peerId = mapToPeerIdOrMultiaddr(peer); + const peerId = this.mapToPeerIdOrMultiaddr(peer); if (typeof protocols === "undefined") { this.relay && _protocols.push(Protocols.Relay); @@ -212,23 +220,9 @@ export class WakuNode implements IWaku { return this.connectionManager.isConnected(); } - /** - * Return the local multiaddr with peer id on which libp2p is listening. - * - * @throws if libp2p is not listening on localhost. - */ - public getLocalMultiaddrWithID(): string { - const localMultiaddr = this.libp2p - .getMultiaddrs() - .find((addr) => addr.toString().match(/127\.0\.0\.1/)); - if (!localMultiaddr || localMultiaddr.toString() === "") { - throw "Not listening on localhost"; - } - return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString(); + private mapToPeerIdOrMultiaddr( + peerId: PeerId | MultiaddrInput + ): PeerId | Multiaddr { + return isPeerId(peerId) ? peerId : multiaddr(peerId); } } -function mapToPeerIdOrMultiaddr( - peerId: PeerId | MultiaddrInput -): PeerId | Multiaddr { - return isPeerId(peerId) ? peerId : multiaddr(peerId); -} From 448e659554d8e19268f25d572378d5cd52598373 Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 8 Oct 2024 13:19:14 +0200 Subject: [PATCH 27/28] rename to waitForPeers --- packages/interfaces/src/relay.ts | 2 +- packages/interfaces/src/waku.ts | 4 +-- packages/relay/src/relay.ts | 2 +- .../sdk/src/waku/wait_for_remote_peer.spec.ts | 2 +- packages/sdk/src/waku/wait_for_remote_peer.ts | 2 +- packages/sdk/src/waku/waku.ts | 2 +- packages/tests/src/lib/runNodes.ts | 2 +- packages/tests/src/utils/nodes.ts | 2 +- packages/tests/tests/enr.node.spec.ts | 6 ++-- packages/tests/tests/ephemeral.node.spec.ts | 6 ++-- packages/tests/tests/filter/push.node.spec.ts | 4 +-- .../single_node/multiple_pubsub.node.spec.ts | 6 ++-- .../filter/single_node/push.node.spec.ts | 4 +-- .../filter/single_node/subscribe.node.spec.ts | 2 +- packages/tests/tests/filter/utils.ts | 2 +- packages/tests/tests/getPeers.spec.ts | 16 +++++----- .../single_node/multiple_pubsub.node.spec.ts | 6 ++-- .../tests/tests/relay/interop.node.spec.ts | 2 +- .../tests/relay/multiple_pubsub.node.spec.ts | 32 +++++++++---------- packages/tests/tests/relay/utils.ts | 2 +- .../tests/sharding/auto_sharding.spec.ts | 8 ++--- .../tests/sharding/static_sharding.spec.ts | 8 ++--- packages/tests/tests/store/index.node.spec.ts | 2 +- .../tests/tests/store/multiple_pubsub.spec.ts | 6 ++-- packages/tests/tests/store/utils.ts | 2 +- .../tests/wait_for_remote_peer.node.spec.ts | 14 ++++---- packages/tests/tests/waku.node.spec.ts | 8 ++--- 27 files changed, 77 insertions(+), 77 deletions(-) diff --git a/packages/interfaces/src/relay.ts b/packages/interfaces/src/relay.ts index 126022ee2a..2172ac7be8 100644 --- a/packages/interfaces/src/relay.ts +++ b/packages/interfaces/src/relay.ts @@ -16,7 +16,7 @@ export interface IRelayAPI { readonly pubsubTopics: Set; readonly gossipSub: GossipSub; start: () => Promise; - waitForPeer: () => Promise; + waitForPeers: () => Promise; getMeshPeers: (topic?: TopicStr) => PeerIdStr[]; } diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index ca49e489f8..4f3edf4f86 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -101,7 +101,7 @@ export interface IWaku { * ```typescript * try { * // let's wait for at least one LightPush node and timeout in 1 second - * await waku.waitForPeer([Protocols.LightPush], 1000); + * await waku.waitForPeers([Protocols.LightPush], 1000); * } catch(e) { * waku.isConnected() === false; * console.error("Failed to connect due to", e); @@ -110,7 +110,7 @@ export interface IWaku { * waku.isConnected() === true; * ``` */ - waitForPeer(protocols?: Protocols[], timeoutMs?: number): Promise; + waitForPeers(protocols?: Protocols[], timeoutMs?: number): Promise; /** * @returns {boolean} `true` if the node was started and `false` otherwise diff --git a/packages/relay/src/relay.ts b/packages/relay/src/relay.ts index 0fabc76dd9..ed1cc0d556 100644 --- a/packages/relay/src/relay.ts +++ b/packages/relay/src/relay.ts @@ -99,7 +99,7 @@ class Relay implements IRelay { * Wait for at least one peer with the given protocol to be connected and in the gossipsub * mesh for all pubsubTopics. */ - public async waitForPeer(): Promise { + public async waitForPeers(): Promise { let peers = this.getMeshPeers(); const pubsubTopics = this.pubsubTopics; diff --git a/packages/sdk/src/waku/wait_for_remote_peer.spec.ts b/packages/sdk/src/waku/wait_for_remote_peer.spec.ts index 9f52552f3f..f3d71a1b32 100644 --- a/packages/sdk/src/waku/wait_for_remote_peer.spec.ts +++ b/packages/sdk/src/waku/wait_for_remote_peer.spec.ts @@ -321,7 +321,7 @@ describe("waitForRemotePeer", () => { mockPeer("2", ["random"]) ] }); - wakuNode.relay = { waitForPeer: waitForRelaySpy } as unknown as IRelay; + wakuNode.relay = { waitForPeers: waitForRelaySpy } as unknown as IRelay; let err: Error | undefined; try { diff --git a/packages/sdk/src/waku/wait_for_remote_peer.ts b/packages/sdk/src/waku/wait_for_remote_peer.ts index 52506cfa8c..8e2e7a7296 100644 --- a/packages/sdk/src/waku/wait_for_remote_peer.ts +++ b/packages/sdk/src/waku/wait_for_remote_peer.ts @@ -54,7 +54,7 @@ export async function waitForRemotePeer( if (!waku.relay) { throw Error("Cannot wait for Relay peer: protocol not mounted"); } - promises.push(waku.relay.waitForPeer()); + promises.push(waku.relay.waitForPeers()); } if (protocols.includes(Protocols.Store)) { diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index 414abefbea..c461a63ab3 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -205,7 +205,7 @@ export class WakuNode implements IWaku { await this.libp2p.stop(); } - public async waitForPeer( + public async waitForPeers( protocols?: Protocols[], timeoutMs?: number ): Promise { diff --git a/packages/tests/src/lib/runNodes.ts b/packages/tests/src/lib/runNodes.ts index 6f7b1775e1..adb37170f8 100644 --- a/packages/tests/src/lib/runNodes.ts +++ b/packages/tests/src/lib/runNodes.ts @@ -63,7 +63,7 @@ export async function runNodes( if (waku) { await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer(protocols); + await waku.waitForPeers(protocols); await nwaku.ensureSubscriptions(pubsubTopics); return [nwaku, waku as T]; } else { diff --git a/packages/tests/src/utils/nodes.ts b/packages/tests/src/utils/nodes.ts index 6bce4b063e..920bb392ba 100644 --- a/packages/tests/src/utils/nodes.ts +++ b/packages/tests/src/utils/nodes.ts @@ -52,7 +52,7 @@ export async function runMultipleNodes( for (const node of serviceNodes.nodes) { await waku.dial(await node.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); await node.ensureSubscriptions( derivePubsubTopicsFromNetworkConfig(networkConfig) ); diff --git a/packages/tests/tests/enr.node.spec.ts b/packages/tests/tests/enr.node.spec.ts index 33d9e30344..57eee7d62e 100644 --- a/packages/tests/tests/enr.node.spec.ts +++ b/packages/tests/tests/enr.node.spec.ts @@ -40,7 +40,7 @@ describe("ENR Interop: ServiceNode", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waku.waitForPeer([Protocols.Relay]); + await waku.waitForPeers([Protocols.Relay]); const nwakuInfo = await nwaku.info(); const nimPeerId = await nwaku.getPeerId(); @@ -74,7 +74,7 @@ describe("ENR Interop: ServiceNode", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waku.waitForPeer([Protocols.Relay]); + await waku.waitForPeers([Protocols.Relay]); const nwakuInfo = await nwaku.info(); const nimPeerId = await nwaku.getPeerId(); @@ -109,7 +109,7 @@ describe("ENR Interop: ServiceNode", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waku.waitForPeer([Protocols.Relay]); + await waku.waitForPeers([Protocols.Relay]); const nwakuInfo = await nwaku.info(); const nimPeerId = await nwaku.getPeerId(); diff --git a/packages/tests/tests/ephemeral.node.spec.ts b/packages/tests/tests/ephemeral.node.spec.ts index 58582a99e9..2abed950d2 100644 --- a/packages/tests/tests/ephemeral.node.spec.ts +++ b/packages/tests/tests/ephemeral.node.spec.ts @@ -110,7 +110,7 @@ describe("Waku Message Ephemeral field", function () { await waku.start(); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); }); it("Ephemeral messages are not stored", async function () { @@ -157,7 +157,7 @@ describe("Waku Message Ephemeral field", function () { log.info("Waku nodes connected to nwaku"); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); log.info("Sending messages using light push"); await Promise.all([ @@ -166,7 +166,7 @@ describe("Waku Message Ephemeral field", function () { waku1.lightPush.send(ClearEncoder, clearMsg) ]); - await waku2.waitForPeer([Protocols.Store]); + await waku2.waitForPeers([Protocols.Store]); const messages: DecodedMessage[] = []; diff --git a/packages/tests/tests/filter/push.node.spec.ts b/packages/tests/tests/filter/push.node.spec.ts index 2598db30d4..8d2cff4240 100644 --- a/packages/tests/tests/filter/push.node.spec.ts +++ b/packages/tests/tests/filter/push.node.spec.ts @@ -236,7 +236,7 @@ const runTests = (strictCheckNodes: boolean): void => { // Redo the connection and create a new subscription for (const node of this.serviceNodes) { await waku.dial(await node.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); } await waku.filter.subscribe( @@ -276,7 +276,7 @@ const runTests = (strictCheckNodes: boolean): void => { // Restart nwaku node await teardownNodesWithRedundancy(serviceNodes, []); await serviceNodes.start(); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M2") }); diff --git a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts index 9e05ba4bfd..ec7ba216f0 100644 --- a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts @@ -116,7 +116,7 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () { clusterId: 3 }); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptions([customPubsubTopic2]); @@ -269,7 +269,7 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () { contentTopic: [customContentTopic2] }); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptionsAutosharding([customContentTopic2]); @@ -408,7 +408,7 @@ describe("Waku Filter V2 (Named sharding): Multiple PubsubTopics", function () { clusterId: 3 }); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptions([customPubsubTopic2]); diff --git a/packages/tests/tests/filter/single_node/push.node.spec.ts b/packages/tests/tests/filter/single_node/push.node.spec.ts index 1ccb1c6bde..5677190a1f 100644 --- a/packages/tests/tests/filter/single_node/push.node.spec.ts +++ b/packages/tests/tests/filter/single_node/push.node.spec.ts @@ -215,7 +215,7 @@ describe("Waku Filter V2: FilterPush", function () { // Redo the connection and create a new subscription await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); await waku.filter.subscribe([TestDecoder], messageCollector.callback); @@ -242,7 +242,7 @@ describe("Waku Filter V2: FilterPush", function () { // Restart nwaku node await tearDownNodes(nwaku, []); await nwaku.start(); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M2") }); diff --git a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts index f628579107..4eb49a7129 100644 --- a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts +++ b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts @@ -445,7 +445,7 @@ describe("Waku Filter V2: Subscribe: Single Service Node", function () { // Set up and start a new nwaku node [nwaku2, waku2] = await runNodes(ctx, TestShardInfo); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); await nwaku2.ensureSubscriptions([TestPubsubTopic]); // Send a message using the new subscription diff --git a/packages/tests/tests/filter/utils.ts b/packages/tests/tests/filter/utils.ts index e65540700c..649c302cf0 100644 --- a/packages/tests/tests/filter/utils.ts +++ b/packages/tests/tests/filter/utils.ts @@ -107,7 +107,7 @@ export async function runMultipleNodes( for (const node of serviceNodes.nodes) { await waku.dial(await node.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Filter, Protocols.LightPush]); + await waku.waitForPeers([Protocols.Filter, Protocols.LightPush]); await node.ensureSubscriptions(pubsubTopics); const wakuConnections = waku.libp2p.getConnections(); diff --git a/packages/tests/tests/getPeers.spec.ts b/packages/tests/tests/getPeers.spec.ts index 017cc76fa3..b38fe6b5bc 100644 --- a/packages/tests/tests/getPeers.spec.ts +++ b/packages/tests/tests/getPeers.spec.ts @@ -69,7 +69,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { waku = await createLightNode({ networkConfig: shardInfo }); await waku.start(); await waku.libp2p.dialProtocol(serviceNodeMa, LightPushCodec); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), waku.libp2p.peerStore, @@ -119,7 +119,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode1Ma, LightPushCodec); await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -172,7 +172,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -225,7 +225,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await delay(500); await waku.libp2p.dialProtocol(serviceNodeMa2, LightPushCodec); await waku.start(); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -259,7 +259,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { waku = await createLightNode({ networkConfig: shardInfo }); await waku.start(); await waku.libp2p.dialProtocol(serviceNodeMa, LightPushCodec); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), waku.libp2p.peerStore, @@ -312,7 +312,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -366,7 +366,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await waku.libp2p.dialProtocol(serviceNode2Ma, LightPushCodec); await waku.start(); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), @@ -420,7 +420,7 @@ describe("getConnectedPeersForProtocolAndShard", function () { await delay(500); await waku.libp2p.dialProtocol(serviceNodeMa2, LightPushCodec); await waku.start(); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), diff --git a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts index c6d1715b61..52cb6f14f8 100644 --- a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts @@ -132,7 +132,7 @@ describe("Waku Light Push : Multiple PubsubTopics", function () { singleShardInfoToPubsubTopic(singleShardInfo2) ]); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); @@ -274,7 +274,7 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () { }); await nwaku2.ensureSubscriptionsAutosharding([customContentTopic2]); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); @@ -425,7 +425,7 @@ describe("Waku Light Push (named sharding): Multiple PubsubTopics", function () await nwaku2.ensureSubscriptions([autoshardingPubsubTopic2]); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); diff --git a/packages/tests/tests/relay/interop.node.spec.ts b/packages/tests/tests/relay/interop.node.spec.ts index 727cfd3785..efa977e479 100644 --- a/packages/tests/tests/relay/interop.node.spec.ts +++ b/packages/tests/tests/relay/interop.node.spec.ts @@ -108,7 +108,7 @@ describe("Waku Relay, Interop", function () { const nwakuMultiaddr = await nwaku.getMultiaddrWithId(); await waku2.dial(nwakuMultiaddr); - await waku2.waitForPeer([Protocols.Relay]); + await waku2.waitForPeers([Protocols.Relay]); await delay(2000); // Check that the two JS peers are NOT directly connected diff --git a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts index 6b6af4761b..1b88ca5969 100644 --- a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts @@ -114,9 +114,9 @@ describe("Waku Relay, multiple pubsub topics", function () { ]); await Promise.all([ - waku1.waitForPeer([Protocols.Relay]), - waku2.waitForPeer([Protocols.Relay]), - waku2.waitForPeer([Protocols.Relay]) + waku1.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -221,9 +221,9 @@ describe("Waku Relay, multiple pubsub topics", function () { ]); await Promise.all([ - waku1.waitForPeer([Protocols.Relay]), - waku2.waitForPeer([Protocols.Relay]), - waku3.waitForPeer([Protocols.Relay]) + waku1.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]), + waku3.waitForPeers([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -289,8 +289,8 @@ describe("Waku Relay, multiple pubsub topics", function () { ]); await Promise.all([ - waku1.waitForPeer([Protocols.Relay]), - waku2.waitForPeer([Protocols.Relay]) + waku1.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]) ]); const messageText = "Communicating using a custom pubsub topic"; @@ -419,9 +419,9 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { ]); await Promise.all([ - waku1.waitForPeer([Protocols.Relay]), - waku2.waitForPeer([Protocols.Relay]), - waku3.waitForPeer([Protocols.Relay]) + waku1.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]), + waku3.waitForPeers([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -535,9 +535,9 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { ]); await Promise.all([ - waku1.waitForPeer([Protocols.Relay]), - waku2.waitForPeer([Protocols.Relay]), - waku3.waitForPeer([Protocols.Relay]) + waku1.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]), + waku3.waitForPeers([Protocols.Relay]) ]); await waku1.relay.subscribeWithUnsubscribe( @@ -630,8 +630,8 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { ]); await Promise.all([ - waku1.waitForPeer([Protocols.Relay]), - waku2.waitForPeer([Protocols.Relay]) + waku1.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]) ]); const messageText = "Communicating using a custom pubsub topic"; diff --git a/packages/tests/tests/relay/utils.ts b/packages/tests/tests/relay/utils.ts index 2c1c866ed1..2feb0c7a69 100644 --- a/packages/tests/tests/relay/utils.ts +++ b/packages/tests/tests/relay/utils.ts @@ -45,7 +45,7 @@ export async function waitForAllRemotePeers( ): Promise { log.info("Wait for mutual pubsub subscription"); await Promise.all( - nodes.map((node): Promise => node.waitForPeer(RELAY_PROTOCOLS)) + nodes.map((node): Promise => node.waitForPeers(RELAY_PROTOCOLS)) ); } diff --git a/packages/tests/tests/sharding/auto_sharding.spec.ts b/packages/tests/tests/sharding/auto_sharding.spec.ts index 38d9847ad5..e2cdca6d41 100644 --- a/packages/tests/tests/sharding/auto_sharding.spec.ts +++ b/packages/tests/tests/sharding/auto_sharding.spec.ts @@ -56,7 +56,7 @@ describe("Autosharding: Running Nodes", function () { } }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -98,7 +98,7 @@ describe("Autosharding: Running Nodes", function () { } }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -155,7 +155,7 @@ describe("Autosharding: Running Nodes", function () { }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -218,7 +218,7 @@ describe("Autosharding: Running Nodes", function () { } }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const encoder1 = createEncoder({ contentTopic: ContentTopic, diff --git a/packages/tests/tests/sharding/static_sharding.spec.ts b/packages/tests/tests/sharding/static_sharding.spec.ts index 472ba5fdd1..f4cdccb4ee 100644 --- a/packages/tests/tests/sharding/static_sharding.spec.ts +++ b/packages/tests/tests/sharding/static_sharding.spec.ts @@ -57,7 +57,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfo }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -97,7 +97,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfo }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -146,7 +146,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfo }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const encoder = createEncoder({ contentTopic: ContentTopic, @@ -212,7 +212,7 @@ describe("Static Sharding: Running Nodes", function () { networkConfig: shardInfoBothShards }); await waku.dial(await nwaku.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.LightPush]); + await waku.waitForPeers([Protocols.LightPush]); const encoder1 = createEncoder({ contentTopic: ContentTopic, diff --git a/packages/tests/tests/store/index.node.spec.ts b/packages/tests/tests/store/index.node.spec.ts index 07843462b6..d9a055fb3e 100644 --- a/packages/tests/tests/store/index.node.spec.ts +++ b/packages/tests/tests/store/index.node.spec.ts @@ -291,7 +291,7 @@ describe("Waku Store, general", function () { waku.lightPush.send(TestEncoder, clearMsg) ]); - await waku2.waitForPeer([Protocols.Store]); + await waku2.waitForPeers([Protocols.Store]); const messages: DecodedMessage[] = []; log.info("Retrieve messages from store"); diff --git a/packages/tests/tests/store/multiple_pubsub.spec.ts b/packages/tests/tests/store/multiple_pubsub.spec.ts index 4cd4b1cd73..99878acb96 100644 --- a/packages/tests/tests/store/multiple_pubsub.spec.ts +++ b/packages/tests/tests/store/multiple_pubsub.spec.ts @@ -130,7 +130,7 @@ describe("Waku Store, custom pubsub topic", function () { ); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Store]); + await waku.waitForPeers([Protocols.Store]); let customMessages: IMessage[] = []; let testMessages: IMessage[] = []; @@ -263,7 +263,7 @@ describe("Waku Store (Autosharding), custom pubsub topic", function () { await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Store]); + await waku.waitForPeers([Protocols.Store]); let customMessages: IMessage[] = []; let testMessages: IMessage[] = []; @@ -389,7 +389,7 @@ describe("Waku Store (named sharding), custom pubsub topic", function () { ); await waku.dial(await nwaku2.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Store]); + await waku.waitForPeers([Protocols.Store]); let customMessages: IMessage[] = []; let testMessages: IMessage[] = []; diff --git a/packages/tests/tests/store/utils.ts b/packages/tests/tests/store/utils.ts index b6fa62cee6..01419bed50 100644 --- a/packages/tests/tests/store/utils.ts +++ b/packages/tests/tests/store/utils.ts @@ -111,7 +111,7 @@ export async function startAndConnectLightNode( }); await waku.start(); await waku.dial(await instance.getMultiaddrWithId()); - await waku.waitForPeer([Protocols.Store]); + await waku.waitForPeers([Protocols.Store]); const wakuConnections = waku.libp2p.getConnections(); diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 5eba758e97..c1f63479dd 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -60,7 +60,7 @@ describe("Wait for remote peer", function () { }); await waku1.start(); - const waitPromise = waku1.waitForPeer([Protocols.Relay]); + const waitPromise = waku1.waitForPeers([Protocols.Relay]); await delay(1000); await waku1.dial(multiAddrWithId); await waitPromise; @@ -80,7 +80,7 @@ describe("Wait for remote peer", function () { }) .then((waku1) => waku1.start().then(() => waku1)) .then((waku1) => { - waku1.waitForPeer([Protocols.Relay], 200).then( + waku1.waitForPeers([Protocols.Relay], 200).then( () => { throw "Promise expected to reject on time out"; }, @@ -111,7 +111,7 @@ describe("Wait for remote peer", function () { await waku2.start(); await waku2.dial(multiAddrWithId); await delay(1000); - await waku2.waitForPeer([Protocols.Store]); + await waku2.waitForPeers([Protocols.Store]); const peers = (await waku2.store.protocol.connectedPeers()).map((peer) => peer.id.toString() @@ -138,7 +138,7 @@ describe("Wait for remote peer", function () { networkConfig: DefaultTestShardInfo }); await waku2.start(); - const waitPromise = waku2.waitForPeer([Protocols.Store], 2000); + const waitPromise = waku2.waitForPeers([Protocols.Store], 2000); await delay(1000); await waku2.dial(multiAddrWithId); await waitPromise; @@ -170,7 +170,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waku2.waitForPeer([Protocols.LightPush]); + await waku2.waitForPeers([Protocols.LightPush]); const peers = (await waku2.lightPush.protocol.connectedPeers()).map( (peer) => peer.id.toString() @@ -199,7 +199,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waku2.waitForPeer([Protocols.Filter]); + await waku2.waitForPeers([Protocols.Filter]); const peers = (await waku2.filter.protocol.connectedPeers()).map((peer) => peer.id.toString() @@ -228,7 +228,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waku2.waitForPeer([ + await waku2.waitForPeers([ Protocols.Filter, Protocols.Store, Protocols.LightPush diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 93b78cb04b..0d81d7c8bc 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -58,7 +58,7 @@ describe("Waku Dial [node only]", function () { }); await waku.start(); await waku.dial(multiAddrWithId); - await waku.waitForPeer([ + await waku.waitForPeers([ Protocols.Store, Protocols.Filter, Protocols.LightPush @@ -189,8 +189,8 @@ describe("Decryption Keys", function () { await waku1.dial(waku2.libp2p.peerId); await Promise.all([ - waku1.waitForPeer([Protocols.Relay]), - waku1.waitForPeer([Protocols.Relay]) + waku1.waitForPeers([Protocols.Relay]), + waku1.waitForPeers([Protocols.Relay]) ]); }); @@ -267,7 +267,7 @@ describe("User Agent", function () { multiaddrs: waku2.libp2p.getMultiaddrs() }); await waku1.dial(waku2.libp2p.peerId); - await waku1.waitForPeer(); + await waku1.waitForPeers(); const [waku1PeerInfo, waku2PeerInfo] = await Promise.all([ waku2.libp2p.peerStore.get(waku1.libp2p.peerId), From ec8ccb4608b3924d85caefc56b0a81ad133c9cce Mon Sep 17 00:00:00 2001 From: Sasha Date: Wed, 9 Oct 2024 00:24:30 +0200 Subject: [PATCH 28/28] up test --- packages/tests/tests/wait_for_remote_peer.node.spec.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index c1f63479dd..1b3e039c45 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -85,7 +85,9 @@ describe("Wait for remote peer", function () { throw "Promise expected to reject on time out"; }, (reason) => { - expect(reason).to.eq("Timed out waiting for a remote peer."); + expect(reason?.message).to.eq( + "Timed out waiting for a remote peer." + ); done(); } );