From 75fcca4cd99d6aabcbb14afab9332c98ddc6b74f Mon Sep 17 00:00:00 2001 From: Sasha <118575614+weboko@users.noreply.github.com> Date: Wed, 9 Oct 2024 00:43:34 +0200 Subject: [PATCH] feat: replace `waitForRemotePeers()` with `waku.waitForPeer()` method (#2161) * fix comment of default number of peers * export default number of peers from base protocol sdk * rename to light_push, move class to separate file * move waitForRemotePeer to sdk package * add todo to move waitForGossipSubPeerInMesh into @waku/relay * clean up waitForRemotePeer, split metadata await from event and optimise, decouple from protocol implementations * simplify and rename ILightPush interface * use only connected peers in light push based on connections instead of peer renewal mechanism * improve readability of result processing in light push * fix check & update tests * address tests, add new test cases, fix racing condition in StreamManager * use libp2p.getPeers * feat: confirm metadata and protocols needed in waitForRemotePeer * rely on passed protocols and fallback to mounted * add I prefix to Waku interface * implement waku.connect method * add docs to IWaku interface * remove export and usage of waitForRemotePeer * move wait for remote peer related to Realy out of @waku/sdk * change tests to use new API * fix linting * update size limit * rename .connect to .waitForPeer * export waitForRemotePeer and mark as deprecated * 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 * feat: expose peerId and protocols from WakuNode (#2166) * chore: expose peerId and protocols from WakuNode * remove unused method * move to private method * rename to waitForPeers * up test --- .size-limit.cjs | 2 +- package-lock.json | 13 +- packages/interfaces/src/relay.ts | 1 + packages/interfaces/src/waku.ts | 101 ++++- packages/relay/package.json | 3 +- packages/relay/src/relay.ts | 17 + packages/sdk/.mocha.reporters.json | 6 + packages/sdk/.mocharc.cjs | 26 ++ packages/sdk/package.json | 11 +- 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 | 2 + .../sdk/src/waku/wait_for_remote_peer.spec.ts | 399 ++++++++++++++++++ .../src/{ => waku}/wait_for_remote_peer.ts | 35 +- packages/sdk/src/{ => waku}/waku.ts | 61 ++- packages/tests/src/lib/runNodes.ts | 4 +- packages/tests/src/utils/nodes.ts | 10 +- packages/tests/src/utils/teardown.ts | 4 +- 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 | 4 +- packages/tests/tests/filter/utils.ts | 10 +- packages/tests/tests/getPeers.spec.ts | 19 +- .../tests/tests/health-manager/node.spec.ts | 4 +- .../single_node/multiple_pubsub.node.spec.ts | 7 +- packages/tests/tests/multiaddr.node.spec.ts | 4 +- .../tests/tests/relay/interop.node.spec.ts | 3 +- .../tests/relay/multiple_pubsub.node.spec.ts | 33 +- packages/tests/tests/relay/utils.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 | 4 +- .../tests/wait_for_remote_peer.node.spec.ts | 20 +- packages/tests/tests/waku.node.spec.ts | 17 +- 40 files changed, 713 insertions(+), 193 deletions(-) create mode 100644 packages/sdk/.mocha.reporters.json create mode 100644 packages/sdk/.mocharc.cjs create mode 100644 packages/sdk/src/waku/index.ts create mode 100644 packages/sdk/src/waku/wait_for_remote_peer.spec.ts rename packages/sdk/src/{ => waku}/wait_for_remote_peer.ts (88%) rename packages/sdk/src/{ => waku}/waku.ts (81%) 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 }", }, }, { diff --git a/package-lock.json b/package-lock.json index 66344a70e7..6203ae1818 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,19 +39410,23 @@ "@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", "@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/interfaces/src/relay.ts b/packages/interfaces/src/relay.ts index a212337ac1..2172ac7be8 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; + waitForPeers: () => Promise; getMeshPeers: (topic?: TopicStr) => PeerIdStr[]; } diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index c22258e0f5..4f3edf4f86 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -10,36 +10,127 @@ 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; filter?: IFilter; lightPush?: ILightPush; + 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 + * + * @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; + /** + * 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.waitForPeers([Protocols.LightPush], 1000); + * } catch(e) { + * waku.isConnected() === false; + * console.error("Failed to connect due to", e); + * } + * + * waku.isConnected() === true; + * ``` + */ + waitForPeers(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; - - 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/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..ed1cc0d556 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 waitForPeers(): 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/.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 c828b6afa4..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" }, @@ -70,15 +73,19 @@ "@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": { + "@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/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..484b5def3e --- /dev/null +++ b/packages/sdk/src/waku/index.ts @@ -0,0 +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.spec.ts b/packages/sdk/src/waku/wait_for_remote_peer.spec.ts new file mode 100644 index 0000000000..f3d71a1b32 --- /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 = { waitForPeers: 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/wait_for_remote_peer.ts b/packages/sdk/src/waku/wait_for_remote_peer.ts similarity index 88% rename from packages/sdk/src/wait_for_remote_peer.ts rename to packages/sdk/src/waku/wait_for_remote_peer.ts index 1dde3f2367..8e2e7a7296 100644 --- a/packages/sdk/src/wait_for_remote_peer.ts +++ b/packages/sdk/src/waku/wait_for_remote_peer.ts @@ -1,13 +1,14 @@ import type { IdentifyResult } from "@libp2p/interface"; import { FilterCodecs, LightPushCodec, StoreCodec } from "@waku/core"; -import type { IRelay, Libp2p, Waku } 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"); /** + * @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 @@ -27,7 +28,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 { @@ -41,6 +42,7 @@ export async function waitForRemotePeer( if (connections.length > 0 && !protocols.includes(Protocols.Relay)) { const success = await waitForMetadata(waku, protocols); + if (success) { return; } @@ -52,7 +54,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.waitForPeers()); } if (protocols.includes(Protocols.Store)) { @@ -136,7 +138,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(); @@ -189,25 +191,10 @@ 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)); + new Promise((_resolve, reject) => + setTimeout(() => reject(Error(rejectReason)), ms) + ); async function rejectOnTimeout( promise: Promise, @@ -217,7 +204,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/waku.ts similarity index 81% rename from packages/sdk/src/waku.ts rename to packages/sdk/src/waku/waku.ts index 88dddea5df..c461a63ab3 100644 --- a/packages/sdk/src/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -8,18 +8,20 @@ import type { ILightPush, IRelay, IStore, + IWaku, Libp2p, ProtocolCreateOptions, - PubsubTopic, - Waku + PubsubTopic } from "@waku/interfaces"; 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; export const DefaultRelayKeepAliveValueSecs = 5 * 60; @@ -59,7 +61,7 @@ type ProtocolsEnabled = { store?: boolean; }; -export class WakuNode implements Waku { +export class WakuNode implements IWaku { public libp2p: Libp2p; public relay?: IRelay; public store?: IStore; @@ -126,18 +128,20 @@ export class WakuNode implements Waku { ); } - /** - * 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 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); @@ -201,6 +205,13 @@ export class WakuNode implements Waku { await this.libp2p.stop(); } + public async waitForPeers( + protocols?: Protocols[], + timeoutMs?: number + ): Promise { + return waitForRemotePeer(this, protocols, timeoutMs); + } + public isStarted(): boolean { return this.libp2p.status == "started"; } @@ -209,23 +220,9 @@ export class WakuNode implements Waku { 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); -} diff --git a/packages/tests/src/lib/runNodes.ts b/packages/tests/src/lib/runNodes.ts index 82893f4f97..adb37170f8 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.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 07035b382c..920bb392ba 100644 --- a/packages/tests/src/utils/nodes.ts +++ b/packages/tests/src/utils/nodes.ts @@ -1,12 +1,12 @@ import { DefaultNetworkConfig, + IWaku, LightNode, NetworkConfig, ProtocolCreateOptions, - Protocols, - Waku + Protocols } from "@waku/interfaces"; -import { createLightNode, waitForRemotePeer } from "@waku/sdk"; +import { createLightNode } from "@waku/sdk"; import { derivePubsubTopicsFromNetworkConfig } from "@waku/utils"; import { Context } from "mocha"; import pRetry from "p-retry"; @@ -52,7 +52,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.waitForPeers([Protocols.Filter, Protocols.LightPush]); await node.ensureSubscriptions( derivePubsubTopicsFromNetworkConfig(networkConfig) ); @@ -73,7 +73,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]; diff --git a/packages/tests/tests/enr.node.spec.ts b/packages/tests/tests/enr.node.spec.ts index 6a02e70fb9..57eee7d62e 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.waitForPeers([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.waitForPeers([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.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 d89ef201cc..2abed950d2 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, [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 waitForRemotePeer(waku1, [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 waitForRemotePeer(waku2, [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 2daf8b8417..8d2cff4240 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 { @@ -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 waitForRemotePeer(waku, [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 waitForRemotePeer(waku, [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 60aeeebfe9..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 @@ -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.waitForPeers([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.waitForPeers([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.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 baf074b2c2..5677190a1f 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.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 waitForRemotePeer(waku, [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 cf2aef1303..4eb49a7129 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"; @@ -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.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 e0d02767a0..649c302cf0 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.waitForPeers([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..b38fe6b5bc 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.waitForPeers([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.waitForPeers([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.waitForPeers([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.waitForPeers([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.waitForPeers([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.waitForPeers([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.waitForPeers([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.waitForPeers([Protocols.LightPush]); const peers = await getConnectedPeersForProtocolAndShard( waku.libp2p.getConnections(), 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/light-push/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts index f36a614aaf..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 @@ -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.waitForPeers([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.waitForPeers([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.waitForPeers([Protocols.LightPush]); const messageCollector2 = new MessageCollector(nwaku2); 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/relay/interop.node.spec.ts b/packages/tests/tests/relay/interop.node.spec.ts index 3c05bebfa0..efa977e479 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.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 6434153f17..1b88ca5969 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.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([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.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]), + waku3.waitForPeers([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.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([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.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]), + waku3.waitForPeers([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.waitForPeers([Protocols.Relay]), + waku2.waitForPeers([Protocols.Relay]), + waku3.waitForPeers([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.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 f0141e3481..2feb0c7a69 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.waitForPeers(RELAY_PROTOCOLS)) ); } diff --git a/packages/tests/tests/sharding/auto_sharding.spec.ts b/packages/tests/tests/sharding/auto_sharding.spec.ts index 91e58979b7..e2cdca6d41 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.waitForPeers([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.waitForPeers([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.waitForPeers([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.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 47cca240a5..f4cdccb4ee 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.waitForPeers([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.waitForPeers([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.waitForPeers([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.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 e5ce92d7a0..d9a055fb3e 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.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 de2b346b7b..99878acb96 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.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 waitForRemotePeer(waku, [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 waitForRemotePeer(waku, [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 c4d1a1811d..01419bed50 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"; @@ -111,7 +111,7 @@ export async function startAndConnectLightNode( }); await waku.start(); await waku.dial(await instance.getMultiaddrWithId()); - await waitForRemotePeer(waku, [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 71c89eda0f..1b3e039c45 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 } 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.waitForPeers([Protocols.Relay]); await delay(1000); await waku1.dial(multiAddrWithId); await waitPromise; @@ -80,12 +80,14 @@ describe("Wait for remote peer", function () { }) .then((waku1) => waku1.start().then(() => waku1)) .then((waku1) => { - waitForRemotePeer(waku1, [Protocols.Relay], 200).then( + waku1.waitForPeers([Protocols.Relay], 200).then( () => { 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(); } ); @@ -111,7 +113,7 @@ describe("Wait for remote peer", function () { await waku2.start(); await waku2.dial(multiAddrWithId); await delay(1000); - await waitForRemotePeer(waku2, [Protocols.Store]); + await waku2.waitForPeers([Protocols.Store]); const peers = (await waku2.store.protocol.connectedPeers()).map((peer) => peer.id.toString() @@ -138,7 +140,7 @@ describe("Wait for remote peer", function () { networkConfig: DefaultTestShardInfo }); await waku2.start(); - const waitPromise = waitForRemotePeer(waku2, [Protocols.Store], 2000); + const waitPromise = waku2.waitForPeers([Protocols.Store], 2000); await delay(1000); await waku2.dial(multiAddrWithId); await waitPromise; @@ -170,7 +172,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waitForRemotePeer(waku2, [Protocols.LightPush]); + await waku2.waitForPeers([Protocols.LightPush]); const peers = (await waku2.lightPush.protocol.connectedPeers()).map( (peer) => peer.id.toString() @@ -199,7 +201,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waitForRemotePeer(waku2, [Protocols.Filter]); + await waku2.waitForPeers([Protocols.Filter]); const peers = (await waku2.filter.protocol.connectedPeers()).map((peer) => peer.id.toString() @@ -228,7 +230,7 @@ describe("Wait for remote peer", function () { }); await waku2.start(); await waku2.dial(multiAddrWithId); - await waitForRemotePeer(waku2, [ + 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 250835f16b..0d81d7c8bc 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 { @@ -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.waitForPeers([ Protocols.Store, Protocols.Filter, Protocols.LightPush @@ -190,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.waitForPeers([Protocols.Relay]), + waku1.waitForPeers([Protocols.Relay]) ]); }); @@ -239,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]); @@ -268,7 +267,7 @@ describe("User Agent", function () { multiaddrs: waku2.libp2p.getMultiaddrs() }); await waku1.dial(waku2.libp2p.peerId); - await waitForRemotePeer(waku1); + await waku1.waitForPeers(); const [waku1PeerInfo, waku2PeerInfo] = await Promise.all([ waku2.libp2p.peerStore.get(waku1.libp2p.peerId),