@waku/core 0.0.37-7a9850d.0 → 0.0.37-c24842a.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/index.js +4852 -904
- package/bundle/lib/message/version_0.js +1 -1
- package/bundle/{version_0-9DPFjcJG.js → version_0-BHaZD8Qu.js} +485 -60
- package/dist/.tsbuildinfo +1 -1
- package/dist/lib/connection_manager/connection_limiter.d.ts +46 -0
- package/dist/lib/connection_manager/connection_limiter.js +177 -0
- package/dist/lib/connection_manager/connection_limiter.js.map +1 -0
- package/dist/lib/connection_manager/connection_manager.d.ts +18 -106
- package/dist/lib/connection_manager/connection_manager.js +95 -512
- package/dist/lib/connection_manager/connection_manager.js.map +1 -1
- package/dist/lib/connection_manager/dialer.d.ts +34 -0
- package/dist/lib/connection_manager/dialer.js +135 -0
- package/dist/lib/connection_manager/dialer.js.map +1 -0
- package/dist/lib/connection_manager/discovery_dialer.d.ts +26 -0
- package/dist/lib/connection_manager/discovery_dialer.js +68 -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 +36 -0
- package/dist/lib/connection_manager/network_monitor.js +81 -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 +2 -3
- package/dist/lib/filter/filter.js +5 -25
- package/dist/lib/filter/filter.js.map +1 -1
- package/dist/lib/light_push/light_push.d.ts +2 -3
- package/dist/lib/light_push/light_push.js +1 -3
- package/dist/lib/light_push/light_push.js.map +1 -1
- package/dist/lib/metadata/metadata.d.ts +2 -2
- package/dist/lib/metadata/metadata.js +14 -8
- package/dist/lib/metadata/metadata.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/connection_manager/connection_limiter.ts +292 -0
- package/src/lib/connection_manager/connection_manager.ts +112 -673
- package/src/lib/connection_manager/dialer.ts +192 -0
- package/src/lib/connection_manager/discovery_dialer.ts +104 -0
- package/src/lib/connection_manager/keep_alive_manager.ts +154 -87
- package/src/lib/connection_manager/network_monitor.ts +112 -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 +3 -28
- package/src/lib/light_push/light_push.ts +1 -5
- package/src/lib/metadata/metadata.ts +13 -12
@@ -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
@@ -22,7 +22,7 @@ import {
|
|
22
22
|
FilterSubscribeRpc
|
23
23
|
} from "./filter_rpc.js";
|
24
24
|
|
25
|
-
const log = new Logger("filter
|
25
|
+
const log = new Logger("filter-core");
|
26
26
|
|
27
27
|
export const FilterCodecs = {
|
28
28
|
SUBSCRIBE: "/vac/waku/filter-subscribe/2.0.0-beta1",
|
@@ -37,13 +37,11 @@ type IncomingMessageHandler = (
|
|
37
37
|
|
38
38
|
export class FilterCore {
|
39
39
|
private streamManager: StreamManager;
|
40
|
-
private static handleIncomingMessage?: IncomingMessageHandler;
|
41
40
|
|
42
41
|
public readonly multicodec = FilterCodecs.SUBSCRIBE;
|
43
42
|
|
44
43
|
public constructor(
|
45
|
-
handleIncomingMessage: IncomingMessageHandler,
|
46
|
-
public readonly pubsubTopics: PubsubTopic[],
|
44
|
+
private handleIncomingMessage: IncomingMessageHandler,
|
47
45
|
libp2p: Libp2p
|
48
46
|
) {
|
49
47
|
this.streamManager = new StreamManager(
|
@@ -51,29 +49,6 @@ export class FilterCore {
|
|
51
49
|
libp2p.components
|
52
50
|
);
|
53
51
|
|
54
|
-
// TODO(weboko): remove when @waku/sdk 0.0.33 is released
|
55
|
-
const prevHandler = FilterCore.handleIncomingMessage;
|
56
|
-
FilterCore.handleIncomingMessage = !prevHandler
|
57
|
-
? handleIncomingMessage
|
58
|
-
: async (pubsubTopic, message, peerIdStr): Promise<void> => {
|
59
|
-
try {
|
60
|
-
await prevHandler(pubsubTopic, message, peerIdStr);
|
61
|
-
} catch (e) {
|
62
|
-
log.error(
|
63
|
-
"Previous FilterCore incoming message handler failed ",
|
64
|
-
e
|
65
|
-
);
|
66
|
-
}
|
67
|
-
|
68
|
-
try {
|
69
|
-
await handleIncomingMessage(pubsubTopic, message, peerIdStr);
|
70
|
-
} catch (e) {
|
71
|
-
log.error("Present FilterCore incoming message handler failed ", e);
|
72
|
-
}
|
73
|
-
|
74
|
-
return;
|
75
|
-
};
|
76
|
-
|
77
52
|
libp2p
|
78
53
|
.handle(FilterCodecs.PUSH, this.onRequest.bind(this), {
|
79
54
|
maxInboundStreams: 100
|
@@ -327,7 +302,7 @@ export class FilterCore {
|
|
327
302
|
return;
|
328
303
|
}
|
329
304
|
|
330
|
-
await
|
305
|
+
await this.handleIncomingMessage(
|
331
306
|
pubsubTopic,
|
332
307
|
wakuMessage,
|
333
308
|
connection.remotePeer.toString()
|
@@ -5,7 +5,6 @@ import {
|
|
5
5
|
type IMessage,
|
6
6
|
type Libp2p,
|
7
7
|
ProtocolError,
|
8
|
-
PubsubTopic,
|
9
8
|
type ThisOrThat
|
10
9
|
} from "@waku/interfaces";
|
11
10
|
import { PushResponse } from "@waku/proto";
|
@@ -36,10 +35,7 @@ export class LightPushCore {
|
|
36
35
|
|
37
36
|
public readonly multicodec = LightPushCodec;
|
38
37
|
|
39
|
-
public constructor(
|
40
|
-
public readonly pubsubTopics: PubsubTopic[],
|
41
|
-
libp2p: Libp2p
|
42
|
-
) {
|
38
|
+
public constructor(libp2p: Libp2p) {
|
43
39
|
this.streamManager = new StreamManager(LightPushCodec, libp2p.components);
|
44
40
|
}
|
45
41
|
|
@@ -1,16 +1,16 @@
|
|
1
1
|
import type { PeerId } from "@libp2p/interface";
|
2
2
|
import { IncomingStreamData } from "@libp2p/interface";
|
3
3
|
import {
|
4
|
+
type ClusterId,
|
4
5
|
type IMetadata,
|
5
6
|
type Libp2pComponents,
|
6
7
|
type MetadataQueryResult,
|
7
8
|
type PeerIdStr,
|
8
9
|
ProtocolError,
|
9
|
-
PubsubTopic,
|
10
10
|
type ShardInfo
|
11
11
|
} from "@waku/interfaces";
|
12
12
|
import { proto_metadata } from "@waku/proto";
|
13
|
-
import { encodeRelayShard, Logger
|
13
|
+
import { encodeRelayShard, Logger } from "@waku/utils";
|
14
14
|
import all from "it-all";
|
15
15
|
import * as lp from "it-length-prefixed";
|
16
16
|
import { pipe } from "it-pipe";
|
@@ -30,7 +30,7 @@ class Metadata implements IMetadata {
|
|
30
30
|
public readonly multicodec = MetadataCodec;
|
31
31
|
|
32
32
|
public constructor(
|
33
|
-
public
|
33
|
+
public clusterId: ClusterId,
|
34
34
|
libp2p: Libp2pComponents
|
35
35
|
) {
|
36
36
|
this.streamManager = new StreamManager(MetadataCodec, libp2p);
|
@@ -44,9 +44,10 @@ class Metadata implements IMetadata {
|
|
44
44
|
* Make a metadata query to a peer
|
45
45
|
*/
|
46
46
|
public async query(peerId: PeerId): Promise<MetadataQueryResult> {
|
47
|
-
const request = proto_metadata.WakuMetadataRequest.encode(
|
48
|
-
|
49
|
-
|
47
|
+
const request = proto_metadata.WakuMetadataRequest.encode({
|
48
|
+
clusterId: this.clusterId,
|
49
|
+
shards: [] // Only services node need to provide shards
|
50
|
+
});
|
50
51
|
|
51
52
|
const peer = await this.libp2pComponents.peerStore.get(peerId);
|
52
53
|
if (!peer) {
|
@@ -112,9 +113,10 @@ class Metadata implements IMetadata {
|
|
112
113
|
private async onRequest(streamData: IncomingStreamData): Promise<void> {
|
113
114
|
try {
|
114
115
|
const { stream, connection } = streamData;
|
115
|
-
const encodedShardInfo = proto_metadata.WakuMetadataResponse.encode(
|
116
|
-
|
117
|
-
|
116
|
+
const encodedShardInfo = proto_metadata.WakuMetadataResponse.encode({
|
117
|
+
clusterId: this.clusterId,
|
118
|
+
shards: [] // Only service nodes need to provide shards
|
119
|
+
});
|
118
120
|
|
119
121
|
const encodedResponse = await pipe(
|
120
122
|
[encodedShardInfo],
|
@@ -178,8 +180,7 @@ class Metadata implements IMetadata {
|
|
178
180
|
}
|
179
181
|
|
180
182
|
export function wakuMetadata(
|
181
|
-
|
183
|
+
clusterId: ClusterId
|
182
184
|
): (components: Libp2pComponents) => IMetadata {
|
183
|
-
return (components: Libp2pComponents) =>
|
184
|
-
new Metadata(pubsubTopics, components);
|
185
|
+
return (components: Libp2pComponents) => new Metadata(clusterId, components);
|
185
186
|
}
|