@waku/core 0.0.37-7a9850d.0 → 0.0.37-c24842a.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle/index.js +4852 -904
- package/bundle/lib/message/version_0.js +1 -1
- package/bundle/{version_0-9DPFjcJG.js → version_0-BHaZD8Qu.js} +485 -60
- package/dist/.tsbuildinfo +1 -1
- package/dist/lib/connection_manager/connection_limiter.d.ts +46 -0
- package/dist/lib/connection_manager/connection_limiter.js +177 -0
- package/dist/lib/connection_manager/connection_limiter.js.map +1 -0
- package/dist/lib/connection_manager/connection_manager.d.ts +18 -106
- package/dist/lib/connection_manager/connection_manager.js +95 -512
- package/dist/lib/connection_manager/connection_manager.js.map +1 -1
- package/dist/lib/connection_manager/dialer.d.ts +34 -0
- package/dist/lib/connection_manager/dialer.js +135 -0
- package/dist/lib/connection_manager/dialer.js.map +1 -0
- package/dist/lib/connection_manager/discovery_dialer.d.ts +26 -0
- package/dist/lib/connection_manager/discovery_dialer.js +68 -0
- package/dist/lib/connection_manager/discovery_dialer.js.map +1 -0
- package/dist/lib/connection_manager/keep_alive_manager.d.ts +17 -7
- package/dist/lib/connection_manager/keep_alive_manager.js +110 -74
- package/dist/lib/connection_manager/keep_alive_manager.js.map +1 -1
- package/dist/lib/connection_manager/network_monitor.d.ts +36 -0
- package/dist/lib/connection_manager/network_monitor.js +81 -0
- package/dist/lib/connection_manager/network_monitor.js.map +1 -0
- package/dist/lib/connection_manager/shard_reader.d.ts +28 -0
- package/dist/lib/connection_manager/shard_reader.js +70 -0
- package/dist/lib/connection_manager/shard_reader.js.map +1 -0
- package/dist/lib/connection_manager/utils.d.ts +16 -1
- package/dist/lib/connection_manager/utils.js +23 -0
- package/dist/lib/connection_manager/utils.js.map +1 -1
- package/dist/lib/filter/filter.d.ts +2 -3
- package/dist/lib/filter/filter.js +5 -25
- package/dist/lib/filter/filter.js.map +1 -1
- package/dist/lib/light_push/light_push.d.ts +2 -3
- package/dist/lib/light_push/light_push.js +1 -3
- package/dist/lib/light_push/light_push.js.map +1 -1
- package/dist/lib/metadata/metadata.d.ts +2 -2
- package/dist/lib/metadata/metadata.js +14 -8
- package/dist/lib/metadata/metadata.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/connection_manager/connection_limiter.ts +292 -0
- package/src/lib/connection_manager/connection_manager.ts +112 -673
- package/src/lib/connection_manager/dialer.ts +192 -0
- package/src/lib/connection_manager/discovery_dialer.ts +104 -0
- package/src/lib/connection_manager/keep_alive_manager.ts +154 -87
- package/src/lib/connection_manager/network_monitor.ts +112 -0
- package/src/lib/connection_manager/shard_reader.ts +134 -0
- package/src/lib/connection_manager/utils.ts +27 -1
- package/src/lib/filter/filter.ts +3 -28
- package/src/lib/light_push/light_push.ts +1 -5
- package/src/lib/metadata/metadata.ts +13 -12
@@ -0,0 +1,177 @@
|
|
1
|
+
import { CONNECTION_LOCKED_TAG, Tags } from "@waku/interfaces";
|
2
|
+
import { Logger } from "@waku/utils";
|
3
|
+
const log = new Logger("connection-limiter");
|
4
|
+
const DEFAULT_CONNECTION_MONITOR_INTERVAL = 5 * 1_000;
|
5
|
+
/**
|
6
|
+
* This class is responsible for limiting the number of connections to peers.
|
7
|
+
* It also dials all known peers because libp2p might have emitted `peer:discovery` before initialization
|
8
|
+
* and listen to `peer:connect` and `peer:disconnect` events to manage connections.
|
9
|
+
*/
|
10
|
+
export class ConnectionLimiter {
|
11
|
+
libp2p;
|
12
|
+
events;
|
13
|
+
networkMonitor;
|
14
|
+
dialer;
|
15
|
+
connectionMonitorInterval = null;
|
16
|
+
options;
|
17
|
+
constructor(options) {
|
18
|
+
this.libp2p = options.libp2p;
|
19
|
+
this.events = options.events;
|
20
|
+
this.networkMonitor = options.networkMonitor;
|
21
|
+
this.dialer = options.dialer;
|
22
|
+
this.options = options.options;
|
23
|
+
this.onWakuConnectionEvent = this.onWakuConnectionEvent.bind(this);
|
24
|
+
this.onDisconnectedEvent = this.onDisconnectedEvent.bind(this);
|
25
|
+
}
|
26
|
+
start() {
|
27
|
+
// dial all known peers because libp2p might have emitted `peer:discovery` before initialization
|
28
|
+
void this.dialPeersFromStore();
|
29
|
+
if (this.options.enableAutoRecovery &&
|
30
|
+
this.connectionMonitorInterval === null) {
|
31
|
+
this.connectionMonitorInterval = setInterval(() => void this.maintainConnections(), DEFAULT_CONNECTION_MONITOR_INTERVAL);
|
32
|
+
}
|
33
|
+
this.events.addEventListener("waku:connection", this.onWakuConnectionEvent);
|
34
|
+
/**
|
35
|
+
* NOTE: Event is not being emitted on closing nor losing a connection.
|
36
|
+
* @see https://github.com/libp2p/js-libp2p/issues/939
|
37
|
+
* @see https://github.com/status-im/js-waku/issues/252
|
38
|
+
*
|
39
|
+
* >This event will be triggered anytime we are disconnected from another peer,
|
40
|
+
* >regardless of the circumstances of that disconnection.
|
41
|
+
* >If we happen to have multiple connections to a peer,
|
42
|
+
* >this event will **only** be triggered when the last connection is closed.
|
43
|
+
* @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100
|
44
|
+
*/
|
45
|
+
this.libp2p.addEventListener("peer:disconnect", this.onDisconnectedEvent);
|
46
|
+
}
|
47
|
+
stop() {
|
48
|
+
this.events.removeEventListener("waku:connection", this.onWakuConnectionEvent);
|
49
|
+
this.libp2p.removeEventListener("peer:disconnect", this.onDisconnectedEvent);
|
50
|
+
if (this.connectionMonitorInterval) {
|
51
|
+
clearInterval(this.connectionMonitorInterval);
|
52
|
+
this.connectionMonitorInterval = null;
|
53
|
+
}
|
54
|
+
}
|
55
|
+
onWakuConnectionEvent() {
|
56
|
+
if (!this.options.enableAutoRecovery) {
|
57
|
+
log.info(`Auto recovery is disabled, skipping`);
|
58
|
+
return;
|
59
|
+
}
|
60
|
+
if (this.networkMonitor.isBrowserConnected()) {
|
61
|
+
void this.dialPeersFromStore();
|
62
|
+
}
|
63
|
+
}
|
64
|
+
async maintainConnections() {
|
65
|
+
await this.maintainConnectionsCount();
|
66
|
+
await this.maintainBootstrapConnections();
|
67
|
+
}
|
68
|
+
async onDisconnectedEvent() {
|
69
|
+
if (this.libp2p.getConnections().length === 0) {
|
70
|
+
log.info(`No connections, dialing peers from store`);
|
71
|
+
await this.dialPeersFromStore();
|
72
|
+
}
|
73
|
+
}
|
74
|
+
async maintainConnectionsCount() {
|
75
|
+
log.info(`Maintaining connections count`);
|
76
|
+
const connections = this.libp2p.getConnections();
|
77
|
+
if (connections.length <= this.options.maxConnections) {
|
78
|
+
log.info(`Node has less than max connections ${this.options.maxConnections}, trying to dial more peers`);
|
79
|
+
const peers = await this.getPrioritizedPeers();
|
80
|
+
if (peers.length === 0) {
|
81
|
+
log.info(`No peers to dial, node is utilizing all known peers`);
|
82
|
+
return;
|
83
|
+
}
|
84
|
+
const promises = peers
|
85
|
+
.slice(0, this.options.maxConnections - connections.length)
|
86
|
+
.map((p) => this.dialer.dial(p.id));
|
87
|
+
await Promise.all(promises);
|
88
|
+
return;
|
89
|
+
}
|
90
|
+
log.info(`Node has more than max connections ${this.options.maxConnections}, dropping connections`);
|
91
|
+
try {
|
92
|
+
const connectionsToDrop = connections
|
93
|
+
.filter((c) => !c.tags.includes(CONNECTION_LOCKED_TAG))
|
94
|
+
.slice(this.options.maxConnections);
|
95
|
+
if (connectionsToDrop.length === 0) {
|
96
|
+
log.info(`No connections to drop, skipping`);
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
const promises = connectionsToDrop.map((c) => this.libp2p.hangUp(c.remotePeer));
|
100
|
+
await Promise.all(promises);
|
101
|
+
log.info(`Dropped ${connectionsToDrop.length} connections`);
|
102
|
+
}
|
103
|
+
catch (error) {
|
104
|
+
log.error(`Unexpected error while maintaining connections`, error);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
async maintainBootstrapConnections() {
|
108
|
+
log.info(`Maintaining bootstrap connections`);
|
109
|
+
const bootstrapPeers = await this.getBootstrapPeers();
|
110
|
+
if (bootstrapPeers.length <= this.options.maxBootstrapPeers) {
|
111
|
+
return;
|
112
|
+
}
|
113
|
+
try {
|
114
|
+
const peersToDrop = bootstrapPeers.slice(this.options.maxBootstrapPeers);
|
115
|
+
log.info(`Dropping ${peersToDrop.length} bootstrap connections because node has more than max bootstrap connections ${this.options.maxBootstrapPeers}`);
|
116
|
+
const promises = peersToDrop.map((p) => this.libp2p.hangUp(p.id));
|
117
|
+
await Promise.all(promises);
|
118
|
+
log.info(`Dropped ${peersToDrop.length} bootstrap connections`);
|
119
|
+
}
|
120
|
+
catch (error) {
|
121
|
+
log.error(`Unexpected error while maintaining bootstrap connections`, error);
|
122
|
+
}
|
123
|
+
}
|
124
|
+
async dialPeersFromStore() {
|
125
|
+
log.info(`Dialing peers from store`);
|
126
|
+
try {
|
127
|
+
const peers = await this.getPrioritizedPeers();
|
128
|
+
if (peers.length === 0) {
|
129
|
+
log.info(`No peers to dial, skipping`);
|
130
|
+
return;
|
131
|
+
}
|
132
|
+
const promises = peers.map((p) => this.dialer.dial(p.id));
|
133
|
+
log.info(`Dialing ${peers.length} peers from store`);
|
134
|
+
await Promise.all(promises);
|
135
|
+
log.info(`Dialed ${promises.length} peers from store`);
|
136
|
+
}
|
137
|
+
catch (error) {
|
138
|
+
log.error(`Unexpected error while dialing peer store peers`, error);
|
139
|
+
}
|
140
|
+
}
|
141
|
+
/**
|
142
|
+
* Returns a list of peers ordered by priority:
|
143
|
+
* - bootstrap peers
|
144
|
+
* - peers from peer exchange
|
145
|
+
* - peers from local store (last because we are not sure that locally stored information is up to date)
|
146
|
+
*/
|
147
|
+
async getPrioritizedPeers() {
|
148
|
+
const allPeers = await this.libp2p.peerStore.all();
|
149
|
+
const allConnections = this.libp2p.getConnections();
|
150
|
+
log.info(`Found ${allPeers.length} peers in store, and found ${allConnections.length} connections`);
|
151
|
+
const notConnectedPeers = allPeers.filter((p) => !allConnections.some((c) => c.remotePeer.equals(p.id)) &&
|
152
|
+
p.addresses.some((a) => a.multiaddr.toString().includes("wss") ||
|
153
|
+
a.multiaddr.toString().includes("ws")));
|
154
|
+
const bootstrapPeers = notConnectedPeers.filter((p) => p.tags.has(Tags.BOOTSTRAP));
|
155
|
+
const peerExchangePeers = notConnectedPeers.filter((p) => p.tags.has(Tags.PEER_EXCHANGE));
|
156
|
+
const localStorePeers = notConnectedPeers.filter((p) => p.tags.has(Tags.LOCAL));
|
157
|
+
return [...bootstrapPeers, ...peerExchangePeers, ...localStorePeers];
|
158
|
+
}
|
159
|
+
async getBootstrapPeers() {
|
160
|
+
const peers = await Promise.all(this.libp2p
|
161
|
+
.getConnections()
|
162
|
+
.map((conn) => conn.remotePeer)
|
163
|
+
.map((id) => this.getPeer(id)));
|
164
|
+
const bootstrapPeers = peers.filter((peer) => peer && peer.tags.has(Tags.BOOTSTRAP));
|
165
|
+
return bootstrapPeers;
|
166
|
+
}
|
167
|
+
async getPeer(peerId) {
|
168
|
+
try {
|
169
|
+
return await this.libp2p.peerStore.get(peerId);
|
170
|
+
}
|
171
|
+
catch (error) {
|
172
|
+
log.error(`Failed to get peer ${peerId}, error: ${error}`);
|
173
|
+
return null;
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
//# sourceMappingURL=connection_limiter.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"connection_limiter.js","sourceRoot":"","sources":["../../../src/lib/connection_manager/connection_limiter.ts"],"names":[],"mappings":"AACA,OAAO,EACL,qBAAqB,EAKrB,IAAI,EACL,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAKrC,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,oBAAoB,CAAC,CAAC;AAE7C,MAAM,mCAAmC,GAAG,CAAC,GAAG,KAAK,CAAC;AAetD;;;;GAIG;AACH,MAAM,OAAO,iBAAiB;IACX,MAAM,CAAS;IACf,MAAM,CAAoB;IAC1B,cAAc,CAAiB;IAC/B,MAAM,CAAS;IAExB,yBAAyB,GAA0B,IAAI,C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K,EAAE,CAAC,CAAC;YAC3D,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
|
@@ -1,119 +1,31 @@
|
|
1
|
-
import { type
|
1
|
+
import { type Peer, type PeerId, type Stream } from "@libp2p/interface";
|
2
2
|
import { MultiaddrInput } from "@multiformats/multiaddr";
|
3
|
-
import { ConnectionManagerOptions, IConnectionManager,
|
3
|
+
import { ConnectionManagerOptions, IConnectionManager, IRelay, IWakuEventEmitter, NetworkConfig } from "@waku/interfaces";
|
4
4
|
import { Libp2p } from "@waku/interfaces";
|
5
5
|
type ConnectionManagerConstructorOptions = {
|
6
6
|
libp2p: Libp2p;
|
7
|
-
|
7
|
+
events: IWakuEventEmitter;
|
8
|
+
networkConfig: NetworkConfig;
|
8
9
|
relay?: IRelay;
|
9
10
|
config?: Partial<ConnectionManagerOptions>;
|
10
11
|
};
|
11
|
-
export declare class ConnectionManager
|
12
|
-
readonly
|
13
|
-
private
|
12
|
+
export declare class ConnectionManager implements IConnectionManager {
|
13
|
+
private readonly keepAliveManager;
|
14
|
+
private readonly discoveryDialer;
|
15
|
+
private readonly dialer;
|
16
|
+
private readonly shardReader;
|
17
|
+
private readonly networkMonitor;
|
18
|
+
private readonly connectionLimiter;
|
14
19
|
private options;
|
15
20
|
private libp2p;
|
16
|
-
private dialAttemptsForPeer;
|
17
|
-
private dialErrorsForPeer;
|
18
|
-
private currentActiveParallelDialCount;
|
19
|
-
private pendingPeerDialQueue;
|
20
|
-
private isP2PNetworkConnected;
|
21
|
-
isConnected(): boolean;
|
22
|
-
stop(): void;
|
23
|
-
dropConnection(peerId: PeerId): Promise<void>;
|
24
|
-
getPeersByDiscovery(): Promise<PeersByDiscoveryResult>;
|
25
21
|
constructor(options: ConnectionManagerConstructorOptions);
|
22
|
+
start(): void;
|
23
|
+
stop(): void;
|
24
|
+
isConnected(): boolean;
|
25
|
+
dial(peer: PeerId | MultiaddrInput, protocolCodecs: string[]): Promise<Stream>;
|
26
|
+
hangUp(peer: PeerId | MultiaddrInput): Promise<boolean>;
|
26
27
|
getConnectedPeers(codec?: string): Promise<Peer[]>;
|
27
|
-
|
28
|
-
|
29
|
-
/**
|
30
|
-
* Attempts to establish a connection with a peer and set up specified protocols.
|
31
|
-
* The method handles both PeerId and Multiaddr inputs, manages connection attempts,
|
32
|
-
* and maintains the connection state.
|
33
|
-
*
|
34
|
-
* The dialing process includes:
|
35
|
-
* 1. Converting input to dialable peer info
|
36
|
-
* 2. Managing parallel dial attempts
|
37
|
-
* 3. Attempting to establish protocol-specific connections
|
38
|
-
* 4. Handling connection failures and retries
|
39
|
-
* 5. Updating the peer store and connection state
|
40
|
-
*
|
41
|
-
* @param {PeerId | MultiaddrInput} peer - The peer to connect to, either as a PeerId or multiaddr
|
42
|
-
* @param {string[]} [protocolCodecs] - Optional array of protocol-specific codec strings to establish
|
43
|
-
* (e.g., for LightPush, Filter, Store protocols)
|
44
|
-
*
|
45
|
-
* @throws {Error} If the multiaddr is missing a peer ID
|
46
|
-
* @throws {Error} If the maximum dial attempts are reached and the peer cannot be dialed
|
47
|
-
* @throws {Error} If there's an error deleting an undialable peer from the peer store
|
48
|
-
*
|
49
|
-
* @example
|
50
|
-
* ```typescript
|
51
|
-
* // Dial using PeerId
|
52
|
-
* await connectionManager.dialPeer(peerId);
|
53
|
-
*
|
54
|
-
* // Dial using multiaddr with specific protocols
|
55
|
-
* await connectionManager.dialPeer(multiaddr, [
|
56
|
-
* "/vac/waku/relay/2.0.0",
|
57
|
-
* "/vac/waku/lightpush/2.0.0-beta1"
|
58
|
-
* ]);
|
59
|
-
* ```
|
60
|
-
*
|
61
|
-
* @remarks
|
62
|
-
* - The method implements exponential backoff through multiple dial attempts
|
63
|
-
* - Maintains a queue for parallel dial attempts (limited by maxParallelDials)
|
64
|
-
* - Integrates with the KeepAliveManager for connection maintenance
|
65
|
-
* - Updates the peer store and connection state after successful/failed attempts
|
66
|
-
* - If all dial attempts fail, triggers DNS discovery as a fallback
|
67
|
-
*/
|
68
|
-
dialPeer(peer: PeerId | MultiaddrInput): Promise<Connection>;
|
69
|
-
/**
|
70
|
-
* Dial a peer with specific protocols.
|
71
|
-
* This method is a raw proxy to the libp2p dialProtocol method.
|
72
|
-
* @param peer - The peer to connect to, either as a PeerId or multiaddr
|
73
|
-
* @param protocolCodecs - Optional array of protocol-specific codec strings to establish
|
74
|
-
* @returns A stream to the peer
|
75
|
-
*/
|
76
|
-
rawDialPeerWithProtocols(peer: PeerId | MultiaddrInput, protocolCodecs: string[]): Promise<Stream>;
|
77
|
-
/**
|
78
|
-
* Internal utility to extract a PeerId or Multiaddr from a peer input.
|
79
|
-
* This is used internally by the connection manager to handle different peer input formats.
|
80
|
-
* @internal
|
81
|
-
*/
|
82
|
-
private getDialablePeerInfo;
|
83
|
-
private attemptDnsDiscovery;
|
84
|
-
private processDialQueue;
|
85
|
-
private startPeerDiscoveryListener;
|
86
|
-
private startPeerConnectionListener;
|
87
|
-
private startPeerDisconnectionListener;
|
88
|
-
attemptDial(peerId: PeerId): Promise<void>;
|
89
|
-
private onEventHandlers;
|
90
|
-
/**
|
91
|
-
* Checks if the peer should be dialed based on the following conditions:
|
92
|
-
* 1. If the peer is already connected, don't dial
|
93
|
-
* 2. If the peer is not part of any of the configured pubsub topics, don't dial
|
94
|
-
* 3. If the peer is not dialable based on bootstrap status, don't dial
|
95
|
-
* 4. If the peer is already has an active dial attempt, or has been dialed before, don't dial it
|
96
|
-
* @returns true if the peer should be dialed, false otherwise
|
97
|
-
*/
|
98
|
-
private shouldDialPeer;
|
99
|
-
/**
|
100
|
-
* Checks if the peer is dialable based on the following conditions:
|
101
|
-
* 1. If the peer is a bootstrap peer, it is only dialable if the number of current bootstrap connections is less than the max allowed.
|
102
|
-
* 2. If the peer is not a bootstrap peer
|
103
|
-
*/
|
104
|
-
private isPeerDialableBasedOnBootstrapStatus;
|
105
|
-
private dispatchDiscoveryEvent;
|
106
|
-
/**
|
107
|
-
* Fetches the tag names for a given peer
|
108
|
-
*/
|
109
|
-
private getTagNamesForPeer;
|
110
|
-
isPeerOnSameShard(peerId: PeerId): Promise<boolean>;
|
111
|
-
isPeerOnPubsubTopic(peerId: PeerId, pubsubTopic: string): Promise<boolean>;
|
112
|
-
private getPeerShardInfo;
|
113
|
-
private startNetworkStatusListener;
|
114
|
-
private stopNetworkStatusListener;
|
115
|
-
private setP2PNetworkConnected;
|
116
|
-
private setP2PNetworkDisconnected;
|
117
|
-
private dispatchWakuConnectionEvent;
|
28
|
+
hasShardInfo(peerId: PeerId): Promise<boolean>;
|
29
|
+
isPeerOnTopic(peerId: PeerId, pubsubTopic: string): Promise<boolean>;
|
118
30
|
}
|
119
31
|
export {};
|