@waku/core 0.0.33-f599932.0 → 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.
Files changed (93) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/bundle/base_protocol-Bp5a9PNG.js +152 -0
  3. package/bundle/{index-BVysxsMu.js → index-G1eRBjeI.js} +136 -215
  4. package/bundle/index.js +1963 -621
  5. package/bundle/lib/base_protocol.js +2 -2
  6. package/bundle/lib/message/version_0.js +2 -2
  7. package/bundle/{version_0-C5ObpJ_0.js → version_0-DJZG2fB2.js} +285 -41
  8. package/dist/.tsbuildinfo +1 -1
  9. package/dist/index.d.ts +2 -5
  10. package/dist/index.js +2 -5
  11. package/dist/index.js.map +1 -1
  12. package/dist/lib/base_protocol.d.ts +3 -23
  13. package/dist/lib/base_protocol.js +3 -51
  14. package/dist/lib/base_protocol.js.map +1 -1
  15. package/dist/lib/connection_manager/connection_manager.d.ts +118 -0
  16. package/dist/lib/{connection_manager.js → connection_manager/connection_manager.js} +136 -36
  17. package/dist/lib/connection_manager/connection_manager.js.map +1 -0
  18. package/dist/lib/connection_manager/index.d.ts +1 -0
  19. package/dist/lib/connection_manager/index.js +2 -0
  20. package/dist/lib/connection_manager/index.js.map +1 -0
  21. package/dist/lib/{keep_alive_manager.d.ts → connection_manager/keep_alive_manager.d.ts} +4 -2
  22. package/dist/lib/{keep_alive_manager.js → connection_manager/keep_alive_manager.js} +2 -2
  23. package/dist/lib/connection_manager/keep_alive_manager.js.map +1 -0
  24. package/dist/lib/connection_manager/utils.d.ts +7 -0
  25. package/dist/lib/connection_manager/utils.js +22 -0
  26. package/dist/lib/connection_manager/utils.js.map +1 -0
  27. package/dist/lib/filter/filter.d.ts +18 -0
  28. package/dist/lib/filter/filter.js +209 -0
  29. package/dist/lib/filter/filter.js.map +1 -0
  30. package/dist/lib/filter/index.d.ts +1 -19
  31. package/dist/lib/filter/index.js +1 -211
  32. package/dist/lib/filter/index.js.map +1 -1
  33. package/dist/lib/light_push/index.d.ts +1 -15
  34. package/dist/lib/light_push/index.js +1 -143
  35. package/dist/lib/light_push/index.js.map +1 -1
  36. package/dist/lib/light_push/light_push.d.ts +15 -0
  37. package/dist/lib/light_push/light_push.js +144 -0
  38. package/dist/lib/light_push/light_push.js.map +1 -0
  39. package/dist/lib/light_push/utils.d.ts +0 -2
  40. package/dist/lib/light_push/utils.js +8 -16
  41. package/dist/lib/light_push/utils.js.map +1 -1
  42. package/dist/lib/metadata/index.d.ts +1 -3
  43. package/dist/lib/metadata/index.js +1 -118
  44. package/dist/lib/metadata/index.js.map +1 -1
  45. package/dist/lib/metadata/metadata.d.ts +3 -0
  46. package/dist/lib/metadata/metadata.js +119 -0
  47. package/dist/lib/metadata/metadata.js.map +1 -0
  48. package/dist/lib/store/index.d.ts +1 -9
  49. package/dist/lib/store/index.js +1 -82
  50. package/dist/lib/store/index.js.map +1 -1
  51. package/dist/lib/store/store.d.ts +9 -0
  52. package/dist/lib/store/store.js +83 -0
  53. package/dist/lib/store/store.js.map +1 -0
  54. package/dist/lib/stream_manager/stream_manager.d.ts +13 -10
  55. package/dist/lib/stream_manager/stream_manager.js +88 -58
  56. package/dist/lib/stream_manager/stream_manager.js.map +1 -1
  57. package/dist/lib/stream_manager/utils.d.ts +1 -1
  58. package/dist/lib/stream_manager/utils.js +5 -17
  59. package/dist/lib/stream_manager/utils.js.map +1 -1
  60. package/package.json +1 -1
  61. package/src/index.ts +2 -7
  62. package/src/lib/base_protocol.ts +3 -82
  63. package/src/lib/{connection_manager.ts → connection_manager/connection_manager.ts} +168 -63
  64. package/src/lib/connection_manager/index.ts +1 -0
  65. package/src/lib/{keep_alive_manager.ts → connection_manager/keep_alive_manager.ts} +7 -3
  66. package/src/lib/connection_manager/utils.ts +25 -0
  67. package/src/lib/filter/filter.ts +315 -0
  68. package/src/lib/filter/index.ts +1 -323
  69. package/src/lib/light_push/index.ts +1 -188
  70. package/src/lib/light_push/light_push.ts +188 -0
  71. package/src/lib/light_push/utils.ts +12 -20
  72. package/src/lib/metadata/index.ts +1 -182
  73. package/src/lib/metadata/metadata.ts +182 -0
  74. package/src/lib/store/index.ts +1 -136
  75. package/src/lib/store/store.ts +136 -0
  76. package/src/lib/stream_manager/stream_manager.ts +125 -69
  77. package/src/lib/stream_manager/utils.ts +5 -17
  78. package/bundle/base_protocol-CS0EDeEY.js +0 -260
  79. package/dist/lib/connection_manager.d.ts +0 -62
  80. package/dist/lib/connection_manager.js.map +0 -1
  81. package/dist/lib/filterPeers.d.ts +0 -13
  82. package/dist/lib/filterPeers.js +0 -38
  83. package/dist/lib/filterPeers.js.map +0 -1
  84. package/dist/lib/health_manager.d.ts +0 -14
  85. package/dist/lib/health_manager.js +0 -70
  86. package/dist/lib/health_manager.js.map +0 -1
  87. package/dist/lib/keep_alive_manager.js.map +0 -1
  88. package/dist/lib/wait_for_remote_peer.d.ts +0 -22
  89. package/dist/lib/wait_for_remote_peer.js +0 -142
  90. package/dist/lib/wait_for_remote_peer.js.map +0 -1
  91. package/src/lib/filterPeers.ts +0 -51
  92. package/src/lib/health_manager.ts +0 -90
  93. package/src/lib/wait_for_remote_peer.ts +0 -200
