diff --git a/packages/core/src/lib/connection_manager.ts b/packages/core/src/lib/connection_manager.ts index fda6ff06bb..d8697784f9 100644 --- a/packages/core/src/lib/connection_manager.ts +++ b/packages/core/src/lib/connection_manager.ts @@ -10,7 +10,6 @@ import { IConnectionStateEvents, IPeersByDiscoveryEvents, IRelay, - KeepAliveOptions, PeersByDiscoveryResult, PubsubTopic, ShardInfo @@ -23,13 +22,15 @@ import { KeepAliveManager } from "./keep_alive_manager.js"; const log = new Logger("connection-manager"); -export const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1; -export const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3; -export const DEFAULT_MAX_PARALLEL_DIALS = 3; +const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1; +const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3; +const DEFAULT_MAX_PARALLEL_DIALS = 3; + +const DEFAULT_PING_KEEP_ALIVE_SEC = 5 * 60; +const DEFAULT_RELAY_KEEP_ALIVE_SEC = 5 * 60; type ConnectionManagerConstructorOptions = { libp2p: Libp2p; - keepAliveOptions: KeepAliveOptions; pubsubTopics: PubsubTopic[]; relay?: IRelay; config?: Partial; @@ -151,13 +152,18 @@ export class ConnectionManager maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER, maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED, maxParallelDials: DEFAULT_MAX_PARALLEL_DIALS, + pingKeepAlive: DEFAULT_PING_KEEP_ALIVE_SEC, + relayKeepAlive: DEFAULT_RELAY_KEEP_ALIVE_SEC, ...options.config }; this.keepAliveManager = new KeepAliveManager({ relay: options.relay, libp2p: options.libp2p, - options: options.keepAliveOptions + options: { + pingKeepAlive: this.options.pingKeepAlive, + relayKeepAlive: this.options.relayKeepAlive + } }); this.startEventListeners() diff --git a/packages/core/src/lib/keep_alive_manager.ts b/packages/core/src/lib/keep_alive_manager.ts index 27754b6b38..3f606acd9b 100644 --- a/packages/core/src/lib/keep_alive_manager.ts +++ b/packages/core/src/lib/keep_alive_manager.ts @@ -1,14 +1,18 @@ import type { PeerId } from "@libp2p/interface"; import type { IRelay, Libp2p, PeerIdStr } from "@waku/interfaces"; -import type { KeepAliveOptions } from "@waku/interfaces"; import { Logger, pubsubTopicToSingleShardInfo } from "@waku/utils"; import { utf8ToBytes } from "@waku/utils/bytes"; import { createEncoder } from "./message/version_0.js"; -export const RelayPingContentTopic = "/relay-ping/1/ping/null"; +const RelayPingContentTopic = "/relay-ping/1/ping/null"; const log = new Logger("keep-alive"); +type KeepAliveOptions = { + pingKeepAlive: number; + relayKeepAlive: number; +}; + type CreateKeepAliveManagerOptions = { options: KeepAliveOptions; libp2p: Libp2p; diff --git a/packages/interfaces/src/connection_manager.ts b/packages/interfaces/src/connection_manager.ts index 1644f75140..f0810e4736 100644 --- a/packages/interfaces/src/connection_manager.ts +++ b/packages/interfaces/src/connection_manager.ts @@ -8,22 +8,44 @@ export enum Tags { LOCAL = "local-peer-cache" } -export interface ConnectionManagerOptions { +export type ConnectionManagerOptions = { /** - * Number of attempts before a peer is considered non-dialable - * This is used to not spam a peer with dial attempts when it is not dialable + * Number of attempts before a peer is considered non-dialable. + * This is used to not spam a peer with dial attempts when it is not dialable. + * + * @default 3 */ maxDialAttemptsForPeer: number; + /** - * Max number of bootstrap peers allowed to be connected to, initially - * This is used to increase intention of dialing non-bootstrap peers, found using other discovery mechanisms (like Peer Exchange) + * Max number of bootstrap peers allowed to be connected to initially. + * This is used to increase intention of dialing non-bootstrap peers, found using other discovery mechanisms (like Peer Exchange). + * + * @default 1 */ maxBootstrapPeersAllowed: number; + /** - * Max number of parallel dials allowed + * Max number of parallel dials allowed. + * + * @default 3 */ maxParallelDials: number; -} + + /** + * Keep alive libp2p pings interval in seconds. + * + * @default 300 seconds + */ + pingKeepAlive: number; + + /** + * Gossip sub specific keep alive interval in seconds. + * + * @default 300 seconds + */ + relayKeepAlive: number; +}; export enum EPeersByDiscoveryEvents { PEER_DISCOVERY_BOOTSTRAP = "peer:discovery:bootstrap", diff --git a/packages/interfaces/src/index.ts b/packages/interfaces/src/index.ts index 8cfe38114f..ed2cb5240a 100644 --- a/packages/interfaces/src/index.ts +++ b/packages/interfaces/src/index.ts @@ -12,7 +12,6 @@ export * from "./sender.js"; export * from "./receiver.js"; export * from "./misc.js"; export * from "./libp2p.js"; -export * from "./keep_alive_manager.js"; export * from "./dns_discovery.js"; export * from "./metadata.js"; export * from "./constants.js"; diff --git a/packages/interfaces/src/keep_alive_manager.ts b/packages/interfaces/src/keep_alive_manager.ts deleted file mode 100644 index f6d7791d10..0000000000 --- a/packages/interfaces/src/keep_alive_manager.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface KeepAliveOptions { - pingKeepAlive: number; - relayKeepAlive: number; -} diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index 74462efa13..e18ca6592a 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -2,6 +2,7 @@ import type { Libp2p } from "@libp2p/interface"; import type { PeerId } from "@libp2p/interface"; import type { Peer } from "@libp2p/interface"; +import type { ConnectionManagerOptions } from "./connection_manager.js"; import type { CreateLibp2pOptions } from "./libp2p.js"; import type { IDecodedMessage } from "./message.js"; import { ThisAndThat, ThisOrThat } from "./misc.js"; @@ -58,6 +59,7 @@ export type ProtocolCreateOptions = { * See [Waku v2 Topic Usage Recommendations](https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md#content-topics) for details. * You cannot add or remove content topics after initialization of the node. */ + /** * Configuration for determining the network in use. * Network configuration refers to the shards and clusters used in the network. @@ -76,6 +78,7 @@ export type ProtocolCreateOptions = { * @default { clusterId: 1, shards: [0, 1, 2, 3, 4, 5, 6, 7] } */ networkConfig?: NetworkConfig; + /** * You can pass options to the `Libp2p` instance used by {@link @waku/sdk!WakuNode} using the `libp2p` property. * This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) @@ -84,28 +87,38 @@ export type ProtocolCreateOptions = { * Notes that some values are overridden by {@link @waku/sdk!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 2. + * + * @default 2. */ 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) * This is only used for test purposes to not run out of entropy during CI runs. */ staticNoiseKey?: Uint8Array; + /** * Use recommended bootstrap method to discovery and connect to new nodes. */ defaultBootstrap?: boolean; + /** * List of peers to use to bootstrap the node. Ignored if defaultBootstrap is set to true. */ bootstrapPeers?: string[]; + + /** + * Configuration for connection manager. If not specified - default values are applied. + */ + connectionManager?: Partial; }; export type Callback = ( diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index b97244950d..0f492857b0 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -23,28 +23,12 @@ import { ReliabilityMonitorManager } from "../reliability_monitor/index.js"; import { waitForRemotePeer } from "./wait_for_remote_peer.js"; -export const DefaultPingKeepAliveValueSecs = 5 * 60; -export const DefaultRelayKeepAliveValueSecs = 5 * 60; export const DefaultUserAgent = "js-waku"; export const DefaultPingMaxInboundStreams = 10; const log = new Logger("waku"); export interface WakuOptions { - /** - * Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0` - * request to each peer after the set number of seconds. Set to 0 to disable. - * - * @default {@link @waku/core.DefaultPingKeepAliveValueSecs} - */ - pingKeepAlive?: number; - /** - * Set keep alive frequency in seconds: Waku will send a ping message over - * relay to each peer after the set number of seconds. Set to 0 to disable. - * - * @default {@link @waku/core.DefaultRelayKeepAliveValueSecs} - */ - relayKeepAlive?: number; /** * Set the user agent string to be used in identification of the node. * @default {@link @waku/core.DefaultUserAgent} @@ -87,19 +71,13 @@ export class WakuNode implements IWaku { ...protocolsEnabled }; - const pingKeepAlive = - options.pingKeepAlive || DefaultPingKeepAliveValueSecs; - const relayKeepAlive = this.relay - ? options.relayKeepAlive || DefaultRelayKeepAliveValueSecs - : 0; - const peerId = this.libp2p.peerId.toString(); this.connectionManager = new ConnectionManager({ libp2p, - keepAliveOptions: { pingKeepAlive, relayKeepAlive }, + relay: this.relay, pubsubTopics: this.pubsubTopics, - relay: this.relay + config: options?.connectionManager }); this.health = getHealthManager();