@waku/core 0.0.29-fd60cc2.0 → 0.0.30-00c77c6.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 +33 -0
- package/bundle/{base_protocol-B8NGoK0E.js → base_protocol-kugTP2Op.js} +2 -2
- package/bundle/{browser-DoQRY-an.js → browser-B9234RhB.js} +5 -0
- package/bundle/{index-DBP1NHED.js → index-egXdK_Fb.js} +6 -14
- package/bundle/index.js +160 -371
- 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-B6lI5jri.js → version_0-BoLZMvhu.js} +3 -3
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/lib/filter/index.d.ts +13 -2
- package/dist/lib/filter/index.js +151 -273
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/keep_alive_manager.js +1 -1
- package/dist/lib/light_push/index.d.ts +3 -4
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.js +1 -1
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/lib/filter/index.ts +223 -481
- package/src/lib/keep_alive_manager.ts +1 -1
- package/src/lib/light_push/index.ts +10 -12
- package/src/lib/metadata/index.ts +6 -4
- package/src/lib/wait_for_remote_peer.ts +1 -1
package/src/lib/filter/index.ts
CHANGED
@@ -1,34 +1,20 @@
|
|
1
|
-
import { Stream } from "@libp2p/interface";
|
2
|
-
import type { Peer } from "@libp2p/interface";
|
1
|
+
import type { Peer, Stream } from "@libp2p/interface";
|
3
2
|
import type { IncomingStreamData } from "@libp2p/interface-internal";
|
4
|
-
import
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
IReceiver,
|
13
|
-
Libp2p,
|
14
|
-
ProtocolCreateOptions,
|
15
|
-
PubsubTopic,
|
16
|
-
SingleShardInfo,
|
17
|
-
Unsubscribe
|
3
|
+
import {
|
4
|
+
type ContentTopic,
|
5
|
+
type CoreProtocolResult,
|
6
|
+
type IBaseProtocolCore,
|
7
|
+
type Libp2p,
|
8
|
+
type ProtocolCreateOptions,
|
9
|
+
ProtocolError,
|
10
|
+
type PubsubTopic
|
18
11
|
} from "@waku/interfaces";
|
19
|
-
import { DefaultPubsubTopic } from "@waku/interfaces";
|
20
|
-
import { messageHashStr } from "@waku/message-hash";
|
21
12
|
import { WakuMessage } from "@waku/proto";
|
22
|
-
import {
|
23
|
-
ensurePubsubTopicIsConfigured,
|
24
|
-
groupByContentTopic,
|
25
|
-
singleShardInfoToPubsubTopic,
|
26
|
-
toAsyncIterator
|
27
|
-
} from "@waku/utils";
|
28
13
|
import { Logger } from "@waku/utils";
|
29
14
|
import all from "it-all";
|
30
15
|
import * as lp from "it-length-prefixed";
|
31
16
|
import { pipe } from "it-pipe";
|
17
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
32
18
|
|
33
19
|
import { BaseProtocol } from "../base_protocol.js";
|
34
20
|
|
@@ -40,329 +26,20 @@ import {
|
|
40
26
|
|
41
27
|
const log = new Logger("filter:v2");
|
42
28
|
|
43
|
-
type SubscriptionCallback<T extends IDecodedMessage> = {
|
44
|
-
decoders: IDecoder<T>[];
|
45
|
-
callback: Callback<T>;
|
46
|
-
};
|
47
|
-
|
48
29
|
export const FilterCodecs = {
|
49
30
|
SUBSCRIBE: "/vac/waku/filter-subscribe/2.0.0-beta1",
|
50
31
|
PUSH: "/vac/waku/filter-push/2.0.0-beta1"
|
51
32
|
};
|
52
33
|
|
53
|
-
|
54
|
-
* A subscription object refers to a subscription to a given pubsub topic.
|
55
|
-
*/
|
56
|
-
class Subscription {
|
57
|
-
readonly peers: Peer[];
|
58
|
-
private readonly pubsubTopic: PubsubTopic;
|
59
|
-
private newStream: (peer: Peer) => Promise<Stream>;
|
60
|
-
readonly receivedMessagesHashStr: string[] = [];
|
61
|
-
|
62
|
-
private subscriptionCallbacks: Map<
|
63
|
-
ContentTopic,
|
64
|
-
SubscriptionCallback<IDecodedMessage>
|
65
|
-
>;
|
66
|
-
|
34
|
+
export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
67
35
|
constructor(
|
68
|
-
|
69
|
-
|
70
|
-
|
36
|
+
private handleIncomingMessage: (
|
37
|
+
pubsubTopic: PubsubTopic,
|
38
|
+
wakuMessage: WakuMessage
|
39
|
+
) => Promise<void>,
|
40
|
+
libp2p: Libp2p,
|
41
|
+
options?: ProtocolCreateOptions
|
71
42
|
) {
|
72
|
-
this.peers = remotePeers;
|
73
|
-
this.pubsubTopic = pubsubTopic;
|
74
|
-
this.newStream = newStream;
|
75
|
-
this.subscriptionCallbacks = new Map();
|
76
|
-
}
|
77
|
-
|
78
|
-
async subscribe<T extends IDecodedMessage>(
|
79
|
-
decoders: IDecoder<T> | IDecoder<T>[],
|
80
|
-
callback: Callback<T>
|
81
|
-
): Promise<void> {
|
82
|
-
const decodersArray = Array.isArray(decoders) ? decoders : [decoders];
|
83
|
-
|
84
|
-
// check that all decoders are configured for the same pubsub topic as this subscription
|
85
|
-
decodersArray.forEach((decoder) => {
|
86
|
-
if (decoder.pubsubTopic !== this.pubsubTopic) {
|
87
|
-
throw new Error(
|
88
|
-
`Pubsub topic not configured: decoder is configured for pubsub topic ${decoder.pubsubTopic} but this subscription is for pubsub topic ${this.pubsubTopic}. Please create a new Subscription for the different pubsub topic.`
|
89
|
-
);
|
90
|
-
}
|
91
|
-
});
|
92
|
-
|
93
|
-
const decodersGroupedByCT = groupByContentTopic(decodersArray);
|
94
|
-
const contentTopics = Array.from(decodersGroupedByCT.keys());
|
95
|
-
|
96
|
-
const promises = this.peers.map(async (peer) => {
|
97
|
-
const stream = await this.newStream(peer);
|
98
|
-
|
99
|
-
const request = FilterSubscribeRpc.createSubscribeRequest(
|
100
|
-
this.pubsubTopic,
|
101
|
-
contentTopics
|
102
|
-
);
|
103
|
-
|
104
|
-
try {
|
105
|
-
const res = await pipe(
|
106
|
-
[request.encode()],
|
107
|
-
lp.encode,
|
108
|
-
stream,
|
109
|
-
lp.decode,
|
110
|
-
async (source) => await all(source)
|
111
|
-
);
|
112
|
-
|
113
|
-
if (!res || !res.length) {
|
114
|
-
throw Error(
|
115
|
-
`No response received for request ${request.requestId}: ${res}`
|
116
|
-
);
|
117
|
-
}
|
118
|
-
|
119
|
-
const { statusCode, requestId, statusDesc } =
|
120
|
-
FilterSubscribeResponse.decode(res[0].slice());
|
121
|
-
|
122
|
-
if (statusCode < 200 || statusCode >= 300) {
|
123
|
-
throw new Error(
|
124
|
-
`Filter subscribe request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
|
125
|
-
);
|
126
|
-
}
|
127
|
-
|
128
|
-
log.info(
|
129
|
-
"Subscribed to peer ",
|
130
|
-
peer.id.toString(),
|
131
|
-
"for content topics",
|
132
|
-
contentTopics
|
133
|
-
);
|
134
|
-
} catch (e) {
|
135
|
-
throw new Error(
|
136
|
-
"Error subscribing to peer: " +
|
137
|
-
peer.id.toString() +
|
138
|
-
" for content topics: " +
|
139
|
-
contentTopics +
|
140
|
-
": " +
|
141
|
-
e
|
142
|
-
);
|
143
|
-
}
|
144
|
-
});
|
145
|
-
|
146
|
-
const results = await Promise.allSettled(promises);
|
147
|
-
|
148
|
-
this.handleErrors(results, "subscribe");
|
149
|
-
|
150
|
-
// Save the callback functions by content topics so they
|
151
|
-
// can easily be removed (reciprocally replaced) if `unsubscribe` (reciprocally `subscribe`)
|
152
|
-
// is called for those content topics
|
153
|
-
decodersGroupedByCT.forEach((decoders, contentTopic) => {
|
154
|
-
// Cast the type because a given `subscriptionCallbacks` map may hold
|
155
|
-
// Decoder that decode to different implementations of `IDecodedMessage`
|
156
|
-
const subscriptionCallback = {
|
157
|
-
decoders,
|
158
|
-
callback
|
159
|
-
} as unknown as SubscriptionCallback<IDecodedMessage>;
|
160
|
-
|
161
|
-
// The callback and decoder may override previous values, this is on
|
162
|
-
// purpose as the user may call `subscribe` to refresh the subscription
|
163
|
-
this.subscriptionCallbacks.set(contentTopic, subscriptionCallback);
|
164
|
-
});
|
165
|
-
}
|
166
|
-
|
167
|
-
async unsubscribe(contentTopics: ContentTopic[]): Promise<void> {
|
168
|
-
const promises = this.peers.map(async (peer) => {
|
169
|
-
const stream = await this.newStream(peer);
|
170
|
-
const unsubscribeRequest = FilterSubscribeRpc.createUnsubscribeRequest(
|
171
|
-
this.pubsubTopic,
|
172
|
-
contentTopics
|
173
|
-
);
|
174
|
-
|
175
|
-
try {
|
176
|
-
await pipe([unsubscribeRequest.encode()], lp.encode, stream.sink);
|
177
|
-
} catch (error) {
|
178
|
-
throw new Error("Error unsubscribing: " + error);
|
179
|
-
}
|
180
|
-
|
181
|
-
contentTopics.forEach((contentTopic: string) => {
|
182
|
-
this.subscriptionCallbacks.delete(contentTopic);
|
183
|
-
});
|
184
|
-
});
|
185
|
-
|
186
|
-
const results = await Promise.allSettled(promises);
|
187
|
-
|
188
|
-
this.handleErrors(results, "unsubscribe");
|
189
|
-
}
|
190
|
-
|
191
|
-
async ping(): Promise<void> {
|
192
|
-
const promises = this.peers.map(async (peer) => {
|
193
|
-
const stream = await this.newStream(peer);
|
194
|
-
|
195
|
-
const request = FilterSubscribeRpc.createSubscriberPingRequest();
|
196
|
-
|
197
|
-
try {
|
198
|
-
const res = await pipe(
|
199
|
-
[request.encode()],
|
200
|
-
lp.encode,
|
201
|
-
stream,
|
202
|
-
lp.decode,
|
203
|
-
async (source) => await all(source)
|
204
|
-
);
|
205
|
-
|
206
|
-
if (!res || !res.length) {
|
207
|
-
throw Error(
|
208
|
-
`No response received for request ${request.requestId}: ${res}`
|
209
|
-
);
|
210
|
-
}
|
211
|
-
|
212
|
-
const { statusCode, requestId, statusDesc } =
|
213
|
-
FilterSubscribeResponse.decode(res[0].slice());
|
214
|
-
|
215
|
-
if (statusCode < 200 || statusCode >= 300) {
|
216
|
-
throw new Error(
|
217
|
-
`Filter ping request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
|
218
|
-
);
|
219
|
-
}
|
220
|
-
log.info(`Ping successful for peer ${peer.id.toString()}`);
|
221
|
-
} catch (error) {
|
222
|
-
log.error("Error pinging: ", error);
|
223
|
-
throw error; // Rethrow the actual error instead of wrapping it
|
224
|
-
}
|
225
|
-
});
|
226
|
-
|
227
|
-
const results = await Promise.allSettled(promises);
|
228
|
-
|
229
|
-
this.handleErrors(results, "ping");
|
230
|
-
}
|
231
|
-
|
232
|
-
async unsubscribeAll(): Promise<void> {
|
233
|
-
const promises = this.peers.map(async (peer) => {
|
234
|
-
const stream = await this.newStream(peer);
|
235
|
-
|
236
|
-
const request = FilterSubscribeRpc.createUnsubscribeAllRequest(
|
237
|
-
this.pubsubTopic
|
238
|
-
);
|
239
|
-
|
240
|
-
try {
|
241
|
-
const res = await pipe(
|
242
|
-
[request.encode()],
|
243
|
-
lp.encode,
|
244
|
-
stream,
|
245
|
-
lp.decode,
|
246
|
-
async (source) => await all(source)
|
247
|
-
);
|
248
|
-
|
249
|
-
if (!res || !res.length) {
|
250
|
-
throw Error(
|
251
|
-
`No response received for request ${request.requestId}: ${res}`
|
252
|
-
);
|
253
|
-
}
|
254
|
-
|
255
|
-
const { statusCode, requestId, statusDesc } =
|
256
|
-
FilterSubscribeResponse.decode(res[0].slice());
|
257
|
-
|
258
|
-
if (statusCode < 200 || statusCode >= 300) {
|
259
|
-
throw new Error(
|
260
|
-
`Filter unsubscribe all request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
|
261
|
-
);
|
262
|
-
}
|
263
|
-
|
264
|
-
this.subscriptionCallbacks.clear();
|
265
|
-
log.info(
|
266
|
-
`Unsubscribed from all content topics for pubsub topic ${this.pubsubTopic}`
|
267
|
-
);
|
268
|
-
} catch (error) {
|
269
|
-
throw new Error(
|
270
|
-
"Error unsubscribing from all content topics: " + error
|
271
|
-
);
|
272
|
-
}
|
273
|
-
});
|
274
|
-
|
275
|
-
const results = await Promise.allSettled(promises);
|
276
|
-
|
277
|
-
this.handleErrors(results, "unsubscribeAll");
|
278
|
-
}
|
279
|
-
|
280
|
-
async processMessage(message: WakuMessage): Promise<void> {
|
281
|
-
const hashedMessageStr = messageHashStr(
|
282
|
-
this.pubsubTopic,
|
283
|
-
message as IProtoMessage
|
284
|
-
);
|
285
|
-
if (this.receivedMessagesHashStr.includes(hashedMessageStr)) {
|
286
|
-
log.info("Message already received, skipping");
|
287
|
-
return;
|
288
|
-
}
|
289
|
-
this.receivedMessagesHashStr.push(hashedMessageStr);
|
290
|
-
|
291
|
-
const { contentTopic } = message;
|
292
|
-
const subscriptionCallback = this.subscriptionCallbacks.get(contentTopic);
|
293
|
-
if (!subscriptionCallback) {
|
294
|
-
log.error("No subscription callback available for ", contentTopic);
|
295
|
-
return;
|
296
|
-
}
|
297
|
-
log.info(
|
298
|
-
"Processing message with content topic ",
|
299
|
-
contentTopic,
|
300
|
-
" on pubsub topic ",
|
301
|
-
this.pubsubTopic
|
302
|
-
);
|
303
|
-
await pushMessage(subscriptionCallback, this.pubsubTopic, message);
|
304
|
-
}
|
305
|
-
|
306
|
-
// Filter out only the rejected promises and extract & handle their reasons
|
307
|
-
private handleErrors(
|
308
|
-
results: PromiseSettledResult<void>[],
|
309
|
-
type: "ping" | "subscribe" | "unsubscribe" | "unsubscribeAll"
|
310
|
-
): void {
|
311
|
-
const errors = results
|
312
|
-
.filter(
|
313
|
-
(result): result is PromiseRejectedResult =>
|
314
|
-
result.status === "rejected"
|
315
|
-
)
|
316
|
-
.map((rejectedResult) => rejectedResult.reason);
|
317
|
-
|
318
|
-
if (errors.length === this.peers.length) {
|
319
|
-
const errorCounts = new Map<string, number>();
|
320
|
-
// TODO: streamline error logging with https://github.com/orgs/waku-org/projects/2/views/1?pane=issue&itemId=42849952
|
321
|
-
errors.forEach((error) => {
|
322
|
-
const message = error instanceof Error ? error.message : String(error);
|
323
|
-
errorCounts.set(message, (errorCounts.get(message) || 0) + 1);
|
324
|
-
});
|
325
|
-
|
326
|
-
const uniqueErrorMessages = Array.from(
|
327
|
-
errorCounts,
|
328
|
-
([message, count]) => `${message} (occurred ${count} times)`
|
329
|
-
).join(", ");
|
330
|
-
throw new Error(`Error ${type} all peers: ${uniqueErrorMessages}`);
|
331
|
-
} else if (errors.length > 0) {
|
332
|
-
// TODO: handle renewing faulty peers with new peers (https://github.com/waku-org/js-waku/issues/1463)
|
333
|
-
log.warn(
|
334
|
-
`Some ${type} failed. These will be refreshed with new peers`,
|
335
|
-
errors
|
336
|
-
);
|
337
|
-
} else {
|
338
|
-
log.info(`${type} successful for all peers`);
|
339
|
-
}
|
340
|
-
}
|
341
|
-
}
|
342
|
-
|
343
|
-
const DEFAULT_NUM_PEERS = 3;
|
344
|
-
|
345
|
-
class Filter extends BaseProtocol implements IReceiver {
|
346
|
-
private activeSubscriptions = new Map<string, Subscription>();
|
347
|
-
|
348
|
-
private getActiveSubscription(
|
349
|
-
pubsubTopic: PubsubTopic
|
350
|
-
): Subscription | undefined {
|
351
|
-
return this.activeSubscriptions.get(pubsubTopic);
|
352
|
-
}
|
353
|
-
|
354
|
-
private setActiveSubscription(
|
355
|
-
pubsubTopic: PubsubTopic,
|
356
|
-
subscription: Subscription
|
357
|
-
): Subscription {
|
358
|
-
this.activeSubscriptions.set(pubsubTopic, subscription);
|
359
|
-
return subscription;
|
360
|
-
}
|
361
|
-
|
362
|
-
//TODO: Remove when FilterCore and FilterSDK are introduced
|
363
|
-
private readonly numPeersToUse: number;
|
364
|
-
|
365
|
-
constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
|
366
43
|
super(
|
367
44
|
FilterCodecs.SUBSCRIBE,
|
368
45
|
libp2p.components,
|
@@ -371,106 +48,9 @@ class Filter extends BaseProtocol implements IReceiver {
|
|
371
48
|
options
|
372
49
|
);
|
373
50
|
|
374
|
-
this.numPeersToUse = options?.numPeersToUse ?? DEFAULT_NUM_PEERS;
|
375
|
-
|
376
51
|
libp2p.handle(FilterCodecs.PUSH, this.onRequest.bind(this)).catch((e) => {
|
377
52
|
log.error("Failed to register ", FilterCodecs.PUSH, e);
|
378
53
|
});
|
379
|
-
|
380
|
-
this.activeSubscriptions = new Map();
|
381
|
-
}
|
382
|
-
|
383
|
-
/**
|
384
|
-
* Creates a new subscription to the given pubsub topic.
|
385
|
-
* The subscription is made to multiple peers for decentralization.
|
386
|
-
* @param pubsubTopicShardInfo The pubsub topic to subscribe to.
|
387
|
-
* @returns The subscription object.
|
388
|
-
*/
|
389
|
-
async createSubscription(
|
390
|
-
pubsubTopicShardInfo: SingleShardInfo | PubsubTopic = DefaultPubsubTopic
|
391
|
-
): Promise<Subscription> {
|
392
|
-
const pubsubTopic =
|
393
|
-
typeof pubsubTopicShardInfo == "string"
|
394
|
-
? pubsubTopicShardInfo
|
395
|
-
: singleShardInfoToPubsubTopic(pubsubTopicShardInfo);
|
396
|
-
|
397
|
-
ensurePubsubTopicIsConfigured(pubsubTopic, this.pubsubTopics);
|
398
|
-
|
399
|
-
const peers = await this.getPeers({
|
400
|
-
maxBootstrapPeers: 1,
|
401
|
-
numPeers: this.numPeersToUse
|
402
|
-
});
|
403
|
-
if (peers.length === 0) {
|
404
|
-
throw new Error("No peer found to initiate subscription.");
|
405
|
-
}
|
406
|
-
|
407
|
-
log.info(
|
408
|
-
`Creating filter subscription with ${peers.length} peers: `,
|
409
|
-
peers.map((peer) => peer.id.toString())
|
410
|
-
);
|
411
|
-
|
412
|
-
const subscription =
|
413
|
-
this.getActiveSubscription(pubsubTopic) ??
|
414
|
-
this.setActiveSubscription(
|
415
|
-
pubsubTopic,
|
416
|
-
new Subscription(pubsubTopic, peers, this.getStream.bind(this))
|
417
|
-
);
|
418
|
-
|
419
|
-
return subscription;
|
420
|
-
}
|
421
|
-
|
422
|
-
public toSubscriptionIterator<T extends IDecodedMessage>(
|
423
|
-
decoders: IDecoder<T> | IDecoder<T>[]
|
424
|
-
): Promise<IAsyncIterator<T>> {
|
425
|
-
return toAsyncIterator(this, decoders);
|
426
|
-
}
|
427
|
-
|
428
|
-
/**
|
429
|
-
* This method is used to satisfy the `IReceiver` interface.
|
430
|
-
*
|
431
|
-
* @hidden
|
432
|
-
*
|
433
|
-
* @param decoders The decoders to use for the subscription.
|
434
|
-
* @param callback The callback function to use for the subscription.
|
435
|
-
* @param opts Optional protocol options for the subscription.
|
436
|
-
*
|
437
|
-
* @returns A Promise that resolves to a function that unsubscribes from the subscription.
|
438
|
-
*
|
439
|
-
* @remarks
|
440
|
-
* This method should not be used directly.
|
441
|
-
* Instead, use `createSubscription` to create a new subscription.
|
442
|
-
*/
|
443
|
-
async subscribe<T extends IDecodedMessage>(
|
444
|
-
decoders: IDecoder<T> | IDecoder<T>[],
|
445
|
-
callback: Callback<T>
|
446
|
-
): Promise<Unsubscribe> {
|
447
|
-
const pubsubTopics = this.getPubsubTopics(decoders);
|
448
|
-
|
449
|
-
if (pubsubTopics.length === 0) {
|
450
|
-
throw Error(
|
451
|
-
"Failed to subscribe: no pubsubTopic found on decoders provided."
|
452
|
-
);
|
453
|
-
}
|
454
|
-
|
455
|
-
if (pubsubTopics.length > 1) {
|
456
|
-
throw Error(
|
457
|
-
"Failed to subscribe: all decoders should have the same pubsub topic. Use createSubscription to be more agile."
|
458
|
-
);
|
459
|
-
}
|
460
|
-
|
461
|
-
const subscription = await this.createSubscription(pubsubTopics[0]);
|
462
|
-
|
463
|
-
await subscription.subscribe(decoders, callback);
|
464
|
-
|
465
|
-
const contentTopics = Array.from(
|
466
|
-
groupByContentTopic(
|
467
|
-
Array.isArray(decoders) ? decoders : [decoders]
|
468
|
-
).keys()
|
469
|
-
);
|
470
|
-
|
471
|
-
return async () => {
|
472
|
-
await subscription.unsubscribe(contentTopics);
|
473
|
-
};
|
474
54
|
}
|
475
55
|
|
476
56
|
private onRequest(streamData: IncomingStreamData): void {
|
@@ -494,16 +74,7 @@ class Filter extends BaseProtocol implements IReceiver {
|
|
494
74
|
return;
|
495
75
|
}
|
496
76
|
|
497
|
-
|
498
|
-
|
499
|
-
if (!subscription) {
|
500
|
-
log.error(
|
501
|
-
`No subscription locally registered for topic ${pubsubTopic}`
|
502
|
-
);
|
503
|
-
return;
|
504
|
-
}
|
505
|
-
|
506
|
-
await subscription.processMessage(wakuMessage);
|
77
|
+
await this.handleIncomingMessage(pubsubTopic, wakuMessage);
|
507
78
|
}
|
508
79
|
}).then(
|
509
80
|
() => {
|
@@ -518,51 +89,222 @@ class Filter extends BaseProtocol implements IReceiver {
|
|
518
89
|
}
|
519
90
|
}
|
520
91
|
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
92
|
+
async subscribe(
|
93
|
+
pubsubTopic: PubsubTopic,
|
94
|
+
peer: Peer,
|
95
|
+
contentTopics: ContentTopic[]
|
96
|
+
): Promise<CoreProtocolResult> {
|
97
|
+
const stream = await this.getStream(peer);
|
98
|
+
|
99
|
+
const request = FilterSubscribeRpc.createSubscribeRequest(
|
100
|
+
pubsubTopic,
|
101
|
+
contentTopics
|
102
|
+
);
|
525
103
|
|
526
|
-
|
527
|
-
|
104
|
+
let res: Uint8ArrayList[] | undefined;
|
105
|
+
try {
|
106
|
+
res = await pipe(
|
107
|
+
[request.encode()],
|
108
|
+
lp.encode,
|
109
|
+
stream,
|
110
|
+
lp.decode,
|
111
|
+
async (source) => await all(source)
|
112
|
+
);
|
113
|
+
} catch (error) {
|
114
|
+
log.error("Failed to send subscribe request", error);
|
115
|
+
return {
|
116
|
+
success: null,
|
117
|
+
failure: {
|
118
|
+
error: ProtocolError.GENERIC_FAIL,
|
119
|
+
peerId: peer.id
|
120
|
+
}
|
121
|
+
};
|
528
122
|
}
|
529
123
|
|
530
|
-
const
|
124
|
+
const { statusCode, requestId, statusDesc } =
|
125
|
+
FilterSubscribeResponse.decode(res[0].slice());
|
126
|
+
|
127
|
+
if (statusCode < 200 || statusCode >= 300) {
|
128
|
+
log.error(
|
129
|
+
`Filter subscribe request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
|
130
|
+
);
|
131
|
+
return {
|
132
|
+
failure: {
|
133
|
+
error: ProtocolError.REMOTE_PEER_REJECTED,
|
134
|
+
peerId: peer.id
|
135
|
+
},
|
136
|
+
success: null
|
137
|
+
};
|
138
|
+
}
|
531
139
|
|
532
|
-
return
|
140
|
+
return {
|
141
|
+
failure: null,
|
142
|
+
success: peer.id
|
143
|
+
};
|
533
144
|
}
|
534
|
-
}
|
535
145
|
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
146
|
+
async unsubscribe(
|
147
|
+
pubsubTopic: PubsubTopic,
|
148
|
+
peer: Peer,
|
149
|
+
contentTopics: ContentTopic[]
|
150
|
+
): Promise<CoreProtocolResult> {
|
151
|
+
let stream: Stream | undefined;
|
152
|
+
try {
|
153
|
+
stream = await this.getStream(peer);
|
154
|
+
} catch (error) {
|
155
|
+
log.error(
|
156
|
+
`Failed to get a stream for remote peer${peer.id.toString()}`,
|
157
|
+
error
|
158
|
+
);
|
159
|
+
return {
|
160
|
+
success: null,
|
161
|
+
failure: {
|
162
|
+
error: ProtocolError.REMOTE_PEER_FAULT,
|
163
|
+
peerId: peer.id
|
164
|
+
}
|
165
|
+
};
|
166
|
+
}
|
167
|
+
|
168
|
+
const unsubscribeRequest = FilterSubscribeRpc.createUnsubscribeRequest(
|
169
|
+
pubsubTopic,
|
170
|
+
contentTopics
|
171
|
+
);
|
541
172
|
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
173
|
+
try {
|
174
|
+
await pipe([unsubscribeRequest.encode()], lp.encode, stream.sink);
|
175
|
+
} catch (error) {
|
176
|
+
log.error("Failed to send unsubscribe request", error);
|
177
|
+
return {
|
178
|
+
success: null,
|
179
|
+
failure: {
|
180
|
+
error: ProtocolError.GENERIC_FAIL,
|
181
|
+
peerId: peer.id
|
182
|
+
}
|
183
|
+
};
|
184
|
+
}
|
185
|
+
|
186
|
+
return {
|
187
|
+
success: peer.id,
|
188
|
+
failure: null
|
189
|
+
};
|
553
190
|
}
|
554
191
|
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
192
|
+
async unsubscribeAll(
|
193
|
+
pubsubTopic: PubsubTopic,
|
194
|
+
peer: Peer
|
195
|
+
): Promise<CoreProtocolResult> {
|
196
|
+
const stream = await this.getStream(peer);
|
197
|
+
|
198
|
+
const request = FilterSubscribeRpc.createUnsubscribeAllRequest(pubsubTopic);
|
199
|
+
|
200
|
+
const res = await pipe(
|
201
|
+
[request.encode()],
|
202
|
+
lp.encode,
|
203
|
+
stream,
|
204
|
+
lp.decode,
|
205
|
+
async (source) => await all(source)
|
560
206
|
);
|
561
207
|
|
562
|
-
|
208
|
+
if (!res || !res.length) {
|
209
|
+
return {
|
210
|
+
failure: {
|
211
|
+
error: ProtocolError.REMOTE_PEER_FAULT,
|
212
|
+
peerId: peer.id
|
213
|
+
},
|
214
|
+
success: null
|
215
|
+
};
|
216
|
+
}
|
217
|
+
|
218
|
+
const { statusCode, requestId, statusDesc } =
|
219
|
+
FilterSubscribeResponse.decode(res[0].slice());
|
220
|
+
|
221
|
+
if (statusCode < 200 || statusCode >= 300) {
|
222
|
+
log.error(
|
223
|
+
`Filter unsubscribe all request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
|
224
|
+
);
|
225
|
+
return {
|
226
|
+
failure: {
|
227
|
+
error: ProtocolError.REMOTE_PEER_REJECTED,
|
228
|
+
peerId: peer.id
|
229
|
+
},
|
230
|
+
success: null
|
231
|
+
};
|
232
|
+
}
|
233
|
+
|
234
|
+
return {
|
235
|
+
failure: null,
|
236
|
+
success: peer.id
|
237
|
+
};
|
238
|
+
}
|
239
|
+
|
240
|
+
async ping(peer: Peer): Promise<CoreProtocolResult> {
|
241
|
+
let stream: Stream | undefined;
|
242
|
+
try {
|
243
|
+
stream = await this.getStream(peer);
|
244
|
+
} catch (error) {
|
245
|
+
log.error(
|
246
|
+
`Failed to get a stream for remote peer${peer.id.toString()}`,
|
247
|
+
error
|
248
|
+
);
|
249
|
+
return {
|
250
|
+
success: null,
|
251
|
+
failure: {
|
252
|
+
error: ProtocolError.REMOTE_PEER_FAULT,
|
253
|
+
peerId: peer.id
|
254
|
+
}
|
255
|
+
};
|
256
|
+
}
|
257
|
+
|
258
|
+
const request = FilterSubscribeRpc.createSubscriberPingRequest();
|
259
|
+
|
260
|
+
let res: Uint8ArrayList[] | undefined;
|
261
|
+
try {
|
262
|
+
res = await pipe(
|
263
|
+
[request.encode()],
|
264
|
+
lp.encode,
|
265
|
+
stream,
|
266
|
+
lp.decode,
|
267
|
+
async (source) => await all(source)
|
268
|
+
);
|
269
|
+
} catch (error) {
|
270
|
+
log.error("Failed to send ping request", error);
|
271
|
+
return {
|
272
|
+
success: null,
|
273
|
+
failure: {
|
274
|
+
error: ProtocolError.GENERIC_FAIL,
|
275
|
+
peerId: peer.id
|
276
|
+
}
|
277
|
+
};
|
278
|
+
}
|
279
|
+
|
280
|
+
if (!res || !res.length) {
|
281
|
+
return {
|
282
|
+
success: null,
|
283
|
+
failure: {
|
284
|
+
error: ProtocolError.REMOTE_PEER_FAULT,
|
285
|
+
peerId: peer.id
|
286
|
+
}
|
287
|
+
};
|
288
|
+
}
|
289
|
+
|
290
|
+
const { statusCode, requestId, statusDesc } =
|
291
|
+
FilterSubscribeResponse.decode(res[0].slice());
|
563
292
|
|
564
|
-
|
565
|
-
|
566
|
-
|
293
|
+
if (statusCode < 200 || statusCode >= 300) {
|
294
|
+
log.error(
|
295
|
+
`Filter ping request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
|
296
|
+
);
|
297
|
+
return {
|
298
|
+
success: null,
|
299
|
+
failure: {
|
300
|
+
error: ProtocolError.REMOTE_PEER_REJECTED,
|
301
|
+
peerId: peer.id
|
302
|
+
}
|
303
|
+
};
|
304
|
+
}
|
305
|
+
return {
|
306
|
+
success: peer.id,
|
307
|
+
failure: null
|
308
|
+
};
|
567
309
|
}
|
568
310
|
}
|