diff --git a/packages/core/src/lib/base_protocol.ts b/packages/core/src/lib/base_protocol.ts index 36b2d4860a..b96e8c208c 100644 --- a/packages/core/src/lib/base_protocol.ts +++ b/packages/core/src/lib/base_protocol.ts @@ -17,6 +17,8 @@ import { import { filterPeersByDiscovery } from "./filterPeers.js"; import { StreamManager } from "./stream_manager.js"; +const DEFAULT_NUM_PEERS_TO_USE = 3; + /** * A class with predefined helpers, to be used as a base to implement Waku * Protocols. @@ -24,7 +26,7 @@ import { StreamManager } from "./stream_manager.js"; export class BaseProtocol implements IBaseProtocol { public readonly addLibp2pEventListener: Libp2p["addEventListener"]; public readonly removeLibp2pEventListener: Libp2p["removeEventListener"]; - readonly NUM_PEERS_TO_USE = 3; + readonly numPeersTouse: number; protected streamManager: StreamManager; protected pubsubTopics: PubsubTopic[]; @@ -36,6 +38,8 @@ export class BaseProtocol implements IBaseProtocol { ) { this.pubsubTopics = this.initializePubsubTopic(options); + this.numPeersTouse = options?.numPeersToUse ?? DEFAULT_NUM_PEERS_TO_USE; + this.addLibp2pEventListener = components.events.addEventListener.bind( components.events ); diff --git a/packages/core/src/lib/filter/index.ts b/packages/core/src/lib/filter/index.ts index 2e043d2906..85366489c7 100644 --- a/packages/core/src/lib/filter/index.ts +++ b/packages/core/src/lib/filter/index.ts @@ -385,7 +385,7 @@ class Filter extends BaseProtocol implements IReceiver { const peers = await this.getPeers({ maxBootstrapPeers: 1, - numPeers: this.NUM_PEERS_TO_USE + numPeers: this.numPeersTouse }); if (peers.length === 0) { throw new Error("No peer found to initiate subscription."); diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index 6cab8ff8b1..96e8c8f2ce 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -104,7 +104,7 @@ class LightPush extends BaseProtocol implements ILightPush { const peers = await this.getPeers({ maxBootstrapPeers: 1, - numPeers: this.NUM_PEERS_TO_USE + numPeers: this.numPeersTouse }); if (!peers.length) { diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index 712ec9fe8e..0d30c230e9 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -69,6 +69,14 @@ export type ProtocolCreateOptions = { * Notes that some values are overridden by {@link @waku/core!WakuNode} to ensure it implements the Waku protocol. */ libp2p?: Partial; + /** + * Number of peers to connect to, for the usage of the protocol. + * This is used by: + * - Light Push to send messages, + * - Filter to retrieve messages. + * Defaults to 3. + */ + numPeersToUse?: number; /** * Byte array used as key for the noise protocol used for connection encryption * by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)