@waku/core 0.0.33 → 0.0.34-09108d9.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 +1894 -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 +8 -16
- 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 -126
- 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 +12 -20
- 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
@@ -0,0 +1,144 @@
|
|
1
|
+
import { ProtocolError } from "@waku/interfaces";
|
2
|
+
import { PushResponse } from "@waku/proto";
|
3
|
+
import { isMessageSizeUnderCap } from "@waku/utils";
|
4
|
+
import { Logger } from "@waku/utils";
|
5
|
+
import all from "it-all";
|
6
|
+
import * as lp from "it-length-prefixed";
|
7
|
+
import { pipe } from "it-pipe";
|
8
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
9
|
+
import { BaseProtocol } from "../base_protocol.js";
|
10
|
+
import { PushRpc } from "./push_rpc.js";
|
11
|
+
import { isRLNResponseError } from "./utils.js";
|
12
|
+
const log = new Logger("light-push");
|
13
|
+
export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
14
|
+
export { PushResponse };
|
15
|
+
/**
|
16
|
+
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
17
|
+
*/
|
18
|
+
export class LightPushCore extends BaseProtocol {
|
19
|
+
pubsubTopics;
|
20
|
+
constructor(pubsubTopics, libp2p) {
|
21
|
+
super(LightPushCodec, libp2p.components, pubsubTopics);
|
22
|
+
this.pubsubTopics = pubsubTopics;
|
23
|
+
}
|
24
|
+
async preparePushMessage(encoder, message) {
|
25
|
+
try {
|
26
|
+
if (!message.payload || message.payload.length === 0) {
|
27
|
+
log.error("Failed to send waku light push: payload is empty");
|
28
|
+
return { query: null, error: ProtocolError.EMPTY_PAYLOAD };
|
29
|
+
}
|
30
|
+
if (!(await isMessageSizeUnderCap(encoder, message))) {
|
31
|
+
log.error("Failed to send waku light push: message is bigger than 1MB");
|
32
|
+
return { query: null, error: ProtocolError.SIZE_TOO_BIG };
|
33
|
+
}
|
34
|
+
const protoMessage = await encoder.toProtoObj(message);
|
35
|
+
if (!protoMessage) {
|
36
|
+
log.error("Failed to encode to protoMessage, aborting push");
|
37
|
+
return {
|
38
|
+
query: null,
|
39
|
+
error: ProtocolError.ENCODE_FAILED
|
40
|
+
};
|
41
|
+
}
|
42
|
+
const query = PushRpc.createRequest(protoMessage, encoder.pubsubTopic);
|
43
|
+
return { query, error: null };
|
44
|
+
}
|
45
|
+
catch (error) {
|
46
|
+
log.error("Failed to prepare push message", error);
|
47
|
+
return {
|
48
|
+
query: null,
|
49
|
+
error: ProtocolError.GENERIC_FAIL
|
50
|
+
};
|
51
|
+
}
|
52
|
+
}
|
53
|
+
async send(encoder, message, peerId) {
|
54
|
+
const { query, error: preparationError } = await this.preparePushMessage(encoder, message);
|
55
|
+
if (preparationError || !query) {
|
56
|
+
return {
|
57
|
+
success: null,
|
58
|
+
failure: {
|
59
|
+
error: preparationError,
|
60
|
+
peerId
|
61
|
+
}
|
62
|
+
};
|
63
|
+
}
|
64
|
+
let stream;
|
65
|
+
try {
|
66
|
+
stream = await this.getStream(peerId);
|
67
|
+
}
|
68
|
+
catch (error) {
|
69
|
+
log.error("Failed to get stream", error);
|
70
|
+
return {
|
71
|
+
success: null,
|
72
|
+
failure: {
|
73
|
+
error: ProtocolError.NO_STREAM_AVAILABLE,
|
74
|
+
peerId: peerId
|
75
|
+
}
|
76
|
+
};
|
77
|
+
}
|
78
|
+
let res;
|
79
|
+
try {
|
80
|
+
res = await pipe([query.encode()], lp.encode, stream, lp.decode, async (source) => await all(source));
|
81
|
+
}
|
82
|
+
catch (err) {
|
83
|
+
// can fail only because of `stream` abortion
|
84
|
+
log.error("Failed to send waku light push request", err);
|
85
|
+
return {
|
86
|
+
success: null,
|
87
|
+
failure: {
|
88
|
+
error: ProtocolError.STREAM_ABORTED,
|
89
|
+
peerId: peerId
|
90
|
+
}
|
91
|
+
};
|
92
|
+
}
|
93
|
+
const bytes = new Uint8ArrayList();
|
94
|
+
res.forEach((chunk) => {
|
95
|
+
bytes.append(chunk);
|
96
|
+
});
|
97
|
+
let response;
|
98
|
+
try {
|
99
|
+
response = PushRpc.decode(bytes).response;
|
100
|
+
}
|
101
|
+
catch (err) {
|
102
|
+
log.error("Failed to decode push reply", err);
|
103
|
+
return {
|
104
|
+
success: null,
|
105
|
+
failure: {
|
106
|
+
error: ProtocolError.DECODE_FAILED,
|
107
|
+
peerId: peerId
|
108
|
+
}
|
109
|
+
};
|
110
|
+
}
|
111
|
+
if (!response) {
|
112
|
+
log.error("Remote peer fault: No response in PushRPC");
|
113
|
+
return {
|
114
|
+
success: null,
|
115
|
+
failure: {
|
116
|
+
error: ProtocolError.NO_RESPONSE,
|
117
|
+
peerId: peerId
|
118
|
+
}
|
119
|
+
};
|
120
|
+
}
|
121
|
+
if (isRLNResponseError(response.info)) {
|
122
|
+
log.error("Remote peer fault: RLN generation");
|
123
|
+
return {
|
124
|
+
success: null,
|
125
|
+
failure: {
|
126
|
+
error: ProtocolError.RLN_PROOF_GENERATION,
|
127
|
+
peerId: peerId
|
128
|
+
}
|
129
|
+
};
|
130
|
+
}
|
131
|
+
if (!response.isSuccess) {
|
132
|
+
log.error("Remote peer rejected the message: ", response.info);
|
133
|
+
return {
|
134
|
+
success: null,
|
135
|
+
failure: {
|
136
|
+
error: ProtocolError.REMOTE_PEER_REJECTED,
|
137
|
+
peerId: peerId
|
138
|
+
}
|
139
|
+
};
|
140
|
+
}
|
141
|
+
return { success: peerId, failure: null };
|
142
|
+
}
|
143
|
+
}
|
144
|
+
//# sourceMappingURL=light_push.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"light_push.js","sourceRoot":"","sources":["../../../src/lib/light_push/light_push.ts"],"names":[],"mappings":"AACA,OAAO,EAML,aAAa,EAGd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa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,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,GAAG,CAAC,KAAK,CAAC,2CAA2C,CAAC,CAAC;YACvD,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,OAAO,EAAE;oBACP,KAAK,EAAE,aAAa,CAAC,WAAW;oBAChC,MAAM,EAAE,MAAM;iBACf;aACF,CAAC;QACJ,CAAC;QAED,IAAI,kBAAkB,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YACtC,GAAG,CAAC,KAAK,CAAC,mCAAmC,CAAC,CAAC;YAC/C,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,OAAO,EAAE;oBACP,KAAK,EAAE,aAAa,CAAC,oBAAoB;oBACzC,MAAM,EAAE,MAAM;iBACf;aACF,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;YACxB,GAAG,CAAC,KAAK,CAAC,oCAAoC,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC;YAC/D,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,OAAO,EAAE;oBACP,KAAK,EAAE,aAAa,CAAC,oBAAoB;oBACzC,MAAM,EAAE,MAAM;iBACf;aACF,CAAC;QACJ,CAAC;QAED,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC5C,CAAC;CACF"}
|
@@ -1,26 +1,18 @@
|
|
1
|
-
import { ProtocolError } from "@waku/interfaces";
|
2
1
|
// should match nwaku
|
3
2
|
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/rln_relay.nim#L309
|
4
3
|
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/tests/waku_rln_relay/rln/waku_rln_relay_utils.nim#L20
|
5
4
|
const RLN_GENERATION_PREFIX_ERROR = "could not generate rln-v2 proof";
|
5
|
+
const RLN_MESSAGE_ID_PREFIX_ERROR = "could not get new message id to generate an rln proof";
|
6
|
+
// rare case on nwaku side
|
7
|
+
// https://github.com/waku-org/nwaku/blob/a4e92a3d02448fd708857b7b6cac2a7faa7eb4f9/waku/waku_lightpush/callbacks.nim#L49
|
8
|
+
// https://github.com/waku-org/nwaku/blob/a4e92a3d02448fd708857b7b6cac2a7faa7eb4f9/waku/node/waku_node.nim#L1117
|
9
|
+
const RLN_REMOTE_VALIDATION = "RLN validation failed";
|
6
10
|
export const isRLNResponseError = (info) => {
|
7
11
|
if (!info) {
|
8
12
|
return false;
|
9
13
|
}
|
10
|
-
return info.includes(RLN_GENERATION_PREFIX_ERROR)
|
11
|
-
|
12
|
-
|
13
|
-
const rlnErrorMap = {
|
14
|
-
[ProtocolError.RLN_IDENTITY_MISSING]: ProtocolError.RLN_IDENTITY_MISSING,
|
15
|
-
[ProtocolError.RLN_MEMBERSHIP_INDEX]: ProtocolError.RLN_MEMBERSHIP_INDEX,
|
16
|
-
[ProtocolError.RLN_LIMIT_MISSING]: ProtocolError.RLN_LIMIT_MISSING
|
17
|
-
};
|
18
|
-
const infoLowerCase = info.toLowerCase();
|
19
|
-
for (const errorKey in rlnErrorMap) {
|
20
|
-
if (infoLowerCase.includes(errorKey.toLowerCase())) {
|
21
|
-
return rlnErrorMap[errorKey];
|
22
|
-
}
|
23
|
-
}
|
24
|
-
return ProtocolError.RLN_PROOF_GENERATION;
|
14
|
+
return (info.includes(RLN_GENERATION_PREFIX_ERROR) ||
|
15
|
+
info.includes(RLN_MESSAGE_ID_PREFIX_ERROR) ||
|
16
|
+
info.includes(RLN_REMOTE_VALIDATION));
|
25
17
|
};
|
26
18
|
//# sourceMappingURL=utils.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/light_push/utils.ts"],"names":[],"mappings":"AAAA,
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/light_push/utils.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,yHAAyH;AACzH,wIAAwI;AACxI,MAAM,2BAA2B,GAAG,iCAAiC,CAAC;AACtE,MAAM,2BAA2B,GAC/B,uDAAuD,CAAC;AAE1D,0BAA0B;AAC1B,wHAAwH;AACxH,gHAAgH;AAChH,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AAEtD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAAa,EAAW,EAAE;IAC3D,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,CACL,IAAI,CAAC,QAAQ,CAAC,2BAA2B,CAAC;QAC1C,IAAI,CAAC,QAAQ,CAAC,2BAA2B,CAAC;QAC1C,IAAI,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CACrC,CAAC;AACJ,CAAC,CAAC"}
|
@@ -1,3 +1 @@
|
|
1
|
-
|
2
|
-
export declare const MetadataCodec = "/vac/waku/metadata/1.0.0";
|
3
|
-
export declare function wakuMetadata(pubsubTopics: PubsubTopic[]): (components: Libp2pComponents) => IMetadata;
|
1
|
+
export { wakuMetadata, MetadataCodec } from "./metadata.js";
|
@@ -1,119 +1,2 @@
|
|
1
|
-
|
2
|
-
import { proto_metadata } from "@waku/proto";
|
3
|
-
import { encodeRelayShard, Logger, pubsubTopicsToShardInfo } from "@waku/utils";
|
4
|
-
import all from "it-all";
|
5
|
-
import * as lp from "it-length-prefixed";
|
6
|
-
import { pipe } from "it-pipe";
|
7
|
-
import { Uint8ArrayList } from "uint8arraylist";
|
8
|
-
import { BaseProtocol } from "../base_protocol.js";
|
9
|
-
const log = new Logger("metadata");
|
10
|
-
export const MetadataCodec = "/vac/waku/metadata/1.0.0";
|
11
|
-
class Metadata extends BaseProtocol {
|
12
|
-
pubsubTopics;
|
13
|
-
libp2pComponents;
|
14
|
-
handshakesConfirmed = new Map();
|
15
|
-
constructor(pubsubTopics, libp2p) {
|
16
|
-
super(MetadataCodec, libp2p.components, log, pubsubTopics);
|
17
|
-
this.pubsubTopics = pubsubTopics;
|
18
|
-
this.libp2pComponents = libp2p;
|
19
|
-
void libp2p.registrar.handle(MetadataCodec, (streamData) => {
|
20
|
-
void this.onRequest(streamData);
|
21
|
-
});
|
22
|
-
}
|
23
|
-
/**
|
24
|
-
* Make a metadata query to a peer
|
25
|
-
*/
|
26
|
-
async query(peerId) {
|
27
|
-
const request = proto_metadata.WakuMetadataRequest.encode(pubsubTopicsToShardInfo(this.pubsubTopics));
|
28
|
-
const peer = await this.libp2pComponents.peerStore.get(peerId);
|
29
|
-
if (!peer) {
|
30
|
-
return {
|
31
|
-
shardInfo: null,
|
32
|
-
error: ProtocolError.NO_PEER_AVAILABLE
|
33
|
-
};
|
34
|
-
}
|
35
|
-
let stream;
|
36
|
-
try {
|
37
|
-
stream = await this.getStream(peer);
|
38
|
-
}
|
39
|
-
catch (error) {
|
40
|
-
log.error("Failed to get stream", error);
|
41
|
-
return {
|
42
|
-
shardInfo: null,
|
43
|
-
error: ProtocolError.NO_STREAM_AVAILABLE
|
44
|
-
};
|
45
|
-
}
|
46
|
-
const encodedResponse = await pipe([request], lp.encode, stream, lp.decode, async (source) => await all(source));
|
47
|
-
const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
|
48
|
-
if (error) {
|
49
|
-
return {
|
50
|
-
shardInfo: null,
|
51
|
-
error
|
52
|
-
};
|
53
|
-
}
|
54
|
-
await this.savePeerShardInfo(peerId, shardInfo);
|
55
|
-
return {
|
56
|
-
shardInfo,
|
57
|
-
error: null
|
58
|
-
};
|
59
|
-
}
|
60
|
-
async confirmOrAttemptHandshake(peerId) {
|
61
|
-
const shardInfo = this.handshakesConfirmed.get(peerId.toString());
|
62
|
-
if (shardInfo) {
|
63
|
-
return {
|
64
|
-
shardInfo,
|
65
|
-
error: null
|
66
|
-
};
|
67
|
-
}
|
68
|
-
return await this.query(peerId);
|
69
|
-
}
|
70
|
-
/**
|
71
|
-
* Handle an incoming metadata request
|
72
|
-
*/
|
73
|
-
async onRequest(streamData) {
|
74
|
-
try {
|
75
|
-
const { stream, connection } = streamData;
|
76
|
-
const encodedShardInfo = proto_metadata.WakuMetadataResponse.encode(pubsubTopicsToShardInfo(this.pubsubTopics));
|
77
|
-
const encodedResponse = await pipe([encodedShardInfo], lp.encode, stream, lp.decode, async (source) => await all(source));
|
78
|
-
const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
|
79
|
-
if (error) {
|
80
|
-
return;
|
81
|
-
}
|
82
|
-
await this.savePeerShardInfo(connection.remotePeer, shardInfo);
|
83
|
-
}
|
84
|
-
catch (error) {
|
85
|
-
log.error("Error handling metadata request", error);
|
86
|
-
}
|
87
|
-
}
|
88
|
-
decodeMetadataResponse(encodedResponse) {
|
89
|
-
const bytes = new Uint8ArrayList();
|
90
|
-
encodedResponse.forEach((chunk) => {
|
91
|
-
bytes.append(chunk);
|
92
|
-
});
|
93
|
-
const response = proto_metadata.WakuMetadataResponse.decode(bytes);
|
94
|
-
if (!response) {
|
95
|
-
log.error("Error decoding metadata response");
|
96
|
-
return {
|
97
|
-
shardInfo: null,
|
98
|
-
error: ProtocolError.DECODE_FAILED
|
99
|
-
};
|
100
|
-
}
|
101
|
-
return {
|
102
|
-
shardInfo: response,
|
103
|
-
error: null
|
104
|
-
};
|
105
|
-
}
|
106
|
-
async savePeerShardInfo(peerId, shardInfo) {
|
107
|
-
// add or update the shardInfo to peer store
|
108
|
-
await this.libp2pComponents.peerStore.merge(peerId, {
|
109
|
-
metadata: {
|
110
|
-
shardInfo: encodeRelayShard(shardInfo)
|
111
|
-
}
|
112
|
-
});
|
113
|
-
this.handshakesConfirmed.set(peerId.toString(), shardInfo);
|
114
|
-
}
|
115
|
-
}
|
116
|
-
export function wakuMetadata(pubsubTopics) {
|
117
|
-
return (components) => new Metadata(pubsubTopics, components);
|
118
|
-
}
|
1
|
+
export { wakuMetadata, MetadataCodec } from "./metadata.js";
|
119
2
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/metadata/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/metadata/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC"}
|
@@ -0,0 +1,119 @@
|
|
1
|
+
import { ProtocolError } from "@waku/interfaces";
|
2
|
+
import { proto_metadata } from "@waku/proto";
|
3
|
+
import { encodeRelayShard, Logger, pubsubTopicsToShardInfo } from "@waku/utils";
|
4
|
+
import all from "it-all";
|
5
|
+
import * as lp from "it-length-prefixed";
|
6
|
+
import { pipe } from "it-pipe";
|
7
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
8
|
+
import { BaseProtocol } from "../base_protocol.js";
|
9
|
+
const log = new Logger("metadata");
|
10
|
+
export const MetadataCodec = "/vac/waku/metadata/1.0.0";
|
11
|
+
class Metadata extends BaseProtocol {
|
12
|
+
pubsubTopics;
|
13
|
+
libp2pComponents;
|
14
|
+
handshakesConfirmed = new Map();
|
15
|
+
constructor(pubsubTopics, libp2p) {
|
16
|
+
super(MetadataCodec, libp2p.components, pubsubTopics);
|
17
|
+
this.pubsubTopics = pubsubTopics;
|
18
|
+
this.libp2pComponents = libp2p;
|
19
|
+
void libp2p.registrar.handle(MetadataCodec, (streamData) => {
|
20
|
+
void this.onRequest(streamData);
|
21
|
+
});
|
22
|
+
}
|
23
|
+
/**
|
24
|
+
* Make a metadata query to a peer
|
25
|
+
*/
|
26
|
+
async query(peerId) {
|
27
|
+
const request = proto_metadata.WakuMetadataRequest.encode(pubsubTopicsToShardInfo(this.pubsubTopics));
|
28
|
+
const peer = await this.libp2pComponents.peerStore.get(peerId);
|
29
|
+
if (!peer) {
|
30
|
+
return {
|
31
|
+
shardInfo: null,
|
32
|
+
error: ProtocolError.NO_PEER_AVAILABLE
|
33
|
+
};
|
34
|
+
}
|
35
|
+
let stream;
|
36
|
+
try {
|
37
|
+
stream = await this.getStream(peerId);
|
38
|
+
}
|
39
|
+
catch (error) {
|
40
|
+
log.error("Failed to get stream", error);
|
41
|
+
return {
|
42
|
+
shardInfo: null,
|
43
|
+
error: ProtocolError.NO_STREAM_AVAILABLE
|
44
|
+
};
|
45
|
+
}
|
46
|
+
const encodedResponse = await pipe([request], lp.encode, stream, lp.decode, async (source) => await all(source));
|
47
|
+
const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
|
48
|
+
if (error) {
|
49
|
+
return {
|
50
|
+
shardInfo: null,
|
51
|
+
error
|
52
|
+
};
|
53
|
+
}
|
54
|
+
await this.savePeerShardInfo(peerId, shardInfo);
|
55
|
+
return {
|
56
|
+
shardInfo,
|
57
|
+
error: null
|
58
|
+
};
|
59
|
+
}
|
60
|
+
async confirmOrAttemptHandshake(peerId) {
|
61
|
+
const shardInfo = this.handshakesConfirmed.get(peerId.toString());
|
62
|
+
if (shardInfo) {
|
63
|
+
return {
|
64
|
+
shardInfo,
|
65
|
+
error: null
|
66
|
+
};
|
67
|
+
}
|
68
|
+
return await this.query(peerId);
|
69
|
+
}
|
70
|
+
/**
|
71
|
+
* Handle an incoming metadata request
|
72
|
+
*/
|
73
|
+
async onRequest(streamData) {
|
74
|
+
try {
|
75
|
+
const { stream, connection } = streamData;
|
76
|
+
const encodedShardInfo = proto_metadata.WakuMetadataResponse.encode(pubsubTopicsToShardInfo(this.pubsubTopics));
|
77
|
+
const encodedResponse = await pipe([encodedShardInfo], lp.encode, stream, lp.decode, async (source) => await all(source));
|
78
|
+
const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
|
79
|
+
if (error) {
|
80
|
+
return;
|
81
|
+
}
|
82
|
+
await this.savePeerShardInfo(connection.remotePeer, shardInfo);
|
83
|
+
}
|
84
|
+
catch (error) {
|
85
|
+
log.error("Error handling metadata request", error);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
decodeMetadataResponse(encodedResponse) {
|
89
|
+
const bytes = new Uint8ArrayList();
|
90
|
+
encodedResponse.forEach((chunk) => {
|
91
|
+
bytes.append(chunk);
|
92
|
+
});
|
93
|
+
const response = proto_metadata.WakuMetadataResponse.decode(bytes);
|
94
|
+
if (!response) {
|
95
|
+
log.error("Error decoding metadata response");
|
96
|
+
return {
|
97
|
+
shardInfo: null,
|
98
|
+
error: ProtocolError.DECODE_FAILED
|
99
|
+
};
|
100
|
+
}
|
101
|
+
return {
|
102
|
+
shardInfo: response,
|
103
|
+
error: null
|
104
|
+
};
|
105
|
+
}
|
106
|
+
async savePeerShardInfo(peerId, shardInfo) {
|
107
|
+
// add or update the shardInfo to peer store
|
108
|
+
await this.libp2pComponents.peerStore.merge(peerId, {
|
109
|
+
metadata: {
|
110
|
+
shardInfo: encodeRelayShard(shardInfo)
|
111
|
+
}
|
112
|
+
});
|
113
|
+
this.handshakesConfirmed.set(peerId.toString(), shardInfo);
|
114
|
+
}
|
115
|
+
}
|
116
|
+
export function wakuMetadata(pubsubTopics) {
|
117
|
+
return (components) => new Metadata(pubsubTopics, components);
|
118
|
+
}
|
119
|
+
//# sourceMappingURL=metadata.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"metadata.js","sourceRoot":"","sources":["../../../src/lib/metadata/metadata.ts"],"names":[],"mappings":"AAEA,OAAO,EAKL,aAAa,EAGd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,MAAM,GAAG,GAAG,IAAI,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CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,SAAS,CAAC,CAAC;IAC7D,CAAC;CACF;AAED,MAAM,UAAU,YAAY,CAC1B,YAA2B;IAE3B,OAAO,CAAC,UAA4B,EAAE,EAAE,CACtC,IAAI,QAAQ,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;AAC3C,CAAC"}
|
@@ -1,9 +1 @@
|
|
1
|
-
|
2
|
-
import { IDecodedMessage, IDecoder, IStoreCore, Libp2p, PubsubTopic, QueryRequestParams } from "@waku/interfaces";
|
3
|
-
import { BaseProtocol } from "../base_protocol.js";
|
4
|
-
export declare const StoreCodec = "/vac/waku/store-query/3.0.0";
|
5
|
-
export declare class StoreCore extends BaseProtocol implements IStoreCore {
|
6
|
-
readonly pubsubTopics: PubsubTopic[];
|
7
|
-
constructor(pubsubTopics: PubsubTopic[], libp2p: Libp2p);
|
8
|
-
queryPerPage<T extends IDecodedMessage>(queryOpts: QueryRequestParams, decoders: Map<string, IDecoder<T>>, peer: Peer): AsyncGenerator<Promise<T | undefined>[]>;
|
9
|
-
}
|
1
|
+
export { StoreCore, StoreCodec } from "./store.js";
|
package/dist/lib/store/index.js
CHANGED
@@ -1,83 +1,2 @@
|
|
1
|
-
|
2
|
-
import all from "it-all";
|
3
|
-
import * as lp from "it-length-prefixed";
|
4
|
-
import { pipe } from "it-pipe";
|
5
|
-
import { Uint8ArrayList } from "uint8arraylist";
|
6
|
-
import { BaseProtocol } from "../base_protocol.js";
|
7
|
-
import { toProtoMessage } from "../to_proto_message.js";
|
8
|
-
import { DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE, StoreQueryRequest, StoreQueryResponse } from "./rpc.js";
|
9
|
-
const log = new Logger("store");
|
10
|
-
export const StoreCodec = "/vac/waku/store-query/3.0.0";
|
11
|
-
export class StoreCore extends BaseProtocol {
|
12
|
-
pubsubTopics;
|
13
|
-
constructor(pubsubTopics, libp2p) {
|
14
|
-
super(StoreCodec, libp2p.components, log, pubsubTopics);
|
15
|
-
this.pubsubTopics = pubsubTopics;
|
16
|
-
}
|
17
|
-
async *queryPerPage(queryOpts, decoders, peer) {
|
18
|
-
if (queryOpts.contentTopics.toString() !==
|
19
|
-
Array.from(decoders.keys()).toString()) {
|
20
|
-
throw new Error("Internal error, the decoders should match the query's content topics");
|
21
|
-
}
|
22
|
-
let currentCursor = queryOpts.paginationCursor;
|
23
|
-
while (true) {
|
24
|
-
const storeQueryRequest = StoreQueryRequest.create({
|
25
|
-
...queryOpts,
|
26
|
-
paginationCursor: currentCursor
|
27
|
-
});
|
28
|
-
let stream;
|
29
|
-
try {
|
30
|
-
stream = await this.getStream(peer);
|
31
|
-
}
|
32
|
-
catch (e) {
|
33
|
-
log.error("Failed to get stream", e);
|
34
|
-
break;
|
35
|
-
}
|
36
|
-
const res = await pipe([storeQueryRequest.encode()], lp.encode, stream, lp.decode, async (source) => await all(source));
|
37
|
-
const bytes = new Uint8ArrayList();
|
38
|
-
res.forEach((chunk) => {
|
39
|
-
bytes.append(chunk);
|
40
|
-
});
|
41
|
-
const storeQueryResponse = StoreQueryResponse.decode(bytes);
|
42
|
-
if (!storeQueryResponse.statusCode ||
|
43
|
-
storeQueryResponse.statusCode >= 300) {
|
44
|
-
const errorMessage = `Store query failed with status code: ${storeQueryResponse.statusCode}, description: ${storeQueryResponse.statusDesc}`;
|
45
|
-
log.error(errorMessage);
|
46
|
-
throw new Error(errorMessage);
|
47
|
-
}
|
48
|
-
if (!storeQueryResponse.messages || !storeQueryResponse.messages.length) {
|
49
|
-
log.warn("Stopping pagination due to empty messages in response");
|
50
|
-
break;
|
51
|
-
}
|
52
|
-
log.info(`${storeQueryResponse.messages.length} messages retrieved from store`);
|
53
|
-
const decodedMessages = storeQueryResponse.messages.map((protoMsg) => {
|
54
|
-
if (!protoMsg.message) {
|
55
|
-
return Promise.resolve(undefined);
|
56
|
-
}
|
57
|
-
const contentTopic = protoMsg.message.contentTopic;
|
58
|
-
if (contentTopic) {
|
59
|
-
const decoder = decoders.get(contentTopic);
|
60
|
-
if (decoder) {
|
61
|
-
return decoder.fromProtoObj(protoMsg.pubsubTopic || "", toProtoMessage(protoMsg.message));
|
62
|
-
}
|
63
|
-
}
|
64
|
-
return Promise.resolve(undefined);
|
65
|
-
});
|
66
|
-
yield decodedMessages;
|
67
|
-
if (queryOpts.paginationForward) {
|
68
|
-
currentCursor =
|
69
|
-
storeQueryResponse.messages[storeQueryResponse.messages.length - 1]
|
70
|
-
.messageHash;
|
71
|
-
}
|
72
|
-
else {
|
73
|
-
currentCursor = storeQueryResponse.messages[0].messageHash;
|
74
|
-
}
|
75
|
-
if (storeQueryResponse.messages.length > MAX_PAGE_SIZE &&
|
76
|
-
storeQueryResponse.messages.length <
|
77
|
-
(queryOpts.paginationLimit || DEFAULT_PAGE_SIZE)) {
|
78
|
-
break;
|
79
|
-
}
|
80
|
-
}
|
81
|
-
}
|
82
|
-
}
|
1
|
+
export { StoreCore, StoreCodec } from "./store.js";
|
83
2
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/store/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/store/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import type { PeerId } from "@libp2p/interface";
|
2
|
+
import { IDecodedMessage, IDecoder, IStoreCore, Libp2p, PubsubTopic, QueryRequestParams } from "@waku/interfaces";
|
3
|
+
import { BaseProtocol } from "../base_protocol.js";
|
4
|
+
export declare const StoreCodec = "/vac/waku/store-query/3.0.0";
|
5
|
+
export declare class StoreCore extends BaseProtocol implements IStoreCore {
|
6
|
+
readonly pubsubTopics: PubsubTopic[];
|
7
|
+
constructor(pubsubTopics: PubsubTopic[], libp2p: Libp2p);
|
8
|
+
queryPerPage<T extends IDecodedMessage>(queryOpts: QueryRequestParams, decoders: Map<string, IDecoder<T>>, peerId: PeerId): AsyncGenerator<Promise<T | undefined>[]>;
|
9
|
+
}
|