From 372ff6454ff5e791d67a7984dbb2e9f270195937 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 25 Jan 2023 17:20:33 +1100 Subject: [PATCH] chore: remove `@waku/peer-exchange` dependency As per its name, `@waku/core` aims to contain, and only contains, the minimal set of core functionalities needed for a developer to use Waku in their webapp. Hence, `@waku/core` should avoid depending on other Waku packages. If a developer wishes to use functionality from other packages, they should explicitly import such packages. --- package-lock.json | 2 - packages/core/package.json | 1 - packages/core/src/lib/wait_for_remote_peer.ts | 34 +++++-------- packages/core/src/lib/waku.ts | 49 +++++++++++++++---- 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index bd9334c02e..ce3d8fbd6e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26317,7 +26317,6 @@ "@waku/byte-utils": "*", "@waku/interfaces": "*", "@waku/libp2p-utils": "*", - "@waku/peer-exchange": "*", "@waku/proto": "*", "debug": "^4.3.4", "it-all": "^1.0.6", @@ -29834,7 +29833,6 @@ "@waku/byte-utils": "*", "@waku/interfaces": "*", "@waku/libp2p-utils": "*", - "@waku/peer-exchange": "*", "@waku/proto": "*", "app-root-path": "^3.0.0", "chai": "^4.3.4", diff --git a/packages/core/package.json b/packages/core/package.json index b7531abc57..6c6f5119a8 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -91,7 +91,6 @@ "@waku/byte-utils": "*", "@waku/interfaces": "*", "@waku/libp2p-utils": "*", - "@waku/peer-exchange": "*", "@waku/proto": "*", "debug": "^4.3.4", "it-all": "^1.0.6", diff --git a/packages/core/src/lib/wait_for_remote_peer.ts b/packages/core/src/lib/wait_for_remote_peer.ts index db0e05debc..4b6d8487f7 100644 --- a/packages/core/src/lib/wait_for_remote_peer.ts +++ b/packages/core/src/lib/wait_for_remote_peer.ts @@ -1,14 +1,9 @@ import { PeerProtocolsChangeData } from "@libp2p/interface-peer-store"; import type { IRelay, PointToPointProtocol, Waku } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; -import { PeerExchangeCodec } from "@waku/peer-exchange"; import debug from "debug"; import { pEvent } from "p-event"; -import { FilterCodec } from "./filter/index.js"; -import { LightPushCodec } from "./light_push/index.js"; -import { StoreCodec } from "./store/index.js"; - const log = debug("waku:wait-for-remote-peer"); /** @@ -50,19 +45,19 @@ export async function waitForRemotePeer( if (protocols.includes(Protocols.Store)) { if (!waku.store) throw new Error("Cannot wait for Store peer: protocol not mounted"); - promises.push(waitForConnectedPeer(waku.store, [StoreCodec])); + promises.push(waitForConnectedPeer(waku.store)); } if (protocols.includes(Protocols.LightPush)) { if (!waku.lightPush) throw new Error("Cannot wait for LightPush peer: protocol not mounted"); - promises.push(waitForConnectedPeer(waku.lightPush, [LightPushCodec])); + promises.push(waitForConnectedPeer(waku.lightPush)); } if (protocols.includes(Protocols.Filter)) { if (!waku.filter) throw new Error("Cannot wait for Filter peer: protocol not mounted"); - promises.push(waitForConnectedPeer(waku.filter, [FilterCodec])); + promises.push(waitForConnectedPeer(waku.filter)); } if (protocols.includes(Protocols.PeerExchange)) { @@ -70,7 +65,7 @@ export async function waitForRemotePeer( throw new Error( "Cannot wait for Peer Exchange peer: protocol not mounted" ); - promises.push(waitForConnectedPeer(waku.peerExchange, [PeerExchangeCodec])); + promises.push(waitForConnectedPeer(waku.peerExchange)); } if (timeoutMs) { @@ -88,28 +83,25 @@ export async function waitForRemotePeer( * Wait for a peer with the given protocol to be connected. */ async function waitForConnectedPeer( - waku: PointToPointProtocol, - codecs: string[] + protocol: PointToPointProtocol ): Promise { - const peers = await waku.peers(); + const codec = protocol.multicodec; + const peers = await protocol.peers(); if (peers.length) { - log(`${codecs} peer found: `, peers[0].id.toString()); + log(`${codec} peer found: `, peers[0].id.toString()); return; } await new Promise((resolve) => { const cb = (evt: CustomEvent): void => { - for (const codec of codecs) { - if (evt.detail.protocols.includes(codec)) { - log("Resolving for", codec, evt.detail.protocols); - waku.peerStore.removeEventListener("change:protocols", cb); - resolve(); - break; - } + if (evt.detail.protocols.includes(codec)) { + log("Resolving for", codec, evt.detail.protocols); + protocol.peerStore.removeEventListener("change:protocols", cb); + resolve(); } }; - waku.peerStore.addEventListener("change:protocols", cb); + protocol.peerStore.addEventListener("change:protocols", cb); }); } diff --git a/packages/core/src/lib/waku.ts b/packages/core/src/lib/waku.ts index eaaa1af75a..7e2d04b9d8 100644 --- a/packages/core/src/lib/waku.ts +++ b/packages/core/src/lib/waku.ts @@ -12,16 +12,15 @@ import type { Waku, } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; -import { PeerExchangeCodec } from "@waku/peer-exchange"; import debug from "debug"; import type { Libp2p } from "libp2p"; -import { FilterCodec, FilterComponents } from "./filter/index.js"; -import { LightPushCodec, LightPushComponents } from "./light_push/index.js"; +import { FilterComponents } from "./filter/index.js"; +import { LightPushComponents } from "./light_push/index.js"; import { createEncoder } from "./message/version_0.js"; import * as relayConstants from "./relay/constants.js"; -import { RelayCodecs, RelayPingContentTopic } from "./relay/constants.js"; -import { StoreCodec, StoreComponents } from "./store/index.js"; +import { RelayPingContentTopic } from "./relay/constants.js"; +import { StoreComponents } from "./store/index.js"; export const DefaultPingKeepAliveValueSecs = 0; export const DefaultRelayKeepAliveValueSecs = 5 * 60; @@ -165,20 +164,50 @@ export class WakuNode implements Waku { const codecs: string[] = []; if (_protocols.includes(Protocols.Relay)) { - RelayCodecs.forEach((codec) => codecs.push(codec)); + if (this.relay) { + this.relay.multicodecs.forEach((codec) => codecs.push(codec)); + } else { + log( + "Relay codec not included in dial codec: protocol not mounted locally" + ); + } } if (_protocols.includes(Protocols.Store)) { - codecs.push(StoreCodec); + if (this.store) { + codecs.push(this.store.multicodec); + } else { + log( + "Store codec not included in dial codec: protocol not mounted locally" + ); + } } if (_protocols.includes(Protocols.LightPush)) { - codecs.push(LightPushCodec); + if (this.lightPush) { + codecs.push(this.lightPush.multicodec); + } else { + log( + "Light Push codec not included in dial codec: protocol not mounted locally" + ); + } } if (_protocols.includes(Protocols.Filter)) { - codecs.push(FilterCodec); + if (this.filter) { + codecs.push(this.filter.multicodec); + } else { + log( + "Filter codec not included in dial codec: protocol not mounted locally" + ); + } } if (_protocols.includes(Protocols.PeerExchange)) { - codecs.push(PeerExchangeCodec); + if (this.peerExchange) { + codecs.push(this.peerExchange.multicodec); + } else { + log( + "Peer Exchange codec not included in dial codec: protocol not mounted locally" + ); + } } log(`Dialing to ${peer.toString()} with protocols ${_protocols}`);