@waku/core 0.0.30-682cc66.0 → 0.0.30-d8ed83f.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/{base_protocol-CrPXdVvO.js → base_protocol-DCOj0QWD.js} +54 -20
- package/bundle/{browser-DoQRY-an.js → browser-zSUobdfj.js} +10 -0
- package/bundle/{index-NYIjIEV5.js → index-BcSodzY4.js} +1 -1
- package/bundle/index.js +156 -32
- package/bundle/lib/base_protocol.js +3 -3
- package/bundle/lib/message/version_0.js +3 -3
- package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
- package/bundle/{version_0-C6GyvOeg.js → version_0-TEIsGmpJ.js} +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/lib/filter/index.d.ts +5 -5
- package/dist/lib/filter/index.js +129 -21
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/light_push/index.d.ts +3 -4
- package/dist/lib/light_push/index.js +3 -3
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/metadata/index.js +11 -1
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/store/index.js +8 -1
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager.d.ts +5 -4
- package/dist/lib/stream_manager.js +52 -18
- package/dist/lib/stream_manager.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/filter/index.ts +164 -52
- package/src/lib/light_push/index.ts +14 -19
- package/src/lib/metadata/index.ts +16 -5
- package/src/lib/store/index.ts +7 -1
- package/src/lib/stream_manager.ts +66 -25
@@ -3,9 +3,9 @@ import { IncomingStreamData } from "@libp2p/interface";
|
|
3
3
|
import {
|
4
4
|
type IMetadata,
|
5
5
|
type Libp2pComponents,
|
6
|
+
type MetadataQueryResult,
|
6
7
|
type PeerIdStr,
|
7
8
|
ProtocolError,
|
8
|
-
QueryResult,
|
9
9
|
type ShardInfo
|
10
10
|
} from "@waku/interfaces";
|
11
11
|
import { proto_metadata } from "@waku/proto";
|
@@ -74,7 +74,7 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
74
74
|
/**
|
75
75
|
* Make a metadata query to a peer
|
76
76
|
*/
|
77
|
-
async query(peerId: PeerId): Promise<
|
77
|
+
async query(peerId: PeerId): Promise<MetadataQueryResult> {
|
78
78
|
const request = proto_metadata.WakuMetadataRequest.encode(this.shardInfo);
|
79
79
|
|
80
80
|
const peer = await this.peerStore.get(peerId);
|
@@ -85,7 +85,16 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
85
85
|
};
|
86
86
|
}
|
87
87
|
|
88
|
-
|
88
|
+
let stream;
|
89
|
+
try {
|
90
|
+
stream = await this.getStream(peer);
|
91
|
+
} catch (error) {
|
92
|
+
log.error("Failed to get stream", error);
|
93
|
+
return {
|
94
|
+
shardInfo: null,
|
95
|
+
error: ProtocolError.NO_STREAM_AVAILABLE
|
96
|
+
};
|
97
|
+
}
|
89
98
|
|
90
99
|
const encodedResponse = await pipe(
|
91
100
|
[request],
|
@@ -112,7 +121,9 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
112
121
|
};
|
113
122
|
}
|
114
123
|
|
115
|
-
public async confirmOrAttemptHandshake(
|
124
|
+
public async confirmOrAttemptHandshake(
|
125
|
+
peerId: PeerId
|
126
|
+
): Promise<MetadataQueryResult> {
|
116
127
|
const shardInfo = this.handshakesConfirmed.get(peerId.toString());
|
117
128
|
if (shardInfo) {
|
118
129
|
return {
|
@@ -126,7 +137,7 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
126
137
|
|
127
138
|
private decodeMetadataResponse(
|
128
139
|
encodedResponse: Uint8ArrayList[]
|
129
|
-
):
|
140
|
+
): MetadataQueryResult {
|
130
141
|
const bytes = new Uint8ArrayList();
|
131
142
|
|
132
143
|
encodedResponse.forEach((chunk) => {
|
package/src/lib/store/index.ts
CHANGED
@@ -92,7 +92,13 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
|
|
92
92
|
|
93
93
|
const historyRpcQuery = HistoryRpc.createQuery(queryOpts);
|
94
94
|
|
95
|
-
|
95
|
+
let stream;
|
96
|
+
try {
|
97
|
+
stream = await this.getStream(peer);
|
98
|
+
} catch (e) {
|
99
|
+
log.error("Failed to get stream", e);
|
100
|
+
break;
|
101
|
+
}
|
96
102
|
|
97
103
|
const res = await pipe(
|
98
104
|
[historyRpcQuery.encode()],
|
@@ -1,11 +1,15 @@
|
|
1
1
|
import type { PeerUpdate, Stream } from "@libp2p/interface";
|
2
|
-
import { Peer } from "@libp2p/interface";
|
2
|
+
import type { Peer, PeerId } from "@libp2p/interface";
|
3
3
|
import { Libp2p } from "@waku/interfaces";
|
4
4
|
import { Logger } from "@waku/utils";
|
5
5
|
import { selectConnection } from "@waku/utils/libp2p";
|
6
6
|
|
7
|
+
const CONNECTION_TIMEOUT = 5_000;
|
8
|
+
const RETRY_BACKOFF_BASE = 1_000;
|
9
|
+
const MAX_RETRIES = 3;
|
10
|
+
|
7
11
|
export class StreamManager {
|
8
|
-
private streamPool: Map<string, Promise<Stream | void>>;
|
12
|
+
private readonly streamPool: Map<string, Promise<Stream | void>>;
|
9
13
|
private readonly log: Logger;
|
10
14
|
|
11
15
|
constructor(
|
@@ -14,12 +18,8 @@ export class StreamManager {
|
|
14
18
|
public addEventListener: Libp2p["addEventListener"]
|
15
19
|
) {
|
16
20
|
this.log = new Logger(`stream-manager:${multicodec}`);
|
17
|
-
this.addEventListener(
|
18
|
-
"peer:update",
|
19
|
-
this.handlePeerUpdateStreamPool.bind(this)
|
20
|
-
);
|
21
|
-
this.getStream = this.getStream.bind(this);
|
22
21
|
this.streamPool = new Map();
|
22
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
23
23
|
}
|
24
24
|
|
25
25
|
public async getStream(peer: Peer): Promise<Stream> {
|
@@ -27,47 +27,88 @@ export class StreamManager {
|
|
27
27
|
const streamPromise = this.streamPool.get(peerIdStr);
|
28
28
|
|
29
29
|
if (!streamPromise) {
|
30
|
-
return this.
|
30
|
+
return this.createStream(peer);
|
31
31
|
}
|
32
32
|
|
33
|
-
// We have the stream, let's remove it from the map
|
34
33
|
this.streamPool.delete(peerIdStr);
|
34
|
+
this.prepareStream(peer);
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
try {
|
37
|
+
const stream = await streamPromise;
|
38
|
+
if (stream && stream.status !== "closed") {
|
39
|
+
return stream;
|
40
|
+
}
|
41
|
+
} catch (error) {
|
42
|
+
this.log.warn(`Failed to get stream for ${peerIdStr} -- `, error);
|
43
|
+
this.log.warn("Attempting to create a new stream for the peer");
|
42
44
|
}
|
43
45
|
|
44
|
-
return
|
46
|
+
return this.createStream(peer);
|
45
47
|
}
|
46
48
|
|
47
|
-
private async
|
49
|
+
private async createStream(peer: Peer, retries = 0): Promise<Stream> {
|
48
50
|
const connections = this.getConnections(peer.id);
|
49
51
|
const connection = selectConnection(connections);
|
52
|
+
|
50
53
|
if (!connection) {
|
51
54
|
throw new Error("Failed to get a connection to the peer");
|
52
55
|
}
|
53
|
-
|
56
|
+
|
57
|
+
try {
|
58
|
+
return await connection.newStream(this.multicodec);
|
59
|
+
} catch (error) {
|
60
|
+
if (retries < MAX_RETRIES) {
|
61
|
+
const backoff = RETRY_BACKOFF_BASE * Math.pow(2, retries);
|
62
|
+
await new Promise((resolve) => setTimeout(resolve, backoff));
|
63
|
+
return this.createStream(peer, retries + 1);
|
64
|
+
}
|
65
|
+
throw new Error(
|
66
|
+
`Failed to create a new stream for ${peer.id.toString()} -- ` + error
|
67
|
+
);
|
68
|
+
}
|
54
69
|
}
|
55
70
|
|
56
|
-
private
|
57
|
-
const
|
58
|
-
|
71
|
+
private prepareStream(peer: Peer): void {
|
72
|
+
const timeoutPromise = new Promise<void>((resolve) =>
|
73
|
+
setTimeout(resolve, CONNECTION_TIMEOUT)
|
74
|
+
);
|
75
|
+
|
76
|
+
const streamPromise = Promise.race([
|
77
|
+
this.createStream(peer),
|
78
|
+
timeoutPromise.then(() => {
|
79
|
+
throw new Error("Connection timeout");
|
80
|
+
})
|
81
|
+
]).catch((error) => {
|
59
82
|
this.log.error(
|
60
|
-
`Failed to prepare a new stream for ${peer.id.toString()}
|
83
|
+
`Failed to prepare a new stream for ${peer.id.toString()} -- `,
|
84
|
+
error
|
61
85
|
);
|
62
86
|
});
|
87
|
+
|
63
88
|
this.streamPool.set(peer.id.toString(), streamPromise);
|
64
89
|
}
|
65
90
|
|
66
91
|
private handlePeerUpdateStreamPool = (evt: CustomEvent<PeerUpdate>): void => {
|
67
|
-
const peer = evt.detail
|
92
|
+
const { peer } = evt.detail;
|
93
|
+
|
68
94
|
if (peer.protocols.includes(this.multicodec)) {
|
69
|
-
this.
|
70
|
-
|
95
|
+
const isConnected = this.isConnectedTo(peer.id);
|
96
|
+
|
97
|
+
if (isConnected) {
|
98
|
+
this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
|
99
|
+
this.prepareStream(peer);
|
100
|
+
} else {
|
101
|
+
const peerIdStr = peer.id.toString();
|
102
|
+
this.streamPool.delete(peerIdStr);
|
103
|
+
this.log.info(
|
104
|
+
`Removed pending stream for disconnected peer ${peerIdStr}`
|
105
|
+
);
|
106
|
+
}
|
71
107
|
}
|
72
108
|
};
|
109
|
+
|
110
|
+
private isConnectedTo(peerId: PeerId): boolean {
|
111
|
+
const connections = this.getConnections(peerId);
|
112
|
+
return connections.some((connection) => connection.status === "open");
|
113
|
+
}
|
73
114
|
}
|