@waku/core 0.0.30 → 0.0.31-1887f4f.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-C6HnrRx8.js → base_protocol-WTp5jKZK.js} +20 -19
- package/bundle/{index-DnW8ifxc.js → index-COT0phC8.js} +3 -4
- package/bundle/index.js +82 -84
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-DQ9xsSLk.js → version_0-SMRUXDNR.js} +2 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +2 -2
- package/dist/lib/base_protocol.js +1 -1
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager.d.ts +1 -1
- package/dist/lib/connection_manager.js +21 -23
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/filter_rpc.js.map +1 -1
- package/dist/lib/filter/index.d.ts +2 -2
- package/dist/lib/filter/index.js +29 -29
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/keep_alive_manager.d.ts +13 -7
- package/dist/lib/keep_alive_manager.js +9 -9
- package/dist/lib/keep_alive_manager.js.map +1 -1
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/push_rpc.js.map +1 -1
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/metadata/index.js +18 -18
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/store/history_rpc.js.map +1 -1
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager/index.d.ts +1 -0
- package/dist/lib/stream_manager/index.js +2 -0
- package/dist/lib/stream_manager/index.js.map +1 -0
- package/dist/lib/{stream_manager.js → stream_manager/stream_manager.js} +1 -1
- package/dist/lib/stream_manager/stream_manager.js.map +1 -0
- package/dist/lib/stream_manager/utils.d.ts +2 -0
- package/dist/lib/stream_manager/utils.js +19 -0
- package/dist/lib/stream_manager/utils.js.map +1 -0
- package/package.json +1 -130
- package/src/index.ts +1 -1
- package/src/lib/base_protocol.ts +4 -3
- package/src/lib/connection_manager.ts +28 -29
- package/src/lib/filter/filter_rpc.ts +21 -19
- package/src/lib/filter/index.ts +47 -42
- package/src/lib/keep_alive_manager.ts +27 -18
- package/src/lib/light_push/index.ts +2 -2
- package/src/lib/light_push/push_rpc.ts +5 -5
- package/src/lib/message/version_0.ts +17 -15
- package/src/lib/metadata/index.ts +33 -33
- package/src/lib/store/history_rpc.ts +5 -5
- package/src/lib/store/index.ts +2 -2
- package/src/lib/stream_manager/index.ts +1 -0
- package/src/lib/{stream_manager.ts → stream_manager/stream_manager.ts} +3 -2
- package/src/lib/stream_manager/utils.ts +22 -0
- package/dist/lib/stream_manager.js.map +0 -1
- /package/dist/lib/{stream_manager.d.ts → stream_manager/stream_manager.d.ts} +0 -0
package/src/lib/base_protocol.ts
CHANGED
@@ -14,7 +14,7 @@ import {
|
|
14
14
|
} from "@waku/utils/libp2p";
|
15
15
|
|
16
16
|
import { filterPeersByDiscovery } from "./filterPeers.js";
|
17
|
-
import { StreamManager } from "./stream_manager.js";
|
17
|
+
import { StreamManager } from "./stream_manager/index.js";
|
18
18
|
|
19
19
|
/**
|
20
20
|
* A class with predefined helpers, to be used as a base to implement Waku
|
@@ -25,7 +25,7 @@ export class BaseProtocol implements IBaseProtocolCore {
|
|
25
25
|
public readonly removeLibp2pEventListener: Libp2p["removeEventListener"];
|
26
26
|
protected streamManager: StreamManager;
|
27
27
|
|
28
|
-
constructor(
|
28
|
+
protected constructor(
|
29
29
|
public multicodec: string,
|
30
30
|
private components: Libp2pComponents,
|
31
31
|
private log: Logger,
|
@@ -47,6 +47,7 @@ export class BaseProtocol implements IBaseProtocolCore {
|
|
47
47
|
this.addLibp2pEventListener
|
48
48
|
);
|
49
49
|
}
|
50
|
+
|
50
51
|
protected async getStream(peer: Peer): Promise<Stream> {
|
51
52
|
return this.streamManager.getStream(peer);
|
52
53
|
}
|
@@ -81,7 +82,7 @@ export class BaseProtocol implements IBaseProtocolCore {
|
|
81
82
|
|
82
83
|
* @returns A list of peers that support the protocol sorted by latency.
|
83
84
|
*/
|
84
|
-
async getPeers(
|
85
|
+
public async getPeers(
|
85
86
|
{
|
86
87
|
numPeers,
|
87
88
|
maxBootstrapPeers
|
@@ -89,7 +89,7 @@ export class ConnectionManager
|
|
89
89
|
return instance;
|
90
90
|
}
|
91
91
|
|
92
|
-
stop(): void {
|
92
|
+
public stop(): void {
|
93
93
|
this.keepAliveManager.stopAll();
|
94
94
|
this.libp2p.removeEventListener(
|
95
95
|
"peer:connect",
|
@@ -105,7 +105,7 @@ export class ConnectionManager
|
|
105
105
|
);
|
106
106
|
}
|
107
107
|
|
108
|
-
async dropConnection(peerId: PeerId): Promise<void> {
|
108
|
+
public async dropConnection(peerId: PeerId): Promise<void> {
|
109
109
|
try {
|
110
110
|
this.keepAliveManager.stop(peerId);
|
111
111
|
await this.libp2p.hangUp(peerId);
|
@@ -187,7 +187,11 @@ export class ConnectionManager
|
|
187
187
|
...options
|
188
188
|
};
|
189
189
|
|
190
|
-
this.keepAliveManager = new KeepAliveManager(
|
190
|
+
this.keepAliveManager = new KeepAliveManager({
|
191
|
+
relay,
|
192
|
+
libp2p,
|
193
|
+
options: keepAliveOptions
|
194
|
+
});
|
191
195
|
|
192
196
|
this.run()
|
193
197
|
.then(() => log.info(`Connection Manager is now running`))
|
@@ -250,6 +254,7 @@ export class ConnectionManager
|
|
250
254
|
this.dialAttemptsForPeer.set(peerId.toString(), -1);
|
251
255
|
|
252
256
|
// Dialing succeeded, break the loop
|
257
|
+
this.keepAliveManager.start(peerId);
|
253
258
|
break;
|
254
259
|
} catch (error) {
|
255
260
|
if (error instanceof AggregateError) {
|
@@ -356,7 +361,7 @@ export class ConnectionManager
|
|
356
361
|
);
|
357
362
|
}
|
358
363
|
|
359
|
-
|
364
|
+
public async attemptDial(peerId: PeerId): Promise<void> {
|
360
365
|
if (!(await this.shouldDialPeer(peerId))) return;
|
361
366
|
|
362
367
|
if (this.currentActiveParallelDialCount >= this.options.maxParallelDials) {
|
@@ -364,9 +369,7 @@ export class ConnectionManager
|
|
364
369
|
return;
|
365
370
|
}
|
366
371
|
|
367
|
-
this.dialPeer(peerId)
|
368
|
-
log.error(`Error dialing peer ${peerId.toString()} : ${err}`);
|
369
|
-
});
|
372
|
+
await this.dialPeer(peerId);
|
370
373
|
}
|
371
374
|
|
372
375
|
private onEventHandlers = {
|
@@ -389,11 +392,7 @@ export class ConnectionManager
|
|
389
392
|
|
390
393
|
const peerId = evt.detail;
|
391
394
|
|
392
|
-
this.keepAliveManager.start(
|
393
|
-
peerId,
|
394
|
-
this.libp2p.services.ping,
|
395
|
-
this.libp2p.peerStore
|
396
|
-
);
|
395
|
+
this.keepAliveManager.start(peerId);
|
397
396
|
|
398
397
|
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
|
399
398
|
Tags.BOOTSTRAP
|
@@ -449,38 +448,40 @@ export class ConnectionManager
|
|
449
448
|
* @returns true if the peer should be dialed, false otherwise
|
450
449
|
*/
|
451
450
|
private async shouldDialPeer(peerId: PeerId): Promise<boolean> {
|
452
|
-
// if we're already connected to the peer, don't dial
|
453
451
|
const isConnected = this.libp2p.getConnections(peerId).length > 0;
|
454
452
|
if (isConnected) {
|
455
453
|
log.warn(`Already connected to peer ${peerId.toString()}. Not dialing.`);
|
456
454
|
return false;
|
457
455
|
}
|
458
456
|
|
459
|
-
|
460
|
-
if (!
|
457
|
+
const isSameShard = await this.isPeerTopicConfigured(peerId);
|
458
|
+
if (!isSameShard) {
|
461
459
|
const shardInfo = await this.getPeerShardInfo(
|
462
460
|
peerId,
|
463
461
|
this.libp2p.peerStore
|
464
462
|
);
|
463
|
+
|
465
464
|
log.warn(
|
466
465
|
`Discovered peer ${peerId.toString()} with ShardInfo ${shardInfo} is not part of any of the configured pubsub topics (${
|
467
466
|
this.configuredPubsubTopics
|
468
467
|
}).
|
469
468
|
Not dialing.`
|
470
469
|
);
|
470
|
+
|
471
471
|
return false;
|
472
472
|
}
|
473
473
|
|
474
|
-
|
475
|
-
|
474
|
+
const isPreferredBasedOnBootstrap =
|
475
|
+
await this.isPeerDialableBasedOnBootstrapStatus(peerId);
|
476
|
+
if (!isPreferredBasedOnBootstrap) {
|
476
477
|
log.warn(
|
477
478
|
`Peer ${peerId.toString()} is not dialable based on bootstrap status. Not dialing.`
|
478
479
|
);
|
479
480
|
return false;
|
480
481
|
}
|
481
482
|
|
482
|
-
|
483
|
-
if (
|
483
|
+
const hasBeenDialed = this.dialAttemptsForPeer.has(peerId.toString());
|
484
|
+
if (hasBeenDialed) {
|
484
485
|
log.warn(
|
485
486
|
`Peer ${peerId.toString()} has already been attempted dial before, or already has a dial attempt in progress, skipping dial`
|
486
487
|
);
|
@@ -502,19 +503,17 @@ export class ConnectionManager
|
|
502
503
|
|
503
504
|
const isBootstrap = tagNames.some((tagName) => tagName === Tags.BOOTSTRAP);
|
504
505
|
|
505
|
-
if (isBootstrap) {
|
506
|
-
const currentBootstrapConnections = this.libp2p
|
507
|
-
.getConnections()
|
508
|
-
.filter((conn) => {
|
509
|
-
return conn.tags.find((name) => name === Tags.BOOTSTRAP);
|
510
|
-
}).length;
|
511
|
-
if (currentBootstrapConnections < this.options.maxBootstrapPeersAllowed)
|
512
|
-
return true;
|
513
|
-
} else {
|
506
|
+
if (!isBootstrap) {
|
514
507
|
return true;
|
515
508
|
}
|
516
509
|
|
517
|
-
|
510
|
+
const currentBootstrapConnections = this.libp2p
|
511
|
+
.getConnections()
|
512
|
+
.filter((conn) => {
|
513
|
+
return conn.tags.find((name) => name === Tags.BOOTSTRAP);
|
514
|
+
}).length;
|
515
|
+
|
516
|
+
return currentBootstrapConnections < this.options.maxBootstrapPeersAllowed;
|
518
517
|
}
|
519
518
|
|
520
519
|
private async dispatchDiscoveryEvent(peerId: PeerId): Promise<void> {
|
@@ -8,16 +8,16 @@ import { v4 as uuid } from "uuid";
|
|
8
8
|
export class FilterPushRpc {
|
9
9
|
public constructor(public proto: proto.MessagePush) {}
|
10
10
|
|
11
|
-
static decode(bytes: Uint8Array): FilterPushRpc {
|
11
|
+
public static decode(bytes: Uint8Array): FilterPushRpc {
|
12
12
|
const res = proto.MessagePush.decode(bytes);
|
13
13
|
return new FilterPushRpc(res);
|
14
14
|
}
|
15
15
|
|
16
|
-
encode(): Uint8Array {
|
16
|
+
public encode(): Uint8Array {
|
17
17
|
return proto.MessagePush.encode(this.proto);
|
18
18
|
}
|
19
19
|
|
20
|
-
get wakuMessage(): WakuMessage | undefined {
|
20
|
+
public get wakuMessage(): WakuMessage | undefined {
|
21
21
|
return this.proto.wakuMessage;
|
22
22
|
}
|
23
23
|
|
@@ -25,7 +25,7 @@ export class FilterPushRpc {
|
|
25
25
|
* Get the pubsub topic from the FilterPushRpc object.
|
26
26
|
* @returns string
|
27
27
|
*/
|
28
|
-
get pubsubTopic(): string | undefined {
|
28
|
+
public get pubsubTopic(): string | undefined {
|
29
29
|
return this.proto.pubsubTopic;
|
30
30
|
}
|
31
31
|
}
|
@@ -33,7 +33,7 @@ export class FilterPushRpc {
|
|
33
33
|
export class FilterSubscribeRpc {
|
34
34
|
public constructor(public proto: proto.FilterSubscribeRequest) {}
|
35
35
|
|
36
|
-
static createSubscribeRequest(
|
36
|
+
public static createSubscribeRequest(
|
37
37
|
pubsubTopic: string,
|
38
38
|
contentTopics: string[]
|
39
39
|
): FilterSubscribeRpc {
|
@@ -46,7 +46,7 @@ export class FilterSubscribeRpc {
|
|
46
46
|
});
|
47
47
|
}
|
48
48
|
|
49
|
-
static createUnsubscribeRequest(
|
49
|
+
public static createUnsubscribeRequest(
|
50
50
|
pubsubTopic: string,
|
51
51
|
contentTopics: string[]
|
52
52
|
): FilterSubscribeRpc {
|
@@ -59,7 +59,9 @@ export class FilterSubscribeRpc {
|
|
59
59
|
});
|
60
60
|
}
|
61
61
|
|
62
|
-
static createUnsubscribeAllRequest(
|
62
|
+
public static createUnsubscribeAllRequest(
|
63
|
+
pubsubTopic: string
|
64
|
+
): FilterSubscribeRpc {
|
63
65
|
return new FilterSubscribeRpc({
|
64
66
|
requestId: uuid(),
|
65
67
|
filterSubscribeType:
|
@@ -69,7 +71,7 @@ export class FilterSubscribeRpc {
|
|
69
71
|
});
|
70
72
|
}
|
71
73
|
|
72
|
-
static createSubscriberPingRequest(): FilterSubscribeRpc {
|
74
|
+
public static createSubscriberPingRequest(): FilterSubscribeRpc {
|
73
75
|
return new FilterSubscribeRpc({
|
74
76
|
requestId: uuid(),
|
75
77
|
filterSubscribeType:
|
@@ -79,28 +81,28 @@ export class FilterSubscribeRpc {
|
|
79
81
|
});
|
80
82
|
}
|
81
83
|
|
82
|
-
static decode(bytes: Uint8Array): FilterSubscribeRpc {
|
84
|
+
public static decode(bytes: Uint8Array): FilterSubscribeRpc {
|
83
85
|
const res = proto.FilterSubscribeRequest.decode(bytes);
|
84
86
|
return new FilterSubscribeRpc(res);
|
85
87
|
}
|
86
88
|
|
87
|
-
encode(): Uint8Array {
|
89
|
+
public encode(): Uint8Array {
|
88
90
|
return proto.FilterSubscribeRequest.encode(this.proto);
|
89
91
|
}
|
90
92
|
|
91
|
-
get filterSubscribeType(): proto.FilterSubscribeRequest.FilterSubscribeType {
|
93
|
+
public get filterSubscribeType(): proto.FilterSubscribeRequest.FilterSubscribeType {
|
92
94
|
return this.proto.filterSubscribeType;
|
93
95
|
}
|
94
96
|
|
95
|
-
get requestId(): string {
|
97
|
+
public get requestId(): string {
|
96
98
|
return this.proto.requestId;
|
97
99
|
}
|
98
100
|
|
99
|
-
get pubsubTopic(): string | undefined {
|
101
|
+
public get pubsubTopic(): string | undefined {
|
100
102
|
return this.proto.pubsubTopic;
|
101
103
|
}
|
102
104
|
|
103
|
-
get contentTopics(): string[] {
|
105
|
+
public get contentTopics(): string[] {
|
104
106
|
return this.proto.contentTopics;
|
105
107
|
}
|
106
108
|
}
|
@@ -108,24 +110,24 @@ export class FilterSubscribeRpc {
|
|
108
110
|
export class FilterSubscribeResponse {
|
109
111
|
public constructor(public proto: proto.FilterSubscribeResponse) {}
|
110
112
|
|
111
|
-
static decode(bytes: Uint8Array): FilterSubscribeResponse {
|
113
|
+
public static decode(bytes: Uint8Array): FilterSubscribeResponse {
|
112
114
|
const res = proto.FilterSubscribeResponse.decode(bytes);
|
113
115
|
return new FilterSubscribeResponse(res);
|
114
116
|
}
|
115
117
|
|
116
|
-
encode(): Uint8Array {
|
118
|
+
public encode(): Uint8Array {
|
117
119
|
return proto.FilterSubscribeResponse.encode(this.proto);
|
118
120
|
}
|
119
121
|
|
120
|
-
get statusCode(): number {
|
122
|
+
public get statusCode(): number {
|
121
123
|
return this.proto.statusCode;
|
122
124
|
}
|
123
125
|
|
124
|
-
get statusDesc(): string | undefined {
|
126
|
+
public get statusDesc(): string | undefined {
|
125
127
|
return this.proto.statusDesc;
|
126
128
|
}
|
127
129
|
|
128
|
-
get requestId(): string {
|
130
|
+
public get requestId(): string {
|
129
131
|
return this.proto.requestId;
|
130
132
|
}
|
131
133
|
}
|
package/src/lib/filter/index.ts
CHANGED
@@ -32,10 +32,11 @@ export const FilterCodecs = {
|
|
32
32
|
};
|
33
33
|
|
34
34
|
export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
35
|
-
constructor(
|
35
|
+
public constructor(
|
36
36
|
private handleIncomingMessage: (
|
37
37
|
pubsubTopic: PubsubTopic,
|
38
|
-
wakuMessage: WakuMessage
|
38
|
+
wakuMessage: WakuMessage,
|
39
|
+
peerIdStr: string
|
39
40
|
) => Promise<void>,
|
40
41
|
libp2p: Libp2p,
|
41
42
|
options?: ProtocolCreateOptions
|
@@ -57,43 +58,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
57
58
|
});
|
58
59
|
}
|
59
60
|
|
60
|
-
|
61
|
-
const { connection, stream } = streamData;
|
62
|
-
const { remotePeer } = connection;
|
63
|
-
log.info(`Received message from ${remotePeer.toString()}`);
|
64
|
-
try {
|
65
|
-
pipe(stream, lp.decode, async (source) => {
|
66
|
-
for await (const bytes of source) {
|
67
|
-
const response = FilterPushRpc.decode(bytes.slice());
|
68
|
-
|
69
|
-
const { pubsubTopic, wakuMessage } = response;
|
70
|
-
|
71
|
-
if (!wakuMessage) {
|
72
|
-
log.error("Received empty message");
|
73
|
-
return;
|
74
|
-
}
|
75
|
-
|
76
|
-
if (!pubsubTopic) {
|
77
|
-
log.error("Pubsub topic missing from push message");
|
78
|
-
return;
|
79
|
-
}
|
80
|
-
|
81
|
-
await this.handleIncomingMessage(pubsubTopic, wakuMessage);
|
82
|
-
}
|
83
|
-
}).then(
|
84
|
-
() => {
|
85
|
-
log.info("Receiving pipe closed.");
|
86
|
-
},
|
87
|
-
(e) => {
|
88
|
-
log.error("Error with receiving pipe", e);
|
89
|
-
}
|
90
|
-
);
|
91
|
-
} catch (e) {
|
92
|
-
log.error("Error decoding message", e);
|
93
|
-
}
|
94
|
-
}
|
95
|
-
|
96
|
-
async subscribe(
|
61
|
+
public async subscribe(
|
97
62
|
pubsubTopic: PubsubTopic,
|
98
63
|
peer: Peer,
|
99
64
|
contentTopics: ContentTopic[]
|
@@ -147,7 +112,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
147
112
|
};
|
148
113
|
}
|
149
114
|
|
150
|
-
async unsubscribe(
|
115
|
+
public async unsubscribe(
|
151
116
|
pubsubTopic: PubsubTopic,
|
152
117
|
peer: Peer,
|
153
118
|
contentTopics: ContentTopic[]
|
@@ -193,7 +158,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
193
158
|
};
|
194
159
|
}
|
195
160
|
|
196
|
-
async unsubscribeAll(
|
161
|
+
public async unsubscribeAll(
|
197
162
|
pubsubTopic: PubsubTopic,
|
198
163
|
peer: Peer
|
199
164
|
): Promise<CoreProtocolResult> {
|
@@ -241,7 +206,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
241
206
|
};
|
242
207
|
}
|
243
208
|
|
244
|
-
async ping(peer: Peer): Promise<CoreProtocolResult> {
|
209
|
+
public async ping(peer: Peer): Promise<CoreProtocolResult> {
|
245
210
|
let stream: Stream | undefined;
|
246
211
|
try {
|
247
212
|
stream = await this.getStream(peer);
|
@@ -311,4 +276,44 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
311
276
|
failure: null
|
312
277
|
};
|
313
278
|
}
|
279
|
+
|
280
|
+
private onRequest(streamData: IncomingStreamData): void {
|
281
|
+
const { connection, stream } = streamData;
|
282
|
+
const { remotePeer } = connection;
|
283
|
+
log.info(`Received message from ${remotePeer.toString()}`);
|
284
|
+
try {
|
285
|
+
pipe(stream, lp.decode, async (source) => {
|
286
|
+
for await (const bytes of source) {
|
287
|
+
const response = FilterPushRpc.decode(bytes.slice());
|
288
|
+
|
289
|
+
const { pubsubTopic, wakuMessage } = response;
|
290
|
+
|
291
|
+
if (!wakuMessage) {
|
292
|
+
log.error("Received empty message");
|
293
|
+
return;
|
294
|
+
}
|
295
|
+
|
296
|
+
if (!pubsubTopic) {
|
297
|
+
log.error("Pubsub topic missing from push message");
|
298
|
+
return;
|
299
|
+
}
|
300
|
+
|
301
|
+
await this.handleIncomingMessage(
|
302
|
+
pubsubTopic,
|
303
|
+
wakuMessage,
|
304
|
+
connection.remotePeer.toString()
|
305
|
+
);
|
306
|
+
}
|
307
|
+
}).then(
|
308
|
+
() => {
|
309
|
+
log.info("Receiving pipe closed.");
|
310
|
+
},
|
311
|
+
(e) => {
|
312
|
+
log.error("Error with receiving pipe", e);
|
313
|
+
}
|
314
|
+
);
|
315
|
+
} catch (e) {
|
316
|
+
log.error("Error decoding message", e);
|
317
|
+
}
|
318
|
+
}
|
314
319
|
}
|
@@ -1,6 +1,5 @@
|
|
1
|
-
import type { PeerId
|
2
|
-
import type {
|
3
|
-
import type { IRelay, PeerIdStr } from "@waku/interfaces";
|
1
|
+
import type { PeerId } from "@libp2p/interface";
|
2
|
+
import type { IRelay, Libp2p, PeerIdStr } from "@waku/interfaces";
|
4
3
|
import type { KeepAliveOptions } from "@waku/interfaces";
|
5
4
|
import { Logger, pubsubTopicToSingleShardInfo } from "@waku/utils";
|
6
5
|
import { utf8ToBytes } from "@waku/utils/bytes";
|
@@ -10,24 +9,34 @@ import { createEncoder } from "./message/version_0.js";
|
|
10
9
|
export const RelayPingContentTopic = "/relay-ping/1/ping/null";
|
11
10
|
const log = new Logger("keep-alive");
|
12
11
|
|
12
|
+
type CreateKeepAliveManagerOptions = {
|
13
|
+
options: KeepAliveOptions;
|
14
|
+
libp2p: Libp2p;
|
15
|
+
relay?: IRelay;
|
16
|
+
};
|
17
|
+
|
13
18
|
export class KeepAliveManager {
|
14
|
-
private
|
15
|
-
private
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
private readonly relay?: IRelay;
|
20
|
+
private readonly libp2p: Libp2p;
|
21
|
+
|
22
|
+
private readonly options: KeepAliveOptions;
|
23
|
+
|
24
|
+
private pingKeepAliveTimers: Map<string, ReturnType<typeof setInterval>> =
|
25
|
+
new Map();
|
26
|
+
private relayKeepAliveTimers: Map<PeerId, ReturnType<typeof setInterval>[]> =
|
27
|
+
new Map();
|
28
|
+
|
29
|
+
public constructor({
|
30
|
+
options,
|
31
|
+
relay,
|
32
|
+
libp2p
|
33
|
+
}: CreateKeepAliveManagerOptions) {
|
22
34
|
this.options = options;
|
23
35
|
this.relay = relay;
|
36
|
+
this.libp2p = libp2p;
|
24
37
|
}
|
25
38
|
|
26
|
-
public start(
|
27
|
-
peerId: PeerId,
|
28
|
-
libp2pPing: PingService,
|
29
|
-
peerStore: PeerStore
|
30
|
-
): void {
|
39
|
+
public start(peerId: PeerId): void {
|
31
40
|
// Just in case a timer already exists for this peer
|
32
41
|
this.stop(peerId);
|
33
42
|
|
@@ -46,7 +55,7 @@ export class KeepAliveManager {
|
|
46
55
|
// ping the peer for keep alive
|
47
56
|
// also update the peer store with the latency
|
48
57
|
try {
|
49
|
-
ping = await
|
58
|
+
ping = await this.libp2p.services.ping.ping(peerId);
|
50
59
|
log.info(`Ping succeeded (${peerIdStr})`, ping);
|
51
60
|
} catch (error) {
|
52
61
|
log.error(`Ping failed for peer (${peerIdStr}).
|
@@ -56,7 +65,7 @@ export class KeepAliveManager {
|
|
56
65
|
}
|
57
66
|
|
58
67
|
try {
|
59
|
-
await peerStore.merge(peerId, {
|
68
|
+
await this.libp2p.peerStore.merge(peerId, {
|
60
69
|
metadata: {
|
61
70
|
ping: utf8ToBytes(ping.toString())
|
62
71
|
}
|
@@ -32,7 +32,7 @@ type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
|
|
32
32
|
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
33
33
|
*/
|
34
34
|
export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
35
|
-
constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
|
35
|
+
public constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
|
36
36
|
super(
|
37
37
|
LightPushCodec,
|
38
38
|
libp2p.components,
|
@@ -78,7 +78,7 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
78
78
|
}
|
79
79
|
}
|
80
80
|
|
81
|
-
async send(
|
81
|
+
public async send(
|
82
82
|
encoder: IEncoder,
|
83
83
|
message: IMessage,
|
84
84
|
peer: Peer
|
@@ -5,7 +5,7 @@ import { v4 as uuid } from "uuid";
|
|
5
5
|
export class PushRpc {
|
6
6
|
public constructor(public proto: proto.PushRpc) {}
|
7
7
|
|
8
|
-
static createRequest(
|
8
|
+
public static createRequest(
|
9
9
|
message: proto.WakuMessage,
|
10
10
|
pubsubTopic: string
|
11
11
|
): PushRpc {
|
@@ -19,20 +19,20 @@ export class PushRpc {
|
|
19
19
|
});
|
20
20
|
}
|
21
21
|
|
22
|
-
static decode(bytes: Uint8ArrayList): PushRpc {
|
22
|
+
public static decode(bytes: Uint8ArrayList): PushRpc {
|
23
23
|
const res = proto.PushRpc.decode(bytes);
|
24
24
|
return new PushRpc(res);
|
25
25
|
}
|
26
26
|
|
27
|
-
encode(): Uint8Array {
|
27
|
+
public encode(): Uint8Array {
|
28
28
|
return proto.PushRpc.encode(this.proto);
|
29
29
|
}
|
30
30
|
|
31
|
-
get query(): proto.PushRequest | undefined {
|
31
|
+
public get query(): proto.PushRequest | undefined {
|
32
32
|
return this.proto.request;
|
33
33
|
}
|
34
34
|
|
35
|
-
get response(): proto.PushResponse | undefined {
|
35
|
+
public get response(): proto.PushResponse | undefined {
|
36
36
|
return this.proto.response;
|
37
37
|
}
|
38
38
|
}
|
@@ -20,28 +20,28 @@ export const Version = 0;
|
|
20
20
|
export { proto };
|
21
21
|
|
22
22
|
export class DecodedMessage implements IDecodedMessage {
|
23
|
-
constructor(
|
23
|
+
public constructor(
|
24
24
|
public pubsubTopic: string,
|
25
25
|
protected proto: proto.WakuMessage
|
26
26
|
) {}
|
27
27
|
|
28
|
-
get ephemeral(): boolean {
|
28
|
+
public get ephemeral(): boolean {
|
29
29
|
return Boolean(this.proto.ephemeral);
|
30
30
|
}
|
31
31
|
|
32
|
-
get payload(): Uint8Array {
|
32
|
+
public get payload(): Uint8Array {
|
33
33
|
return this.proto.payload;
|
34
34
|
}
|
35
35
|
|
36
|
-
get contentTopic(): string {
|
36
|
+
public get contentTopic(): string {
|
37
37
|
return this.proto.contentTopic;
|
38
38
|
}
|
39
39
|
|
40
|
-
get _rawTimestamp(): bigint | undefined {
|
40
|
+
public get _rawTimestamp(): bigint | undefined {
|
41
41
|
return this.proto.timestamp;
|
42
42
|
}
|
43
43
|
|
44
|
-
get timestamp(): Date | undefined {
|
44
|
+
public get timestamp(): Date | undefined {
|
45
45
|
// In the case we receive a value that is bigger than JS's max number,
|
46
46
|
// we catch the error and return undefined.
|
47
47
|
try {
|
@@ -56,23 +56,23 @@ export class DecodedMessage implements IDecodedMessage {
|
|
56
56
|
}
|
57
57
|
}
|
58
58
|
|
59
|
-
get meta(): Uint8Array | undefined {
|
59
|
+
public get meta(): Uint8Array | undefined {
|
60
60
|
return this.proto.meta;
|
61
61
|
}
|
62
62
|
|
63
|
-
get version(): number {
|
63
|
+
public get version(): number {
|
64
64
|
// https://rfc.vac.dev/spec/14/
|
65
65
|
// > If omitted, the value SHOULD be interpreted as version 0.
|
66
66
|
return this.proto.version ?? 0;
|
67
67
|
}
|
68
68
|
|
69
|
-
get rateLimitProof(): IRateLimitProof | undefined {
|
69
|
+
public get rateLimitProof(): IRateLimitProof | undefined {
|
70
70
|
return this.proto.rateLimitProof;
|
71
71
|
}
|
72
72
|
}
|
73
73
|
|
74
74
|
export class Encoder implements IEncoder {
|
75
|
-
constructor(
|
75
|
+
public constructor(
|
76
76
|
public contentTopic: string,
|
77
77
|
public ephemeral: boolean = false,
|
78
78
|
public pubsubTopic: PubsubTopic,
|
@@ -83,11 +83,11 @@ export class Encoder implements IEncoder {
|
|
83
83
|
}
|
84
84
|
}
|
85
85
|
|
86
|
-
async toWire(message: IMessage): Promise<Uint8Array> {
|
86
|
+
public async toWire(message: IMessage): Promise<Uint8Array> {
|
87
87
|
return proto.WakuMessage.encode(await this.toProtoObj(message));
|
88
88
|
}
|
89
89
|
|
90
|
-
async toProtoObj(message: IMessage): Promise<IProtoMessage> {
|
90
|
+
public async toProtoObj(message: IMessage): Promise<IProtoMessage> {
|
91
91
|
const timestamp = message.timestamp ?? new Date();
|
92
92
|
|
93
93
|
const protoMessage = {
|
@@ -133,7 +133,7 @@ export function createEncoder({
|
|
133
133
|
}
|
134
134
|
|
135
135
|
export class Decoder implements IDecoder<DecodedMessage> {
|
136
|
-
constructor(
|
136
|
+
public constructor(
|
137
137
|
public pubsubTopic: PubsubTopic,
|
138
138
|
public contentTopic: string
|
139
139
|
) {
|
@@ -142,7 +142,9 @@ export class Decoder implements IDecoder<DecodedMessage> {
|
|
142
142
|
}
|
143
143
|
}
|
144
144
|
|
145
|
-
fromWireToProtoObj(
|
145
|
+
public fromWireToProtoObj(
|
146
|
+
bytes: Uint8Array
|
147
|
+
): Promise<IProtoMessage | undefined> {
|
146
148
|
const protoMessage = proto.WakuMessage.decode(bytes);
|
147
149
|
return Promise.resolve({
|
148
150
|
payload: protoMessage.payload,
|
@@ -155,7 +157,7 @@ export class Decoder implements IDecoder<DecodedMessage> {
|
|
155
157
|
});
|
156
158
|
}
|
157
159
|
|
158
|
-
async fromProtoObj(
|
160
|
+
public async fromProtoObj(
|
159
161
|
pubsubTopic: string,
|
160
162
|
proto: IProtoMessage
|
161
163
|
): Promise<DecodedMessage | undefined> {
|