@waku/core 0.0.33-acef1ac.0 → 0.0.33-c50088a.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.
@@ -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,28 +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
62
  const connections = this.components.connectionManager.getConnections(
70
63
  peer.id
71
64
  );
72
- return connections.some((c) =>
73
- c.streams.some((s) => s.protocol === this.multicodec)
74
- );
65
+ if (withOpenStreams) {
66
+ return connections.some((c) =>
67
+ c.streams.some((s) => s.protocol === this.multicodec)
68
+ );
69
+ }
70
+ return connections.length > 0;
75
71
  });
76
72
  }
77
73
 
@@ -80,9 +76,8 @@ export class BaseProtocol implements IBaseProtocolCore {
80
76
  *
81
77
  * @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
82
78
  * @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
83
-
84
- * @returns A list of peers that support the protocol sorted by latency.
85
- */
79
+ * @returns A list of peers that support the protocol sorted by latency. By default, returns all peers available, including bootstrap.
80
+ */
86
81
  public async getPeers(
87
82
  {
88
83
  numPeers,
@@ -91,29 +86,23 @@ export class BaseProtocol implements IBaseProtocolCore {
91
86
  numPeers: number;
92
87
  maxBootstrapPeers: number;
93
88
  } = {
94
- maxBootstrapPeers: 1,
89
+ maxBootstrapPeers: 0,
95
90
  numPeers: 0
96
91
  }
97
92
  ): Promise<Peer[]> {
98
93
  // Retrieve all connected peers that support the protocol & shard (if configured)
99
- const connectedPeersForProtocolAndShard =
100
- await getConnectedPeersForProtocolAndShard(
101
- this.components.connectionManager.getConnections(),
102
- this.peerStore,
103
- [this.multicodec],
104
- pubsubTopicsToShardInfo(this.pubsubTopics)
105
- );
94
+ const allAvailableConnectedPeers = await this.connectedPeers();
106
95
 
107
96
  // Filter the peers based on discovery & number of peers requested
108
97
  const filteredPeers = filterPeersByDiscovery(
109
- connectedPeersForProtocolAndShard,
98
+ allAvailableConnectedPeers,
110
99
  numPeers,
111
100
  maxBootstrapPeers
112
101
  );
113
102
 
114
103
  // Sort the peers by latency
115
104
  const sortedFilteredPeers = await sortPeersByLatency(
116
- this.peerStore,
105
+ this.components.peerStore,
117
106
  filteredPeers
118
107
  );
119
108
 
@@ -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,