@waku/core 0.0.36-f911bf8.0 → 0.0.37-2ed5ddc.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 +39 -0
- package/bundle/index.js +5647 -1359
- package/bundle/lib/message/version_0.js +1 -2
- package/bundle/{version_0-CiYGrPc2.js → version_0-Bc0h7ah2.js} +1886 -31
- 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/connection_manager/connection_limiter.d.ts +35 -0
- package/dist/lib/connection_manager/connection_limiter.js +103 -0
- package/dist/lib/connection_manager/connection_limiter.js.map +1 -0
- package/dist/lib/connection_manager/connection_manager.d.ts +19 -104
- package/dist/lib/connection_manager/connection_manager.js +64 -499
- package/dist/lib/connection_manager/connection_manager.js.map +1 -1
- package/dist/lib/connection_manager/discovery_dialer.d.ts +32 -0
- package/dist/lib/connection_manager/discovery_dialer.js +131 -0
- package/dist/lib/connection_manager/discovery_dialer.js.map +1 -0
- package/dist/lib/connection_manager/keep_alive_manager.d.ts +17 -7
- package/dist/lib/connection_manager/keep_alive_manager.js +110 -74
- package/dist/lib/connection_manager/keep_alive_manager.js.map +1 -1
- package/dist/lib/connection_manager/network_monitor.d.ts +22 -0
- package/dist/lib/connection_manager/network_monitor.js +63 -0
- package/dist/lib/connection_manager/network_monitor.js.map +1 -0
- package/dist/lib/connection_manager/shard_reader.d.ts +28 -0
- package/dist/lib/connection_manager/shard_reader.js +70 -0
- package/dist/lib/connection_manager/shard_reader.js.map +1 -0
- package/dist/lib/connection_manager/utils.d.ts +16 -1
- package/dist/lib/connection_manager/utils.js +23 -0
- package/dist/lib/connection_manager/utils.js.map +1 -1
- package/dist/lib/filter/filter.d.ts +7 -5
- package/dist/lib/filter/filter.js +14 -11
- package/dist/lib/filter/filter.js.map +1 -1
- package/dist/lib/light_push/light_push.d.ts +5 -5
- package/dist/lib/light_push/light_push.js +7 -7
- package/dist/lib/light_push/light_push.js.map +1 -1
- package/dist/lib/message/version_0.d.ts +3 -4
- package/dist/lib/message/version_0.js +1 -4
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/message_hash/index.d.ts +1 -0
- package/dist/lib/message_hash/index.js +2 -0
- package/dist/lib/message_hash/index.js.map +1 -0
- package/dist/lib/message_hash/message_hash.d.ts +52 -0
- package/dist/lib/message_hash/message_hash.js +84 -0
- package/dist/lib/message_hash/message_hash.js.map +1 -0
- package/dist/lib/metadata/metadata.js +6 -4
- package/dist/lib/metadata/metadata.js.map +1 -1
- package/dist/lib/store/rpc.js +16 -10
- package/dist/lib/store/rpc.js.map +1 -1
- package/dist/lib/store/store.d.ts +5 -5
- package/dist/lib/store/store.js +19 -9
- package/dist/lib/store/store.js.map +1 -1
- package/dist/lib/stream_manager/stream_manager.d.ts +3 -4
- package/dist/lib/stream_manager/stream_manager.js +6 -8
- 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/connection_manager/connection_limiter.ts +161 -0
- package/src/lib/connection_manager/connection_manager.ts +87 -668
- package/src/lib/connection_manager/discovery_dialer.ts +195 -0
- package/src/lib/connection_manager/keep_alive_manager.ts +154 -87
- package/src/lib/connection_manager/network_monitor.ts +88 -0
- package/src/lib/connection_manager/shard_reader.ts +134 -0
- package/src/lib/connection_manager/utils.ts +27 -1
- package/src/lib/filter/filter.ts +26 -15
- package/src/lib/light_push/light_push.ts +9 -10
- package/src/lib/message/version_0.ts +3 -7
- package/src/lib/message_hash/index.ts +1 -0
- package/src/lib/message_hash/message_hash.ts +106 -0
- package/src/lib/metadata/metadata.ts +8 -5
- package/src/lib/store/rpc.ts +23 -19
- package/src/lib/store/store.ts +22 -11
- package/src/lib/stream_manager/stream_manager.ts +8 -6
- package/bundle/base_protocol-DvQrudwy.js +0 -152
- package/bundle/index-CTo1my9M.js +0 -1543
- package/bundle/lib/base_protocol.js +0 -2
- package/dist/lib/base_protocol.d.ts +0 -18
- package/dist/lib/base_protocol.js +0 -25
- package/dist/lib/base_protocol.js.map +0 -1
- package/src/lib/base_protocol.ts +0 -44
@@ -0,0 +1,134 @@
|
|
1
|
+
import type { PeerId } from "@libp2p/interface";
|
2
|
+
import type {
|
3
|
+
NetworkConfig,
|
4
|
+
PubsubTopic,
|
5
|
+
ShardInfo,
|
6
|
+
SingleShardInfo,
|
7
|
+
StaticSharding
|
8
|
+
} from "@waku/interfaces";
|
9
|
+
import {
|
10
|
+
contentTopicToShardIndex,
|
11
|
+
decodeRelayShard,
|
12
|
+
Logger,
|
13
|
+
pubsubTopicToSingleShardInfo
|
14
|
+
} from "@waku/utils";
|
15
|
+
import { Libp2p } from "libp2p";
|
16
|
+
|
17
|
+
const log = new Logger("shard-reader");
|
18
|
+
|
19
|
+
type ShardReaderConstructorOptions = {
|
20
|
+
libp2p: Libp2p;
|
21
|
+
networkConfig: NetworkConfig;
|
22
|
+
};
|
23
|
+
|
24
|
+
interface IShardReader {
|
25
|
+
hasShardInfo(id: PeerId): Promise<boolean>;
|
26
|
+
isPeerOnNetwork(id: PeerId): Promise<boolean>;
|
27
|
+
isPeerOnShard(id: PeerId, shard: SingleShardInfo): Promise<boolean>;
|
28
|
+
isPeerOnTopic(id: PeerId, pubsubTopic: PubsubTopic): Promise<boolean>;
|
29
|
+
}
|
30
|
+
|
31
|
+
/**
|
32
|
+
* This class is responsible for reading the shard info from the libp2p peer store or from the current node's network config.
|
33
|
+
*/
|
34
|
+
export class ShardReader implements IShardReader {
|
35
|
+
private readonly libp2p: Libp2p;
|
36
|
+
|
37
|
+
private readonly staticShard: StaticSharding;
|
38
|
+
|
39
|
+
public constructor(options: ShardReaderConstructorOptions) {
|
40
|
+
this.libp2p = options.libp2p;
|
41
|
+
|
42
|
+
this.staticShard = this.getStaticShardFromNetworkConfig(
|
43
|
+
options.networkConfig
|
44
|
+
);
|
45
|
+
}
|
46
|
+
|
47
|
+
public async isPeerOnNetwork(id: PeerId): Promise<boolean> {
|
48
|
+
const shardInfo = await this.getShardInfo(id);
|
49
|
+
|
50
|
+
if (!shardInfo) {
|
51
|
+
return false;
|
52
|
+
}
|
53
|
+
|
54
|
+
const clusterMatch = shardInfo.clusterId === this.staticShard.clusterId;
|
55
|
+
const shardOverlap = this.staticShard.shards.some((s) =>
|
56
|
+
shardInfo.shards.includes(s)
|
57
|
+
);
|
58
|
+
|
59
|
+
return clusterMatch && shardOverlap;
|
60
|
+
}
|
61
|
+
|
62
|
+
public async hasShardInfo(id: PeerId): Promise<boolean> {
|
63
|
+
const shardInfo = await this.getShardInfo(id);
|
64
|
+
return !!shardInfo;
|
65
|
+
}
|
66
|
+
|
67
|
+
public async isPeerOnTopic(
|
68
|
+
id: PeerId,
|
69
|
+
pubsubTopic: PubsubTopic
|
70
|
+
): Promise<boolean> {
|
71
|
+
try {
|
72
|
+
const shardInfo = pubsubTopicToSingleShardInfo(pubsubTopic);
|
73
|
+
return await this.isPeerOnShard(id, shardInfo);
|
74
|
+
} catch (error) {
|
75
|
+
log.error(
|
76
|
+
`Error comparing pubsub topic ${pubsubTopic} with shard info for ${id}`,
|
77
|
+
error
|
78
|
+
);
|
79
|
+
return false;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
public async isPeerOnShard(
|
84
|
+
id: PeerId,
|
85
|
+
shard: SingleShardInfo
|
86
|
+
): Promise<boolean> {
|
87
|
+
const peerShardInfo = await this.getShardInfo(id);
|
88
|
+
|
89
|
+
if (!peerShardInfo || shard.shard === undefined) {
|
90
|
+
return false;
|
91
|
+
}
|
92
|
+
|
93
|
+
return (
|
94
|
+
peerShardInfo.clusterId === shard.clusterId &&
|
95
|
+
peerShardInfo.shards.includes(shard.shard)
|
96
|
+
);
|
97
|
+
}
|
98
|
+
|
99
|
+
private async getShardInfo(id: PeerId): Promise<ShardInfo | undefined> {
|
100
|
+
try {
|
101
|
+
const peer = await this.libp2p.peerStore.get(id);
|
102
|
+
|
103
|
+
const shardInfoBytes = peer.metadata.get("shardInfo");
|
104
|
+
|
105
|
+
if (!shardInfoBytes) {
|
106
|
+
return undefined;
|
107
|
+
}
|
108
|
+
|
109
|
+
const decodedShardInfo = decodeRelayShard(shardInfoBytes);
|
110
|
+
|
111
|
+
return decodedShardInfo;
|
112
|
+
} catch (error) {
|
113
|
+
log.error(`Error getting shard info for ${id}`, error);
|
114
|
+
return undefined;
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
private getStaticShardFromNetworkConfig(
|
119
|
+
networkConfig: NetworkConfig
|
120
|
+
): StaticSharding {
|
121
|
+
if ("shards" in networkConfig) {
|
122
|
+
return networkConfig;
|
123
|
+
}
|
124
|
+
|
125
|
+
const shards = networkConfig.contentTopics.map((topic) =>
|
126
|
+
contentTopicToShardIndex(topic)
|
127
|
+
);
|
128
|
+
|
129
|
+
return {
|
130
|
+
clusterId: networkConfig.clusterId!,
|
131
|
+
shards
|
132
|
+
};
|
133
|
+
}
|
134
|
+
}
|
@@ -1,4 +1,6 @@
|
|
1
|
-
import type
|
1
|
+
import { isPeerId, type Peer, type PeerId } from "@libp2p/interface";
|
2
|
+
import { peerIdFromString } from "@libp2p/peer-id";
|
3
|
+
import { Multiaddr, multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
|
2
4
|
import { bytesToUtf8 } from "@waku/utils/bytes";
|
3
5
|
|
4
6
|
/**
|
@@ -23,3 +25,27 @@ export const getPeerPing = (peer: Peer | null): number => {
|
|
23
25
|
return -1;
|
24
26
|
}
|
25
27
|
};
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Maps a PeerId or MultiaddrInput to a PeerId or Multiaddr.
|
31
|
+
* @param input - The PeerId or MultiaddrInput to map.
|
32
|
+
* @returns The PeerId or Multiaddr.
|
33
|
+
* @throws {Error} If the input is not a valid PeerId or MultiaddrInput.
|
34
|
+
*/
|
35
|
+
export const mapToPeerIdOrMultiaddr = (
|
36
|
+
input: PeerId | MultiaddrInput
|
37
|
+
): PeerId | Multiaddr => {
|
38
|
+
return isPeerId(input) ? input : multiaddr(input);
|
39
|
+
};
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Maps a PeerId or MultiaddrInput to a PeerId.
|
43
|
+
* @param input - The PeerId or MultiaddrInput to map.
|
44
|
+
* @returns The PeerId.
|
45
|
+
* @throws {Error} If the input is not a valid PeerId or MultiaddrInput.
|
46
|
+
*/
|
47
|
+
export const mapToPeerId = (input: PeerId | MultiaddrInput): PeerId => {
|
48
|
+
return isPeerId(input)
|
49
|
+
? input
|
50
|
+
: peerIdFromString(multiaddr(input).getPeerId()!);
|
51
|
+
};
|
package/src/lib/filter/filter.ts
CHANGED
@@ -3,7 +3,6 @@ import type { IncomingStreamData } from "@libp2p/interface-internal";
|
|
3
3
|
import {
|
4
4
|
type ContentTopic,
|
5
5
|
type CoreProtocolResult,
|
6
|
-
type IBaseProtocolCore,
|
7
6
|
type Libp2p,
|
8
7
|
ProtocolError,
|
9
8
|
type PubsubTopic
|
@@ -15,7 +14,7 @@ import * as lp from "it-length-prefixed";
|
|
15
14
|
import { pipe } from "it-pipe";
|
16
15
|
import { Uint8ArrayList } from "uint8arraylist";
|
17
16
|
|
18
|
-
import {
|
17
|
+
import { StreamManager } from "../stream_manager/index.js";
|
19
18
|
|
20
19
|
import {
|
21
20
|
FilterPushRpc,
|
@@ -23,24 +22,32 @@ import {
|
|
23
22
|
FilterSubscribeRpc
|
24
23
|
} from "./filter_rpc.js";
|
25
24
|
|
26
|
-
const log = new Logger("filter
|
25
|
+
const log = new Logger("filter-core");
|
27
26
|
|
28
27
|
export const FilterCodecs = {
|
29
28
|
SUBSCRIBE: "/vac/waku/filter-subscribe/2.0.0-beta1",
|
30
29
|
PUSH: "/vac/waku/filter-push/2.0.0-beta1"
|
31
30
|
};
|
32
31
|
|
33
|
-
|
32
|
+
type IncomingMessageHandler = (
|
33
|
+
pubsubTopic: PubsubTopic,
|
34
|
+
wakuMessage: WakuMessage,
|
35
|
+
peerIdStr: string
|
36
|
+
) => Promise<void>;
|
37
|
+
|
38
|
+
export class FilterCore {
|
39
|
+
private streamManager: StreamManager;
|
40
|
+
|
41
|
+
public readonly multicodec = FilterCodecs.SUBSCRIBE;
|
42
|
+
|
34
43
|
public constructor(
|
35
|
-
private handleIncomingMessage:
|
36
|
-
pubsubTopic: PubsubTopic,
|
37
|
-
wakuMessage: WakuMessage,
|
38
|
-
peerIdStr: string
|
39
|
-
) => Promise<void>,
|
40
|
-
public readonly pubsubTopics: PubsubTopic[],
|
44
|
+
private handleIncomingMessage: IncomingMessageHandler,
|
41
45
|
libp2p: Libp2p
|
42
46
|
) {
|
43
|
-
|
47
|
+
this.streamManager = new StreamManager(
|
48
|
+
FilterCodecs.SUBSCRIBE,
|
49
|
+
libp2p.components
|
50
|
+
);
|
44
51
|
|
45
52
|
libp2p
|
46
53
|
.handle(FilterCodecs.PUSH, this.onRequest.bind(this), {
|
@@ -56,7 +63,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
56
63
|
peerId: PeerId,
|
57
64
|
contentTopics: ContentTopic[]
|
58
65
|
): Promise<CoreProtocolResult> {
|
59
|
-
const stream = await this.getStream(peerId);
|
66
|
+
const stream = await this.streamManager.getStream(peerId);
|
60
67
|
|
61
68
|
const request = FilterSubscribeRpc.createSubscribeRequest(
|
62
69
|
pubsubTopic,
|
@@ -72,6 +79,10 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
72
79
|
lp.decode,
|
73
80
|
async (source) => await all(source)
|
74
81
|
);
|
82
|
+
|
83
|
+
if (!res?.length) {
|
84
|
+
throw Error("Received no response from subscription request.");
|
85
|
+
}
|
75
86
|
} catch (error) {
|
76
87
|
log.error("Failed to send subscribe request", error);
|
77
88
|
return {
|
@@ -112,7 +123,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
112
123
|
): Promise<CoreProtocolResult> {
|
113
124
|
let stream: Stream | undefined;
|
114
125
|
try {
|
115
|
-
stream = await this.getStream(peerId);
|
126
|
+
stream = await this.streamManager.getStream(peerId);
|
116
127
|
} catch (error) {
|
117
128
|
log.error(
|
118
129
|
`Failed to get a stream for remote peer${peerId.toString()}`,
|
@@ -155,7 +166,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
155
166
|
pubsubTopic: PubsubTopic,
|
156
167
|
peerId: PeerId
|
157
168
|
): Promise<CoreProtocolResult> {
|
158
|
-
const stream = await this.getStream(peerId);
|
169
|
+
const stream = await this.streamManager.getStream(peerId);
|
159
170
|
|
160
171
|
const request = FilterSubscribeRpc.createUnsubscribeAllRequest(pubsubTopic);
|
161
172
|
|
@@ -202,7 +213,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
202
213
|
public async ping(peerId: PeerId): Promise<CoreProtocolResult> {
|
203
214
|
let stream: Stream | undefined;
|
204
215
|
try {
|
205
|
-
stream = await this.getStream(peerId);
|
216
|
+
stream = await this.streamManager.getStream(peerId);
|
206
217
|
} catch (error) {
|
207
218
|
log.error(
|
208
219
|
`Failed to get a stream for remote peer${peerId.toString()}`,
|
@@ -1,12 +1,10 @@
|
|
1
1
|
import type { PeerId, Stream } from "@libp2p/interface";
|
2
2
|
import {
|
3
3
|
type CoreProtocolResult,
|
4
|
-
type IBaseProtocolCore,
|
5
4
|
type IEncoder,
|
6
5
|
type IMessage,
|
7
6
|
type Libp2p,
|
8
7
|
ProtocolError,
|
9
|
-
PubsubTopic,
|
10
8
|
type ThisOrThat
|
11
9
|
} from "@waku/interfaces";
|
12
10
|
import { PushResponse } from "@waku/proto";
|
@@ -17,7 +15,7 @@ import * as lp from "it-length-prefixed";
|
|
17
15
|
import { pipe } from "it-pipe";
|
18
16
|
import { Uint8ArrayList } from "uint8arraylist";
|
19
17
|
|
20
|
-
import {
|
18
|
+
import { StreamManager } from "../stream_manager/index.js";
|
21
19
|
|
22
20
|
import { PushRpc } from "./push_rpc.js";
|
23
21
|
import { isRLNResponseError } from "./utils.js";
|
@@ -32,12 +30,13 @@ type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
|
|
32
30
|
/**
|
33
31
|
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
34
32
|
*/
|
35
|
-
export class LightPushCore
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
33
|
+
export class LightPushCore {
|
34
|
+
private readonly streamManager: StreamManager;
|
35
|
+
|
36
|
+
public readonly multicodec = LightPushCodec;
|
37
|
+
|
38
|
+
public constructor(libp2p: Libp2p) {
|
39
|
+
this.streamManager = new StreamManager(LightPushCodec, libp2p.components);
|
41
40
|
}
|
42
41
|
|
43
42
|
private async preparePushMessage(
|
@@ -98,7 +97,7 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
98
97
|
|
99
98
|
let stream: Stream;
|
100
99
|
try {
|
101
|
-
stream = await this.getStream(peerId);
|
100
|
+
stream = await this.streamManager.getStream(peerId);
|
102
101
|
} catch (error) {
|
103
102
|
log.error("Failed to get stream", error);
|
104
103
|
return {
|
@@ -22,7 +22,7 @@ export { proto };
|
|
22
22
|
export class DecodedMessage implements IDecodedMessage {
|
23
23
|
public constructor(
|
24
24
|
public pubsubTopic: string,
|
25
|
-
|
25
|
+
private proto: proto.WakuMessage
|
26
26
|
) {}
|
27
27
|
|
28
28
|
public get ephemeral(): boolean {
|
@@ -37,10 +37,6 @@ export class DecodedMessage implements IDecodedMessage {
|
|
37
37
|
return this.proto.contentTopic;
|
38
38
|
}
|
39
39
|
|
40
|
-
public get _rawTimestamp(): bigint | undefined {
|
41
|
-
return this.proto.timestamp;
|
42
|
-
}
|
43
|
-
|
44
40
|
public get timestamp(): Date | undefined {
|
45
41
|
// In the case we receive a value that is bigger than JS's max number,
|
46
42
|
// we catch the error and return undefined.
|
@@ -63,7 +59,7 @@ export class DecodedMessage implements IDecodedMessage {
|
|
63
59
|
public get version(): number {
|
64
60
|
// https://rfc.vac.dev/spec/14/
|
65
61
|
// > If omitted, the value SHOULD be interpreted as version 0.
|
66
|
-
return this.proto.version ??
|
62
|
+
return this.proto.version ?? Version;
|
67
63
|
}
|
68
64
|
|
69
65
|
public get rateLimitProof(): IRateLimitProof | undefined {
|
@@ -160,7 +156,7 @@ export class Decoder implements IDecoder<IDecodedMessage> {
|
|
160
156
|
public async fromProtoObj(
|
161
157
|
pubsubTopic: string,
|
162
158
|
proto: IProtoMessage
|
163
|
-
): Promise<
|
159
|
+
): Promise<IDecodedMessage | undefined> {
|
164
160
|
// https://rfc.vac.dev/spec/14/
|
165
161
|
// > If omitted, the value SHOULD be interpreted as version 0.
|
166
162
|
if (proto.version ?? 0 !== Version) {
|
@@ -0,0 +1 @@
|
|
1
|
+
export { messageHash, messageHashStr } from "./message_hash.js";
|
@@ -0,0 +1,106 @@
|
|
1
|
+
import { sha256 } from "@noble/hashes/sha256";
|
2
|
+
import type { IDecodedMessage, IProtoMessage } from "@waku/interfaces";
|
3
|
+
import { isDefined } from "@waku/utils";
|
4
|
+
import {
|
5
|
+
bytesToHex,
|
6
|
+
concat,
|
7
|
+
numberToBytes,
|
8
|
+
utf8ToBytes
|
9
|
+
} from "@waku/utils/bytes";
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Deterministic Message Hashing as defined in
|
13
|
+
* [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/#deterministic-message-hashing)
|
14
|
+
*
|
15
|
+
* Computes a SHA-256 hash of the concatenation of pubsub topic, payload, content topic, meta, and timestamp.
|
16
|
+
*
|
17
|
+
* @param pubsubTopic - The pubsub topic string
|
18
|
+
* @param message - The message to be hashed
|
19
|
+
* @returns A Uint8Array containing the SHA-256 hash
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* ```typescript
|
23
|
+
* import { messageHash } from "@waku/core";
|
24
|
+
*
|
25
|
+
* const pubsubTopic = "/waku/2/default-waku/proto";
|
26
|
+
* const message = {
|
27
|
+
* payload: new Uint8Array([1, 2, 3, 4]),
|
28
|
+
* contentTopic: "/waku/2/default-content/proto",
|
29
|
+
* meta: new Uint8Array([5, 6, 7, 8]),
|
30
|
+
* timestamp: new Date()
|
31
|
+
* };
|
32
|
+
*
|
33
|
+
* const hash = messageHash(pubsubTopic, message);
|
34
|
+
* ```
|
35
|
+
*/
|
36
|
+
export function messageHash(
|
37
|
+
pubsubTopic: string,
|
38
|
+
message: IProtoMessage | IDecodedMessage
|
39
|
+
): Uint8Array {
|
40
|
+
const pubsubTopicBytes = utf8ToBytes(pubsubTopic);
|
41
|
+
const contentTopicBytes = utf8ToBytes(message.contentTopic);
|
42
|
+
const timestampBytes = tryConvertTimestampToBytes(message.timestamp);
|
43
|
+
|
44
|
+
const bytes = concat(
|
45
|
+
[
|
46
|
+
pubsubTopicBytes,
|
47
|
+
message.payload,
|
48
|
+
contentTopicBytes,
|
49
|
+
message.meta,
|
50
|
+
timestampBytes
|
51
|
+
].filter(isDefined)
|
52
|
+
);
|
53
|
+
|
54
|
+
return sha256(bytes);
|
55
|
+
}
|
56
|
+
|
57
|
+
function tryConvertTimestampToBytes(
|
58
|
+
timestamp: Date | number | bigint | undefined
|
59
|
+
): undefined | Uint8Array {
|
60
|
+
if (!timestamp) {
|
61
|
+
return;
|
62
|
+
}
|
63
|
+
|
64
|
+
let bigIntTimestamp: bigint;
|
65
|
+
|
66
|
+
if (typeof timestamp === "bigint") {
|
67
|
+
bigIntTimestamp = timestamp;
|
68
|
+
} else {
|
69
|
+
bigIntTimestamp = BigInt(timestamp.valueOf()) * 1000000n;
|
70
|
+
}
|
71
|
+
|
72
|
+
return numberToBytes(bigIntTimestamp);
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Computes a deterministic message hash and returns it as a hexadecimal string.
|
77
|
+
* This is a convenience wrapper around messageHash that converts the result to a hex string.
|
78
|
+
*
|
79
|
+
* @param pubsubTopic - The pubsub topic string
|
80
|
+
* @param message - The message to be hashed
|
81
|
+
* @returns A string containing the hex representation of the SHA-256 hash
|
82
|
+
*
|
83
|
+
* @example
|
84
|
+
* ```typescript
|
85
|
+
* import { messageHashStr } from "@waku/core";
|
86
|
+
*
|
87
|
+
* const pubsubTopic = "/waku/2/default-waku/proto";
|
88
|
+
* const message = {
|
89
|
+
* payload: new Uint8Array([1, 2, 3, 4]),
|
90
|
+
* contentTopic: "/waku/2/default-content/proto",
|
91
|
+
* meta: new Uint8Array([5, 6, 7, 8]),
|
92
|
+
* timestamp: new Date()
|
93
|
+
* };
|
94
|
+
*
|
95
|
+
* const hashString = messageHashStr(pubsubTopic, message);
|
96
|
+
* console.log(hashString); // e.g. "a1b2c3d4..."
|
97
|
+
* ```
|
98
|
+
*/
|
99
|
+
export function messageHashStr(
|
100
|
+
pubsubTopic: string,
|
101
|
+
message: IProtoMessage | IDecodedMessage
|
102
|
+
): string {
|
103
|
+
const hash = messageHash(pubsubTopic, message);
|
104
|
+
const hashStr = bytesToHex(hash);
|
105
|
+
return hashStr;
|
106
|
+
}
|
@@ -16,21 +16,24 @@ import * as lp from "it-length-prefixed";
|
|
16
16
|
import { pipe } from "it-pipe";
|
17
17
|
import { Uint8ArrayList } from "uint8arraylist";
|
18
18
|
|
19
|
-
import {
|
19
|
+
import { StreamManager } from "../stream_manager/index.js";
|
20
20
|
|
21
21
|
const log = new Logger("metadata");
|
22
22
|
|
23
23
|
export const MetadataCodec = "/vac/waku/metadata/1.0.0";
|
24
24
|
|
25
|
-
class Metadata
|
26
|
-
private
|
25
|
+
class Metadata implements IMetadata {
|
26
|
+
private readonly streamManager: StreamManager;
|
27
|
+
private readonly libp2pComponents: Libp2pComponents;
|
27
28
|
protected handshakesConfirmed: Map<PeerIdStr, ShardInfo> = new Map();
|
28
29
|
|
30
|
+
public readonly multicodec = MetadataCodec;
|
31
|
+
|
29
32
|
public constructor(
|
30
33
|
public pubsubTopics: PubsubTopic[],
|
31
34
|
libp2p: Libp2pComponents
|
32
35
|
) {
|
33
|
-
|
36
|
+
this.streamManager = new StreamManager(MetadataCodec, libp2p);
|
34
37
|
this.libp2pComponents = libp2p;
|
35
38
|
void libp2p.registrar.handle(MetadataCodec, (streamData) => {
|
36
39
|
void this.onRequest(streamData);
|
@@ -55,7 +58,7 @@ class Metadata extends BaseProtocol implements IMetadata {
|
|
55
58
|
|
56
59
|
let stream;
|
57
60
|
try {
|
58
|
-
stream = await this.getStream(peerId);
|
61
|
+
stream = await this.streamManager.getStream(peerId);
|
59
62
|
} catch (error) {
|
60
63
|
log.error("Failed to get stream", error);
|
61
64
|
return {
|
package/src/lib/store/rpc.ts
CHANGED
@@ -14,6 +14,7 @@ export class StoreQueryRequest {
|
|
14
14
|
public static create(params: QueryRequestParams): StoreQueryRequest {
|
15
15
|
const request = new StoreQueryRequest({
|
16
16
|
...params,
|
17
|
+
contentTopics: params.contentTopics || [],
|
17
18
|
requestId: uuid(),
|
18
19
|
timeStart: params.timeStart
|
19
20
|
? BigInt(params.timeStart.getTime() * ONE_MILLION)
|
@@ -27,26 +28,29 @@ export class StoreQueryRequest {
|
|
27
28
|
: undefined
|
28
29
|
});
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
) {
|
35
|
-
throw new Error(
|
36
|
-
"Both pubsubTopic and contentTopics must be set or unset"
|
37
|
-
);
|
38
|
-
}
|
31
|
+
const isHashQuery = params.messageHashes && params.messageHashes.length > 0;
|
32
|
+
const hasContentTopics =
|
33
|
+
params.contentTopics && params.contentTopics.length > 0;
|
34
|
+
const hasTimeFilter = params.timeStart || params.timeEnd;
|
39
35
|
|
40
|
-
if (
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
36
|
+
if (isHashQuery) {
|
37
|
+
if (hasContentTopics || hasTimeFilter) {
|
38
|
+
throw new Error(
|
39
|
+
"Message hash lookup queries cannot include content filter criteria (contentTopics, timeStart, or timeEnd)"
|
40
|
+
);
|
41
|
+
}
|
42
|
+
} else {
|
43
|
+
if (
|
44
|
+
(params.pubsubTopic &&
|
45
|
+
(!params.contentTopics || params.contentTopics.length === 0)) ||
|
46
|
+
(!params.pubsubTopic &&
|
47
|
+
params.contentTopics &&
|
48
|
+
params.contentTopics.length > 0)
|
49
|
+
) {
|
50
|
+
throw new Error(
|
51
|
+
"Both pubsubTopic and contentTopics must be set together for content-filtered queries"
|
52
|
+
);
|
53
|
+
}
|
50
54
|
}
|
51
55
|
|
52
56
|
return request;
|
package/src/lib/store/store.ts
CHANGED
@@ -2,9 +2,7 @@ import type { PeerId } from "@libp2p/interface";
|
|
2
2
|
import {
|
3
3
|
IDecodedMessage,
|
4
4
|
IDecoder,
|
5
|
-
IStoreCore,
|
6
5
|
Libp2p,
|
7
|
-
PubsubTopic,
|
8
6
|
QueryRequestParams
|
9
7
|
} from "@waku/interfaces";
|
10
8
|
import { Logger } from "@waku/utils";
|
@@ -13,7 +11,7 @@ import * as lp from "it-length-prefixed";
|
|
13
11
|
import { pipe } from "it-pipe";
|
14
12
|
import { Uint8ArrayList } from "uint8arraylist";
|
15
13
|
|
16
|
-
import {
|
14
|
+
import { StreamManager } from "../stream_manager/index.js";
|
17
15
|
import { toProtoMessage } from "../to_proto_message.js";
|
18
16
|
|
19
17
|
import {
|
@@ -27,12 +25,13 @@ const log = new Logger("store");
|
|
27
25
|
|
28
26
|
export const StoreCodec = "/vac/waku/store-query/3.0.0";
|
29
27
|
|
30
|
-
export class StoreCore
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
28
|
+
export class StoreCore {
|
29
|
+
private readonly streamManager: StreamManager;
|
30
|
+
|
31
|
+
public readonly multicodec = StoreCodec;
|
32
|
+
|
33
|
+
public constructor(libp2p: Libp2p) {
|
34
|
+
this.streamManager = new StreamManager(StoreCodec, libp2p.components);
|
36
35
|
}
|
37
36
|
|
38
37
|
public async *queryPerPage<T extends IDecodedMessage>(
|
@@ -40,9 +39,14 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
|
|
40
39
|
decoders: Map<string, IDecoder<T>>,
|
41
40
|
peerId: PeerId
|
42
41
|
): AsyncGenerator<Promise<T | undefined>[]> {
|
42
|
+
// Only validate decoder content topics for content-filtered queries
|
43
|
+
const isHashQuery =
|
44
|
+
queryOpts.messageHashes && queryOpts.messageHashes.length > 0;
|
43
45
|
if (
|
46
|
+
!isHashQuery &&
|
47
|
+
queryOpts.contentTopics &&
|
44
48
|
queryOpts.contentTopics.toString() !==
|
45
|
-
|
49
|
+
Array.from(decoders.keys()).toString()
|
46
50
|
) {
|
47
51
|
throw new Error(
|
48
52
|
"Internal error, the decoders should match the query's content topics"
|
@@ -56,9 +60,16 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
|
|
56
60
|
paginationCursor: currentCursor
|
57
61
|
});
|
58
62
|
|
63
|
+
log.info("Sending store query request:", {
|
64
|
+
hasMessageHashes: !!queryOpts.messageHashes?.length,
|
65
|
+
messageHashCount: queryOpts.messageHashes?.length,
|
66
|
+
pubsubTopic: queryOpts.pubsubTopic,
|
67
|
+
contentTopics: queryOpts.contentTopics
|
68
|
+
});
|
69
|
+
|
59
70
|
let stream;
|
60
71
|
try {
|
61
|
-
stream = await this.getStream(peerId);
|
72
|
+
stream = await this.streamManager.getStream(peerId);
|
62
73
|
} catch (e) {
|
63
74
|
log.error("Failed to get stream", e);
|
64
75
|
break;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import type { Peer, PeerId, PeerUpdate, Stream } from "@libp2p/interface";
|
2
|
-
import type {
|
2
|
+
import type { Libp2pComponents } from "@waku/interfaces";
|
3
3
|
import { Logger } from "@waku/utils";
|
4
4
|
|
5
5
|
import { selectOpenConnection } from "./utils.js";
|
@@ -14,11 +14,13 @@ export class StreamManager {
|
|
14
14
|
|
15
15
|
public constructor(
|
16
16
|
private multicodec: string,
|
17
|
-
private
|
18
|
-
private addEventListener: Libp2p["addEventListener"]
|
17
|
+
private readonly libp2p: Libp2pComponents
|
19
18
|
) {
|
20
19
|
this.log = new Logger(`stream-manager:${multicodec}`);
|
21
|
-
this.addEventListener(
|
20
|
+
this.libp2p.events.addEventListener(
|
21
|
+
"peer:update",
|
22
|
+
this.handlePeerUpdateStreamPool
|
23
|
+
);
|
22
24
|
}
|
23
25
|
|
24
26
|
public async getStream(peerId: PeerId): Promise<Stream> {
|
@@ -47,7 +49,7 @@ export class StreamManager {
|
|
47
49
|
}
|
48
50
|
|
49
51
|
private async createStream(peerId: PeerId, retries = 0): Promise<Stream> {
|
50
|
-
const connections = this.getConnections(peerId);
|
52
|
+
const connections = this.libp2p.connectionManager.getConnections(peerId);
|
51
53
|
const connection = selectOpenConnection(connections);
|
52
54
|
|
53
55
|
if (!connection) {
|
@@ -135,7 +137,7 @@ export class StreamManager {
|
|
135
137
|
}
|
136
138
|
|
137
139
|
private getOpenStreamForCodec(peerId: PeerId): Stream | undefined {
|
138
|
-
const connections = this.getConnections(peerId);
|
140
|
+
const connections = this.libp2p.connectionManager.getConnections(peerId);
|
139
141
|
const connection = selectOpenConnection(connections);
|
140
142
|
|
141
143
|
if (!connection) {
|