@@ -1,38 +0,0 @@
1
- import { Tags } from "@waku/interfaces";
2
- /**
3
- * Retrieves a list of peers based on the specified criteria:
4
- * 1. If numPeers is 0, return all peers
5
- * 2. Bootstrap peers are prioritized
6
- * 3. Non-bootstrap peers are randomly selected to fill up to numPeers
7
- *
8
- * @param peers - The list of peers to filter from.
9
- * @param numPeers - The total number of peers to retrieve. If 0, all peers are returned, irrespective of `maxBootstrapPeers`.
10
- * @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
11
- * @returns An array of peers based on the specified criteria.
12
- */
13
- export function filterPeersByDiscovery(peers, numPeers, maxBootstrapPeers) {
14
- // Collect the bootstrap peers up to the specified maximum
15
- let bootstrapPeers = peers
16
- .filter((peer) => peer.tags.has(Tags.BOOTSTRAP))
17
- .slice(0, maxBootstrapPeers);
18
- // If numPeers is less than the number of bootstrap peers, adjust the bootstrapPeers array
19
- if (numPeers > 0 && numPeers < bootstrapPeers.length) {
20
- bootstrapPeers = bootstrapPeers.slice(0, numPeers);
21
- }
22
- // Collect non-bootstrap peers
23
- const nonBootstrapPeers = peers.filter((peer) => !peer.tags.has(Tags.BOOTSTRAP));
24
- // If numPeers is 0, return all peers
25
- if (numPeers === 0) {
26
- return [...bootstrapPeers, ...nonBootstrapPeers];
27
- }
28
- // Initialize the list of selected peers with the bootstrap peers
29
- const selectedPeers = [...bootstrapPeers];
30
- // Fill up to numPeers with remaining random peers if needed
31
- while (selectedPeers.length < numPeers && nonBootstrapPeers.length > 0) {
32
- const randomIndex = Math.floor(Math.random() * nonBootstrapPeers.length);
33
- const randomPeer = nonBootstrapPeers.splice(randomIndex, 1)[0];
34
- selectedPeers.push(randomPeer);
35
- }
36
- return selectedPeers;
37
- }
38
- //# sourceMappingURL=filterPeers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"filterPeers.js","sourceRoot":"","sources":["../../src/lib/filterPeers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAExC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,sBAAsB,CACpC,KAAa,EACb,QAAgB,EAChB,iBAAyB;IAEzB,0DAA0D;IAC1D,IAAI,cAAc,GAAG,KAAK;SACvB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;SAC/C,KAAK,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC;IAE/B,0FAA0F;IAC1F,IAAI,QAAQ,GAAG,CAAC,IAAI,QAAQ,GAAG,cAAc,CAAC,MAAM,EAAE,CAAC;QACrD,cAAc,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;IACrD,CAAC;IAED,8BAA8B;IAC9B,MAAM,iBAAiB,GAAG,KAAK,CAAC,MAAM,CACpC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CACzC,CAAC;IAEF,qCAAqC;IACrC,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;QACnB,OAAO,CAAC,GAAG,cAAc,EAAE,GAAG,iBAAiB,CAAC,CAAC;IACnD,CAAC;IAED,iEAAiE;IACjE,MAAM,aAAa,GAAW,CAAC,GAAG,cAAc,CAAC,CAAC;IAElD,4DAA4D;IAC5D,OAAO,aAAa,CAAC,MAAM,GAAG,QAAQ,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvE,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;QACzE,MAAM,UAAU,GAAG,iBAAiB,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/D,aAAa,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,aAAa,CAAC;AACvB,CAAC"}
@@ -1,14 +0,0 @@
1
- import { HealthStatus, type IHealthManager, type ProtocolHealth, Protocols } from "@waku/interfaces";
2
- declare class HealthManager implements IHealthManager {
3
- static instance: HealthManager;
4
- private readonly health;
5
- private constructor();
6
- static getInstance(): HealthManager;
7
- getHealthStatus(): HealthStatus;
8
- getProtocolStatus(protocol: Protocols): ProtocolHealth | undefined;
9
- updateProtocolHealth(multicodec: string, connectedPeers: number): void;
10
- private getNameFromMulticodec;
11
- private updateOverallHealth;
12
- }
13
- export declare const getHealthManager: () => HealthManager;
14
- export {};
@@ -1,70 +0,0 @@
1
- import { HealthStatus, Protocols } from "@waku/interfaces";
2
- class HealthManager {
3
- static instance;
4
- health;
5
- constructor() {
6
- this.health = {
7
- overallStatus: HealthStatus.Unhealthy,
8
- protocolStatuses: new Map()
9
- };
10
- }
11
- static getInstance() {
12
- if (!HealthManager.instance) {
13
- HealthManager.instance = new HealthManager();
14
- }
15
- return HealthManager.instance;
16
- }
17
- getHealthStatus() {
18
- return this.health.overallStatus;
19
- }
20
- getProtocolStatus(protocol) {
21
- return this.health.protocolStatuses.get(protocol);
22
- }
23
- updateProtocolHealth(multicodec, connectedPeers) {
24
- const protocol = this.getNameFromMulticodec(multicodec);
25
- let status = HealthStatus.Unhealthy;
26
- if (connectedPeers == 1) {
27
- status = HealthStatus.MinimallyHealthy;
28
- }
29
- else if (connectedPeers >= 2) {
30
- status = HealthStatus.SufficientlyHealthy;
31
- }
32
- this.health.protocolStatuses.set(protocol, {
33
- name: protocol,
34
- status: status,
35
- lastUpdate: new Date()
36
- });
37
- this.updateOverallHealth();
38
- }
39
- getNameFromMulticodec(multicodec) {
40
- let name;
41
- if (multicodec.includes("filter")) {
42
- name = Protocols.Filter;
43
- }
44
- else if (multicodec.includes("lightpush")) {
45
- name = Protocols.LightPush;
46
- }
47
- else if (multicodec.includes("store")) {
48
- name = Protocols.Store;
49
- }
50
- else {
51
- throw new Error(`Unknown protocol: ${multicodec}`);
52
- }
53
- return name;
54
- }
55
- updateOverallHealth() {
56
- const relevantProtocols = [Protocols.LightPush, Protocols.Filter];
57
- const statuses = relevantProtocols.map((p) => this.getProtocolStatus(p)?.status);
58
- if (statuses.some((status) => status === HealthStatus.Unhealthy)) {
59
- this.health.overallStatus = HealthStatus.Unhealthy;
60
- }
61
- else if (statuses.some((status) => status === HealthStatus.MinimallyHealthy)) {
62
- this.health.overallStatus = HealthStatus.MinimallyHealthy;
63
- }
64
- else {
65
- this.health.overallStatus = HealthStatus.SufficientlyHealthy;
66
- }
67
- }
68
- }
69
- export const getHealthManager = () => HealthManager.getInstance();
70
- //# sourceMappingURL=health_manager.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"health_manager.js","sourceRoot":"","sources":["../../src/lib/health_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;IAEO,kBAAkB,CACxB,KAAa,EACb,eAAuB,EACvB,SAAoB;QAEpB,8DAA8D;QAC9D,MAAM,SAAS,GAAqB,EAAE,CAAC;QACvC,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,YAAY,EAAE,CAAC;YACvC,MAAM,SAAS,GAAG,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;YAC5C,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC;gBAAE,SAAS;YAE7C,MAAM,OAAO,GAAG,aAAa,CAAC;gBAC5B,oBAAoB,EAAE,4BAA4B,CAAC,KAAK,CAAC;gBACzD,YAAY,EAAE,qBAAqB;gBACnC,SAAS,EAAE,IAAI;aAChB,CAAC,CAAC;YACH,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE;gBAChC,GAAG,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAC;gBAC5C,KAAK;qBACF,IAAI,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;qBAC/C,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,2BAA2B,EAAE,CAAC,CAAC,CAAC,CAAC;YAC7D,CAAC,EAAE,eAAe,GAAG,IAAI,CAAC,CAAC;YAC3B,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC3B,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;CACF"}
@@ -1,22 +0,0 @@
1
- import type { Waku } from "@waku/interfaces";
2
- import { Protocols } from "@waku/interfaces";
3
- /**
4
- * Wait for a remote peer to be ready given the passed protocols.
5
- * Must be used after attempting to connect to nodes, using
6
- * {@link @waku/sdk!WakuNode.dial} or a bootstrap method with
7
- * {@link @waku/sdk!createLightNode}.
8
- *
9
- * If the passed protocols is a GossipSub protocol, then it resolves only once
10
- * a peer is in a mesh, to help ensure that other peers will send and receive
11
- * message to us.
12
- *
13
- * @param waku The Waku Node
14
- * @param protocols The protocols that need to be enabled by remote peers.
15
- * @param timeoutMs A timeout value in milliseconds..
16
- *
17
- * @returns A promise that **resolves** if all desired protocols are fulfilled by
18
- * remote nodes, **rejects** if the timeoutMs is reached.
19
- * @throws If passing a protocol that is not mounted
20
- * @default Wait for remote peers with protocols enabled locally and no time out is applied.
21
- */
22
- export declare function waitForRemotePeer(waku: Waku, protocols?: Protocols[], timeoutMs?: number): Promise<void>;
@@ -1,142 +0,0 @@
1
- import { Protocols } from "@waku/interfaces";
2
- import { Logger } from "@waku/utils";
3
- import { pEvent } from "p-event";
4
- const log = new Logger("wait-for-remote-peer");
5
- //TODO: move this function within the Waku class: https://github.com/waku-org/js-waku/issues/1761
6
- /**
7
- * Wait for a remote peer to be ready given the passed protocols.
8
- * Must be used after attempting to connect to nodes, using
9
- * {@link @waku/sdk!WakuNode.dial} or a bootstrap method with
10
- * {@link @waku/sdk!createLightNode}.
11
- *
12
- * If the passed protocols is a GossipSub protocol, then it resolves only once
13
- * a peer is in a mesh, to help ensure that other peers will send and receive
14
- * message to us.
15
- *
16
- * @param waku The Waku Node
17
- * @param protocols The protocols that need to be enabled by remote peers.
18
- * @param timeoutMs A timeout value in milliseconds..
19
- *
20
- * @returns A promise that **resolves** if all desired protocols are fulfilled by
21
- * remote nodes, **rejects** if the timeoutMs is reached.
22
- * @throws If passing a protocol that is not mounted
23
- * @default Wait for remote peers with protocols enabled locally and no time out is applied.
24
- */
25
- export async function waitForRemotePeer(waku, protocols, timeoutMs) {
26
- protocols = protocols ?? getEnabledProtocols(waku);
27
- if (!waku.isStarted())
28
- return Promise.reject("Waku node is not started");
29
- const promises = [];
30
- if (protocols.includes(Protocols.Relay)) {
31
- if (!waku.relay)
32
- throw new Error("Cannot wait for Relay peer: protocol not mounted");
33
- promises.push(waitForGossipSubPeerInMesh(waku.relay));
34
- }
35
- if (protocols.includes(Protocols.Store)) {
36
- if (!waku.store)
37
- throw new Error("Cannot wait for Store peer: protocol not mounted");
38
- promises.push(waitForConnectedPeer(waku.store.protocol, waku.libp2p.services.metadata));
39
- }
40
- if (protocols.includes(Protocols.LightPush)) {
41
- if (!waku.lightPush)
42
- throw new Error("Cannot wait for LightPush peer: protocol not mounted");
43
- promises.push(waitForConnectedPeer(waku.lightPush.protocol, waku.libp2p.services.metadata));
44
- }
45
- if (protocols.includes(Protocols.Filter)) {
46
- if (!waku.filter)
47
- throw new Error("Cannot wait for Filter peer: protocol not mounted");
48
- promises.push(waitForConnectedPeer(waku.filter.protocol, waku.libp2p.services.metadata));
49
- }
50
- if (timeoutMs) {
51
- await rejectOnTimeout(Promise.all(promises), timeoutMs, "Timed out waiting for a remote peer.");
52
- }
53
- else {
54
- await Promise.all(promises);
55
- }
56
- }
57
- //TODO: move this function within protocol SDK class: https://github.com/waku-org/js-waku/issues/1761
58
- /**
59
- * Wait for a peer with the given protocol to be connected.
60
- * If sharding is enabled on the node, it will also wait for the peer to be confirmed by the metadata service.
61
- */
62
- async function waitForConnectedPeer(protocol, metadataService) {
63
- const codec = protocol.multicodec;
64
- const peers = await protocol.connectedPeers();
65
- if (peers.length) {
66
- if (!metadataService) {
67
- log.info(`${codec} peer found: `, peers[0].id.toString());
68
- return;
69
- }
70
- // once a peer is connected, we need to confirm the metadata handshake with at least one of those peers if sharding is enabled
71
- try {
72
- await Promise.any(peers.map((peer) => metadataService.confirmOrAttemptHandshake(peer.id)));
73
- return;
74
- }
75
- catch (e) {
76
- if (e.code === "ERR_CONNECTION_BEING_CLOSED")
77
- log.error(`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`);
78
- log.error(`Error waiting for handshake confirmation: ${e}`);
79
- }
80
- }
81
- log.info(`Waiting for ${codec} peer`);
82
- // else we'll just wait for the next peer to connect
83
- await new Promise((resolve) => {
84
- const cb = (evt) => {
85
- if (evt.detail?.protocols?.includes(codec)) {
86
- if (metadataService) {
87
- metadataService
88
- .confirmOrAttemptHandshake(evt.detail.peerId)
89
- .then(() => {
90
- protocol.removeLibp2pEventListener("peer:identify", cb);
91
- resolve();
92
- })
93
- .catch((e) => {
94
- if (e.code === "ERR_CONNECTION_BEING_CLOSED")
95
- log.error(`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`);
96
- log.error(`Error waiting for handshake confirmation: ${e}`);
97
- });
98
- }
99
- else {
100
- protocol.removeLibp2pEventListener("peer:identify", cb);
101
- resolve();
102
- }
103
- }
104
- };
105
- protocol.addLibp2pEventListener("peer:identify", cb);
106
- });
107
- }
108
- /**
109
- * Wait for at least one peer with the given protocol to be connected and in the gossipsub
110
- * mesh for all pubsubTopics.
111
- */
112
- async function waitForGossipSubPeerInMesh(waku) {
113
- let peers = waku.getMeshPeers();
114
- const pubsubTopics = waku.pubsubTopics;
115
- for (const topic of pubsubTopics) {
116
- while (peers.length == 0) {
117
- await pEvent(waku.gossipSub, "gossipsub:heartbeat");
118
- peers = waku.getMeshPeers(topic);
119
- }
120
- }
121
- }
122
- const awaitTimeout = (ms, rejectReason) => new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
123
- async function rejectOnTimeout(promise, timeoutMs, rejectReason) {
124
- await Promise.race([promise, awaitTimeout(timeoutMs, rejectReason)]);
125
- }
126
- function getEnabledProtocols(waku) {
127
- const protocols = [];
128
- if (waku.relay) {
129
- protocols.push(Protocols.Relay);
130
- }
131
- if (waku.filter) {
132
- protocols.push(Protocols.Filter);
133
- }
134
- if (waku.store) {
135
- protocols.push(Protocols.Store);
136
- }
137
- if (waku.lightPush) {
138
- protocols.push(Protocols.LightPush);
139
- }
140
- return protocols;
141
- }
142
- //# sourceMappingURL=wait_for_remote_peer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"wait_for_remote_peer.js","sourceRoot":"","sources":["../../src/lib/wait_for_remote_peer.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;A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AC;IAClC,CAAC;IAED,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;QACnB,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IACtC,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -1,51 +0,0 @@
1
- import { Peer } from "@libp2p/interface";
2
- import { Tags } from "@waku/interfaces";
3
-
4
- /**
5
- * Retrieves a list of peers based on the specified criteria:
6
- * 1. If numPeers is 0, return all peers
7
- * 2. Bootstrap peers are prioritized
8
- * 3. Non-bootstrap peers are randomly selected to fill up to numPeers
9
- *
10
- * @param peers - The list of peers to filter from.
11
- * @param numPeers - The total number of peers to retrieve. If 0, all peers are returned, irrespective of `maxBootstrapPeers`.
12
- * @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
13
- * @returns An array of peers based on the specified criteria.
14
- */
15
- export function filterPeersByDiscovery(
16
- peers: Peer[],
17
- numPeers: number,
18
- maxBootstrapPeers: number
19
- ): Peer[] {
20
- // Collect the bootstrap peers up to the specified maximum
21
- let bootstrapPeers = peers
22
- .filter((peer) => peer.tags.has(Tags.BOOTSTRAP))
23
- .slice(0, maxBootstrapPeers);
24
-
25
- // If numPeers is less than the number of bootstrap peers, adjust the bootstrapPeers array
26
- if (numPeers > 0 && numPeers < bootstrapPeers.length) {
27
- bootstrapPeers = bootstrapPeers.slice(0, numPeers);
28
- }
29
-
30
- // Collect non-bootstrap peers
31
- const nonBootstrapPeers = peers.filter(
32
- (peer) => !peer.tags.has(Tags.BOOTSTRAP)
33
- );
34
-
35
- // If numPeers is 0, return all peers
36
- if (numPeers === 0) {
37
- return [...bootstrapPeers, ...nonBootstrapPeers];
38
- }
39
-
40
- // Initialize the list of selected peers with the bootstrap peers
41
- const selectedPeers: Peer[] = [...bootstrapPeers];
42
-
43
- // Fill up to numPeers with remaining random peers if needed
44
- while (selectedPeers.length < numPeers && nonBootstrapPeers.length > 0) {
45
- const randomIndex = Math.floor(Math.random() * nonBootstrapPeers.length);
46
- const randomPeer = nonBootstrapPeers.splice(randomIndex, 1)[0];
47
- selectedPeers.push(randomPeer);
48
- }
49
-
50
- return selectedPeers;
51
- }
@@ -1,90 +0,0 @@
1
- import {
2
- HealthStatus,
3
- type IHealthManager,
4
- NodeHealth,
5
- type ProtocolHealth,
6
- Protocols
7
- } from "@waku/interfaces";
8
-
9
- class HealthManager implements IHealthManager {
10
- public static instance: HealthManager;
11
- private readonly health: NodeHealth;
12
-
13
- private constructor() {
14
- this.health = {
15
- overallStatus: HealthStatus.Unhealthy,
16
- protocolStatuses: new Map()
17
- };
18
- }
19
-
20
- public static getInstance(): HealthManager {
21
- if (!HealthManager.instance) {
22
- HealthManager.instance = new HealthManager();
23
- }
24
- return HealthManager.instance;
25
- }
26
-
27
- public getHealthStatus(): HealthStatus {
28
- return this.health.overallStatus;
29
- }
30
-
31
- public getProtocolStatus(protocol: Protocols): ProtocolHealth | undefined {
32
- return this.health.protocolStatuses.get(protocol);
33
- }
34
-
35
- public updateProtocolHealth(
36
- multicodec: string,
37
- connectedPeers: number
38
- ): void {
39
- const protocol = this.getNameFromMulticodec(multicodec);
40
-
41
- let status: HealthStatus = HealthStatus.Unhealthy;
42
- if (connectedPeers == 1) {
43
- status = HealthStatus.MinimallyHealthy;
44
- } else if (connectedPeers >= 2) {
45
- status = HealthStatus.SufficientlyHealthy;
46
- }
47
-
48
- this.health.protocolStatuses.set(protocol, {
49
- name: protocol,
50
- status: status,
51
- lastUpdate: new Date()
52
- });
53
-
54
- this.updateOverallHealth();
55
- }
56
-
57
- private getNameFromMulticodec(multicodec: string): Protocols {
58
- let name: Protocols;
59
- if (multicodec.includes("filter")) {
60
- name = Protocols.Filter;
61
- } else if (multicodec.includes("lightpush")) {
62
- name = Protocols.LightPush;
63
- } else if (multicodec.includes("store")) {
64
- name = Protocols.Store;
65
- } else {
66
- throw new Error(`Unknown protocol: ${multicodec}`);
67
- }
68
- return name;
69
- }
70
-
71
- private updateOverallHealth(): void {
72
- const relevantProtocols = [Protocols.LightPush, Protocols.Filter];
73
- const statuses = relevantProtocols.map(
74
- (p) => this.getProtocolStatus(p)?.status
75
- );
76
-
77
- if (statuses.some((status) => status === HealthStatus.Unhealthy)) {
78
- this.health.overallStatus = HealthStatus.Unhealthy;
79
- } else if (
80
- statuses.some((status) => status === HealthStatus.MinimallyHealthy)
81
- ) {
82
- this.health.overallStatus = HealthStatus.MinimallyHealthy;
83
- } else {
84
- this.health.overallStatus = HealthStatus.SufficientlyHealthy;
85
- }
86
- }
87
- }
88
-
89
- export const getHealthManager = (): HealthManager =>
90
- HealthManager.getInstance();
@@ -1,200 +0,0 @@
1
- import type { IdentifyResult } from "@libp2p/interface";
2
- import type {
3
- IBaseProtocolCore,
4
- IMetadata,
5
- IRelay,
6
- Waku
7
- } from "@waku/interfaces";
8
- import { Protocols } from "@waku/interfaces";
9
- import { Logger } from "@waku/utils";
10
- import { pEvent } from "p-event";
11
- const log = new Logger("wait-for-remote-peer");
12
-
13
- //TODO: move this function within the Waku class: https://github.com/waku-org/js-waku/issues/1761
14
- /**
15
- * Wait for a remote peer to be ready given the passed protocols.
16
- * Must be used after attempting to connect to nodes, using
17
- * {@link @waku/sdk!WakuNode.dial} or a bootstrap method with
18
- * {@link @waku/sdk!createLightNode}.
19
- *
20
- * If the passed protocols is a GossipSub protocol, then it resolves only once
21
- * a peer is in a mesh, to help ensure that other peers will send and receive
22
- * message to us.
23
- *
24
- * @param waku The Waku Node
25
- * @param protocols The protocols that need to be enabled by remote peers.
26
- * @param timeoutMs A timeout value in milliseconds..
27
- *
28
- * @returns A promise that **resolves** if all desired protocols are fulfilled by
29
- * remote nodes, **rejects** if the timeoutMs is reached.
30
- * @throws If passing a protocol that is not mounted
31
- * @default Wait for remote peers with protocols enabled locally and no time out is applied.
32
- */
33
- export async function waitForRemotePeer(
34
- waku: Waku,
35
- protocols?: Protocols[],
36
- timeoutMs?: number
37
- ): Promise<void> {
38
- protocols = protocols ?? getEnabledProtocols(waku);
39
-
40
- if (!waku.isStarted()) return Promise.reject("Waku node is not started");
41
-
42
- const promises = [];
43
-
44
- if (protocols.includes(Protocols.Relay)) {
45
- if (!waku.relay)
46
- throw new Error("Cannot wait for Relay peer: protocol not mounted");
47
- promises.push(waitForGossipSubPeerInMesh(waku.relay));
48
- }
49
-
50
- if (protocols.includes(Protocols.Store)) {
51
- if (!waku.store)
52
- throw new Error("Cannot wait for Store peer: protocol not mounted");
53
- promises.push(
54
- waitForConnectedPeer(waku.store.protocol, waku.libp2p.services.metadata)
55
- );
56
- }
57
-
58
- if (protocols.includes(Protocols.LightPush)) {
59
- if (!waku.lightPush)
60
- throw new Error("Cannot wait for LightPush peer: protocol not mounted");
61
- promises.push(
62
- waitForConnectedPeer(
63
- waku.lightPush.protocol,
64
- waku.libp2p.services.metadata
65
- )
66
- );
67
- }
68
-
69
- if (protocols.includes(Protocols.Filter)) {
70
- if (!waku.filter)
71
- throw new Error("Cannot wait for Filter peer: protocol not mounted");
72
- promises.push(
73
- waitForConnectedPeer(waku.filter.protocol, waku.libp2p.services.metadata)
74
- );
75
- }
76
-
77
- if (timeoutMs) {
78
- await rejectOnTimeout(
79
- Promise.all(promises),
80
- timeoutMs,
81
- "Timed out waiting for a remote peer."
82
- );
83
- } else {
84
- await Promise.all(promises);
85
- }
86
- }
87
-
88
- //TODO: move this function within protocol SDK class: https://github.com/waku-org/js-waku/issues/1761
89
- /**
90
- * Wait for a peer with the given protocol to be connected.
91
- * If sharding is enabled on the node, it will also wait for the peer to be confirmed by the metadata service.
92
- */
93
- async function waitForConnectedPeer(
94
- protocol: IBaseProtocolCore,
95
- metadataService?: IMetadata
96
- ): Promise<void> {
97
- const codec = protocol.multicodec;
98
- const peers = await protocol.connectedPeers();
99
-
100
- if (peers.length) {
101
- if (!metadataService) {
102
- log.info(`${codec} peer found: `, peers[0].id.toString());
103
- return;
104
- }
105
-
106
- // once a peer is connected, we need to confirm the metadata handshake with at least one of those peers if sharding is enabled
107
- try {
108
- await Promise.any(
109
- peers.map((peer) => metadataService.confirmOrAttemptHandshake(peer.id))
110
- );
111
- return;
112
- } catch (e) {
113
- if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED")
114
- log.error(
115
- `Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`
116
- );
117
-
118
- log.error(`Error waiting for handshake confirmation: ${e}`);
119
- }
120
- }
121
-
122
- log.info(`Waiting for ${codec} peer`);
123
-
124
- // else we'll just wait for the next peer to connect
125
- await new Promise<void>((resolve) => {
126
- const cb = (evt: CustomEvent<IdentifyResult>): void => {
127
- if (evt.detail?.protocols?.includes(codec)) {
128
- if (metadataService) {
129
- metadataService
130
- .confirmOrAttemptHandshake(evt.detail.peerId)
131
- .then(() => {
132
- protocol.removeLibp2pEventListener("peer:identify", cb);
133
- resolve();
134
- })
135
- .catch((e) => {
136
- if (e.code === "ERR_CONNECTION_BEING_CLOSED")
137
- log.error(
138
- `Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`
139
- );
140
-
141
- log.error(`Error waiting for handshake confirmation: ${e}`);
142
- });
143
- } else {
144
- protocol.removeLibp2pEventListener("peer:identify", cb);
145
- resolve();
146
- }
147
- }
148
- };
149
- protocol.addLibp2pEventListener("peer:identify", cb);
150
- });
151
- }
152
-
153
- /**
154
- * Wait for at least one peer with the given protocol to be connected and in the gossipsub
155
- * mesh for all pubsubTopics.
156
- */
157
- async function waitForGossipSubPeerInMesh(waku: IRelay): Promise<void> {
158
- let peers = waku.getMeshPeers();
159
- const pubsubTopics = waku.pubsubTopics;
160
-
161
- for (const topic of pubsubTopics) {
162
- while (peers.length == 0) {
163
- await pEvent(waku.gossipSub, "gossipsub:heartbeat");
164
- peers = waku.getMeshPeers(topic);
165
- }
166
- }
167
- }
168
-
169
- const awaitTimeout = (ms: number, rejectReason: string): Promise<void> =>
170
- new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
171
-
172
- async function rejectOnTimeout<T>(
173
- promise: Promise<T>,
174
- timeoutMs: number,
175
- rejectReason: string
176
- ): Promise<void> {
177
- await Promise.race([promise, awaitTimeout(timeoutMs, rejectReason)]);
178
- }
179
-
180
- function getEnabledProtocols(waku: Waku): Protocols[] {
181
- const protocols = [];
182
-
183
- if (waku.relay) {
184
- protocols.push(Protocols.Relay);
185
- }
186
-
187
- if (waku.filter) {
188
- protocols.push(Protocols.Filter);
189
- }
190
-
191
- if (waku.store) {
192
- protocols.push(Protocols.Store);
193
- }
194
-
195
- if (waku.lightPush) {
196
- protocols.push(Protocols.LightPush);
197
- }
198
-
199
- return protocols;
200
- }