refact: getPeersForProtocol only needs the peer store

This commit is contained in:
fryorcraken.eth 2022-09-11 09:49:17 +10:00
parent 0c83953e55
commit 3b05bfe988
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 6 additions and 7 deletions

View File

@ -1,5 +1,4 @@
import { Peer } from "@libp2p/interface-peer-store";
import { Libp2p } from "libp2p";
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
/**
* Returns a pseudo-random peer that supports the given protocol.
@ -18,11 +17,11 @@ export async function selectRandomPeer(
* Returns the list of peers that supports the given protocol.
*/
export async function getPeersForProtocol(
libp2p: Libp2p,
peerStore: PeerStore,
protocols: string[]
): Promise<Peer[]> {
const peers: Peer[] = [];
await libp2p.peerStore.forEach((peer) => {
await peerStore.forEach((peer) => {
for (let i = 0; i < protocols.length; i++) {
if (peer.protocols.includes(protocols[i])) {
peers.push(peer);

View File

@ -272,7 +272,7 @@ export class WakuFilter {
}
async peers(): Promise<Peer[]> {
return getPeersForProtocol(this.libp2p, [FilterCodec]);
return getPeersForProtocol(this.libp2p.peerStore, [FilterCodec]);
}
async randomPeer(): Promise<Peer | undefined> {

View File

@ -109,7 +109,7 @@ export class WakuLightPush {
* peers.
*/
async peers(): Promise<Peer[]> {
return getPeersForProtocol(this.libp2p, [LightPushCodec]);
return getPeersForProtocol(this.libp2p.peerStore, [LightPushCodec]);
}
/**

View File

@ -312,7 +312,7 @@ export class WakuStore {
codecs.push(codec);
}
return getPeersForProtocol(this.libp2p, codecs);
return getPeersForProtocol(this.libp2p.peerStore, codecs);
}
/**