From 6117593d724ee008fb19b8146bb10455446ad133 Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Mon, 23 Sep 2024 10:14:50 +0530 Subject: [PATCH] chore: trimming down BaseProtocolCore --- packages/core/src/lib/base_protocol.ts | 40 ++++++++++++-------------- packages/interfaces/src/protocols.ts | 5 +--- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/packages/core/src/lib/base_protocol.ts b/packages/core/src/lib/base_protocol.ts index fd270b4724..d59ddce86b 100644 --- a/packages/core/src/lib/base_protocol.ts +++ b/packages/core/src/lib/base_protocol.ts @@ -1,5 +1,5 @@ import type { Libp2p } from "@libp2p/interface"; -import type { Peer, PeerStore, Stream } from "@libp2p/interface"; +import type { Peer, Stream } from "@libp2p/interface"; import type { IBaseProtocolCore, Libp2pComponents, @@ -8,7 +8,6 @@ import type { import { Logger, pubsubTopicsToShardInfo } from "@waku/utils"; import { getConnectedPeersForProtocolAndShard, - getPeersForProtocol, sortPeersByLatency } from "@waku/utils/libp2p"; @@ -50,30 +49,27 @@ export class BaseProtocol implements IBaseProtocolCore { return this.streamManager.getStream(peer); } - public get peerStore(): PeerStore { - return this.components.peerStore; - } - + //TODO: move to SDK /** * 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. */ - public async allPeers(): Promise { - return getPeersForProtocol(this.peerStore, [this.multicodec]); - } + // public async allPeers(): Promise { + // return getPeersForProtocol(this.peerStore, [this.multicodec]); + // } - public async connectedPeers(): Promise { - const peers = await this.allPeers(); - return peers.filter((peer) => { - const connections = this.components.connectionManager.getConnections( - peer.id - ); - return connections.some((c) => - c.streams.some((s) => s.protocol === this.multicodec) - ); - }); - } + // public async connectedPeers(): Promise { + // const peers = await this.allPeers(); + // return peers.filter((peer) => { + // const connections = this.components.connectionManager.getConnections( + // peer.id + // ); + // return connections.some((c) => + // c.streams.some((s) => s.protocol === this.multicodec) + // ); + // }); + // } /** * Retrieves a list of connected peers that support the protocol. The list is sorted by latency. @@ -99,7 +95,7 @@ export class BaseProtocol implements IBaseProtocolCore { const connectedPeersForProtocolAndShard = await getConnectedPeersForProtocolAndShard( this.components.connectionManager.getConnections(), - this.peerStore, + this.components.peerStore, [this.multicodec], pubsubTopicsToShardInfo(this.pubsubTopics) ); @@ -113,7 +109,7 @@ export class BaseProtocol implements IBaseProtocolCore { // Sort the peers by latency const sortedFilteredPeers = await sortPeersByLatency( - this.peerStore, + this.components.peerStore, filteredPeers ); diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index 11ac6c984f..341f0bf1db 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -1,6 +1,6 @@ import type { Libp2p } from "@libp2p/interface"; import type { PeerId } from "@libp2p/interface"; -import type { Peer, PeerStore } from "@libp2p/interface"; +import type { Peer } from "@libp2p/interface"; import type { CreateLibp2pOptions } from "./libp2p.js"; import type { IDecodedMessage } from "./message.js"; @@ -16,9 +16,6 @@ export enum Protocols { export type IBaseProtocolCore = { multicodec: string; - peerStore: PeerStore; - allPeers: () => Promise; - connectedPeers: () => Promise; addLibp2pEventListener: Libp2p["addEventListener"]; removeLibp2pEventListener: Libp2p["removeEventListener"]; };