@waku/core 0.0.30-e49e728.0 → 0.0.31-04bd518.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/CHANGELOG.md +27 -0
- package/bundle/{base_protocol-DCOj0QWD.js → base_protocol-BM6GbS_d.js} +20 -20
- package/bundle/{browser-zSUobdfj.js → index-DnW8ifxc.js} +616 -5
- package/bundle/index.js +103 -102
- package/bundle/lib/base_protocol.js +2 -3
- package/bundle/lib/message/version_0.js +2 -3
- package/bundle/{version_0-TEIsGmpJ.js → version_0-WhJLc79v.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 +4 -3
- package/dist/lib/connection_manager.js +37 -39
- 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 +34 -30
- 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 -1
- package/src/index.ts +1 -1
- package/src/lib/base_protocol.ts +4 -3
- package/src/lib/connection_manager.ts +54 -55
- package/src/lib/filter/filter_rpc.ts +21 -19
- package/src/lib/filter/index.ts +54 -45
- 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/bundle/index-BcSodzY4.js +0 -614
- package/bundle/lib/predefined_bootstrap_nodes.js +0 -81
- package/dist/lib/predefined_bootstrap_nodes.d.ts +0 -34
- package/dist/lib/predefined_bootstrap_nodes.js +0 -54
- package/dist/lib/predefined_bootstrap_nodes.js.map +0 -1
- package/dist/lib/stream_manager.js.map +0 -1
- package/src/lib/predefined_bootstrap_nodes.ts +0 -68
- /package/dist/lib/{stream_manager.d.ts → stream_manager/stream_manager.d.ts} +0 -0
@@ -89,6 +89,34 @@ export class ConnectionManager
|
|
89
89
|
return instance;
|
90
90
|
}
|
91
91
|
|
92
|
+
public stop(): void {
|
93
|
+
this.keepAliveManager.stopAll();
|
94
|
+
this.libp2p.removeEventListener(
|
95
|
+
"peer:connect",
|
96
|
+
this.onEventHandlers["peer:connect"]
|
97
|
+
);
|
98
|
+
this.libp2p.removeEventListener(
|
99
|
+
"peer:disconnect",
|
100
|
+
this.onEventHandlers["peer:disconnect"]
|
101
|
+
);
|
102
|
+
this.libp2p.removeEventListener(
|
103
|
+
"peer:discovery",
|
104
|
+
this.onEventHandlers["peer:discovery"]
|
105
|
+
);
|
106
|
+
}
|
107
|
+
|
108
|
+
public async dropConnection(peerId: PeerId): Promise<void> {
|
109
|
+
try {
|
110
|
+
this.keepAliveManager.stop(peerId);
|
111
|
+
await this.libp2p.hangUp(peerId);
|
112
|
+
log.info(`Dropped connection with peer ${peerId.toString()}`);
|
113
|
+
} catch (error) {
|
114
|
+
log.error(
|
115
|
+
`Error dropping connection with peer ${peerId.toString()} - ${error}`
|
116
|
+
);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
92
120
|
public async getPeersByDiscovery(): Promise<PeersByDiscoveryResult> {
|
93
121
|
const peersDiscovered = await this.libp2p.peerStore.all();
|
94
122
|
const peersConnected = this.libp2p
|
@@ -159,7 +187,11 @@ export class ConnectionManager
|
|
159
187
|
...options
|
160
188
|
};
|
161
189
|
|
162
|
-
this.keepAliveManager = new KeepAliveManager(
|
190
|
+
this.keepAliveManager = new KeepAliveManager({
|
191
|
+
relay,
|
192
|
+
libp2p,
|
193
|
+
options: keepAliveOptions
|
194
|
+
});
|
163
195
|
|
164
196
|
this.run()
|
165
197
|
.then(() => log.info(`Connection Manager is now running`))
|
@@ -200,22 +232,6 @@ export class ConnectionManager
|
|
200
232
|
this.startPeerDisconnectionListener();
|
201
233
|
}
|
202
234
|
|
203
|
-
stop(): void {
|
204
|
-
this.keepAliveManager.stopAll();
|
205
|
-
this.libp2p.removeEventListener(
|
206
|
-
"peer:connect",
|
207
|
-
this.onEventHandlers["peer:connect"]
|
208
|
-
);
|
209
|
-
this.libp2p.removeEventListener(
|
210
|
-
"peer:disconnect",
|
211
|
-
this.onEventHandlers["peer:disconnect"]
|
212
|
-
);
|
213
|
-
this.libp2p.removeEventListener(
|
214
|
-
"peer:discovery",
|
215
|
-
this.onEventHandlers["peer:discovery"]
|
216
|
-
);
|
217
|
-
}
|
218
|
-
|
219
235
|
private async dialPeer(peerId: PeerId): Promise<void> {
|
220
236
|
this.currentActiveParallelDialCount += 1;
|
221
237
|
let dialAttempt = 0;
|
@@ -238,6 +254,7 @@ export class ConnectionManager
|
|
238
254
|
this.dialAttemptsForPeer.set(peerId.toString(), -1);
|
239
255
|
|
240
256
|
// Dialing succeeded, break the loop
|
257
|
+
this.keepAliveManager.start(peerId);
|
241
258
|
break;
|
242
259
|
} catch (error) {
|
243
260
|
if (error instanceof AggregateError) {
|
@@ -298,18 +315,6 @@ export class ConnectionManager
|
|
298
315
|
}
|
299
316
|
}
|
300
317
|
|
301
|
-
private async dropConnection(peerId: PeerId): Promise<void> {
|
302
|
-
try {
|
303
|
-
this.keepAliveManager.stop(peerId);
|
304
|
-
await this.libp2p.hangUp(peerId);
|
305
|
-
log.info(`Dropped connection with peer ${peerId.toString()}`);
|
306
|
-
} catch (error) {
|
307
|
-
log.error(
|
308
|
-
`Error dropping connection with peer ${peerId.toString()} - ${error}`
|
309
|
-
);
|
310
|
-
}
|
311
|
-
}
|
312
|
-
|
313
318
|
private processDialQueue(): void {
|
314
319
|
if (
|
315
320
|
this.pendingPeerDialQueue.length > 0 &&
|
@@ -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
|
@@ -48,48 +49,16 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
48
49
|
options
|
49
50
|
);
|
50
51
|
|
51
|
-
libp2p
|
52
|
-
|
53
|
-
|
52
|
+
libp2p
|
53
|
+
.handle(FilterCodecs.PUSH, this.onRequest.bind(this), {
|
54
|
+
maxInboundStreams: 100
|
55
|
+
})
|
56
|
+
.catch((e) => {
|
57
|
+
log.error("Failed to register ", FilterCodecs.PUSH, e);
|
58
|
+
});
|
54
59
|
}
|
55
60
|
|
56
|
-
|
57
|
-
const { connection, stream } = streamData;
|
58
|
-
const { remotePeer } = connection;
|
59
|
-
log.info(`Received message from ${remotePeer.toString()}`);
|
60
|
-
try {
|
61
|
-
pipe(stream, lp.decode, async (source) => {
|
62
|
-
for await (const bytes of source) {
|
63
|
-
const response = FilterPushRpc.decode(bytes.slice());
|
64
|
-
|
65
|
-
const { pubsubTopic, wakuMessage } = response;
|
66
|
-
|
67
|
-
if (!wakuMessage) {
|
68
|
-
log.error("Received empty message");
|
69
|
-
return;
|
70
|
-
}
|
71
|
-
|
72
|
-
if (!pubsubTopic) {
|
73
|
-
log.error("Pubsub topic missing from push message");
|
74
|
-
return;
|
75
|
-
}
|
76
|
-
|
77
|
-
await this.handleIncomingMessage(pubsubTopic, wakuMessage);
|
78
|
-
}
|
79
|
-
}).then(
|
80
|
-
() => {
|
81
|
-
log.info("Receiving pipe closed.");
|
82
|
-
},
|
83
|
-
(e) => {
|
84
|
-
log.error("Error with receiving pipe", e);
|
85
|
-
}
|
86
|
-
);
|
87
|
-
} catch (e) {
|
88
|
-
log.error("Error decoding message", e);
|
89
|
-
}
|
90
|
-
}
|
91
|
-
|
92
|
-
async subscribe(
|
61
|
+
public async subscribe(
|
93
62
|
pubsubTopic: PubsubTopic,
|
94
63
|
peer: Peer,
|
95
64
|
contentTopics: ContentTopic[]
|
@@ -143,7 +112,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
143
112
|
};
|
144
113
|
}
|
145
114
|
|
146
|
-
async unsubscribe(
|
115
|
+
public async unsubscribe(
|
147
116
|
pubsubTopic: PubsubTopic,
|
148
117
|
peer: Peer,
|
149
118
|
contentTopics: ContentTopic[]
|
@@ -189,7 +158,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
189
158
|
};
|
190
159
|
}
|
191
160
|
|
192
|
-
async unsubscribeAll(
|
161
|
+
public async unsubscribeAll(
|
193
162
|
pubsubTopic: PubsubTopic,
|
194
163
|
peer: Peer
|
195
164
|
): Promise<CoreProtocolResult> {
|
@@ -237,7 +206,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
237
206
|
};
|
238
207
|
}
|
239
208
|
|
240
|
-
async ping(peer: Peer): Promise<CoreProtocolResult> {
|
209
|
+
public async ping(peer: Peer): Promise<CoreProtocolResult> {
|
241
210
|
let stream: Stream | undefined;
|
242
211
|
try {
|
243
212
|
stream = await this.getStream(peer);
|
@@ -307,4 +276,44 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
307
276
|
failure: null
|
308
277
|
};
|
309
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
|
+
}
|
310
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
|
}
|