@waku/core 0.0.33-2fe9875.0 → 0.0.33-338250a.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/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@waku/core","version":"0.0.33-2fe9875.0","description":"TypeScript implementation of the Waku v2 protocol","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"},"./lib/message/version_0":{"types":"./dist/lib/message/version_0.d.ts","import":"./dist/lib/message/version_0.js"},"./lib/base_protocol":{"types":"./dist/lib/base_protocol.d.ts","import":"./dist/lib/base_protocol.js"}},"typesVersions":{"*":{"lib/*":["dist/lib/*"],"constants/*":["dist/constants/*"]}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/core#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralised","communication","web3","ethereum","dapps"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha","test:browser":"NODE_ENV=test karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=20"},"dependencies":{"@libp2p/ping":"^1.1.2","@waku/enr":"0.0.27-2fe9875.0","@waku/interfaces":"0.0.28-2fe9875.0","@waku/proto":"0.0.9-2fe9875.0","@waku/utils":"0.0.21-2fe9875.0","debug":"^4.3.4","it-all":"^3.0.4","it-length-prefixed":"^9.0.4","it-pipe":"^3.0.1","p-event":"^6.0.1","uint8arraylist":"^2.4.3","uuid":"^9.0.0"},"devDependencies":{"@multiformats/multiaddr":"^12.0.0","@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.11","@types/debug":"^4.1.12","@types/mocha":"^10.0.6","@types/uuid":"^9.0.8","@waku/build-utils":"*","chai":"^4.3.10","cspell":"^8.6.1","fast-check":"^3.19.0","ignore-loader":"^0.1.2","isomorphic-fetch":"^3.0.0","mocha":"^10.3.0","npm-run-all":"^4.1.5","process":"^0.11.10","rollup":"^4.12.0"},"peerDependencies":{"@multiformats/multiaddr":"^12.0.0","libp2p":"^1.8.1"},"peerDependenciesMeta":{"@multiformats/multiaddr":{"optional":true},"libp2p":{"optional":true}},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
1
+ {"name":"@waku/core","version":"0.0.33-338250a.0","description":"TypeScript implementation of the Waku v2 protocol","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"},"./lib/message/version_0":{"types":"./dist/lib/message/version_0.d.ts","import":"./dist/lib/message/version_0.js"},"./lib/base_protocol":{"types":"./dist/lib/base_protocol.d.ts","import":"./dist/lib/base_protocol.js"}},"typesVersions":{"*":{"lib/*":["dist/lib/*"],"constants/*":["dist/constants/*"]}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/core#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralised","communication","web3","ethereum","dapps"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha","test:browser":"NODE_ENV=test karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=20"},"dependencies":{"@libp2p/ping":"^1.1.2","@waku/enr":"0.0.27-338250a.0","@waku/interfaces":"0.0.28-338250a.0","@waku/proto":"0.0.9-338250a.0","@waku/utils":"0.0.21-338250a.0","debug":"^4.3.4","it-all":"^3.0.4","it-length-prefixed":"^9.0.4","it-pipe":"^3.0.1","p-event":"^6.0.1","uint8arraylist":"^2.4.3","uuid":"^9.0.0"},"devDependencies":{"@multiformats/multiaddr":"^12.0.0","@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.11","@types/debug":"^4.1.12","@types/mocha":"^10.0.6","@types/uuid":"^9.0.8","@waku/build-utils":"*","chai":"^4.3.10","cspell":"^8.6.1","fast-check":"^3.19.0","ignore-loader":"^0.1.2","isomorphic-fetch":"^3.0.0","mocha":"^10.3.0","npm-run-all":"^4.1.5","process":"^0.11.10","rollup":"^4.12.0"},"peerDependencies":{"@multiformats/multiaddr":"^12.0.0","libp2p":"^1.8.1"},"peerDependenciesMeta":{"@multiformats/multiaddr":{"optional":true},"libp2p":{"optional":true}},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
@@ -1,16 +1,12 @@
1
1
  import type { Libp2p } from "@libp2p/interface";
2
- import type { Peer, PeerStore, Stream } from "@libp2p/interface";
2
+ import type { Peer, Stream } from "@libp2p/interface";
3
3
  import type {
4
4
  IBaseProtocolCore,
5
5
  Libp2pComponents,
6
6
  PubsubTopic
7
7
  } from "@waku/interfaces";
8
- import { Logger, pubsubTopicsToShardInfo } from "@waku/utils";
9
- import {
10
- getConnectedPeersForProtocolAndShard,
11
- getPeersForProtocol,
12
- sortPeersByLatency
13
- } from "@waku/utils/libp2p";
8
+ import { Logger } from "@waku/utils";
9
+ import { getPeersForProtocol, sortPeersByLatency } from "@waku/utils/libp2p";
14
10
 
15
11
  import { filterPeersByDiscovery } from "./filterPeers.js";
16
12
  import { StreamManager } from "./stream_manager/index.js";
@@ -26,7 +22,7 @@ export class BaseProtocol implements IBaseProtocolCore {
26
22
 
27
23
  protected constructor(
28
24
  public multicodec: string,
29
- private components: Libp2pComponents,
25
+ protected components: Libp2pComponents,
30
26
  private log: Logger,
31
27
  public readonly pubsubTopics: PubsubTopic[]
32
28
  ) {
@@ -50,25 +46,28 @@ export class BaseProtocol implements IBaseProtocolCore {
50
46
  return this.streamManager.getStream(peer);
51
47
  }
52
48
 
53
- public get peerStore(): PeerStore {
54
- return this.components.peerStore;
55
- }
56
-
49
+ //TODO: move to SDK
57
50
  /**
58
51
  * Returns known peers from the address book (`libp2p.peerStore`) that support
59
52
  * the class protocol. Waku may or may not be currently connected to these
60
53
  * peers.
61
54
  */
