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

View File

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

View File

@ -109,7 +109,7 @@ export class WakuLightPush {
* peers. * peers.
*/ */
async peers(): Promise<Peer[]> { 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); codecs.push(codec);
} }
return getPeersForProtocol(this.libp2p, codecs); return getPeersForProtocol(this.libp2p.peerStore, codecs);
} }
/** /**