@waku/core 0.0.34-9f1d8ca.0 → 0.0.34-a3e7f15.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-Bp5a9PNG.js +152 -0
- package/bundle/{index-BIW3qNYx.js → index-G1eRBjeI.js} +118 -204
- package/bundle/index.js +1895 -178
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-CdmZMfkQ.js → version_0-DJZG2fB2.js} +283 -45
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -3
- package/dist/index.js +1 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +3 -23
- package/dist/lib/base_protocol.js +3 -47
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager/connection_manager.d.ts +118 -0
- package/dist/lib/{connection_manager.js → connection_manager/connection_manager.js} +136 -36
- package/dist/lib/connection_manager/connection_manager.js.map +1 -0
- package/dist/lib/connection_manager/index.d.ts +1 -0
- package/dist/lib/connection_manager/index.js +2 -0
- package/dist/lib/connection_manager/index.js.map +1 -0
- package/dist/lib/{keep_alive_manager.d.ts → connection_manager/keep_alive_manager.d.ts} +4 -2
- package/dist/lib/{keep_alive_manager.js → connection_manager/keep_alive_manager.js} +2 -2
- package/dist/lib/connection_manager/keep_alive_manager.js.map +1 -0
- package/dist/lib/connection_manager/utils.d.ts +7 -0
- package/dist/lib/connection_manager/utils.js +22 -0
- package/dist/lib/connection_manager/utils.js.map +1 -0
- package/dist/lib/filter/filter.d.ts +18 -0
- package/dist/lib/filter/filter.js +209 -0
- package/dist/lib/filter/filter.js.map +1 -0
- package/dist/lib/filter/index.d.ts +1 -18
- package/dist/lib/filter/index.js +1 -208
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/light_push/index.d.ts +1 -15
- package/dist/lib/light_push/index.js +1 -143
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/light_push.d.ts +15 -0
- package/dist/lib/light_push/light_push.js +144 -0
- package/dist/lib/light_push/light_push.js.map +1 -0
- package/dist/lib/light_push/utils.d.ts +0 -2
- package/dist/lib/light_push/utils.js +9 -17
- package/dist/lib/light_push/utils.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +1 -3
- package/dist/lib/metadata/index.js +1 -118
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/metadata/metadata.d.ts +3 -0
- package/dist/lib/metadata/metadata.js +119 -0
- package/dist/lib/metadata/metadata.js.map +1 -0
- package/dist/lib/store/index.d.ts +1 -9
- package/dist/lib/store/index.js +1 -82
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/store/store.d.ts +9 -0
- package/dist/lib/store/store.js +83 -0
- package/dist/lib/store/store.js.map +1 -0
- package/dist/lib/stream_manager/stream_manager.d.ts +2 -2
- package/dist/lib/stream_manager/stream_manager.js +16 -17
- package/dist/lib/stream_manager/stream_manager.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -4
- package/src/lib/base_protocol.ts +3 -76
- package/src/lib/{connection_manager.ts → connection_manager/connection_manager.ts} +168 -63
- package/src/lib/connection_manager/index.ts +1 -0
- package/src/lib/{keep_alive_manager.ts → connection_manager/keep_alive_manager.ts} +7 -3
- package/src/lib/connection_manager/utils.ts +25 -0
- package/src/lib/filter/filter.ts +315 -0
- package/src/lib/filter/index.ts +1 -315
- package/src/lib/light_push/index.ts +1 -188
- package/src/lib/light_push/light_push.ts +188 -0
- package/src/lib/light_push/utils.ts +13 -21
- package/src/lib/metadata/index.ts +1 -182
- package/src/lib/metadata/metadata.ts +182 -0
- package/src/lib/store/index.ts +1 -136
- package/src/lib/store/store.ts +136 -0
- package/src/lib/stream_manager/stream_manager.ts +16 -18
- package/bundle/base_protocol-Dzv-QHPR.js +0 -275
- package/dist/lib/connection_manager.d.ts +0 -62
- package/dist/lib/connection_manager.js.map +0 -1
- package/dist/lib/filterPeers.d.ts +0 -13
- package/dist/lib/filterPeers.js +0 -38
- package/dist/lib/filterPeers.js.map +0 -1
- package/dist/lib/health_manager.d.ts +0 -14
- package/dist/lib/health_manager.js +0 -70
- package/dist/lib/health_manager.js.map +0 -1
- package/dist/lib/keep_alive_manager.js.map +0 -1
- package/src/lib/filterPeers.ts +0 -51
- package/src/lib/health_manager.ts +0 -90
package/src/lib/filter/index.ts
CHANGED
@@ -1,315 +1 @@
|
|
1
|
-
|
2
|
-
import type { IncomingStreamData } from "@libp2p/interface-internal";
|
3
|
-
import {
|
4
|
-
type ContentTopic,
|
5
|
-
type CoreProtocolResult,
|
6
|
-
type IBaseProtocolCore,
|
7
|
-
type Libp2p,
|
8
|
-
ProtocolError,
|
9
|
-
type PubsubTopic
|
10
|
-
} from "@waku/interfaces";
|
11
|
-
import { WakuMessage } from "@waku/proto";
|
12
|
-
import { Logger } from "@waku/utils";
|
13
|
-
import all from "it-all";
|
14
|
-
import * as lp from "it-length-prefixed";
|
15
|
-
import { pipe } from "it-pipe";
|
16
|
-
import { Uint8ArrayList } from "uint8arraylist";
|
17
|
-
|
18
|
-
import { BaseProtocol } from "../base_protocol.js";
|
19
|
-
|
20
|
-
import {
|
21
|
-
FilterPushRpc,
|
22
|
-
FilterSubscribeResponse,
|
23
|
-
FilterSubscribeRpc
|
24
|
-
} from "./filter_rpc.js";
|
25
|
-
|
26
|
-
const log = new Logger("filter:v2");
|
27
|
-
|
28
|
-
export const FilterCodecs = {
|
29
|
-
SUBSCRIBE: "/vac/waku/filter-subscribe/2.0.0-beta1",
|
30
|
-
PUSH: "/vac/waku/filter-push/2.0.0-beta1"
|
31
|
-
};
|
32
|
-
|
33
|
-
export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
34
|
-
public constructor(
|
35
|
-
private handleIncomingMessage: (
|
36
|
-
pubsubTopic: PubsubTopic,
|
37
|
-
wakuMessage: WakuMessage,
|
38
|
-
peerIdStr: string
|
39
|
-
) => Promise<void>,
|
40
|
-
public readonly pubsubTopics: PubsubTopic[],
|
41
|
-
libp2p: Libp2p
|
42
|
-
) {
|
43
|
-
super(FilterCodecs.SUBSCRIBE, libp2p.components, log, pubsubTopics);
|
44
|
-
|
45
|
-
libp2p
|
46
|
-
.handle(FilterCodecs.PUSH, this.onRequest.bind(this), {
|
47
|
-
maxInboundStreams: 100
|
48
|
-
})
|
49
|
-
.catch((e) => {
|
50
|
-
log.error("Failed to register ", FilterCodecs.PUSH, e);
|
51
|
-
});
|
52
|
-
}
|
53
|
-
|
54
|
-
public async subscribe(
|
55
|
-
pubsubTopic: PubsubTopic,
|
56
|
-
peer: Peer,
|
57
|
-
contentTopics: ContentTopic[]
|
58
|
-
): Promise<CoreProtocolResult> {
|
59
|
-
const stream = await this.getStream(peer);
|
60
|
-
|
61
|
-
const request = FilterSubscribeRpc.createSubscribeRequest(
|
62
|
-
pubsubTopic,
|
63
|
-
contentTopics
|
64
|
-
);
|
65
|
-
|
66
|
-
let res: Uint8ArrayList[] | undefined;
|
67
|
-
try {
|
68
|
-
res = await pipe(
|
69
|
-
[request.encode()],
|
70
|
-
lp.encode,
|
71
|
-
stream,
|
72
|
-
lp.decode,
|
73
|
-
async (source) => await all(source)
|
74
|
-
);
|
75
|
-
} catch (error) {
|
76
|
-
log.error("Failed to send subscribe request", error);
|
77
|
-
return {
|
78
|
-
success: null,
|
79
|
-
failure: {
|
80
|
-
error: ProtocolError.GENERIC_FAIL,
|
81
|
-
peerId: peer.id
|
82
|
-
}
|
83
|
-
};
|
84
|
-
}
|
85
|
-
|
86
|
-
const { statusCode, requestId, statusDesc } =
|
87
|
-
FilterSubscribeResponse.decode(res[0].slice());
|
88
|
-
|
89
|
-
if (statusCode < 200 || statusCode >= 300) {
|
90
|
-
log.error(
|
91
|
-
`Filter subscribe request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
|
92
|
-
);
|
93
|
-
return {
|
94
|
-
failure: {
|
95
|
-
error: ProtocolError.REMOTE_PEER_REJECTED,
|
96
|
-
peerId: peer.id
|
97
|
-
},
|
98
|
-
success: null
|
99
|
-
};
|
100
|
-
}
|
101
|
-
|
102
|
-
return {
|
103
|
-
failure: null,
|
104
|
-
success: peer.id
|
105
|
-
};
|
106
|
-
}
|
107
|
-
|
108
|
-
public async unsubscribe(
|
109
|
-
pubsubTopic: PubsubTopic,
|
110
|
-
peer: Peer,
|
111
|
-
contentTopics: ContentTopic[]
|
112
|
-
): Promise<CoreProtocolResult> {
|
113
|
-
let stream: Stream | undefined;
|
114
|
-
try {
|
115
|
-
stream = await this.getStream(peer);
|
116
|
-
} catch (error) {
|
117
|
-
log.error(
|
118
|
-
`Failed to get a stream for remote peer${peer.id.toString()}`,
|
119
|
-
error
|
120
|
-
);
|
121
|
-
return {
|
122
|
-
success: null,
|
123
|
-
failure: {
|
124
|
-
error: ProtocolError.NO_STREAM_AVAILABLE,
|
125
|
-
peerId: peer.id
|
126
|
-
}
|
127
|
-
};
|
128
|
-
}
|
129
|
-
|
130
|
-
const unsubscribeRequest = FilterSubscribeRpc.createUnsubscribeRequest(
|
131
|
-
pubsubTopic,
|
132
|
-
contentTopics
|
133
|
-
);
|
134
|
-
|
135
|
-
try {
|
136
|
-
await pipe([unsubscribeRequest.encode()], lp.encode, stream.sink);
|
137
|
-
} catch (error) {
|
138
|
-
log.error("Failed to send unsubscribe request", error);
|
139
|
-
return {
|
140
|
-
success: null,
|
141
|
-
failure: {
|
142
|
-
error: ProtocolError.GENERIC_FAIL,
|
143
|
-
peerId: peer.id
|
144
|
-
}
|
145
|
-
};
|
146
|
-
}
|
147
|
-
|
148
|
-
return {
|
149
|
-
success: peer.id,
|
150
|
-
failure: null
|
151
|
-
};
|
152
|
-
}
|
153
|
-
|
154
|
-
public async unsubscribeAll(
|
155
|
-
pubsubTopic: PubsubTopic,
|
156
|
-
peer: Peer
|
157
|
-
): Promise<CoreProtocolResult> {
|
158
|
-
const stream = await this.getStream(peer);
|
159
|
-
|
160
|
-
const request = FilterSubscribeRpc.createUnsubscribeAllRequest(pubsubTopic);
|
161
|
-
|
162
|
-
const res = await pipe(
|
163
|
-
[request.encode()],
|
164
|
-
lp.encode,
|
165
|
-
stream,
|
166
|
-
lp.decode,
|
167
|
-
async (source) => await all(source)
|
168
|
-
);
|
169
|
-
|
170
|
-
if (!res || !res.length) {
|
171
|
-
return {
|
172
|
-
failure: {
|
173
|
-
error: ProtocolError.NO_RESPONSE,
|
174
|
-
peerId: peer.id
|
175
|
-
},
|
176
|
-
success: null
|
177
|
-
};
|
178
|
-
}
|
179
|
-
|
180
|
-
const { statusCode, requestId, statusDesc } =
|
181
|
-
FilterSubscribeResponse.decode(res[0].slice());
|
182
|
-
|
183
|
-
if (statusCode < 200 || statusCode >= 300) {
|
184
|
-
log.error(
|
185
|
-
`Filter unsubscribe all request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
|
186
|
-
);
|
187
|
-
return {
|
188
|
-
failure: {
|
189
|
-
error: ProtocolError.REMOTE_PEER_REJECTED,
|
190
|
-
peerId: peer.id
|
191
|
-
},
|
192
|
-
success: null
|
193
|
-
};
|
194
|
-
}
|
195
|
-
|
196
|
-
return {
|
197
|
-
failure: null,
|
198
|
-
success: peer.id
|
199
|
-
};
|
200
|
-
}
|
201
|
-
|
202
|
-
public async ping(peer: Peer): Promise<CoreProtocolResult> {
|
203
|
-
let stream: Stream | undefined;
|
204
|
-
try {
|
205
|
-
stream = await this.getStream(peer);
|
206
|
-
} catch (error) {
|
207
|
-
log.error(
|
208
|
-
`Failed to get a stream for remote peer${peer.id.toString()}`,
|
209
|
-
error
|
210
|
-
);
|
211
|
-
return {
|
212
|
-
success: null,
|
213
|
-
failure: {
|
214
|
-
error: ProtocolError.NO_STREAM_AVAILABLE,
|
215
|
-
peerId: peer.id
|
216
|
-
}
|
217
|
-
};
|
218
|
-
}
|
219
|
-
|
220
|
-
const request = FilterSubscribeRpc.createSubscriberPingRequest();
|
221
|
-
|
222
|
-
let res: Uint8ArrayList[] | undefined;
|
223
|
-
try {
|
224
|
-
res = await pipe(
|
225
|
-
[request.encode()],
|
226
|
-
lp.encode,
|
227
|
-
stream,
|
228
|
-
lp.decode,
|
229
|
-
async (source) => await all(source)
|
230
|
-
);
|
231
|
-
} catch (error) {
|
232
|
-
log.error("Failed to send ping request", error);
|
233
|
-
return {
|
234
|
-
success: null,
|
235
|
-
failure: {
|
236
|
-
error: ProtocolError.GENERIC_FAIL,
|
237
|
-
peerId: peer.id
|
238
|
-
}
|
239
|
-
};
|
240
|
-
}
|
241
|
-
|
242
|
-
if (!res || !res.length) {
|
243
|
-
return {
|
244
|
-
success: null,
|
245
|
-
failure: {
|
246
|
-
error: ProtocolError.NO_RESPONSE,
|
247
|
-
peerId: peer.id
|
248
|
-
}
|
249
|
-
};
|
250
|
-
}
|
251
|
-
|
252
|
-
const { statusCode, requestId, statusDesc } =
|
253
|
-
FilterSubscribeResponse.decode(res[0].slice());
|
254
|
-
|
255
|
-
if (statusCode < 200 || statusCode >= 300) {
|
256
|
-
log.error(
|
257
|
-
`Filter ping request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
|
258
|
-
);
|
259
|
-
return {
|
260
|
-
success: null,
|
261
|
-
failure: {
|
262
|
-
error: ProtocolError.REMOTE_PEER_REJECTED,
|
263
|
-
peerId: peer.id
|
264
|
-
}
|
265
|
-
};
|
266
|
-
}
|
267
|
-
return {
|
268
|
-
success: peer.id,
|
269
|
-
failure: null
|
270
|
-
};
|
271
|
-
}
|
272
|
-
|
273
|
-
private onRequest(streamData: IncomingStreamData): void {
|
274
|
-
const { connection, stream } = streamData;
|
275
|
-
const { remotePeer } = connection;
|
276
|
-
log.info(`Received message from ${remotePeer.toString()}`);
|
277
|
-
try {
|
278
|
-
pipe(stream, lp.decode, async (source) => {
|
279
|
-
for await (const bytes of source) {
|
280
|
-
const response = FilterPushRpc.decode(bytes.slice());
|
281
|
-
|
282
|
-
const { pubsubTopic, wakuMessage } = response;
|
283
|
-
|
284
|
-
if (!wakuMessage) {
|
285
|
-
log.error("Received empty message");
|
286
|
-
return;
|
287
|
-
}
|
288
|
-
|
289
|
-
if (!pubsubTopic) {
|
290
|
-
log.error("Pubsub topic missing from push message");
|
291
|
-
return;
|
292
|
-
}
|
293
|
-
|
294
|
-
await this.handleIncomingMessage(
|
295
|
-
pubsubTopic,
|
296
|
-
wakuMessage,
|
297
|
-
connection.remotePeer.toString()
|
298
|
-
);
|
299
|
-
}
|
300
|
-
}).then(
|
301
|
-
() => {
|
302
|
-
log.info("Receiving pipe closed.");
|
303
|
-
},
|
304
|
-
async (e) => {
|
305
|
-
log.error(
|
306
|
-
`Error with receiving pipe on peer:${connection.remotePeer.toString()} -- stream:${stream.id} -- protocol:${stream.protocol}: `,
|
307
|
-
e
|
308
|
-
);
|
309
|
-
}
|
310
|
-
);
|
311
|
-
} catch (e) {
|
312
|
-
log.error("Error decoding message", e);
|
313
|
-
}
|
314
|
-
}
|
315
|
-
}
|
1
|
+
export { FilterCodecs, FilterCore } from "./filter.js";
|
@@ -1,188 +1 @@
|
|
1
|
-
|
2
|
-
import {
|
3
|
-
type CoreProtocolResult,
|
4
|
-
type IBaseProtocolCore,
|
5
|
-
type IEncoder,
|
6
|
-
type IMessage,
|
7
|
-
type Libp2p,
|
8
|
-
ProtocolError,
|
9
|
-
PubsubTopic,
|
10
|
-
type ThisOrThat
|
11
|
-
} from "@waku/interfaces";
|
12
|
-
import { PushResponse } from "@waku/proto";
|
13
|
-
import { isMessageSizeUnderCap } from "@waku/utils";
|
14
|
-
import { Logger } from "@waku/utils";
|
15
|
-
import all from "it-all";
|
16
|
-
import * as lp from "it-length-prefixed";
|
17
|
-
import { pipe } from "it-pipe";
|
18
|
-
import { Uint8ArrayList } from "uint8arraylist";
|
19
|
-
|
20
|
-
import { BaseProtocol } from "../base_protocol.js";
|
21
|
-
|
22
|
-
import { PushRpc } from "./push_rpc.js";
|
23
|
-
import { isRLNResponseError, matchRLNErrorMessage } from "./utils.js";
|
24
|
-
|
25
|
-
const log = new Logger("light-push");
|
26
|
-
|
27
|
-
export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
28
|
-
export { PushResponse };
|
29
|
-
|
30
|
-
type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
|
31
|
-
|
32
|
-
/**
|
33
|
-
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
34
|
-
*/
|
35
|
-
export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
36
|
-
public constructor(
|
37
|
-
public readonly pubsubTopics: PubsubTopic[],
|
38
|
-
libp2p: Libp2p
|
39
|
-
) {
|
40
|
-
super(LightPushCodec, libp2p.components, log, pubsubTopics);
|
41
|
-
}
|
42
|
-
|
43
|
-
private async preparePushMessage(
|
44
|
-
encoder: IEncoder,
|
45
|
-
message: IMessage
|
46
|
-
): Promise<PreparePushMessageResult> {
|
47
|
-
try {
|
48
|
-
if (!message.payload || message.payload.length === 0) {
|
49
|
-
log.error("Failed to send waku light push: payload is empty");
|
50
|
-
return { query: null, error: ProtocolError.EMPTY_PAYLOAD };
|
51
|
-
}
|
52
|
-
|
53
|
-
if (!(await isMessageSizeUnderCap(encoder, message))) {
|
54
|
-
log.error("Failed to send waku light push: message is bigger than 1MB");
|
55
|
-
return { query: null, error: ProtocolError.SIZE_TOO_BIG };
|
56
|
-
}
|
57
|
-
|
58
|
-
const protoMessage = await encoder.toProtoObj(message);
|
59
|
-
if (!protoMessage) {
|
60
|
-
log.error("Failed to encode to protoMessage, aborting push");
|
61
|
-
return {
|
62
|
-
query: null,
|
63
|
-
error: ProtocolError.ENCODE_FAILED
|
64
|
-
};
|
65
|
-
}
|
66
|
-
|
67
|
-
const query = PushRpc.createRequest(protoMessage, encoder.pubsubTopic);
|
68
|
-
return { query, error: null };
|
69
|
-
} catch (error) {
|
70
|
-
log.error("Failed to prepare push message", error);
|
71
|
-
|
72
|
-
return {
|
73
|
-
query: null,
|
74
|
-
error: ProtocolError.GENERIC_FAIL
|
75
|
-
};
|
76
|
-
}
|
77
|
-
}
|
78
|
-
|
79
|
-
public async send(
|
80
|
-
encoder: IEncoder,
|
81
|
-
message: IMessage,
|
82
|
-
peer: Peer
|
83
|
-
): Promise<CoreProtocolResult> {
|
84
|
-
const { query, error: preparationError } = await this.preparePushMessage(
|
85
|
-
encoder,
|
86
|
-
message
|
87
|
-
);
|
88
|
-
|
89
|
-
if (preparationError || !query) {
|
90
|
-
return {
|
91
|
-
success: null,
|
92
|
-
failure: {
|
93
|
-
error: preparationError,
|
94
|
-
peerId: peer.id
|
95
|
-
}
|
96
|
-
};
|
97
|
-
}
|
98
|
-
|
99
|
-
let stream: Stream;
|
100
|
-
try {
|
101
|
-
stream = await this.getStream(peer);
|
102
|
-
} catch (error) {
|
103
|
-
log.error("Failed to get stream", error);
|
104
|
-
return {
|
105
|
-
success: null,
|
106
|
-
failure: {
|
107
|
-
error: ProtocolError.NO_STREAM_AVAILABLE,
|
108
|
-
peerId: peer.id
|
109
|
-
}
|
110
|
-
};
|
111
|
-
}
|
112
|
-
|
113
|
-
let res: Uint8ArrayList[] | undefined;
|
114
|
-
try {
|
115
|
-
res = await pipe(
|
116
|
-
[query.encode()],
|
117
|
-
lp.encode,
|
118
|
-
stream,
|
119
|
-
lp.decode,
|
120
|
-
async (source) => await all(source)
|
121
|
-
);
|
122
|
-
} catch (err) {
|
123
|
-
log.error("Failed to send waku light push request", err);
|
124
|
-
return {
|
125
|
-
success: null,
|
126
|
-
failure: {
|
127
|
-
error: ProtocolError.GENERIC_FAIL,
|
128
|
-
peerId: peer.id
|
129
|
-
}
|
130
|
-
};
|
131
|
-
}
|
132
|
-
|
133
|
-
const bytes = new Uint8ArrayList();
|
134
|
-
res.forEach((chunk) => {
|
135
|
-
bytes.append(chunk);
|
136
|
-
});
|
137
|
-
|
138
|
-
let response: PushResponse | undefined;
|
139
|
-
try {
|
140
|
-
response = PushRpc.decode(bytes).response;
|
141
|
-
} catch (err) {
|
142
|
-
log.error("Failed to decode push reply", err);
|
143
|
-
return {
|
144
|
-
success: null,
|
145
|
-
failure: {
|
146
|
-
error: ProtocolError.DECODE_FAILED,
|
147
|
-
peerId: peer.id
|
148
|
-
}
|
149
|
-
};
|
150
|
-
}
|
151
|
-
|
152
|
-
if (!response) {
|
153
|
-
log.error("Remote peer fault: No response in PushRPC");
|
154
|
-
return {
|
155
|
-
success: null,
|
156
|
-
failure: {
|
157
|
-
error: ProtocolError.NO_RESPONSE,
|
158
|
-
peerId: peer.id
|
159
|
-
}
|
160
|
-
};
|
161
|
-
}
|
162
|
-
|
163
|
-
if (isRLNResponseError(response.info)) {
|
164
|
-
const rlnErrorCase = matchRLNErrorMessage(response.info!);
|
165
|
-
log.error("Remote peer rejected the message: ", rlnErrorCase);
|
166
|
-
return {
|
167
|
-
success: null,
|
168
|
-
failure: {
|
169
|
-
error: rlnErrorCase,
|
170
|
-
peerId: peer.id
|
171
|
-
}
|
172
|
-
};
|
173
|
-
}
|
174
|
-
|
175
|
-
if (!response.isSuccess) {
|
176
|
-
log.error("Remote peer rejected the message: ", response.info);
|
177
|
-
return {
|
178
|
-
success: null,
|
179
|
-
failure: {
|
180
|
-
error: ProtocolError.REMOTE_PEER_REJECTED,
|
181
|
-
peerId: peer.id
|
182
|
-
}
|
183
|
-
};
|
184
|
-
}
|
185
|
-
|
186
|
-
return { success: peer.id, failure: null };
|
187
|
-
}
|
188
|
-
}
|
1
|
+
export { LightPushCore, LightPushCodec, PushResponse } from "./light_push.js";
|
@@ -0,0 +1,188 @@
|
|
1
|
+
import type { PeerId, Stream } from "@libp2p/interface";
|
2
|
+
import {
|
3
|
+
type CoreProtocolResult,
|
4
|
+
type IBaseProtocolCore,
|
5
|
+
type IEncoder,
|
6
|
+
type IMessage,
|
7
|
+
type Libp2p,
|
8
|
+
ProtocolError,
|
9
|
+
PubsubTopic,
|
10
|
+
type ThisOrThat
|
11
|
+
} from "@waku/interfaces";
|
12
|
+
import { PushResponse } from "@waku/proto";
|
13
|
+
import { isMessageSizeUnderCap } from "@waku/utils";
|
14
|
+
import { Logger } from "@waku/utils";
|
15
|
+
import all from "it-all";
|
16
|
+
import * as lp from "it-length-prefixed";
|
17
|
+
import { pipe } from "it-pipe";
|
18
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
19
|
+
|
20
|
+
import { BaseProtocol } from "../base_protocol.js";
|
21
|
+
|
22
|
+
import { PushRpc } from "./push_rpc.js";
|
23
|
+
import { isRLNResponseError } from "./utils.js";
|
24
|
+
|
25
|
+
const log = new Logger("light-push");
|
26
|
+
|
27
|
+
export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
28
|
+
export { PushResponse };
|
29
|
+
|
30
|
+
type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
34
|
+
*/
|
35
|
+
export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
36
|
+
public constructor(
|
37
|
+
public readonly pubsubTopics: PubsubTopic[],
|
38
|
+
libp2p: Libp2p
|
39
|
+
) {
|
40
|
+
super(LightPushCodec, libp2p.components, pubsubTopics);
|
41
|
+
}
|
42
|
+
|
43
|
+
private async preparePushMessage(
|
44
|
+
encoder: IEncoder,
|
45
|
+
message: IMessage
|
46
|
+
): Promise<PreparePushMessageResult> {
|
47
|
+
try {
|
48
|
+
if (!message.payload || message.payload.length === 0) {
|
49
|
+
log.error("Failed to send waku light push: payload is empty");
|
50
|
+
return { query: null, error: ProtocolError.EMPTY_PAYLOAD };
|
51
|
+
}
|
52
|
+
|
53
|
+
if (!(await isMessageSizeUnderCap(encoder, message))) {
|
54
|
+
log.error("Failed to send waku light push: message is bigger than 1MB");
|
55
|
+
return { query: null, error: ProtocolError.SIZE_TOO_BIG };
|
56
|
+
}
|
57
|
+
|
58
|
+
const protoMessage = await encoder.toProtoObj(message);
|
59
|
+
if (!protoMessage) {
|
60
|
+
log.error("Failed to encode to protoMessage, aborting push");
|
61
|
+
return {
|
62
|
+
query: null,
|
63
|
+
error: ProtocolError.ENCODE_FAILED
|
64
|
+
};
|
65
|
+
}
|
66
|
+
|
67
|
+
const query = PushRpc.createRequest(protoMessage, encoder.pubsubTopic);
|
68
|
+
return { query, error: null };
|
69
|
+
} catch (error) {
|
70
|
+
log.error("Failed to prepare push message", error);
|
71
|
+
|
72
|
+
return {
|
73
|
+
query: null,
|
74
|
+
error: ProtocolError.GENERIC_FAIL
|
75
|
+
};
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
public async send(
|
80
|
+
encoder: IEncoder,
|
81
|
+
message: IMessage,
|
82
|
+
peerId: PeerId
|
83
|
+
): Promise<CoreProtocolResult> {
|
84
|
+
const { query, error: preparationError } = await this.preparePushMessage(
|
85
|
+
encoder,
|
86
|
+
message
|
87
|
+
);
|
88
|
+
|
89
|
+
if (preparationError || !query) {
|
90
|
+
return {
|
91
|
+
success: null,
|
92
|
+
failure: {
|
93
|
+
error: preparationError,
|
94
|
+
peerId
|
95
|
+
}
|
96
|
+
};
|
97
|
+
}
|
98
|
+
|
99
|
+
let stream: Stream;
|
100
|
+
try {
|
101
|
+
stream = await this.getStream(peerId);
|
102
|
+
} catch (error) {
|
103
|
+
log.error("Failed to get stream", error);
|
104
|
+
return {
|
105
|
+
success: null,
|
106
|
+
failure: {
|
107
|
+
error: ProtocolError.NO_STREAM_AVAILABLE,
|
108
|
+
peerId: peerId
|
109
|
+
}
|
110
|
+
};
|
111
|
+
}
|
112
|
+
|
113
|
+
let res: Uint8ArrayList[] | undefined;
|
114
|
+
try {
|
115
|
+
res = await pipe(
|
116
|
+
[query.encode()],
|
117
|
+
lp.encode,
|
118
|
+
stream,
|
119
|
+
lp.decode,
|
120
|
+
async (source) => await all(source)
|
121
|
+
);
|
122
|
+
} catch (err) {
|
123
|
+
// can fail only because of `stream` abortion
|
124
|
+
log.error("Failed to send waku light push request", err);
|
125
|
+
return {
|
126
|
+
success: null,
|
127
|
+
failure: {
|
128
|
+
error: ProtocolError.STREAM_ABORTED,
|
129
|
+
peerId: peerId
|
130
|
+
}
|
131
|
+
};
|
132
|
+
}
|
133
|
+
|
134
|
+
const bytes = new Uint8ArrayList();
|
135
|
+
res.forEach((chunk) => {
|
136
|
+
bytes.append(chunk);
|
137
|
+
});
|
138
|
+
|
139
|
+
let response: PushResponse | undefined;
|
140
|
+
try {
|
141
|
+
response = PushRpc.decode(bytes).response;
|
142
|
+
} catch (err) {
|
143
|
+
log.error("Failed to decode push reply", err);
|
144
|
+
return {
|
145
|
+
success: null,
|
146
|
+
failure: {
|
147
|
+
error: ProtocolError.DECODE_FAILED,
|
148
|
+
peerId: peerId
|
149
|
+
}
|
150
|
+
};
|
151
|
+
}
|
152
|
+
|
153
|
+
if (!response) {
|
154
|
+
log.error("Remote peer fault: No response in PushRPC");
|
155
|
+
return {
|
156
|
+
success: null,
|
157
|
+
failure: {
|
158
|
+
error: ProtocolError.NO_RESPONSE,
|
159
|
+
peerId: peerId
|
160
|
+
}
|
161
|
+
};
|
162
|
+
}
|
163
|
+
|
164
|
+
if (isRLNResponseError(response.info)) {
|
165
|
+
log.error("Remote peer fault: RLN generation");
|
166
|
+
return {
|
167
|
+
success: null,
|
168
|
+
failure: {
|
169
|
+
error: ProtocolError.RLN_PROOF_GENERATION,
|
170
|
+
peerId: peerId
|
171
|
+
}
|
172
|
+
};
|
173
|
+
}
|
174
|
+
|
175
|
+
if (!response.isSuccess) {
|
176
|
+
log.error("Remote peer rejected the message: ", response.info);
|
177
|
+
return {
|
178
|
+
success: null,
|
179
|
+
failure: {
|
180
|
+
error: ProtocolError.REMOTE_PEER_REJECTED,
|
181
|
+
peerId: peerId
|
182
|
+
}
|
183
|
+
};
|
184
|
+
}
|
185
|
+
|
186
|
+
return { success: peerId, failure: null };
|
187
|
+
}
|
188
|
+
}
|