62
55
  public async allPeers(): Promise<Peer[]> {
63
- return getPeersForProtocol(this.peerStore, [this.multicodec]);
56
+ return getPeersForProtocol(this.components.peerStore, [this.multicodec]);
64
57
  }
65
58
 
66
- public async connectedPeers(): Promise<Peer[]> {
59
+ public async connectedPeers(withOpenStreams = false): Promise<Peer[]> {
67
60
  const peers = await this.allPeers();
68
61
  return peers.filter((peer) => {
69
- return (
70
- this.components.connectionManager.getConnections(peer.id).length > 0
62
+ const connections = this.components.connectionManager.getConnections(
63
+ peer.id
71
64
  );
65
+ if (withOpenStreams) {
66
+ return connections.some((c) =>
67
+ c.streams.some((s) => s.protocol === this.multicodec)
68
+ );
69
+ }
70
+ return connections.length > 0;
72
71
  });
73
72
  }
74
73
 
@@ -77,9 +76,8 @@ export class BaseProtocol implements IBaseProtocolCore {
77
76
  *
78
77
  * @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
79
78
  * @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
80
-
81
- * @returns A list of peers that support the protocol sorted by latency.
82
- */
79
+ * @returns A list of peers that support the protocol sorted by latency. By default, returns all peers available, including bootstrap.
80
+ */
83
81
  public async getPeers(
84
82
  {
85
83
  numPeers,
@@ -88,29 +86,23 @@ export class BaseProtocol implements IBaseProtocolCore {
88
86
  numPeers: number;
89
87
  maxBootstrapPeers: number;
90
88
  } = {
91
- maxBootstrapPeers: 1,
89
+ maxBootstrapPeers: 0,
92
90
  numPeers: 0
93
91
  }
94
92
  ): Promise<Peer[]> {
95
93
  // Retrieve all connected peers that support the protocol & shard (if configured)
96
- const connectedPeersForProtocolAndShard =
97
- await getConnectedPeersForProtocolAndShard(
98
- this.components.connectionManager.getConnections(),
99
- this.peerStore,
100
- [this.multicodec],
101
- pubsubTopicsToShardInfo(this.pubsubTopics)
102
- );
94
+ const allAvailableConnectedPeers = await this.connectedPeers();
103
95
 
104
96
  // Filter the peers based on discovery & number of peers requested
105
97
  const filteredPeers = filterPeersByDiscovery(
106
- connectedPeersForProtocolAndShard,
98
+ allAvailableConnectedPeers,
107
99
  numPeers,
108
100
  maxBootstrapPeers
109
101
  );
110
102
 
111
103
  // Sort the peers by latency
112
104
  const sortedFilteredPeers = await sortPeersByLatency(
113
- this.peerStore,
105
+ this.components.peerStore,
114
106
  filteredPeers
115
107
  );
116
108
 
@@ -37,6 +37,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
37
37
  wakuMessage: WakuMessage,
38
38
  peerIdStr: string
39
39
  ) => Promise<void>,
40
+ private handleError: (error: Error) => Promise<void>,
40
41
  public readonly pubsubTopics: PubsubTopic[],
41
42
  libp2p: Libp2p
42
43
  ) {
@@ -301,8 +302,18 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
301
302
  () => {
302
303
  log.info("Receiving pipe closed.");
303
304
  },
304
- (e) => {
305
- log.error("Error with receiving pipe", e);
305
+ async (e) => {
306
+ log.error(
307
+ "Error with receiving pipe",
308
+ e,
309
+ " -- ",
310
+ "on peer ",
311
+ connection.remotePeer.toString(),
312
+ " -- ",
313
+ "stream ",
314
+ stream
315
+ );
316
+ await this.handleError(e);
306
317
  }
307
318
  );
308
319
  } catch (e) {
@@ -45,7 +45,7 @@ class Metadata extends BaseProtocol implements IMetadata {
45
45
  pubsubTopicsToShardInfo(this.pubsubTopics)
46
46
  );
47
47
 
48
- const peer = await this.peerStore.get(peerId);
48
+ const peer = await this.libp2pComponents.peerStore.get(peerId);
49
49
  if (!peer) {
50
50
  return {
51
51
  shardInfo: null,