@waku/core 0.0.26-7eb3375.0 → 0.0.27
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/CHANGELOG.md +56 -0
- package/bundle/base_protocol-LhsIWF3-.js +308 -0
- package/bundle/{browser-9a6558bb.js → browser-BQyFvtq6.js} +579 -699
- package/bundle/index-8YyfzF9R.js +650 -0
- package/bundle/index.js +3076 -21759
- package/bundle/lib/base_protocol.js +3 -3
- package/bundle/lib/message/version_0.js +3 -3
- package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
- package/bundle/{version_0-7190df43.js → version_0-FXfzO8Km.js} +1368 -2459
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +2 -5
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +14 -10
- package/dist/lib/base_protocol.js +38 -16
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager.d.ts +2 -2
- package/dist/lib/connection_manager.js +16 -6
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/index.d.ts +1 -1
- package/dist/lib/filter/index.js +146 -85
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/filterPeers.d.ts +8 -5
- package/dist/lib/filterPeers.js +12 -5
- package/dist/lib/filterPeers.js.map +1 -1
- package/dist/lib/keep_alive_manager.d.ts +2 -3
- package/dist/lib/keep_alive_manager.js +2 -2
- package/dist/lib/keep_alive_manager.js.map +1 -1
- package/dist/lib/light_push/index.js +3 -7
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/message/version_0.d.ts +3 -3
- package/dist/lib/message/version_0.js +5 -6
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +3 -0
- package/dist/lib/metadata/index.js +81 -0
- package/dist/lib/metadata/index.js.map +1 -0
- package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
- package/dist/lib/store/history_rpc.js.map +1 -1
- package/dist/lib/store/index.js +1 -4
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager.d.ts +2 -2
- package/dist/lib/stream_manager.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.d.ts +1 -1
- package/dist/lib/wait_for_remote_peer.js +40 -10
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/package.json +129 -1
- package/src/index.ts +3 -6
- package/src/lib/base_protocol.ts +71 -24
- package/src/lib/connection_manager.ts +17 -10
- package/src/lib/filter/index.ts +237 -139
- package/src/lib/filterPeers.ts +15 -7
- package/src/lib/keep_alive_manager.ts +4 -5
- package/src/lib/light_push/index.ts +11 -11
- package/src/lib/message/version_0.ts +16 -8
- package/src/lib/metadata/index.ts +142 -0
- package/src/lib/store/index.ts +3 -7
- package/src/lib/stream_manager.ts +2 -3
- package/src/lib/wait_for_remote_peer.ts +58 -12
- package/bundle/base_protocol-46017f51.js +0 -468
- package/bundle/index-7581d519.js +0 -31
- package/dist/lib/constants.d.ts +0 -4
- package/dist/lib/constants.js +0 -5
- package/dist/lib/constants.js.map +0 -1
- package/dist/lib/waku.d.ts +0 -57
- package/dist/lib/waku.js +0 -124
- package/dist/lib/waku.js.map +0 -1
- package/src/lib/constants.ts +0 -4
- package/src/lib/waku.ts +0 -204
package/CHANGELOG.md
CHANGED
@@ -5,6 +5,62 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The file is maintained by [Release Please](https://github.com/googleapis/release-please) based on [Conventional Commits](https://www.conventionalcommits.org) specification,
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [0.0.27](https://github.com/waku-org/js-waku/compare/core-v0.0.26...core-v0.0.27) (2024-03-04)
|
9
|
+
|
10
|
+
|
11
|
+
### ⚠ BREAKING CHANGES
|
12
|
+
|
13
|
+
* protocols filter peers as per configured shard ([#1756](https://github.com/waku-org/js-waku/issues/1756))
|
14
|
+
|
15
|
+
### Features
|
16
|
+
|
17
|
+
* Create node and subscription by content topic ([ee2d417](https://github.com/waku-org/js-waku/commit/ee2d4176f8cca45a51b7dac0009f0eb01952f540))
|
18
|
+
* Decouple sharding params out of core ([e138b4f](https://github.com/waku-org/js-waku/commit/e138b4f5c49a35a37830e31e8be87d824f53249f))
|
19
|
+
* Lightpush & filter send requests to multiple peers ([#1779](https://github.com/waku-org/js-waku/issues/1779)) ([7affbe2](https://github.com/waku-org/js-waku/commit/7affbe222dd30ccb6619839f4bc5eb86433a80f7))
|
20
|
+
* Local discovery ([#1811](https://github.com/waku-org/js-waku/issues/1811)) ([199f6ab](https://github.com/waku-org/js-waku/commit/199f6ab2ff83694b93e94e935e2925537e01e281))
|
21
|
+
* Make ShardingParams optional in sdk, required internally ([68d3229](https://github.com/waku-org/js-waku/commit/68d3229644f395bd84b2e2a7067c7b51e9da3dd0))
|
22
|
+
* Protocols filter peers as per configured shard ([#1756](https://github.com/waku-org/js-waku/issues/1756)) ([477c2a5](https://github.com/waku-org/js-waku/commit/477c2a5918f2f75cd2c14bc6ed75e1687c5a09b4))
|
23
|
+
|
24
|
+
|
25
|
+
### Dependencies
|
26
|
+
|
27
|
+
* The following workspace dependencies were updated
|
28
|
+
* dependencies
|
29
|
+
* @waku/enr bumped from ^0.0.20 to ^0.0.21
|
30
|
+
* @waku/interfaces bumped from 0.0.21 to 0.0.22
|
31
|
+
* @waku/message-hash bumped from ^0.1.10 to ^0.1.11
|
32
|
+
* @waku/utils bumped from 0.0.14 to 0.0.15
|
33
|
+
|
34
|
+
## [0.0.26](https://github.com/waku-org/js-waku/compare/core-v0.0.25...core-v0.0.26) (2024-01-10)
|
35
|
+
|
36
|
+
|
37
|
+
### ⚠ BREAKING CHANGES
|
38
|
+
|
39
|
+
* add support for sharded pubsub topics & remove support for named pubsub topics ([#1697](https://github.com/waku-org/js-waku/issues/1697))
|
40
|
+
* change all instances of `PubSubTopic` to `PubsubTopic` ([#1703](https://github.com/waku-org/js-waku/issues/1703))
|
41
|
+
|
42
|
+
### Features
|
43
|
+
|
44
|
+
* Add support for autosharded pubsub topics ([2bc3735](https://github.com/waku-org/js-waku/commit/2bc3735e4dcf85f06b3dee542024d7f20a40fac2))
|
45
|
+
* Add support for sharded pubsub topics & remove support for named pubsub topics ([#1697](https://github.com/waku-org/js-waku/issues/1697)) ([4cf2ffe](https://github.com/waku-org/js-waku/commit/4cf2ffefa75e0571805036b71644d2cdd4fe3192))
|
46
|
+
* Metadata protocol ([#1732](https://github.com/waku-org/js-waku/issues/1732)) ([9ac2a3f](https://github.com/waku-org/js-waku/commit/9ac2a3f36352523b79fcd8f8a94bd6e0e109fc30))
|
47
|
+
* Track node connection state ([#1719](https://github.com/waku-org/js-waku/issues/1719)) ([1d0e2ac](https://github.com/waku-org/js-waku/commit/1d0e2ace7fa5b44ab192505c7ebce01a7ce343e0))
|
48
|
+
|
49
|
+
|
50
|
+
### Miscellaneous Chores
|
51
|
+
|
52
|
+
* Change all instances of `PubSubTopic` to `PubsubTopic` ([#1703](https://github.com/waku-org/js-waku/issues/1703)) ([3166a51](https://github.com/waku-org/js-waku/commit/3166a5135e77583da4fa722ee2aa47c785854a38))
|
53
|
+
|
54
|
+
|
55
|
+
### Dependencies
|
56
|
+
|
57
|
+
* The following workspace dependencies were updated
|
58
|
+
* dependencies
|
59
|
+
* @waku/enr bumped from ^0.0.19 to ^0.0.20
|
60
|
+
* @waku/interfaces bumped from 0.0.20 to 0.0.21
|
61
|
+
* @waku/proto bumped from 0.0.5 to 0.0.6
|
62
|
+
* @waku/utils bumped from 0.0.13 to 0.0.14
|
63
|
+
|
8
64
|
## [0.0.25](https://github.com/waku-org/js-waku/compare/core-v0.0.24...core-v0.0.25) (2023-11-01)
|
9
65
|
|
10
66
|
|
@@ -0,0 +1,308 @@
|
|
1
|
+
import { b as bytesToUtf8, L as Logger, k as ensureShardingConfigured } from './index-8YyfzF9R.js';
|
2
|
+
import { T as Tags } from './browser-BQyFvtq6.js';
|
3
|
+
|
4
|
+
const decodeRelayShard = (bytes) => {
|
5
|
+
// explicitly converting to Uint8Array to avoid Buffer
|
6
|
+
// https://github.com/libp2p/js-libp2p/issues/2146
|
7
|
+
bytes = new Uint8Array(bytes);
|
8
|
+
if (bytes.length < 3)
|
9
|
+
throw new Error("Insufficient data");
|
10
|
+
const view = new DataView(bytes.buffer);
|
11
|
+
const clusterId = view.getUint16(0);
|
12
|
+
const shards = [];
|
13
|
+
if (bytes.length === 130) {
|
14
|
+
// rsv format (Bit Vector)
|
15
|
+
for (let i = 0; i < 1024; i++) {
|
16
|
+
const byteIndex = Math.floor(i / 8) + 2; // Adjusted for the 2-byte cluster field
|
17
|
+
const bitIndex = 7 - (i % 8);
|
18
|
+
if (view.getUint8(byteIndex) & (1 << bitIndex)) {
|
19
|
+
shards.push(i);
|
20
|
+
}
|
21
|
+
}
|
22
|
+
}
|
23
|
+
else {
|
24
|
+
// rs format (Index List)
|
25
|
+
const numIndices = view.getUint8(2);
|
26
|
+
for (let i = 0, offset = 3; i < numIndices; i++, offset += 2) {
|
27
|
+
if (offset + 1 >= bytes.length)
|
28
|
+
throw new Error("Unexpected end of data");
|
29
|
+
shards.push(view.getUint16(offset));
|
30
|
+
}
|
31
|
+
}
|
32
|
+
return { clusterId, shards };
|
33
|
+
};
|
34
|
+
const encodeRelayShard = (shardInfo) => {
|
35
|
+
const { clusterId, shards } = shardInfo;
|
36
|
+
const totalLength = shards.length >= 64 ? 130 : 3 + 2 * shards.length;
|
37
|
+
const buffer = new ArrayBuffer(totalLength);
|
38
|
+
const view = new DataView(buffer);
|
39
|
+
view.setUint16(0, clusterId);
|
40
|
+
if (shards.length >= 64) {
|
41
|
+
// rsv format (Bit Vector)
|
42
|
+
for (const index of shards) {
|
43
|
+
const byteIndex = Math.floor(index / 8) + 2; // Adjusted for the 2-byte cluster field
|
44
|
+
const bitIndex = 7 - (index % 8);
|
45
|
+
view.setUint8(byteIndex, view.getUint8(byteIndex) | (1 << bitIndex));
|
46
|
+
}
|
47
|
+
}
|
48
|
+
else {
|
49
|
+
// rs format (Index List)
|
50
|
+
view.setUint8(2, shards.length);
|
51
|
+
for (let i = 0, offset = 3; i < shards.length; i++, offset += 2) {
|
52
|
+
view.setUint16(offset, shards[i]);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
return new Uint8Array(buffer);
|
56
|
+
};
|
57
|
+
|
58
|
+
/**
|
59
|
+
* Function to sort peers by latency from lowest to highest
|
60
|
+
* @param peerStore - The Libp2p PeerStore
|
61
|
+
* @param peers - The list of peers to choose from
|
62
|
+
* @returns Sorted array of peers by latency
|
63
|
+
*/
|
64
|
+
async function sortPeersByLatency(peerStore, peers) {
|
65
|
+
if (peers.length === 0)
|
66
|
+
return [];
|
67
|
+
const results = await Promise.all(peers.map(async (peer) => {
|
68
|
+
try {
|
69
|
+
const pingBytes = (await peerStore.get(peer.id)).metadata.get("ping");
|
70
|
+
if (!pingBytes)
|
71
|
+
return { peer, ping: Infinity };
|
72
|
+
const ping = Number(bytesToUtf8(pingBytes));
|
73
|
+
return { peer, ping };
|
74
|
+
}
|
75
|
+
catch (error) {
|
76
|
+
return { peer, ping: Infinity };
|
77
|
+
}
|
78
|
+
}));
|
79
|
+
// filter out null values
|
80
|
+
const validResults = results.filter((result) => result !== null);
|
81
|
+
return validResults
|
82
|
+
.sort((a, b) => a.ping - b.ping)
|
83
|
+
.map((result) => result.peer);
|
84
|
+
}
|
85
|
+
/**
|
86
|
+
* Returns the list of peers that supports the given protocol.
|
87
|
+
*/
|
88
|
+
async function getPeersForProtocol(peerStore, protocols) {
|
89
|
+
const peers = [];
|
90
|
+
await peerStore.forEach((peer) => {
|
91
|
+
for (let i = 0; i < protocols.length; i++) {
|
92
|
+
if (peer.protocols.includes(protocols[i])) {
|
93
|
+
peers.push(peer);
|
94
|
+
break;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
});
|
98
|
+
return peers;
|
99
|
+
}
|
100
|
+
async function getConnectedPeersForProtocolAndShard(connections, peerStore, protocols, shardInfo) {
|
101
|
+
const openConnections = connections.filter((connection) => connection.status === "open");
|
102
|
+
const peerPromises = openConnections.map(async (connection) => {
|
103
|
+
const peer = await peerStore.get(connection.remotePeer);
|
104
|
+
const supportsProtocol = protocols.some((protocol) => peer.protocols.includes(protocol));
|
105
|
+
if (supportsProtocol) {
|
106
|
+
if (shardInfo) {
|
107
|
+
const encodedPeerShardInfo = peer.metadata.get("shardInfo");
|
108
|
+
const peerShardInfo = encodedPeerShardInfo && decodeRelayShard(encodedPeerShardInfo);
|
109
|
+
if (peerShardInfo && shardInfo.clusterId === peerShardInfo.clusterId) {
|
110
|
+
return peer;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
else {
|
114
|
+
return peer;
|
115
|
+
}
|
116
|
+
}
|
117
|
+
return null;
|
118
|
+
});
|
119
|
+
const peersWithNulls = await Promise.all(peerPromises);
|
120
|
+
return peersWithNulls.filter((peer) => peer !== null);
|
121
|
+
}
|
122
|
+
function selectConnection(connections) {
|
123
|
+
if (!connections.length)
|
124
|
+
return;
|
125
|
+
if (connections.length === 1)
|
126
|
+
return connections[0];
|
127
|
+
let latestConnection;
|
128
|
+
connections.forEach((connection) => {
|
129
|
+
if (connection.status === "open") {
|
130
|
+
if (!latestConnection) {
|
131
|
+
latestConnection = connection;
|
132
|
+
}
|
133
|
+
else if (connection.timeline.open > latestConnection.timeline.open) {
|
134
|
+
latestConnection = connection;
|
135
|
+
}
|
136
|
+
}
|
137
|
+
});
|
138
|
+
return latestConnection;
|
139
|
+
}
|
140
|
+
|
141
|
+
/**
|
142
|
+
* Retrieves a list of peers based on the specified criteria:
|
143
|
+
* 1. If numPeers is 0, return all peers
|
144
|
+
* 2. Bootstrap peers are prioritized
|
145
|
+
* 3. Non-bootstrap peers are randomly selected to fill up to numPeers
|
146
|
+
*
|
147
|
+
* @param peers - The list of peers to filter from.
|
148
|
+
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned, irrespective of `maxBootstrapPeers`.
|
149
|
+
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
150
|
+
* @returns An array of peers based on the specified criteria.
|
151
|
+
*/
|
152
|
+
function filterPeersByDiscovery(peers, numPeers, maxBootstrapPeers) {
|
153
|
+
// Collect the bootstrap peers up to the specified maximum
|
154
|
+
let bootstrapPeers = peers
|
155
|
+
.filter((peer) => peer.tags.has(Tags.BOOTSTRAP))
|
156
|
+
.slice(0, maxBootstrapPeers);
|
157
|
+
// If numPeers is less than the number of bootstrap peers, adjust the bootstrapPeers array
|
158
|
+
if (numPeers > 0 && numPeers < bootstrapPeers.length) {
|
159
|
+
bootstrapPeers = bootstrapPeers.slice(0, numPeers);
|
160
|
+
}
|
161
|
+
// Collect non-bootstrap peers
|
162
|
+
const nonBootstrapPeers = peers.filter((peer) => !peer.tags.has(Tags.BOOTSTRAP));
|
163
|
+
// If numPeers is 0, return all peers
|
164
|
+
if (numPeers === 0) {
|
165
|
+
return [...bootstrapPeers, ...nonBootstrapPeers];
|
166
|
+
}
|
167
|
+
// Initialize the list of selected peers with the bootstrap peers
|
168
|
+
const selectedPeers = [...bootstrapPeers];
|
169
|
+
// Fill up to numPeers with remaining random peers if needed
|
170
|
+
while (selectedPeers.length < numPeers && nonBootstrapPeers.length > 0) {
|
171
|
+
const randomIndex = Math.floor(Math.random() * nonBootstrapPeers.length);
|
172
|
+
const randomPeer = nonBootstrapPeers.splice(randomIndex, 1)[0];
|
173
|
+
selectedPeers.push(randomPeer);
|
174
|
+
}
|
175
|
+
return selectedPeers;
|
176
|
+
}
|
177
|
+
|
178
|
+
class StreamManager {
|
179
|
+
multicodec;
|
180
|
+
getConnections;
|
181
|
+
addEventListener;
|
182
|
+
streamPool;
|
183
|
+
log;
|
184
|
+
constructor(multicodec, getConnections, addEventListener) {
|
185
|
+
this.multicodec = multicodec;
|
186
|
+
this.getConnections = getConnections;
|
187
|
+
this.addEventListener = addEventListener;
|
188
|
+
this.log = new Logger(`stream-manager:${multicodec}`);
|
189
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool.bind(this));
|
190
|
+
this.getStream = this.getStream.bind(this);
|
191
|
+
this.streamPool = new Map();
|
192
|
+
}
|
193
|
+
async getStream(peer) {
|
194
|
+
const peerIdStr = peer.id.toString();
|
195
|
+
const streamPromise = this.streamPool.get(peerIdStr);
|
196
|
+
if (!streamPromise) {
|
197
|
+
return this.newStream(peer); // fallback by creating a new stream on the spot
|
198
|
+
}
|
199
|
+
// We have the stream, let's remove it from the map
|
200
|
+
this.streamPool.delete(peerIdStr);
|
201
|
+
this.prepareNewStream(peer);
|
202
|
+
const stream = await streamPromise;
|
203
|
+
if (!stream || stream.status === "closed") {
|
204
|
+
return this.newStream(peer); // fallback by creating a new stream on the spot
|
205
|
+
}
|
206
|
+
return stream;
|
207
|
+
}
|
208
|
+
async newStream(peer) {
|
209
|
+
const connections = this.getConnections(peer.id);
|
210
|
+
const connection = selectConnection(connections);
|
211
|
+
if (!connection) {
|
212
|
+
throw new Error("Failed to get a connection to the peer");
|
213
|
+
}
|
214
|
+
return connection.newStream(this.multicodec);
|
215
|
+
}
|
216
|
+
prepareNewStream(peer) {
|
217
|
+
const streamPromise = this.newStream(peer).catch(() => {
|
218
|
+
// No error thrown as this call is not triggered by the user
|
219
|
+
this.log.error(`Failed to prepare a new stream for ${peer.id.toString()}`);
|
220
|
+
});
|
221
|
+
this.streamPool.set(peer.id.toString(), streamPromise);
|
222
|
+
}
|
223
|
+
handlePeerUpdateStreamPool = (evt) => {
|
224
|
+
const peer = evt.detail.peer;
|
225
|
+
if (peer.protocols.includes(this.multicodec)) {
|
226
|
+
this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
|
227
|
+
this.prepareNewStream(peer);
|
228
|
+
}
|
229
|
+
};
|
230
|
+
}
|
231
|
+
|
232
|
+
const DEFAULT_NUM_PEERS_TO_USE = 3;
|
233
|
+
/**
|
234
|
+
* A class with predefined helpers, to be used as a base to implement Waku
|
235
|
+
* Protocols.
|
236
|
+
*/
|
237
|
+
class BaseProtocol {
|
238
|
+
multicodec;
|
239
|
+
components;
|
240
|
+
log;
|
241
|
+
pubsubTopics;
|
242
|
+
options;
|
243
|
+
addLibp2pEventListener;
|
244
|
+
removeLibp2pEventListener;
|
245
|
+
numPeersToUse;
|
246
|
+
streamManager;
|
247
|
+
constructor(multicodec, components, log, pubsubTopics, options) {
|
248
|
+
this.multicodec = multicodec;
|
249
|
+
this.components = components;
|
250
|
+
this.log = log;
|
251
|
+
this.pubsubTopics = pubsubTopics;
|
252
|
+
this.options = options;
|
253
|
+
this.numPeersToUse = options?.numPeersToUse ?? DEFAULT_NUM_PEERS_TO_USE;
|
254
|
+
this.addLibp2pEventListener = components.events.addEventListener.bind(components.events);
|
255
|
+
this.removeLibp2pEventListener = components.events.removeEventListener.bind(components.events);
|
256
|
+
this.streamManager = new StreamManager(multicodec, components.connectionManager.getConnections.bind(components.connectionManager), this.addLibp2pEventListener);
|
257
|
+
}
|
258
|
+
async getStream(peer) {
|
259
|
+
return this.streamManager.getStream(peer);
|
260
|
+
}
|
261
|
+
get peerStore() {
|
262
|
+
return this.components.peerStore;
|
263
|
+
}
|
264
|
+
/**
|
265
|
+
* Returns known peers from the address book (`libp2p.peerStore`) that support
|
266
|
+
* the class protocol. Waku may or may not be currently connected to these
|
267
|
+
* peers.
|
268
|
+
*/
|
269
|
+
async allPeers() {
|
270
|
+
return getPeersForProtocol(this.peerStore, [this.multicodec]);
|
271
|
+
}
|
272
|
+
async connectedPeers() {
|
273
|
+
const peers = await this.allPeers();
|
274
|
+
return peers.filter((peer) => {
|
275
|
+
return (this.components.connectionManager.getConnections(peer.id).length > 0);
|
276
|
+
});
|
277
|
+
}
|
278
|
+
/**
|
279
|
+
* Retrieves a list of connected peers that support the protocol. The list is sorted by latency.
|
280
|
+
*
|
281
|
+
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
282
|
+
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
283
|
+
|
284
|
+
* @returns A list of peers that support the protocol sorted by latency.
|
285
|
+
*/
|
286
|
+
async getPeers({ numPeers, maxBootstrapPeers } = {
|
287
|
+
maxBootstrapPeers: 1,
|
288
|
+
numPeers: 0
|
289
|
+
}) {
|
290
|
+
// Retrieve all connected peers that support the protocol & shard (if configured)
|
291
|
+
const connectedPeersForProtocolAndShard = await getConnectedPeersForProtocolAndShard(this.components.connectionManager.getConnections(), this.peerStore, [this.multicodec], this.options?.shardInfo
|
292
|
+
? ensureShardingConfigured(this.options.shardInfo).shardInfo
|
293
|
+
: undefined);
|
294
|
+
// Filter the peers based on discovery & number of peers requested
|
295
|
+
const filteredPeers = filterPeersByDiscovery(connectedPeersForProtocolAndShard, numPeers, maxBootstrapPeers);
|
296
|
+
// Sort the peers by latency
|
297
|
+
const sortedFilteredPeers = await sortPeersByLatency(this.peerStore, filteredPeers);
|
298
|
+
if (sortedFilteredPeers.length === 0) {
|
299
|
+
this.log.warn("No peers found. Ensure you have a connection to the network.");
|
300
|
+
}
|
301
|
+
if (sortedFilteredPeers.length < numPeers) {
|
302
|
+
this.log.warn(`Only ${sortedFilteredPeers.length} peers found. Requested ${numPeers}.`);
|
303
|
+
}
|
304
|
+
return sortedFilteredPeers;
|
305
|
+
}
|
306
|
+
}
|
307
|
+
|
308
|
+
export { BaseProtocol as B, StreamManager as S, decodeRelayShard as d, encodeRelayShard as e };
|