From 2edd8564345d28caac85d534f3b1fac3b597f2ea Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 22 Oct 2024 15:21:50 +0200 Subject: [PATCH] use only one peerManager from Waku object --- packages/sdk/src/protocols/base_protocol.ts | 6 +----- packages/sdk/src/protocols/filter/index.ts | 9 ++++++--- packages/sdk/src/protocols/store/index.ts | 22 +++++++++++---------- packages/sdk/src/waku/waku.ts | 8 +++++++- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/packages/sdk/src/protocols/base_protocol.ts b/packages/sdk/src/protocols/base_protocol.ts index 3a7abc1b36..566dda9155 100644 --- a/packages/sdk/src/protocols/base_protocol.ts +++ b/packages/sdk/src/protocols/base_protocol.ts @@ -1,5 +1,4 @@ import type { Peer, PeerId } from "@libp2p/interface"; -import { ConnectionManager } from "@waku/core"; import { BaseProtocol } from "@waku/core/lib/base_protocol"; import { IBaseProtocolSDK, ProtocolUseOptions } from "@waku/interfaces"; import { Logger } from "@waku/utils"; @@ -15,7 +14,6 @@ export const DEFAULT_NUM_PEERS_TO_USE = 2; const DEFAULT_MAINTAIN_PEERS_INTERVAL = 30_000; export class BaseProtocolSDK implements IBaseProtocolSDK { - private peerManager: PeerManager; public readonly numPeersToUse: number; private maintainPeersIntervalId: ReturnType< typeof window.setInterval @@ -24,7 +22,7 @@ export class BaseProtocolSDK implements IBaseProtocolSDK { public constructor( protected core: BaseProtocol, - protected connectionManager: ConnectionManager, + protected peerManager: PeerManager, options: Options ) { this.log = new Logger(`sdk:${core.multicodec}`); @@ -33,8 +31,6 @@ export class BaseProtocolSDK implements IBaseProtocolSDK { const maintainPeersInterval = options?.maintainPeersInterval ?? DEFAULT_MAINTAIN_PEERS_INTERVAL; - this.peerManager = new PeerManager(connectionManager); - this.log.info( `Initializing BaseProtocolSDK with numPeersToUse: ${this.numPeersToUse}, maintainPeersInterval: ${maintainPeersInterval}ms` ); diff --git a/packages/sdk/src/protocols/filter/index.ts b/packages/sdk/src/protocols/filter/index.ts index d5509c2337..1db0ed1d37 100644 --- a/packages/sdk/src/protocols/filter/index.ts +++ b/packages/sdk/src/protocols/filter/index.ts @@ -26,6 +26,7 @@ import { } from "@waku/utils"; import { BaseProtocolSDK } from "../base_protocol.js"; +import { PeerManager } from "../peer_manager.js"; import { DEFAULT_SUBSCRIBE_OPTIONS } from "./constants.js"; import { SubscriptionManager } from "./subscription_manager.js"; @@ -38,8 +39,9 @@ class Filter extends BaseProtocolSDK implements IFilter { private activeSubscriptions = new Map(); public constructor( - connectionManager: ConnectionManager, + private connectionManager: ConnectionManager, private libp2p: Libp2p, + peerManager: PeerManager, private lightPush?: ILightPush, options?: ProtocolCreateOptions ) { @@ -59,7 +61,7 @@ class Filter extends BaseProtocolSDK implements IFilter { connectionManager.pubsubTopics, libp2p ), - connectionManager, + peerManager, { numPeersToUse: options?.numPeersToUse } ); @@ -304,9 +306,10 @@ class Filter extends BaseProtocolSDK implements IFilter { export function wakuFilter( connectionManager: ConnectionManager, + peerManager: PeerManager, lightPush?: ILightPush, init?: ProtocolCreateOptions ): (libp2p: Libp2p) => IFilter { return (libp2p: Libp2p) => - new Filter(connectionManager, libp2p, lightPush, init); + new Filter(connectionManager, libp2p, peerManager, lightPush, init); } diff --git a/packages/sdk/src/protocols/store/index.ts b/packages/sdk/src/protocols/store/index.ts index efd58033f8..36249a0f2f 100644 --- a/packages/sdk/src/protocols/store/index.ts +++ b/packages/sdk/src/protocols/store/index.ts @@ -11,6 +11,7 @@ import { messageHash } from "@waku/message-hash"; import { ensurePubsubTopicIsConfigured, isDefined, Logger } from "@waku/utils"; import { BaseProtocolSDK } from "../base_protocol.js"; +import { PeerManager } from "../peer_manager.js"; const DEFAULT_NUM_PEERS = 1; @@ -23,14 +24,14 @@ const log = new Logger("waku:store:sdk"); export class Store extends BaseProtocolSDK implements IStore { public readonly protocol: StoreCore; - public constructor(connectionManager: ConnectionManager, libp2p: Libp2p) { - super( - new StoreCore(connectionManager.pubsubTopics, libp2p), - connectionManager, - { - numPeersToUse: DEFAULT_NUM_PEERS - } - ); + public constructor( + private connectionManager: ConnectionManager, + libp2p: Libp2p, + peerManager: PeerManager + ) { + super(new StoreCore(connectionManager.pubsubTopics, libp2p), peerManager, { + numPeersToUse: DEFAULT_NUM_PEERS + }); this.protocol = this.core as StoreCore; } @@ -236,9 +237,10 @@ export class Store extends BaseProtocolSDK implements IStore { * @returns A function that takes a Libp2p instance and returns a StoreSDK instance. */ export function wakuStore( - connectionManager: ConnectionManager + connectionManager: ConnectionManager, + peerManager: PeerManager ): (libp2p: Libp2p) => IStore { return (libp2p: Libp2p) => { - return new Store(connectionManager, libp2p); + return new Store(connectionManager, libp2p, peerManager); }; } diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index 76bbad601c..90572896d9 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -18,6 +18,7 @@ import { Logger } from "@waku/utils"; import { wakuFilter } from "../protocols/filter/index.js"; import { wakuLightPush } from "../protocols/light_push/index.js"; +import { PeerManager } from "../protocols/peer_manager.js"; import { wakuStore } from "../protocols/store/index.js"; import { ReliabilityMonitorManager } from "../reliability_monitor/index.js"; @@ -54,6 +55,8 @@ export class WakuNode implements IWaku { public connectionManager: ConnectionManager; public readonly health: IHealthManager; + private readonly peerManager: PeerManager; + public constructor( public readonly pubsubTopics: PubsubTopic[], options: CreateWakuNodeOptions, @@ -80,10 +83,12 @@ export class WakuNode implements IWaku { config: options?.connectionManager }); + this.peerManager = new PeerManager(this.connectionManager); + this.health = getHealthManager(); if (protocolsEnabled.store) { - const store = wakuStore(this.connectionManager); + const store = wakuStore(this.connectionManager, this.peerManager); this.store = store(libp2p); } @@ -95,6 +100,7 @@ export class WakuNode implements IWaku { if (protocolsEnabled.filter) { const filter = wakuFilter( this.connectionManager, + this.peerManager, this.lightPush, options );