@waku/core 0.0.34-c41b319.0 → 0.0.34-c43cec2.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-CCK9RCtH.js → base_protocol-DxFKDXX2.js} +20 -19
- package/bundle/{index-Db7LxDrL.js → index-yLOEQnIE.js} +39 -75
- package/bundle/index.js +133 -1815
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-ANFNAdFD.js → version_0-Che4t3mN.js} +42 -163
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +2 -2
- package/dist/lib/base_protocol.js +2 -2
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager/connection_manager.d.ts +3 -56
- package/dist/lib/connection_manager/connection_manager.js +13 -96
- package/dist/lib/connection_manager/connection_manager.js.map +1 -1
- package/dist/lib/filter/index.d.ts +18 -1
- package/dist/lib/filter/index.js +208 -1
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/health_manager.d.ts +14 -0
- package/dist/lib/health_manager.js +70 -0
- package/dist/lib/health_manager.js.map +1 -0
- package/dist/lib/light_push/index.d.ts +15 -1
- package/dist/lib/light_push/index.js +144 -1
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +3 -1
- package/dist/lib/metadata/index.js +118 -1
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/store/index.d.ts +9 -1
- package/dist/lib/store/index.js +82 -1
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager/stream_manager.d.ts +2 -2
- package/dist/lib/stream_manager/stream_manager.js +17 -16
- package/dist/lib/stream_manager/stream_manager.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/lib/base_protocol.ts +3 -3
- package/src/lib/connection_manager/connection_manager.ts +20 -114
- package/src/lib/filter/index.ts +315 -1
- package/src/lib/health_manager.ts +90 -0
- package/src/lib/light_push/index.ts +189 -1
- package/src/lib/metadata/index.ts +182 -1
- package/src/lib/store/index.ts +136 -1
- package/src/lib/stream_manager/stream_manager.ts +18 -16
- package/dist/lib/filter/filter.d.ts +0 -18
- package/dist/lib/filter/filter.js +0 -209
- package/dist/lib/filter/filter.js.map +0 -1
- package/dist/lib/light_push/light_push.d.ts +0 -15
- package/dist/lib/light_push/light_push.js +0 -144
- package/dist/lib/light_push/light_push.js.map +0 -1
- package/dist/lib/metadata/metadata.d.ts +0 -3
- package/dist/lib/metadata/metadata.js +0 -119
- package/dist/lib/metadata/metadata.js.map +0 -1
- package/dist/lib/store/store.d.ts +0 -9
- package/dist/lib/store/store.js +0 -83
- package/dist/lib/store/store.js.map +0 -1
- package/src/lib/filter/filter.ts +0 -315
- package/src/lib/light_push/light_push.ts +0 -188
- package/src/lib/metadata/metadata.ts +0 -182
- package/src/lib/store/store.ts +0 -136
@@ -1 +1,189 @@
|
|
1
|
-
|
1
|
+
import type { Peer, 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, 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, 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
|
+
// TODO(weboko): use peer.id as parameter instead
|
80
|
+
public async send(
|
81
|
+
encoder: IEncoder,
|
82
|
+
message: IMessage,
|
83
|
+
peer: Peer
|
84
|
+
): Promise<CoreProtocolResult> {
|
85
|
+
const { query, error: preparationError } = await this.preparePushMessage(
|
86
|
+
encoder,
|
87
|
+
message
|
88
|
+
);
|
89
|
+
|
90
|
+
if (preparationError || !query) {
|
91
|
+
return {
|
92
|
+
success: null,
|
93
|
+
failure: {
|
94
|
+
error: preparationError,
|
95
|
+
peerId: peer.id
|
96
|
+
}
|
97
|
+
};
|
98
|
+
}
|
99
|
+
|
100
|
+
let stream: Stream;
|
101
|
+
try {
|
102
|
+
stream = await this.getStream(peer);
|
103
|
+
} catch (error) {
|
104
|
+
log.error("Failed to get stream", error);
|
105
|
+
return {
|
106
|
+
success: null,
|
107
|
+
failure: {
|
108
|
+
error: ProtocolError.NO_STREAM_AVAILABLE,
|
109
|
+
peerId: peer.id
|
110
|
+
}
|
111
|
+
};
|
112
|
+
}
|
113
|
+
|
114
|
+
let res: Uint8ArrayList[] | undefined;
|
115
|
+
try {
|
116
|
+
res = await pipe(
|
117
|
+
[query.encode()],
|
118
|
+
lp.encode,
|
119
|
+
stream,
|
120
|
+
lp.decode,
|
121
|
+
async (source) => await all(source)
|
122
|
+
);
|
123
|
+
} catch (err) {
|
124
|
+
log.error("Failed to send waku light push request", err);
|
125
|
+
return {
|
126
|
+
success: null,
|
127
|
+
failure: {
|
128
|
+
error: ProtocolError.GENERIC_FAIL,
|
129
|
+
peerId: peer.id
|
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: peer.id
|
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: peer.id
|
160
|
+
}
|
161
|
+
};
|
162
|
+
}
|
163
|
+
|
164
|
+
if (isRLNResponseError(response.info)) {
|
165
|
+
const rlnErrorCase = matchRLNErrorMessage(response.info!);
|
166
|
+
log.error("Remote peer rejected the message: ", rlnErrorCase);
|
167
|
+
return {
|
168
|
+
success: null,
|
169
|
+
failure: {
|
170
|
+
error: rlnErrorCase,
|
171
|
+
peerId: peer.id
|
172
|
+
}
|
173
|
+
};
|
174
|
+
}
|
175
|
+
|
176
|
+
if (!response.isSuccess) {
|
177
|
+
log.error("Remote peer rejected the message: ", response.info);
|
178
|
+
return {
|
179
|
+
success: null,
|
180
|
+
failure: {
|
181
|
+
error: ProtocolError.REMOTE_PEER_REJECTED,
|
182
|
+
peerId: peer.id
|
183
|
+
}
|
184
|
+
};
|
185
|
+
}
|
186
|
+
|
187
|
+
return { success: peer.id, failure: null };
|
188
|
+
}
|
189
|
+
}
|
@@ -1 +1,182 @@
|
|
1
|
-
|
1
|
+
import type { PeerId } from "@libp2p/interface";
|
2
|
+
import { IncomingStreamData } from "@libp2p/interface";
|
3
|
+
import {
|
4
|
+
type IMetadata,
|
5
|
+
type Libp2pComponents,
|
6
|
+
type MetadataQueryResult,
|
7
|
+
type PeerIdStr,
|
8
|
+
ProtocolError,
|
9
|
+
PubsubTopic,
|
10
|
+
type ShardInfo
|
11
|
+
} from "@waku/interfaces";
|
12
|
+
import { proto_metadata } from "@waku/proto";
|
13
|
+
import { encodeRelayShard, Logger, pubsubTopicsToShardInfo } from "@waku/utils";
|
14
|
+
import all from "it-all";
|
15
|
+
import * as lp from "it-length-prefixed";
|
16
|
+
import { pipe } from "it-pipe";
|
17
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
18
|
+
|
19
|
+
import { BaseProtocol } from "../base_protocol.js";
|
20
|
+
|
21
|
+
const log = new Logger("metadata");
|
22
|
+
|
23
|
+
export const MetadataCodec = "/vac/waku/metadata/1.0.0";
|
24
|
+
|
25
|
+
class Metadata extends BaseProtocol implements IMetadata {
|
26
|
+
private libp2pComponents: Libp2pComponents;
|
27
|
+
protected handshakesConfirmed: Map<PeerIdStr, ShardInfo> = new Map();
|
28
|
+
|
29
|
+
public constructor(
|
30
|
+
public pubsubTopics: PubsubTopic[],
|
31
|
+
libp2p: Libp2pComponents
|
32
|
+
) {
|
33
|
+
super(MetadataCodec, libp2p.components, pubsubTopics);
|
34
|
+
this.libp2pComponents = libp2p;
|
35
|
+
void libp2p.registrar.handle(MetadataCodec, (streamData) => {
|
36
|
+
void this.onRequest(streamData);
|
37
|
+
});
|
38
|
+
}
|
39
|
+
|
40
|
+
/**
|
41
|
+
* Make a metadata query to a peer
|
42
|
+
*/
|
43
|
+
public async query(peerId: PeerId): Promise<MetadataQueryResult> {
|
44
|
+
const request = proto_metadata.WakuMetadataRequest.encode(
|
45
|
+
pubsubTopicsToShardInfo(this.pubsubTopics)
|
46
|
+
);
|
47
|
+
|
48
|
+
const peer = await this.libp2pComponents.peerStore.get(peerId);
|
49
|
+
if (!peer) {
|
50
|
+
return {
|
51
|
+
shardInfo: null,
|
52
|
+
error: ProtocolError.NO_PEER_AVAILABLE
|
53
|
+
};
|
54
|
+
}
|
55
|
+
|
56
|
+
let stream;
|
57
|
+
try {
|
58
|
+
stream = await this.getStream(peer);
|
59
|
+
} catch (error) {
|
60
|
+
log.error("Failed to get stream", error);
|
61
|
+
return {
|
62
|
+
shardInfo: null,
|
63
|
+
error: ProtocolError.NO_STREAM_AVAILABLE
|
64
|
+
};
|
65
|
+
}
|
66
|
+
|
67
|
+
const encodedResponse = await pipe(
|
68
|
+
[request],
|
69
|
+
lp.encode,
|
70
|
+
stream,
|
71
|
+
lp.decode,
|
72
|
+
async (source) => await all(source)
|
73
|
+
);
|
74
|
+
|
75
|
+
const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
|
76
|
+
|
77
|
+
if (error) {
|
78
|
+
return {
|
79
|
+
shardInfo: null,
|
80
|
+
error
|
81
|
+
};
|
82
|
+
}
|
83
|
+
|
84
|
+
await this.savePeerShardInfo(peerId, shardInfo);
|
85
|
+
|
86
|
+
return {
|
87
|
+
shardInfo,
|
88
|
+
error: null
|
89
|
+
};
|
90
|
+
}
|
91
|
+
|
92
|
+
public async confirmOrAttemptHandshake(
|
93
|
+
peerId: PeerId
|
94
|
+
): Promise<MetadataQueryResult> {
|
95
|
+
const shardInfo = this.handshakesConfirmed.get(peerId.toString());
|
96
|
+
if (shardInfo) {
|
97
|
+
return {
|
98
|
+
shardInfo,
|
99
|
+
error: null
|
100
|
+
};
|
101
|
+
}
|
102
|
+
|
103
|
+
return await this.query(peerId);
|
104
|
+
}
|
105
|
+
|
106
|
+
/**
|
107
|
+
* Handle an incoming metadata request
|
108
|
+
*/
|
109
|
+
private async onRequest(streamData: IncomingStreamData): Promise<void> {
|
110
|
+
try {
|
111
|
+
const { stream, connection } = streamData;
|
112
|
+
const encodedShardInfo = proto_metadata.WakuMetadataResponse.encode(
|
113
|
+
pubsubTopicsToShardInfo(this.pubsubTopics)
|
114
|
+
);
|
115
|
+
|
116
|
+
const encodedResponse = await pipe(
|
117
|
+
[encodedShardInfo],
|
118
|
+
lp.encode,
|
119
|
+
stream,
|
120
|
+
lp.decode,
|
121
|
+
async (source) => await all(source)
|
122
|
+
);
|
123
|
+
|
124
|
+
const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
|
125
|
+
|
126
|
+
if (error) {
|
127
|
+
return;
|
128
|
+
}
|
129
|
+
|
130
|
+
await this.savePeerShardInfo(connection.remotePeer, shardInfo);
|
131
|
+
} catch (error) {
|
132
|
+
log.error("Error handling metadata request", error);
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
private decodeMetadataResponse(
|
137
|
+
encodedResponse: Uint8ArrayList[]
|
138
|
+
): MetadataQueryResult {
|
139
|
+
const bytes = new Uint8ArrayList();
|
140
|
+
|
141
|
+
encodedResponse.forEach((chunk) => {
|
142
|
+
bytes.append(chunk);
|
143
|
+
});
|
144
|
+
const response = proto_metadata.WakuMetadataResponse.decode(
|
145
|
+
bytes
|
146
|
+
) as ShardInfo;
|
147
|
+
|
148
|
+
if (!response) {
|
149
|
+
log.error("Error decoding metadata response");
|
150
|
+
return {
|
151
|
+
shardInfo: null,
|
152
|
+
error: ProtocolError.DECODE_FAILED
|
153
|
+
};
|
154
|
+
}
|
155
|
+
|
156
|
+
return {
|
157
|
+
shardInfo: response,
|
158
|
+
error: null
|
159
|
+
};
|
160
|
+
}
|
161
|
+
|
162
|
+
private async savePeerShardInfo(
|
163
|
+
peerId: PeerId,
|
164
|
+
shardInfo: ShardInfo
|
165
|
+
): Promise<void> {
|
166
|
+
// add or update the shardInfo to peer store
|
167
|
+
await this.libp2pComponents.peerStore.merge(peerId, {
|
168
|
+
metadata: {
|
169
|
+
shardInfo: encodeRelayShard(shardInfo)
|
170
|
+
}
|
171
|
+
});
|
172
|
+
|
173
|
+
this.handshakesConfirmed.set(peerId.toString(), shardInfo);
|
174
|
+
}
|
175
|
+
}
|
176
|
+
|
177
|
+
export function wakuMetadata(
|
178
|
+
pubsubTopics: PubsubTopic[]
|
179
|
+
): (components: Libp2pComponents) => IMetadata {
|
180
|
+
return (components: Libp2pComponents) =>
|
181
|
+
new Metadata(pubsubTopics, components);
|
182
|
+
}
|
package/src/lib/store/index.ts
CHANGED
@@ -1 +1,136 @@
|
|
1
|
-
|
1
|
+
import type { Peer } from "@libp2p/interface";
|
2
|
+
import {
|
3
|
+
IDecodedMessage,
|
4
|
+
IDecoder,
|
5
|
+
IStoreCore,
|
6
|
+
Libp2p,
|
7
|
+
PubsubTopic,
|
8
|
+
QueryRequestParams
|
9
|
+
} from "@waku/interfaces";
|
10
|
+
import { Logger } from "@waku/utils";
|
11
|
+
import all from "it-all";
|
12
|
+
import * as lp from "it-length-prefixed";
|
13
|
+
import { pipe } from "it-pipe";
|
14
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
15
|
+
|
16
|
+
import { BaseProtocol } from "../base_protocol.js";
|
17
|
+
import { toProtoMessage } from "../to_proto_message.js";
|
18
|
+
|
19
|
+
import {
|
20
|
+
DEFAULT_PAGE_SIZE,
|
21
|
+
MAX_PAGE_SIZE,
|
22
|
+
StoreQueryRequest,
|
23
|
+
StoreQueryResponse
|
24
|
+
} from "./rpc.js";
|
25
|
+
|
26
|
+
const log = new Logger("store");
|
27
|
+
|
28
|
+
export const StoreCodec = "/vac/waku/store-query/3.0.0";
|
29
|
+
|
30
|
+
export class StoreCore extends BaseProtocol implements IStoreCore {
|
31
|
+
public constructor(
|
32
|
+
public readonly pubsubTopics: PubsubTopic[],
|
33
|
+
libp2p: Libp2p
|
34
|
+
) {
|
35
|
+
super(StoreCodec, libp2p.components, pubsubTopics);
|
36
|
+
}
|
37
|
+
|
38
|
+
public async *queryPerPage<T extends IDecodedMessage>(
|
39
|
+
queryOpts: QueryRequestParams,
|
40
|
+
decoders: Map<string, IDecoder<T>>,
|
41
|
+
peer: Peer
|
42
|
+
): AsyncGenerator<Promise<T | undefined>[]> {
|
43
|
+
if (
|
44
|
+
queryOpts.contentTopics.toString() !==
|
45
|
+
Array.from(decoders.keys()).toString()
|
46
|
+
) {
|
47
|
+
throw new Error(
|
48
|
+
"Internal error, the decoders should match the query's content topics"
|
49
|
+
);
|
50
|
+
}
|
51
|
+
|
52
|
+
let currentCursor = queryOpts.paginationCursor;
|
53
|
+
while (true) {
|
54
|
+
const storeQueryRequest = StoreQueryRequest.create({
|
55
|
+
...queryOpts,
|
56
|
+
paginationCursor: currentCursor
|
57
|
+
});
|
58
|
+
|
59
|
+
let stream;
|
60
|
+
try {
|
61
|
+
stream = await this.getStream(peer);
|
62
|
+
} catch (e) {
|
63
|
+
log.error("Failed to get stream", e);
|
64
|
+
break;
|
65
|
+
}
|
66
|
+
|
67
|
+
const res = await pipe(
|
68
|
+
[storeQueryRequest.encode()],
|
69
|
+
lp.encode,
|
70
|
+
stream,
|
71
|
+
lp.decode,
|
72
|
+
async (source) => await all(source)
|
73
|
+
);
|
74
|
+
|
75
|
+
const bytes = new Uint8ArrayList();
|
76
|
+
res.forEach((chunk) => {
|
77
|
+
bytes.append(chunk);
|
78
|
+
});
|
79
|
+
|
80
|
+
const storeQueryResponse = StoreQueryResponse.decode(bytes);
|
81
|
+
|
82
|
+
if (
|
83
|
+
!storeQueryResponse.statusCode ||
|
84
|
+
storeQueryResponse.statusCode >= 300
|
85
|
+
) {
|
86
|
+
const errorMessage = `Store query failed with status code: ${storeQueryResponse.statusCode}, description: ${storeQueryResponse.statusDesc}`;
|
87
|
+
log.error(errorMessage);
|
88
|
+
throw new Error(errorMessage);
|
89
|
+
}
|
90
|
+
|
91
|
+
if (!storeQueryResponse.messages || !storeQueryResponse.messages.length) {
|
92
|
+
log.warn("Stopping pagination due to empty messages in response");
|
93
|
+
break;
|
94
|
+
}
|
95
|
+
|
96
|
+
log.info(
|
97
|
+
`${storeQueryResponse.messages.length} messages retrieved from store`
|
98
|
+
);
|
99
|
+
|
100
|
+
const decodedMessages = storeQueryResponse.messages.map((protoMsg) => {
|
101
|
+
if (!protoMsg.message) {
|
102
|
+
return Promise.resolve(undefined);
|
103
|
+
}
|
104
|
+
const contentTopic = protoMsg.message.contentTopic;
|
105
|
+
if (contentTopic) {
|
106
|
+
const decoder = decoders.get(contentTopic);
|
107
|
+
if (decoder) {
|
108
|
+
return decoder.fromProtoObj(
|
109
|
+
protoMsg.pubsubTopic || "",
|
110
|
+
toProtoMessage(protoMsg.message)
|
111
|
+
);
|
112
|
+
}
|
113
|
+
}
|
114
|
+
return Promise.resolve(undefined);
|
115
|
+
});
|
116
|
+
|
117
|
+
yield decodedMessages;
|
118
|
+
|
119
|
+
if (queryOpts.paginationForward) {
|
120
|
+
currentCursor =
|
121
|
+
storeQueryResponse.messages[storeQueryResponse.messages.length - 1]
|
122
|
+
.messageHash;
|
123
|
+
} else {
|
124
|
+
currentCursor = storeQueryResponse.messages[0].messageHash;
|
125
|
+
}
|
126
|
+
|
127
|
+
if (
|
128
|
+
storeQueryResponse.messages.length > MAX_PAGE_SIZE &&
|
129
|
+
storeQueryResponse.messages.length <
|
130
|
+
(queryOpts.paginationLimit || DEFAULT_PAGE_SIZE)
|
131
|
+
) {
|
132
|
+
break;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
}
|
@@ -21,38 +21,39 @@ export class StreamManager {
|
|
21
21
|
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
22
22
|
}
|
23
23
|
|
24
|
-
public async getStream(
|
25
|
-
const
|
26
|
-
|
24
|
+
public async getStream(peer: Peer): Promise<Stream> {
|
25
|
+
const peerId = peer.id.toString();
|
26
|
+
|
27
|
+
const scheduledStream = this.streamPool.get(peerId);
|
27
28
|
|
28
29
|
if (scheduledStream) {
|
29
|
-
this.streamPool.delete(
|
30
|
+
this.streamPool.delete(peerId);
|
30
31
|
await scheduledStream;
|
31
32
|
}
|
32
33
|
|
33
|
-
let stream = this.getOpenStreamForCodec(
|
34
|
+
let stream = this.getOpenStreamForCodec(peer.id);
|
34
35
|
|
35
36
|
if (stream) {
|
36
37
|
this.log.info(
|
37
|
-
`Found existing stream peerId=${
|
38
|
+
`Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}`
|
38
39
|
);
|
39
|
-
this.lockStream(
|
40
|
+
this.lockStream(peer.id.toString(), stream);
|
40
41
|
return stream;
|
41
42
|
}
|
42
43
|
|
43
|
-
stream = await this.createStream(
|
44
|
-
this.lockStream(
|
44
|
+
stream = await this.createStream(peer);
|
45
|
+
this.lockStream(peer.id.toString(), stream);
|
45
46
|
|
46
47
|
return stream;
|
47
48
|
}
|
48
49
|
|
49
|
-
private async createStream(
|
50
|
-
const connections = this.getConnections(
|
50
|
+
private async createStream(peer: Peer, retries = 0): Promise<Stream> {
|
51
|
+
const connections = this.getConnections(peer.id);
|
51
52
|
const connection = selectOpenConnection(connections);
|
52
53
|
|
53
54
|
if (!connection) {
|
54
55
|
throw new Error(
|
55
|
-
`Failed to get a connection to the peer peerId=${
|
56
|
+
`Failed to get a connection to the peer peerId=${peer.id.toString()} multicodec=${this.multicodec}`
|
56
57
|
);
|
57
58
|
}
|
58
59
|
|
@@ -62,11 +63,11 @@ export class StreamManager {
|
|
62
63
|
for (let i = 0; i < retries + 1; i++) {
|
63
64
|
try {
|
64
65
|
this.log.info(
|
65
|
-
`Attempting to create a stream for peerId=${
|
66
|
+
`Attempting to create a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
|
66
67
|
);
|
67
68
|
stream = await connection.newStream(this.multicodec);
|
68
69
|
this.log.info(
|
69
|
-
`Created stream for peerId=${
|
70
|
+
`Created stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
|
70
71
|
);
|
71
72
|
break;
|
72
73
|
} catch (error) {
|
@@ -76,7 +77,8 @@ export class StreamManager {
|
|
76
77
|
|
77
78
|
if (!stream) {
|
78
79
|
throw new Error(
|
79
|
-
`Failed to create a new stream for ${
|
80
|
+
`Failed to create a new stream for ${peer.id.toString()} -- ` +
|
81
|
+
lastError
|
80
82
|
);
|
81
83
|
}
|
82
84
|
|
@@ -95,7 +97,7 @@ export class StreamManager {
|
|
95
97
|
|
96
98
|
try {
|
97
99
|
this.ongoingCreation.add(peerId);
|
98
|
-
await this.createStream(peer
|
100
|
+
await this.createStream(peer);
|
99
101
|
} catch (error) {
|
100
102
|
this.log.error(`Failed to createStreamWithLock:`, error);
|
101
103
|
} finally {
|
@@ -1,18 +0,0 @@
|
|
1
|
-
import type { PeerId } from "@libp2p/interface";
|
2
|
-
import { type ContentTopic, type CoreProtocolResult, type IBaseProtocolCore, type Libp2p, type PubsubTopic } from "@waku/interfaces";
|
3
|
-
import { WakuMessage } from "@waku/proto";
|
4
|
-
import { BaseProtocol } from "../base_protocol.js";
|
5
|
-
export declare const FilterCodecs: {
|
6
|
-
SUBSCRIBE: string;
|
7
|
-
PUSH: string;
|
8
|
-
};
|
9
|
-
export declare class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
10
|
-
private handleIncomingMessage;
|
11
|
-
readonly pubsubTopics: PubsubTopic[];
|
12
|
-
constructor(handleIncomingMessage: (pubsubTopic: PubsubTopic, wakuMessage: WakuMessage, peerIdStr: string) => Promise<void>, pubsubTopics: PubsubTopic[], libp2p: Libp2p);
|
13
|
-
subscribe(pubsubTopic: PubsubTopic, peerId: PeerId, contentTopics: ContentTopic[]): Promise<CoreProtocolResult>;
|
14
|
-
unsubscribe(pubsubTopic: PubsubTopic, peerId: PeerId, contentTopics: ContentTopic[]): Promise<CoreProtocolResult>;
|
15
|
-
unsubscribeAll(pubsubTopic: PubsubTopic, peerId: PeerId): Promise<CoreProtocolResult>;
|
16
|
-
ping(peerId: PeerId): Promise<CoreProtocolResult>;
|
17
|
-
private onRequest;
|
18
|
-
}
|