@waku/core 0.0.33-f599932.0 → 0.0.33
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/CHANGELOG.md +22 -0
- package/bundle/{base_protocol-CS0EDeEY.js → base_protocol-Dzv-QHPR.js} +93 -78
- package/bundle/{index-BVysxsMu.js → index-BIW3qNYx.js} +19 -12
- package/bundle/index.js +45 -419
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-C5ObpJ_0.js → version_0-CdmZMfkQ.js} +10 -4
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +1 -1
- package/dist/lib/base_protocol.js +1 -5
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/filter/index.d.ts +1 -2
- package/dist/lib/filter/index.js +2 -5
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/stream_manager/stream_manager.d.ts +12 -9
- package/dist/lib/stream_manager/stream_manager.js +87 -56
- package/dist/lib/stream_manager/stream_manager.js.map +1 -1
- package/dist/lib/stream_manager/utils.d.ts +1 -1
- package/dist/lib/stream_manager/utils.js +5 -17
- package/dist/lib/stream_manager/utils.js.map +1 -1
- package/package.json +126 -1
- package/src/index.ts +1 -3
- package/src/lib/base_protocol.ts +1 -7
- package/src/lib/filter/index.ts +2 -10
- package/src/lib/stream_manager/stream_manager.ts +124 -66
- package/src/lib/stream_manager/utils.ts +5 -17
- package/dist/lib/wait_for_remote_peer.d.ts +0 -22
- package/dist/lib/wait_for_remote_peer.js +0 -142
- package/dist/lib/wait_for_remote_peer.js.map +0 -1
- package/src/lib/wait_for_remote_peer.ts +0 -200
@@ -1,115 +1,173 @@
|
|
1
|
-
import type { PeerUpdate, Stream } from "@libp2p/interface";
|
2
|
-
import type {
|
3
|
-
import { Libp2p } from "@waku/interfaces";
|
1
|
+
import type { Peer, PeerId, PeerUpdate, Stream } from "@libp2p/interface";
|
2
|
+
import type { Libp2p } from "@waku/interfaces";
|
4
3
|
import { Logger } from "@waku/utils";
|
5
4
|
|
6
|
-
import {
|
5
|
+
import { selectOpenConnection } from "./utils.js";
|
7
6
|
|
8
|
-
const
|
9
|
-
const RETRY_BACKOFF_BASE = 1_000;
|
10
|
-
const MAX_RETRIES = 3;
|
7
|
+
const STREAM_LOCK_KEY = "consumed";
|
11
8
|
|
12
9
|
export class StreamManager {
|
13
|
-
private readonly streamPool: Map<string, Promise<Stream | void>>;
|
14
10
|
private readonly log: Logger;
|
15
11
|
|
12
|
+
private ongoingCreation: Set<string> = new Set();
|
13
|
+
private streamPool: Map<string, Promise<void>> = new Map();
|
14
|
+
|
16
15
|
public constructor(
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
private multicodec: string,
|
17
|
+
private getConnections: Libp2p["getConnections"],
|
18
|
+
private addEventListener: Libp2p["addEventListener"]
|
20
19
|
) {
|
21
20
|
this.log = new Logger(`stream-manager:${multicodec}`);
|
22
|
-
this.streamPool = new Map();
|
23
21
|
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
24
22
|
}
|
25
23
|
|
26
24
|
public async getStream(peer: Peer): Promise<Stream> {
|
27
|
-
const
|
28
|
-
|
25
|
+
const peerId = peer.id.toString();
|
26
|
+
|
27
|
+
const scheduledStream = this.streamPool.get(peerId);
|
29
28
|
|
30
|
-
if (
|
31
|
-
|
29
|
+
if (scheduledStream) {
|
30
|
+
this.streamPool.delete(peerId);
|
31
|
+
await scheduledStream;
|
32
32
|
}
|
33
33
|
|
34
|
-
this.
|
35
|
-
this.prepareStream(peer);
|
34
|
+
let stream = this.getOpenStreamForCodec(peer.id);
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
this.log.warn(`Failed to get stream for ${peerIdStr} -- `, error);
|
44
|
-
this.log.warn("Attempting to create a new stream for the peer");
|
36
|
+
if (stream) {
|
37
|
+
this.log.info(
|
38
|
+
`Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}`
|
39
|
+
);
|
40
|
+
this.lockStream(peer.id.toString(), stream);
|
41
|
+
return stream;
|
45
42
|
}
|
46
43
|
|
47
|
-
|
44
|
+
stream = await this.createStream(peer);
|
45
|
+
this.lockStream(peer.id.toString(), stream);
|
46
|
+
|
47
|
+
return stream;
|
48
48
|
}
|
49
49
|
|
50
50
|
private async createStream(peer: Peer, retries = 0): Promise<Stream> {
|
51
51
|
const connections = this.getConnections(peer.id);
|
52
|
-
const connection =
|
52
|
+
const connection = selectOpenConnection(connections);
|
53
53
|
|
54
54
|
if (!connection) {
|
55
|
-
throw new Error(
|
55
|
+
throw new Error(
|
56
|
+
`Failed to get a connection to the peer peerId=${peer.id.toString()} multicodec=${this.multicodec}`
|
57
|
+
);
|
56
58
|
}
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
60
|
+
let lastError: unknown;
|
61
|
+
let stream: Stream | undefined;
|
62
|
+
|
63
|
+
for (let i = 0; i < retries + 1; i++) {
|
64
|
+
try {
|
65
|
+
this.log.info(
|
66
|
+
`Attempting to create a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
|
67
|
+
);
|
68
|
+
stream = await connection.newStream(this.multicodec);
|
69
|
+
this.log.info(
|
70
|
+
`Created stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
|
71
|
+
);
|
72
|
+
break;
|
73
|
+
} catch (error) {
|
74
|
+
lastError = error;
|
65
75
|
}
|
76
|
+
}
|
77
|
+
|
78
|
+
if (!stream) {
|
66
79
|
throw new Error(
|
67
|
-
`Failed to create a new stream for ${peer.id.toString()} -- ` +
|
80
|
+
`Failed to create a new stream for ${peer.id.toString()} -- ` +
|
81
|
+
lastError
|
68
82
|
);
|
69
83
|
}
|
84
|
+
|
85
|
+
return stream;
|
70
86
|
}
|
71
87
|
|
72
|
-
private
|
73
|
-
const
|
74
|
-
setTimeout(resolve, CONNECTION_TIMEOUT)
|
75
|
-
);
|
88
|
+
private async createStreamWithLock(peer: Peer): Promise<void> {
|
89
|
+
const peerId = peer.id.toString();
|
76
90
|
|
77
|
-
|
78
|
-
this.
|
79
|
-
|
80
|
-
throw new Error("Connection timeout");
|
81
|
-
})
|
82
|
-
]).catch((error) => {
|
83
|
-
this.log.error(
|
84
|
-
`Failed to prepare a new stream for ${peer.id.toString()} -- `,
|
85
|
-
error
|
91
|
+
if (this.ongoingCreation.has(peerId)) {
|
92
|
+
this.log.info(
|
93
|
+
`Skipping creation of a stream due to lock for peerId=${peerId} multicodec=${this.multicodec}`
|
86
94
|
);
|
87
|
-
|
95
|
+
return;
|
96
|
+
}
|
97
|
+
|
98
|
+
try {
|
99
|
+
this.ongoingCreation.add(peerId);
|
100
|
+
await this.createStream(peer);
|
101
|
+
} catch (error) {
|
102
|
+
this.log.error(`Failed to createStreamWithLock:`, error);
|
103
|
+
} finally {
|
104
|
+
this.ongoingCreation.delete(peerId);
|
105
|
+
}
|
88
106
|
|
89
|
-
|
107
|
+
return;
|
90
108
|
}
|
91
109
|
|
92
110
|
private handlePeerUpdateStreamPool = (evt: CustomEvent<PeerUpdate>): void => {
|
93
111
|
const { peer } = evt.detail;
|
94
112
|
|
95
|
-
if (peer.protocols.includes(this.multicodec)) {
|
96
|
-
|
113
|
+
if (!peer.protocols.includes(this.multicodec)) {
|
114
|
+
return;
|
115
|
+
}
|
116
|
+
|
117
|
+
const stream = this.getOpenStreamForCodec(peer.id);
|
97
118
|
|
98
|
-
|
99
|
-
|
100
|
-
this.prepareStream(peer);
|
101
|
-
} else {
|
102
|
-
const peerIdStr = peer.id.toString();
|
103
|
-
this.streamPool.delete(peerIdStr);
|
104
|
-
this.log.info(
|
105
|
-
`Removed pending stream for disconnected peer ${peerIdStr}`
|
106
|
-
);
|
107
|
-
}
|
119
|
+
if (stream) {
|
120
|
+
return;
|
108
121
|
}
|
122
|
+
|
123
|
+
this.scheduleNewStream(peer);
|
109
124
|
};
|
110
125
|
|
111
|
-
private
|
126
|
+
private scheduleNewStream(peer: Peer): void {
|
127
|
+
this.log.info(
|
128
|
+
`Scheduling creation of a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
|
129
|
+
);
|
130
|
+
|
131
|
+
// abandon previous attempt
|
132
|
+
if (this.streamPool.has(peer.id.toString())) {
|
133
|
+
this.streamPool.delete(peer.id.toString());
|
134
|
+
}
|
135
|
+
|
136
|
+
this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
|
137
|
+
}
|
138
|
+
|
139
|
+
private getOpenStreamForCodec(peerId: PeerId): Stream | undefined {
|
112
140
|
const connections = this.getConnections(peerId);
|
113
|
-
|
141
|
+
const connection = selectOpenConnection(connections);
|
142
|
+
|
143
|
+
if (!connection) {
|
144
|
+
return;
|
145
|
+
}
|
146
|
+
|
147
|
+
const stream = connection.streams.find(
|
148
|
+
(s) => s.protocol === this.multicodec
|
149
|
+
);
|
150
|
+
|
151
|
+
if (!stream) {
|
152
|
+
return;
|
153
|
+
}
|
154
|
+
|
155
|
+
const isStreamUnusable = ["done", "closed", "closing"].includes(
|
156
|
+
stream.writeStatus || ""
|
157
|
+
);
|
158
|
+
if (isStreamUnusable || this.isStreamLocked(stream)) {
|
159
|
+
return;
|
160
|
+
}
|
161
|
+
|
162
|
+
return stream;
|
163
|
+
}
|
164
|
+
|
165
|
+
private lockStream(peerId: string, stream: Stream): void {
|
166
|
+
this.log.info(`Locking stream for peerId:${peerId}\tstreamId:${stream.id}`);
|
167
|
+
stream.metadata[STREAM_LOCK_KEY] = true;
|
168
|
+
}
|
169
|
+
|
170
|
+
private isStreamLocked(stream: Stream): boolean {
|
171
|
+
return !!stream.metadata[STREAM_LOCK_KEY];
|
114
172
|
}
|
115
173
|
}
|
@@ -1,22 +1,10 @@
|
|
1
1
|
import type { Connection } from "@libp2p/interface";
|
2
2
|
|
3
|
-
export function
|
3
|
+
export function selectOpenConnection(
|
4
4
|
connections: Connection[]
|
5
5
|
): Connection | undefined {
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
connections.forEach((connection) => {
|
12
|
-
if (connection.status === "open") {
|
13
|
-
if (!latestConnection) {
|
14
|
-
latestConnection = connection;
|
15
|
-
} else if (connection.timeline.open > latestConnection.timeline.open) {
|
16
|
-
latestConnection = connection;
|
17
|
-
}
|
18
|
-
}
|
19
|
-
});
|
20
|
-
|
21
|
-
return latestConnection;
|
6
|
+
return connections
|
7
|
+
.filter((c) => c.status === "open")
|
8
|
+
.sort((left, right) => right.timeline.open - left.timeline.open)
|
9
|
+
.at(0);
|
22
10
|
}
|
@@ -1,22 +0,0 @@
|
|
1
|
-
import type { Waku } from "@waku/interfaces";
|
2
|
-
import { Protocols } from "@waku/interfaces";
|
3
|
-
/**
|
4
|
-
* Wait for a remote peer to be ready given the passed protocols.
|
5
|
-
* Must be used after attempting to connect to nodes, using
|
6
|
-
* {@link @waku/sdk!WakuNode.dial} or a bootstrap method with
|
7
|
-
* {@link @waku/sdk!createLightNode}.
|
8
|
-
*
|
9
|
-
* If the passed protocols is a GossipSub protocol, then it resolves only once
|
10
|
-
* a peer is in a mesh, to help ensure that other peers will send and receive
|
11
|
-
* message to us.
|
12
|
-
*
|
13
|
-
* @param waku The Waku Node
|
14
|
-
* @param protocols The protocols that need to be enabled by remote peers.
|
15
|
-
* @param timeoutMs A timeout value in milliseconds..
|
16
|
-
*
|
17
|
-
* @returns A promise that **resolves** if all desired protocols are fulfilled by
|
18
|
-
* remote nodes, **rejects** if the timeoutMs is reached.
|
19
|
-
* @throws If passing a protocol that is not mounted
|
20
|
-
* @default Wait for remote peers with protocols enabled locally and no time out is applied.
|
21
|
-
*/
|
22
|
-
export declare function waitForRemotePeer(waku: Waku, protocols?: Protocols[], timeoutMs?: number): Promise<void>;
|
@@ -1,142 +0,0 @@
|
|
1
|
-
import { Protocols } from "@waku/interfaces";
|
2
|
-
import { Logger } from "@waku/utils";
|
3
|
-
import { pEvent } from "p-event";
|
4
|
-
const log = new Logger("wait-for-remote-peer");
|
5
|
-
//TODO: move this function within the Waku class: https://github.com/waku-org/js-waku/issues/1761
|
6
|
-
/**
|
7
|
-
* Wait for a remote peer to be ready given the passed protocols.
|
8
|
-
* Must be used after attempting to connect to nodes, using
|
9
|
-
* {@link @waku/sdk!WakuNode.dial} or a bootstrap method with
|
10
|
-
* {@link @waku/sdk!createLightNode}.
|
11
|
-
*
|
12
|
-
* If the passed protocols is a GossipSub protocol, then it resolves only once
|
13
|
-
* a peer is in a mesh, to help ensure that other peers will send and receive
|
14
|
-
* message to us.
|
15
|
-
*
|
16
|
-
* @param waku The Waku Node
|
17
|
-
* @param protocols The protocols that need to be enabled by remote peers.
|
18
|
-
* @param timeoutMs A timeout value in milliseconds..
|
19
|
-
*
|
20
|
-
* @returns A promise that **resolves** if all desired protocols are fulfilled by
|
21
|
-
* remote nodes, **rejects** if the timeoutMs is reached.
|
22
|
-
* @throws If passing a protocol that is not mounted
|
23
|
-
* @default Wait for remote peers with protocols enabled locally and no time out is applied.
|
24
|
-
*/
|
25
|
-
export async function waitForRemotePeer(waku, protocols, timeoutMs) {
|
26
|
-
protocols = protocols ?? getEnabledProtocols(waku);
|
27
|
-
if (!waku.isStarted())
|
28
|
-
return Promise.reject("Waku node is not started");
|
29
|
-
const promises = [];
|
30
|
-
if (protocols.includes(Protocols.Relay)) {
|
31
|
-
if (!waku.relay)
|
32
|
-
throw new Error("Cannot wait for Relay peer: protocol not mounted");
|
33
|
-
promises.push(waitForGossipSubPeerInMesh(waku.relay));
|
34
|
-
}
|
35
|
-
if (protocols.includes(Protocols.Store)) {
|
36
|
-
if (!waku.store)
|
37
|
-
throw new Error("Cannot wait for Store peer: protocol not mounted");
|
38
|
-
promises.push(waitForConnectedPeer(waku.store.protocol, waku.libp2p.services.metadata));
|
39
|
-
}
|
40
|
-
if (protocols.includes(Protocols.LightPush)) {
|
41
|
-
if (!waku.lightPush)
|
42
|
-
throw new Error("Cannot wait for LightPush peer: protocol not mounted");
|
43
|
-
promises.push(waitForConnectedPeer(waku.lightPush.protocol, waku.libp2p.services.metadata));
|
44
|
-
}
|
45
|
-
if (protocols.includes(Protocols.Filter)) {
|
46
|
-
if (!waku.filter)
|
47
|
-
throw new Error("Cannot wait for Filter peer: protocol not mounted");
|
48
|
-
promises.push(waitForConnectedPeer(waku.filter.protocol, waku.libp2p.services.metadata));
|
49
|
-
}
|
50
|
-
if (timeoutMs) {
|
51
|
-
await rejectOnTimeout(Promise.all(promises), timeoutMs, "Timed out waiting for a remote peer.");
|
52
|
-
}
|
53
|
-
else {
|
54
|
-
await Promise.all(promises);
|
55
|
-
}
|
56
|
-
}
|
57
|
-
//TODO: move this function within protocol SDK class: https://github.com/waku-org/js-waku/issues/1761
|
58
|
-
/**
|
59
|
-
* Wait for a peer with the given protocol to be connected.
|
60
|
-
* If sharding is enabled on the node, it will also wait for the peer to be confirmed by the metadata service.
|
61
|
-
*/
|
62
|
-
async function waitForConnectedPeer(protocol, metadataService) {
|
63
|
-
const codec = protocol.multicodec;
|
64
|
-
const peers = await protocol.connectedPeers();
|
65
|
-
if (peers.length) {
|
66
|
-
if (!metadataService) {
|
67
|
-
log.info(`${codec} peer found: `, peers[0].id.toString());
|
68
|
-
return;
|
69
|
-
}
|
70
|
-
// once a peer is connected, we need to confirm the metadata handshake with at least one of those peers if sharding is enabled
|
71
|
-
try {
|
72
|
-
await Promise.any(peers.map((peer) => metadataService.confirmOrAttemptHandshake(peer.id)));
|
73
|
-
return;
|
74
|
-
}
|
75
|
-
catch (e) {
|
76
|
-
if (e.code === "ERR_CONNECTION_BEING_CLOSED")
|
77
|
-
log.error(`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`);
|
78
|
-
log.error(`Error waiting for handshake confirmation: ${e}`);
|
79
|
-
}
|
80
|
-
}
|
81
|
-
log.info(`Waiting for ${codec} peer`);
|
82
|
-
// else we'll just wait for the next peer to connect
|
83
|
-
await new Promise((resolve) => {
|
84
|
-
const cb = (evt) => {
|
85
|
-
if (evt.detail?.protocols?.includes(codec)) {
|
86
|
-
if (metadataService) {
|
87
|
-
metadataService
|
88
|
-
.confirmOrAttemptHandshake(evt.detail.peerId)
|
89
|
-
.then(() => {
|
90
|
-
protocol.removeLibp2pEventListener("peer:identify", cb);
|
91
|
-
resolve();
|
92
|
-
})
|
93
|
-
.catch((e) => {
|
94
|
-
if (e.code === "ERR_CONNECTION_BEING_CLOSED")
|
95
|
-
log.error(`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`);
|
96
|
-
log.error(`Error waiting for handshake confirmation: ${e}`);
|
97
|
-
});
|
98
|
-
}
|
99
|
-
else {
|
100
|
-
protocol.removeLibp2pEventListener("peer:identify", cb);
|
101
|
-
resolve();
|
102
|
-
}
|
103
|
-
}
|
104
|
-
};
|
105
|
-
protocol.addLibp2pEventListener("peer:identify", cb);
|
106
|
-
});
|
107
|
-
}
|
108
|
-
/**
|
109
|
-
* Wait for at least one peer with the given protocol to be connected and in the gossipsub
|
110
|
-
* mesh for all pubsubTopics.
|
111
|
-
*/
|
112
|
-
async function waitForGossipSubPeerInMesh(waku) {
|
113
|
-
let peers = waku.getMeshPeers();
|
114
|
-
const pubsubTopics = waku.pubsubTopics;
|
115
|
-
for (const topic of pubsubTopics) {
|
116
|
-
while (peers.length == 0) {
|
117
|
-
await pEvent(waku.gossipSub, "gossipsub:heartbeat");
|
118
|
-
peers = waku.getMeshPeers(topic);
|
119
|
-
}
|
120
|
-
}
|
121
|
-
}
|
122
|
-
const awaitTimeout = (ms, rejectReason) => new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
|
123
|
-
async function rejectOnTimeout(promise, timeoutMs, rejectReason) {
|
124
|
-
await Promise.race([promise, awaitTimeout(timeoutMs, rejectReason)]);
|
125
|
-
}
|
126
|
-
function getEnabledProtocols(waku) {
|
127
|
-
const protocols = [];
|
128
|
-
if (waku.relay) {
|
129
|
-
protocols.push(Protocols.Relay);
|
130
|
-
}
|
131
|
-
if (waku.filter) {
|
132
|
-
protocols.push(Protocols.Filter);
|
133
|
-
}
|
134
|
-
if (waku.store) {
|
135
|
-
protocols.push(Protocols.Store);
|
136
|
-
}
|
137
|
-
if (waku.lightPush) {
|
138
|
-
protocols.push(Protocols.LightPush);
|
139
|
-
}
|
140
|
-
return protocols;
|
141
|
-
}
|
142
|
-
//# sourceMappingURL=wait_for_remote_peer.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"wait_for_remote_peer.js","sourceRoot":"","sources":["../../src/lib/wait_for_remote_peer.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,sBAAsB,CAAC,CAAC;AAE/C,iGAAiG;AACjG;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,IAAU,EACV,SAAuB,EACvB,SAAkB;IAElB,SAAS,GAAG,SAAS,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAC;IAEnD,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;QAAE,OAAO,OAAO,CAAC,MAAM,CAAC,0BAA0B,CAAC,CAAC;IAEzE,MAAM,QAAQ,GAAG,EAAE,CAAC;IAEpB,IAAI,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QACxC,IAAI,CAAC,IAAI,CAAC,KAAK;YACb,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;QACtE,QAAQ,CAAC,IAAI,CAAC,0BAA0B,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,IAAI,SAAS,CAAC,QAAQ,CA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IAEpB,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,YAAY,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;AACvE,CAAC;AAED,SAAS,mBAAmB,CAAC,IAAU;IACrC,MAAM,SAAS,GAAG,EAAE,CAAC;IAErB,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;IAED,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;QAChB,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;IAED,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;QACnB,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IACtC,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
@@ -1,200 +0,0 @@
|
|
1
|
-
import type { IdentifyResult } from "@libp2p/interface";
|
2
|
-
import type {
|
3
|
-
IBaseProtocolCore,
|
4
|
-
IMetadata,
|
5
|
-
IRelay,
|
6
|
-
Waku
|
7
|
-
} from "@waku/interfaces";
|
8
|
-
import { Protocols } from "@waku/interfaces";
|
9
|
-
import { Logger } from "@waku/utils";
|
10
|
-
import { pEvent } from "p-event";
|
11
|
-
const log = new Logger("wait-for-remote-peer");
|
12
|
-
|
13
|
-
//TODO: move this function within the Waku class: https://github.com/waku-org/js-waku/issues/1761
|
14
|
-
/**
|
15
|
-
* Wait for a remote peer to be ready given the passed protocols.
|
16
|
-
* Must be used after attempting to connect to nodes, using
|
17
|
-
* {@link @waku/sdk!WakuNode.dial} or a bootstrap method with
|
18
|
-
* {@link @waku/sdk!createLightNode}.
|
19
|
-
*
|
20
|
-
* If the passed protocols is a GossipSub protocol, then it resolves only once
|
21
|
-
* a peer is in a mesh, to help ensure that other peers will send and receive
|
22
|
-
* message to us.
|
23
|
-
*
|
24
|
-
* @param waku The Waku Node
|
25
|
-
* @param protocols The protocols that need to be enabled by remote peers.
|
26
|
-
* @param timeoutMs A timeout value in milliseconds..
|
27
|
-
*
|
28
|
-
* @returns A promise that **resolves** if all desired protocols are fulfilled by
|
29
|
-
* remote nodes, **rejects** if the timeoutMs is reached.
|
30
|
-
* @throws If passing a protocol that is not mounted
|
31
|
-
* @default Wait for remote peers with protocols enabled locally and no time out is applied.
|
32
|
-
*/
|
33
|
-
export async function waitForRemotePeer(
|
34
|
-
waku: Waku,
|
35
|
-
protocols?: Protocols[],
|
36
|
-
timeoutMs?: number
|
37
|
-
): Promise<void> {
|
38
|
-
protocols = protocols ?? getEnabledProtocols(waku);
|
39
|
-
|
40
|
-
if (!waku.isStarted()) return Promise.reject("Waku node is not started");
|
41
|
-
|
42
|
-
const promises = [];
|
43
|
-
|
44
|
-
if (protocols.includes(Protocols.Relay)) {
|
45
|
-
if (!waku.relay)
|
46
|
-
throw new Error("Cannot wait for Relay peer: protocol not mounted");
|
47
|
-
promises.push(waitForGossipSubPeerInMesh(waku.relay));
|
48
|
-
}
|
49
|
-
|
50
|
-
if (protocols.includes(Protocols.Store)) {
|
51
|
-
if (!waku.store)
|
52
|
-
throw new Error("Cannot wait for Store peer: protocol not mounted");
|
53
|
-
promises.push(
|
54
|
-
waitForConnectedPeer(waku.store.protocol, waku.libp2p.services.metadata)
|
55
|
-
);
|
56
|
-
}
|
57
|
-
|
58
|
-
if (protocols.includes(Protocols.LightPush)) {
|
59
|
-
if (!waku.lightPush)
|
60
|
-
throw new Error("Cannot wait for LightPush peer: protocol not mounted");
|
61
|
-
promises.push(
|
62
|
-
waitForConnectedPeer(
|
63
|
-
waku.lightPush.protocol,
|
64
|
-
waku.libp2p.services.metadata
|
65
|
-
)
|
66
|
-
);
|
67
|
-
}
|
68
|
-
|
69
|
-
if (protocols.includes(Protocols.Filter)) {
|
70
|
-
if (!waku.filter)
|
71
|
-
throw new Error("Cannot wait for Filter peer: protocol not mounted");
|
72
|
-
promises.push(
|
73
|
-
waitForConnectedPeer(waku.filter.protocol, waku.libp2p.services.metadata)
|
74
|
-
);
|
75
|
-
}
|
76
|
-
|
77
|
-
if (timeoutMs) {
|
78
|
-
await rejectOnTimeout(
|
79
|
-
Promise.all(promises),
|
80
|
-
timeoutMs,
|
81
|
-
"Timed out waiting for a remote peer."
|
82
|
-
);
|
83
|
-
} else {
|
84
|
-
await Promise.all(promises);
|
85
|
-
}
|
86
|
-
}
|
87
|
-
|
88
|
-
//TODO: move this function within protocol SDK class: https://github.com/waku-org/js-waku/issues/1761
|
89
|
-
/**
|
90
|
-
* Wait for a peer with the given protocol to be connected.
|
91
|
-
* If sharding is enabled on the node, it will also wait for the peer to be confirmed by the metadata service.
|
92
|
-
*/
|
93
|
-
async function waitForConnectedPeer(
|
94
|
-
protocol: IBaseProtocolCore,
|
95
|
-
metadataService?: IMetadata
|
96
|
-
): Promise<void> {
|
97
|
-
const codec = protocol.multicodec;
|
98
|
-
const peers = await protocol.connectedPeers();
|
99
|
-
|
100
|
-
if (peers.length) {
|
101
|
-
if (!metadataService) {
|
102
|
-
log.info(`${codec} peer found: `, peers[0].id.toString());
|
103
|
-
return;
|
104
|
-
}
|
105
|
-
|
106
|
-
// once a peer is connected, we need to confirm the metadata handshake with at least one of those peers if sharding is enabled
|
107
|
-
try {
|
108
|
-
await Promise.any(
|
109
|
-
peers.map((peer) => metadataService.confirmOrAttemptHandshake(peer.id))
|
110
|
-
);
|
111
|
-
return;
|
112
|
-
} catch (e) {
|
113
|
-
if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED")
|
114
|
-
log.error(
|
115
|
-
`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`
|
116
|
-
);
|
117
|
-
|
118
|
-
log.error(`Error waiting for handshake confirmation: ${e}`);
|
119
|
-
}
|
120
|
-
}
|
121
|
-
|
122
|
-
log.info(`Waiting for ${codec} peer`);
|
123
|
-
|
124
|
-
// else we'll just wait for the next peer to connect
|
125
|
-
await new Promise<void>((resolve) => {
|
126
|
-
const cb = (evt: CustomEvent<IdentifyResult>): void => {
|
127
|
-
if (evt.detail?.protocols?.includes(codec)) {
|
128
|
-
if (metadataService) {
|
129
|
-
metadataService
|
130
|
-
.confirmOrAttemptHandshake(evt.detail.peerId)
|
131
|
-
.then(() => {
|
132
|
-
protocol.removeLibp2pEventListener("peer:identify", cb);
|
133
|
-
resolve();
|
134
|
-
})
|
135
|
-
.catch((e) => {
|
136
|
-
if (e.code === "ERR_CONNECTION_BEING_CLOSED")
|
137
|
-
log.error(
|
138
|
-
`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`
|
139
|
-
);
|
140
|
-
|
141
|
-
log.error(`Error waiting for handshake confirmation: ${e}`);
|
142
|
-
});
|
143
|
-
} else {
|
144
|
-
protocol.removeLibp2pEventListener("peer:identify", cb);
|
145
|
-
resolve();
|
146
|
-
}
|
147
|
-
}
|
148
|
-
};
|
149
|
-
protocol.addLibp2pEventListener("peer:identify", cb);
|
150
|
-
});
|
151
|
-
}
|
152
|
-
|
153
|
-
/**
|
154
|
-
* Wait for at least one peer with the given protocol to be connected and in the gossipsub
|
155
|
-
* mesh for all pubsubTopics.
|
156
|
-
*/
|
157
|
-
async function waitForGossipSubPeerInMesh(waku: IRelay): Promise<void> {
|
158
|
-
let peers = waku.getMeshPeers();
|
159
|
-
const pubsubTopics = waku.pubsubTopics;
|
160
|
-
|
161
|
-
for (const topic of pubsubTopics) {
|
162
|
-
while (peers.length == 0) {
|
163
|
-
await pEvent(waku.gossipSub, "gossipsub:heartbeat");
|
164
|
-
peers = waku.getMeshPeers(topic);
|
165
|
-
}
|
166
|
-
}
|
167
|
-
}
|
168
|
-
|
169
|
-
const awaitTimeout = (ms: number, rejectReason: string): Promise<void> =>
|
170
|
-
new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
|
171
|
-
|
172
|
-
async function rejectOnTimeout<T>(
|
173
|
-
promise: Promise<T>,
|
174
|
-
timeoutMs: number,
|
175
|
-
rejectReason: string
|
176
|
-
): Promise<void> {
|
177
|
-
await Promise.race([promise, awaitTimeout(timeoutMs, rejectReason)]);
|
178
|
-
}
|
179
|
-
|
180
|
-
function getEnabledProtocols(waku: Waku): Protocols[] {
|
181
|
-
const protocols = [];
|
182
|
-
|
183
|
-
if (waku.relay) {
|
184
|
-
protocols.push(Protocols.Relay);
|
185
|
-
}
|
186
|
-
|
187
|
-
if (waku.filter) {
|
188
|
-
protocols.push(Protocols.Filter);
|
189
|
-
}
|
190
|
-
|
191
|
-
if (waku.store) {
|
192
|
-
protocols.push(Protocols.Store);
|
193
|
-
}
|
194
|
-
|
195
|
-
if (waku.lightPush) {
|
196
|
-
protocols.push(Protocols.LightPush);
|
197
|
-
}
|
198
|
-
|
199
|
-
return protocols;
|
200
|
-
}
|