@waku/core 0.0.37-7a9850d.0 → 0.0.37-c7682ea.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.
Files changed (45) hide show
  1. package/bundle/index.js +4745 -899
  2. package/bundle/lib/message/version_0.js +1 -1
  3. package/bundle/{version_0-9DPFjcJG.js → version_0-Bc0h7ah2.js} +312 -33
  4. package/dist/.tsbuildinfo +1 -1
  5. package/dist/lib/connection_manager/connection_limiter.d.ts +37 -0
  6. package/dist/lib/connection_manager/connection_limiter.js +124 -0
  7. package/dist/lib/connection_manager/connection_limiter.js.map +1 -0
  8. package/dist/lib/connection_manager/connection_manager.d.ts +20 -105
  9. package/dist/lib/connection_manager/connection_manager.js +83 -508
  10. package/dist/lib/connection_manager/connection_manager.js.map +1 -1
  11. package/dist/lib/connection_manager/dialer.d.ts +28 -0
  12. package/dist/lib/connection_manager/dialer.js +100 -0
  13. package/dist/lib/connection_manager/dialer.js.map +1 -0
  14. package/dist/lib/connection_manager/discovery_dialer.d.ts +26 -0
  15. package/dist/lib/connection_manager/discovery_dialer.js +68 -0
  16. package/dist/lib/connection_manager/discovery_dialer.js.map +1 -0
  17. package/dist/lib/connection_manager/keep_alive_manager.d.ts +17 -7
  18. package/dist/lib/connection_manager/keep_alive_manager.js +110 -74
  19. package/dist/lib/connection_manager/keep_alive_manager.js.map +1 -1
  20. package/dist/lib/connection_manager/network_monitor.d.ts +36 -0
  21. package/dist/lib/connection_manager/network_monitor.js +81 -0
  22. package/dist/lib/connection_manager/network_monitor.js.map +1 -0
  23. package/dist/lib/connection_manager/shard_reader.d.ts +28 -0
  24. package/dist/lib/connection_manager/shard_reader.js +70 -0
  25. package/dist/lib/connection_manager/shard_reader.js.map +1 -0
  26. package/dist/lib/connection_manager/utils.d.ts +16 -1
  27. package/dist/lib/connection_manager/utils.js +23 -0
  28. package/dist/lib/connection_manager/utils.js.map +1 -1
  29. package/dist/lib/filter/filter.d.ts +2 -3
  30. package/dist/lib/filter/filter.js +5 -25
  31. package/dist/lib/filter/filter.js.map +1 -1
  32. package/dist/lib/light_push/light_push.d.ts +2 -3
  33. package/dist/lib/light_push/light_push.js +1 -3
  34. package/dist/lib/light_push/light_push.js.map +1 -1
  35. package/package.json +1 -1
  36. package/src/lib/connection_manager/connection_limiter.ts +201 -0
  37. package/src/lib/connection_manager/connection_manager.ts +104 -669
  38. package/src/lib/connection_manager/dialer.ts +139 -0
  39. package/src/lib/connection_manager/discovery_dialer.ts +106 -0
  40. package/src/lib/connection_manager/keep_alive_manager.ts +154 -87
  41. package/src/lib/connection_manager/network_monitor.ts +112 -0
  42. package/src/lib/connection_manager/shard_reader.ts +134 -0
  43. package/src/lib/connection_manager/utils.ts +27 -1
  44. package/src/lib/filter/filter.ts +3 -28
  45. package/src/lib/light_push/light_push.ts +1 -5
@@ -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 { Peer } from "@libp2p/interface";
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
+ };
@@ -22,7 +22,7 @@ import {
22
22
  FilterSubscribeRpc
23
23
  } from "./filter_rpc.js";
24
24
 
25
- const log = new Logger("filter:v2");
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 FilterCore.handleIncomingMessage?.(
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