From d9940f4d9bbaa095212efded81deee3058c25337 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 17 May 2022 18:23:50 +1000 Subject: [PATCH] Simplify async iterator usage The iterator will return only once there is a first item available. Hence, there is no need to add an observer on `change:protocols` event. --- src/lib/waku.node.spec.ts | 61 +++++++++++++++++++++++++++++++++------ src/lib/waku.ts | 61 +++++++++------------------------------ 2 files changed, 66 insertions(+), 56 deletions(-) diff --git a/src/lib/waku.node.spec.ts b/src/lib/waku.node.spec.ts index 437573bb99..96d80742bc 100644 --- a/src/lib/waku.node.spec.ts +++ b/src/lib/waku.node.spec.ts @@ -1,5 +1,4 @@ import { expect } from "chai"; -import debug from "debug"; import PeerId from "peer-id"; import { @@ -8,13 +7,12 @@ import { NOISE_KEY_2, Nwaku, } from "../test_utils/"; +import { delay } from "../test_utils/delay"; import { Protocols, Waku } from "./waku"; import { WakuMessage } from "./waku_message"; import { generateSymmetricKey } from "./waku_message/version_1"; -const dbg = debug("waku:test"); - const TestContentTopic = "/test/1/waku/utf8"; describe("Waku Dial [node only]", function () { @@ -175,21 +173,18 @@ describe("Wait for remote peer / get peers", function () { !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); }); - it("Relay", async function () { + it("Relay - dialed first", async function () { this.timeout(20_000); nwaku = new Nwaku(makeLogFileName(this)); await nwaku.start(); const multiAddrWithId = await nwaku.getMultiaddrWithId(); - dbg("Create"); waku = await Waku.create({ staticNoiseKey: NOISE_KEY_1, }); - dbg("Dial"); await waku.dial(multiAddrWithId); - dbg("waitForRemotePeer"); + await delay(1000); await waku.waitForRemotePeer([Protocols.Relay]); - dbg("Done, get peers"); const peers = waku.relay.getPeers(); const nimPeerId = multiAddrWithId.getPeerId(); @@ -197,7 +192,29 @@ describe("Wait for remote peer / get peers", function () { expect(peers.has(nimPeerId as string)).to.be.true; }); - it("Store", async function () { + it("Relay - dialed after", async function () { + this.timeout(20_000); + nwaku = new Nwaku(makeLogFileName(this)); + await nwaku.start(); + const multiAddrWithId = await nwaku.getMultiaddrWithId(); + + waku = await Waku.create({ + staticNoiseKey: NOISE_KEY_1, + }); + + const waitPromise = waku.waitForRemotePeer([Protocols.Relay]); + await delay(1000); + await waku.dial(multiAddrWithId); + await waitPromise; + + const peers = waku.relay.getPeers(); + const nimPeerId = multiAddrWithId.getPeerId(); + + expect(nimPeerId).to.not.be.undefined; + expect(peers.has(nimPeerId as string)).to.be.true; + }); + + it("Store - dialed first", async function () { this.timeout(20_000); nwaku = new Nwaku(makeLogFileName(this)); await nwaku.start({ persistMessages: true }); @@ -207,6 +224,7 @@ describe("Wait for remote peer / get peers", function () { staticNoiseKey: NOISE_KEY_1, }); await waku.dial(multiAddrWithId); + await delay(1000); await waku.waitForRemotePeer([Protocols.Store]); const peers = []; @@ -220,6 +238,31 @@ describe("Wait for remote peer / get peers", function () { expect(peers.includes(nimPeerId as string)).to.be.true; }); + it("Store - dialed after", async function () { + this.timeout(20_000); + nwaku = new Nwaku(makeLogFileName(this)); + await nwaku.start({ persistMessages: true }); + const multiAddrWithId = await nwaku.getMultiaddrWithId(); + + waku = await Waku.create({ + staticNoiseKey: NOISE_KEY_1, + }); + const waitPromise = waku.waitForRemotePeer([Protocols.Store]); + await delay(1000); + await waku.dial(multiAddrWithId); + await waitPromise; + + const peers = []; + for await (const peer of waku.store.peers) { + peers.push(peer.id.toB58String()); + } + + const nimPeerId = multiAddrWithId.getPeerId(); + + expect(nimPeerId).to.not.be.undefined; + expect(peers.includes(nimPeerId as string)).to.be.true; + }); + it("LightPush", async function () { this.timeout(20_000); nwaku = new Nwaku(makeLogFileName(this)); diff --git a/src/lib/waku.ts b/src/lib/waku.ts index ace3238d56..5a06c91f3b 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -353,56 +353,23 @@ export class Waku { } if (desiredProtocols.includes(Protocols.Store)) { - let storePeerFound = false; - - for await (const _peer of this.store.peers) { - storePeerFound = true; - break; - } - - if (!storePeerFound) { - // No peer available for this protocol, waiting to connect to one. - const promise = new Promise((resolve) => { - this.libp2p.peerStore.on( - "change:protocols", - ({ protocols: connectedPeerProtocols }) => { - for (const codec of Object.values(StoreCodecs)) { - if (connectedPeerProtocols.includes(codec)) { - dbg("Resolving for", codec, connectedPeerProtocols); - resolve(); - } - } - } - ); - }); - promises.push(promise); - } + const storePromise = (async (): Promise => { + for await (const peer of this.store.peers) { + dbg("Store peer found", peer.id.toB58String()); + break; + } + })(); + promises.push(storePromise); } if (desiredProtocols.includes(Protocols.LightPush)) { - let lightPushPeerFound = false; - - for await (const _peer of this.lightPush.peers) { - lightPushPeerFound = true; - break; - } - - if (!lightPushPeerFound) { - // No peer available for this protocol, waiting to connect to one. - const promise = new Promise((resolve) => { - this.libp2p.peerStore.on( - "change:protocols", - ({ protocols: connectedPeerProtocols }) => { - if (connectedPeerProtocols.includes(LightPushCodec)) { - dbg("Resolving for", LightPushCodec, connectedPeerProtocols); - resolve(); - } - } - ); - }); - - promises.push(promise); - } + const lightPushPromise = (async (): Promise => { + for await (const peer of this.lightPush.peers) { + dbg("Light Push peer found", peer.id.toB58String()); + break; + } + })(); + promises.push(lightPushPromise); } await Promise.all(promises);