chore: use `BaseProtocol` on `WakuPeerExchange`

Ref: https://github.com/waku-org/js-waku/pull/1137
This commit is contained in:
fryorcraken.eth 2023-02-27 10:14:16 +11:00
parent 3c7c5d290c
commit 1a9b13f902
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
9 changed files with 25 additions and 72 deletions

4
package-lock.json generated
View File

@ -28320,14 +28320,12 @@
"@libp2p/interfaces": "^3.3.1", "@libp2p/interfaces": "^3.3.1",
"@waku/enr": "*", "@waku/enr": "*",
"@waku/proto": "*", "@waku/proto": "*",
"@waku/utils": "*",
"debug": "^4.3.4", "debug": "^4.3.4",
"it-all": "^2.0.0", "it-all": "^2.0.0",
"it-length-prefixed": "^8.0.4", "it-length-prefixed": "^8.0.4",
"it-pipe": "^2.0.5" "it-pipe": "^2.0.5"
}, },
"devDependencies": { "devDependencies": {
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-connection-manager": "^1.3.7", "@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-peer-id": "^2.0.1", "@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8", "@libp2p/interface-peer-info": "^1.0.8",
@ -32967,7 +32965,6 @@
"@waku/peer-exchange": { "@waku/peer-exchange": {
"version": "file:packages/peer-exchange", "version": "file:packages/peer-exchange",
"requires": { "requires": {
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-connection-manager": "^1.3.7", "@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-peer-discovery": "^1.0.5", "@libp2p/interface-peer-discovery": "^1.0.5",
"@libp2p/interface-peer-id": "^2.0.1", "@libp2p/interface-peer-id": "^2.0.1",
@ -32983,7 +32980,6 @@
"@waku/enr": "*", "@waku/enr": "*",
"@waku/interfaces": "*", "@waku/interfaces": "*",
"@waku/proto": "*", "@waku/proto": "*",
"@waku/utils": "*",
"chai": "^4.3.7", "chai": "^4.3.7",
"cspell": "^6.26.3", "cspell": "^6.26.3",
"debug": "^4.3.4", "debug": "^4.3.4",

View File

@ -20,6 +20,10 @@
"./lib/message/topic_only_message": { "./lib/message/topic_only_message": {
"types": "./dist/lib/message/topic_only_message.d.ts", "types": "./dist/lib/message/topic_only_message.d.ts",
"import": "./dist/lib/message/topic_only_message.js" "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": { "typesVersions": {

View File

@ -8,6 +8,7 @@ export default {
"lib/predefined_bootstrap_nodes": "dist/lib/predefined_bootstrap_nodes.js", "lib/predefined_bootstrap_nodes": "dist/lib/predefined_bootstrap_nodes.js",
"lib/message/version_0": "dist/lib/message/version_0.js", "lib/message/version_0": "dist/lib/message/version_0.js",
"lib/message/topic_only_message": "dist/lib/message/topic_only_message.js", "lib/message/topic_only_message": "dist/lib/message/topic_only_message.js",
"lib/base_protocol": "dist/lib/base_protocol.js",
}, },
output: { output: {
dir: "bundle", dir: "bundle",

View File

@ -1,5 +1,4 @@
import type { Stream } from "@libp2p/interface-connection"; import type { Connection, Stream } from "@libp2p/interface-connection";
import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerId } from "@libp2p/interface-peer-id"; import type { PeerId } from "@libp2p/interface-peer-id";
import { Peer, PeerStore } from "@libp2p/interface-peer-store"; import { Peer, PeerStore } from "@libp2p/interface-peer-store";
import { import {
@ -13,7 +12,11 @@ import {
* Protocols. * Protocols.
*/ */
export class BaseProtocol { 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 * Returns known peers from the address book (`libp2p.peerStore`) that support
@ -24,10 +27,6 @@ export class BaseProtocol {
return getPeersForProtocol(this.peerStore, [this.multicodec]); return getPeersForProtocol(this.peerStore, [this.multicodec]);
} }
get peerStore(): PeerStore {
return this.libp2p.peerStore;
}
protected async getPeer(peerId?: PeerId): Promise<Peer> { protected async getPeer(peerId?: PeerId): Promise<Peer> {
const { peer } = await selectPeerForProtocol( const { peer } = await selectPeerForProtocol(
this.peerStore, this.peerStore,
@ -37,7 +36,7 @@ export class BaseProtocol {
return peer; return peer;
} }
protected async newStream(peer: Peer): Promise<Stream> { protected async newStream(peer: Peer): Promise<Stream> {
const connections = this.libp2p.getConnections(peer.id); const connections = this.getConnections(peer.id);
const connection = selectConnection(connections); const connection = selectConnection(connections);
if (!connection) { if (!connection) {
throw new Error("Failed to get a connection to the peer"); throw new Error("Failed to get a connection to the peer");

View File

@ -48,7 +48,7 @@ class Filter extends BaseProtocol implements IFilter {
private subscriptions: Map<RequestID, unknown>; private subscriptions: Map<RequestID, unknown>;
constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) { constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(FilterCodec, libp2p); super(FilterCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
this.options = options ?? {}; this.options = options ?? {};
this.subscriptions = new Map(); this.subscriptions = new Map();
this.libp2p this.libp2p

View File

@ -32,7 +32,7 @@ class LightPush extends BaseProtocol implements ILightPush {
options: ProtocolCreateOptions; options: ProtocolCreateOptions;
constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) { constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(LightPushCodec, libp2p); super(LightPushCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
this.options = options || {}; this.options = options || {};
} }

View File

@ -81,7 +81,7 @@ class Store extends BaseProtocol implements IStore {
options: ProtocolCreateOptions; options: ProtocolCreateOptions;
constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) { constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
super(StoreCodec, libp2p); super(StoreCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
this.options = options ?? {}; this.options = options ?? {};
} }

View File

@ -54,14 +54,12 @@
"@libp2p/interfaces": "^3.3.1", "@libp2p/interfaces": "^3.3.1",
"@waku/enr": "*", "@waku/enr": "*",
"@waku/proto": "*", "@waku/proto": "*",
"@waku/utils": "*",
"debug": "^4.3.4", "debug": "^4.3.4",
"it-all": "^2.0.0", "it-all": "^2.0.0",
"it-length-prefixed": "^8.0.4", "it-length-prefixed": "^8.0.4",
"it-pipe": "^2.0.5" "it-pipe": "^2.0.5"
}, },
"devDependencies": { "devDependencies": {
"@libp2p/interface-connection": "^3.0.8",
"@libp2p/interface-connection-manager": "^1.3.7", "@libp2p/interface-connection-manager": "^1.3.7",
"@libp2p/interface-peer-id": "^2.0.1", "@libp2p/interface-peer-id": "^2.0.1",
"@libp2p/interface-peer-info": "^1.0.8", "@libp2p/interface-peer-info": "^1.0.8",

View File

@ -1,22 +1,16 @@
import type { Stream } from "@libp2p/interface-connection";
import type { ConnectionManager } from "@libp2p/interface-connection-manager"; import type { ConnectionManager } from "@libp2p/interface-connection-manager";
import type { PeerId } from "@libp2p/interface-peer-id"; import type { PeerStore } from "@libp2p/interface-peer-store";
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
import type { import type {
IncomingStreamData, IncomingStreamData,
Registrar, Registrar,
} from "@libp2p/interface-registrar"; } from "@libp2p/interface-registrar";
import { BaseProtocol } from "@waku/core/lib/base_protocol";
import { ENR } from "@waku/enr"; import { ENR } from "@waku/enr";
import type { import type {
IPeerExchange, IPeerExchange,
PeerExchangeQueryParams, PeerExchangeQueryParams,
PeerExchangeResponse, PeerExchangeResponse,
} from "@waku/interfaces"; } from "@waku/interfaces";
import {
getPeersForProtocol,
selectConnection,
selectPeerForProtocol,
} from "@waku/utils";
import debug from "debug"; import debug from "debug";
import all from "it-all"; import all from "it-all";
import * as lp from "it-length-prefixed"; 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/) * Implementation of the Peer Exchange protocol (https://rfc.vac.dev/spec/34/)
*/ */
export class WakuPeerExchange implements IPeerExchange { export class WakuPeerExchange extends BaseProtocol implements IPeerExchange {
multicodec: string;
private callback: private callback:
| ((response: PeerExchangeResponse) => Promise<void>) | ((response: PeerExchangeResponse) => Promise<void>)
| undefined; | undefined;
@ -47,7 +40,13 @@ export class WakuPeerExchange implements IPeerExchange {
* @param components - libp2p components * @param components - libp2p components
*/ */
constructor(public components: PeerExchangeComponents) { constructor(public components: PeerExchangeComponents) {
this.multicodec = PeerExchangeCodec; super(
PeerExchangeCodec,
components.peerStore,
components.connectionManager.getConnections.bind(
components.connectionManager
)
);
this.components.registrar this.components.registrar
.handle(PeerExchangeCodec, this.handler.bind(this)) .handle(PeerExchangeCodec, this.handler.bind(this))
.catch((e) => log("Failed to register peer exchange protocol", e)); .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)); }).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<Peer> {
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<Stream> {
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<Peer[]> {
return getPeersForProtocol(this.components.peerStore, [PeerExchangeCodec]);
}
/**
* @returns The libp2p peer store
*/
get peerStore(): PeerStore {
return this.components.peerStore;
}
} }
/** /**