mirror of https://github.com/waku-org/js-waku.git
chore: use `BaseProtocol` on `WakuPeerExchange`
Ref: https://github.com/waku-org/js-waku/pull/1137
This commit is contained in:
parent
3c7c5d290c
commit
1a9b13f902
|
@ -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",
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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<Peer> {
|
||||
const { peer } = await selectPeerForProtocol(
|
||||
this.peerStore,
|
||||
|
@ -37,7 +36,7 @@ export class BaseProtocol {
|
|||
return peer;
|
||||
}
|
||||
protected async newStream(peer: Peer): Promise<Stream> {
|
||||
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");
|
||||
|
|
|
@ -48,7 +48,7 @@ class Filter extends BaseProtocol implements IFilter {
|
|||
private subscriptions: Map<RequestID, unknown>;
|
||||
|
||||
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
|
||||
|
|
|
@ -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 || {};
|
||||
}
|
||||
|
||||
|
|
|
@ -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 ?? {};
|
||||
}
|
||||
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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<void>)
|
||||
| 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<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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue