2023-08-16 20:18:13 +05:30
|
|
|
import type { Libp2p } from "@libp2p/interface";
|
|
|
|
|
import type { Stream } from "@libp2p/interface/connection";
|
|
|
|
|
import type { PeerId } from "@libp2p/interface/peer-id";
|
|
|
|
|
import { Peer, PeerStore } from "@libp2p/interface/peer-store";
|
2023-07-25 02:17:52 +02:00
|
|
|
import type { IBaseProtocol, Libp2pComponents } from "@waku/interfaces";
|
2023-09-04 10:27:25 +05:30
|
|
|
import { getPeersForProtocol, selectPeerForProtocol } from "@waku/utils/libp2p";
|
|
|
|
|
|
2023-09-07 13:15:49 +05:30
|
|
|
import { filterPeers } from "./filterPeers.js";
|
2023-09-04 10:27:25 +05:30
|
|
|
import { StreamManager } from "./stream_manager.js";
|
2023-02-23 14:04:27 +11:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A class with predefined helpers, to be used as a base to implement Waku
|
|
|
|
|
* Protocols.
|
|
|
|
|
*/
|
2023-07-25 02:17:52 +02:00
|
|
|
export class BaseProtocol implements IBaseProtocol {
|
|
|
|
|
public readonly addLibp2pEventListener: Libp2p["addEventListener"];
|
|
|
|
|
public readonly removeLibp2pEventListener: Libp2p["removeEventListener"];
|
2023-09-04 10:27:25 +05:30
|
|
|
protected streamManager: StreamManager;
|
2023-07-25 02:17:52 +02:00
|
|
|
|
2023-08-11 15:14:02 +02:00
|
|
|
constructor(
|
|
|
|
|
public multicodec: string,
|
2023-08-16 20:18:13 +05:30
|
|
|
private components: Libp2pComponents
|
2023-08-11 15:14:02 +02:00
|
|
|
) {
|
2023-07-25 02:17:52 +02:00
|
|
|
this.addLibp2pEventListener = components.events.addEventListener.bind(
|
2023-08-16 20:18:13 +05:30
|
|
|
components.events
|
2023-07-25 02:17:52 +02:00
|
|
|
);
|
|
|
|
|
this.removeLibp2pEventListener = components.events.removeEventListener.bind(
|
2023-08-16 20:18:13 +05:30
|
|
|
components.events
|
2023-07-25 02:17:52 +02:00
|
|
|
);
|
2023-09-04 10:27:25 +05:30
|
|
|
|
|
|
|
|
this.streamManager = new StreamManager(
|
|
|
|
|
multicodec,
|
|
|
|
|
components.connectionManager.getConnections.bind(
|
|
|
|
|
components.connectionManager
|
|
|
|
|
),
|
|
|
|
|
this.addLibp2pEventListener
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
protected async getStream(peer: Peer): Promise<Stream> {
|
|
|
|
|
return this.streamManager.getStream(peer);
|
2023-07-25 02:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get peerStore(): PeerStore {
|
|
|
|
|
return this.components.peerStore;
|
|
|
|
|
}
|
2023-02-23 14:04:27 +11:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns known peers from the address book (`libp2p.peerStore`) that support
|
|
|
|
|
* the class protocol. Waku may or may not be currently connected to these
|
|
|
|
|
* peers.
|
|
|
|
|
*/
|
2023-07-25 02:17:52 +02:00
|
|
|
public async peers(): Promise<Peer[]> {
|
2023-02-23 14:04:27 +11:00
|
|
|
return getPeersForProtocol(this.peerStore, [this.multicodec]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async getPeer(peerId?: PeerId): Promise<Peer> {
|
|
|
|
|
const { peer } = await selectPeerForProtocol(
|
|
|
|
|
this.peerStore,
|
|
|
|
|
[this.multicodec],
|
2023-08-16 20:18:13 +05:30
|
|
|
peerId
|
2023-02-23 14:04:27 +11:00
|
|
|
);
|
|
|
|
|
return peer;
|
|
|
|
|
}
|
2023-09-07 13:15:49 +05:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a list of peers based on the specified criteria.
|
|
|
|
|
*
|
|
|
|
|
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
|
|
|
|
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
|
|
|
|
* @returns A Promise that resolves to an array of peers based on the specified criteria.
|
|
|
|
|
*/
|
|
|
|
|
protected async getPeers(
|
|
|
|
|
{
|
|
|
|
|
numPeers,
|
|
|
|
|
maxBootstrapPeers
|
|
|
|
|
}: {
|
|
|
|
|
numPeers: number;
|
|
|
|
|
maxBootstrapPeers: number;
|
|
|
|
|
} = {
|
|
|
|
|
maxBootstrapPeers: 1,
|
|
|
|
|
numPeers: 0
|
|
|
|
|
}
|
|
|
|
|
): Promise<Peer[]> {
|
|
|
|
|
// Retrieve all peers that support the protocol
|
|
|
|
|
const allPeersForProtocol = await getPeersForProtocol(this.peerStore, [
|
|
|
|
|
this.multicodec
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Filter the peers based on the specified criteria
|
|
|
|
|
return filterPeers(allPeersForProtocol, numPeers, maxBootstrapPeers);
|
|
|
|
|
}
|
2023-02-23 14:04:27 +11:00
|
|
|
}
|