Skip to content

Commit

Permalink
use only one peerManager from Waku object
Browse files Browse the repository at this point in the history
  • Loading branch information
weboko committed Oct 22, 2024
1 parent 9bdc2af commit 2edd856
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
6 changes: 1 addition & 5 deletions packages/sdk/src/protocols/base_protocol.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
Expand All @@ -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}`);
Expand All @@ -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`
);
Expand Down
9 changes: 6 additions & 3 deletions packages/sdk/src/protocols/filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
) {
Expand All @@ -59,7 +61,7 @@ class Filter extends BaseProtocolSDK implements IFilter {
connectionManager.pubsubTopics,
libp2p
),
connectionManager,
peerManager,
{ numPeersToUse: options?.numPeersToUse }
);

Expand Down Expand Up @@ -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);
}
22 changes: 12 additions & 10 deletions packages/sdk/src/protocols/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
};
}
8 changes: 7 additions & 1 deletion packages/sdk/src/waku/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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,
Expand All @@ -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);
}

Expand All @@ -95,6 +100,7 @@ export class WakuNode implements IWaku {
if (protocolsEnabled.filter) {
const filter = wakuFilter(
this.connectionManager,
this.peerManager,
this.lightPush,
options
);
Expand Down

0 comments on commit 2edd856

Please sign in to comment.