From 1a9b13f902563bbbd0e3443d9c2857382505bee8 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Mon, 27 Feb 2023 10:14:16 +1100 Subject: [PATCH] chore: use `BaseProtocol` on `WakuPeerExchange` Ref: https://github.com/waku-org/js-waku/pull/1137 --- package-lock.json | 4 -- packages/core/package.json | 4 ++ packages/core/rollup.config.js | 1 + packages/core/src/lib/base_protocol.ts | 15 ++--- packages/core/src/lib/filter/index.ts | 2 +- packages/core/src/lib/light_push/index.ts | 2 +- packages/core/src/lib/store/index.ts | 2 +- packages/peer-exchange/package.json | 2 - .../peer-exchange/src/waku_peer_exchange.ts | 65 +++---------------- 9 files changed, 25 insertions(+), 72 deletions(-) diff --git a/package-lock.json b/package-lock.json index f57be5962d..02d6d18e47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28320,14 +28320,12 @@ "@libp2p/interfaces": "^3.3.1", "@waku/enr": "*", "@waku/proto": "*", - "@waku/utils": "*", "debug": "^4.3.4", "it-all": "^2.0.0", "it-length-prefixed": "^8.0.4", "it-pipe": "^2.0.5" }, "devDependencies": { - "@libp2p/interface-connection": "^3.0.8", "@libp2p/interface-connection-manager": "^1.3.7", "@libp2p/interface-peer-id": "^2.0.1", "@libp2p/interface-peer-info": "^1.0.8", @@ -32967,7 +32965,6 @@ "@waku/peer-exchange": { "version": "file:packages/peer-exchange", "requires": { - "@libp2p/interface-connection": "^3.0.8", "@libp2p/interface-connection-manager": "^1.3.7", "@libp2p/interface-peer-discovery": "^1.0.5", "@libp2p/interface-peer-id": "^2.0.1", @@ -32983,7 +32980,6 @@ "@waku/enr": "*", "@waku/interfaces": "*", "@waku/proto": "*", - "@waku/utils": "*", "chai": "^4.3.7", "cspell": "^6.26.3", "debug": "^4.3.4", diff --git a/packages/core/package.json b/packages/core/package.json index fa4184c86f..f4c3b332e1 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -20,6 +20,10 @@ "./lib/message/topic_only_message": { "types": "./dist/lib/message/topic_only_message.d.ts", "import": "./dist/lib/message/topic_only_message.js" + }, + "./lib/base_protocol": { + "types": "./dist/lib/base_protocol.d.ts", + "import": "./dist/lib/base_protocol.js" } }, "typesVersions": { diff --git a/packages/core/rollup.config.js b/packages/core/rollup.config.js index 4896be37cb..c1f9793192 100644 --- a/packages/core/rollup.config.js +++ b/packages/core/rollup.config.js @@ -8,6 +8,7 @@ export default { "lib/predefined_bootstrap_nodes": "dist/lib/predefined_bootstrap_nodes.js", "lib/message/version_0": "dist/lib/message/version_0.js", "lib/message/topic_only_message": "dist/lib/message/topic_only_message.js", + "lib/base_protocol": "dist/lib/base_protocol.js", }, output: { dir: "bundle", diff --git a/packages/core/src/lib/base_protocol.ts b/packages/core/src/lib/base_protocol.ts index 8524cb4069..df18e9fb29 100644 --- a/packages/core/src/lib/base_protocol.ts +++ b/packages/core/src/lib/base_protocol.ts @@ -1,5 +1,4 @@ -import type { Stream } from "@libp2p/interface-connection"; -import type { Libp2p } from "@libp2p/interface-libp2p"; +import type { Connection, Stream } from "@libp2p/interface-connection"; import type { PeerId } from "@libp2p/interface-peer-id"; import { Peer, PeerStore } from "@libp2p/interface-peer-store"; import { @@ -13,7 +12,11 @@ import { * Protocols. */ export class BaseProtocol { - constructor(public multicodec: string, public libp2p: Libp2p) {} + constructor( + public multicodec: string, + public peerStore: PeerStore, + protected getConnections: (peerId?: PeerId) => Connection[] + ) {} /** * Returns known peers from the address book (`libp2p.peerStore`) that support @@ -24,10 +27,6 @@ export class BaseProtocol { return getPeersForProtocol(this.peerStore, [this.multicodec]); } - get peerStore(): PeerStore { - return this.libp2p.peerStore; - } - protected async getPeer(peerId?: PeerId): Promise { const { peer } = await selectPeerForProtocol( this.peerStore, @@ -37,7 +36,7 @@ export class BaseProtocol { return peer; } protected async newStream(peer: Peer): Promise { - const connections = this.libp2p.getConnections(peer.id); + const connections = this.getConnections(peer.id); const connection = selectConnection(connections); if (!connection) { throw new Error("Failed to get a connection to the peer"); diff --git a/packages/core/src/lib/filter/index.ts b/packages/core/src/lib/filter/index.ts index d5ca907afa..5507f94509 100644 --- a/packages/core/src/lib/filter/index.ts +++ b/packages/core/src/lib/filter/index.ts @@ -48,7 +48,7 @@ class Filter extends BaseProtocol implements IFilter { private subscriptions: Map; constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) { - super(FilterCodec, libp2p); + super(FilterCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p)); this.options = options ?? {}; this.subscriptions = new Map(); this.libp2p diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index 05640d2cb6..b31784900a 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -32,7 +32,7 @@ class LightPush extends BaseProtocol implements ILightPush { options: ProtocolCreateOptions; constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) { - super(LightPushCodec, libp2p); + super(LightPushCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p)); this.options = options || {}; } diff --git a/packages/core/src/lib/store/index.ts b/packages/core/src/lib/store/index.ts index 860e407e11..a1e6007e62 100644 --- a/packages/core/src/lib/store/index.ts +++ b/packages/core/src/lib/store/index.ts @@ -81,7 +81,7 @@ class Store extends BaseProtocol implements IStore { options: ProtocolCreateOptions; constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) { - super(StoreCodec, libp2p); + super(StoreCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p)); this.options = options ?? {}; } diff --git a/packages/peer-exchange/package.json b/packages/peer-exchange/package.json index f5c288807c..c8eb2f4729 100644 --- a/packages/peer-exchange/package.json +++ b/packages/peer-exchange/package.json @@ -54,14 +54,12 @@ "@libp2p/interfaces": "^3.3.1", "@waku/enr": "*", "@waku/proto": "*", - "@waku/utils": "*", "debug": "^4.3.4", "it-all": "^2.0.0", "it-length-prefixed": "^8.0.4", "it-pipe": "^2.0.5" }, "devDependencies": { - "@libp2p/interface-connection": "^3.0.8", "@libp2p/interface-connection-manager": "^1.3.7", "@libp2p/interface-peer-id": "^2.0.1", "@libp2p/interface-peer-info": "^1.0.8", diff --git a/packages/peer-exchange/src/waku_peer_exchange.ts b/packages/peer-exchange/src/waku_peer_exchange.ts index 79c7180c1c..9fedc0d96c 100644 --- a/packages/peer-exchange/src/waku_peer_exchange.ts +++ b/packages/peer-exchange/src/waku_peer_exchange.ts @@ -1,22 +1,16 @@ -import type { Stream } from "@libp2p/interface-connection"; import type { ConnectionManager } from "@libp2p/interface-connection-manager"; -import type { PeerId } from "@libp2p/interface-peer-id"; -import type { Peer, PeerStore } from "@libp2p/interface-peer-store"; +import type { PeerStore } from "@libp2p/interface-peer-store"; import type { IncomingStreamData, Registrar, } from "@libp2p/interface-registrar"; +import { BaseProtocol } from "@waku/core/lib/base_protocol"; import { ENR } from "@waku/enr"; import type { IPeerExchange, PeerExchangeQueryParams, PeerExchangeResponse, } from "@waku/interfaces"; -import { - getPeersForProtocol, - selectConnection, - selectPeerForProtocol, -} from "@waku/utils"; import debug from "debug"; import all from "it-all"; import * as lp from "it-length-prefixed"; @@ -37,8 +31,7 @@ export interface PeerExchangeComponents { /** * Implementation of the Peer Exchange protocol (https://rfc.vac.dev/spec/34/) */ -export class WakuPeerExchange implements IPeerExchange { - multicodec: string; +export class WakuPeerExchange extends BaseProtocol implements IPeerExchange { private callback: | ((response: PeerExchangeResponse) => Promise) | undefined; @@ -47,7 +40,13 @@ export class WakuPeerExchange implements IPeerExchange { * @param components - libp2p components */ constructor(public components: PeerExchangeComponents) { - this.multicodec = PeerExchangeCodec; + super( + PeerExchangeCodec, + components.peerStore, + components.connectionManager.getConnections.bind( + components.connectionManager + ) + ); this.components.registrar .handle(PeerExchangeCodec, this.handler.bind(this)) .catch((e) => log("Failed to register peer exchange protocol", e)); @@ -112,50 +111,6 @@ export class WakuPeerExchange implements IPeerExchange { } }).catch((err) => log("Failed to handle peer exchange request", err)); } - - /** - * - * @param peerId - Optional peer ID to select a peer - * @returns A peer to query - */ - private async getPeer(peerId?: PeerId): Promise { - const { peer } = await selectPeerForProtocol( - this.peerStore, - [PeerExchangeCodec], - peerId - ); - return peer; - } - - /** - * @param peer - Peer to open a stream with - * @returns A new stream - */ - private async newStream(peer: Peer): Promise { - const connections = this.components.connectionManager.getConnections( - peer.id - ); - const connection = selectConnection(connections); - if (!connection) { - throw new Error("Failed to get a connection to the peer"); - } - - return connection.newStream(PeerExchangeCodec); - } - - /** - * @returns All peers that support the peer exchange protocol - */ - async peers(): Promise { - return getPeersForProtocol(this.components.peerStore, [PeerExchangeCodec]); - } - - /** - * @returns The libp2p peer store - */ - get peerStore(): PeerStore { - return this.components.peerStore; - } } /**