mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-20 16:18:21 +00:00
use only one peerManager from Waku object
This commit is contained in:
parent
9bdc2af13b
commit
2edd856434
@ -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`
|
||||
);
|
||||
|
@ -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<string, SubscriptionManager>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
@ -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
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user