@waku/discovery 0.0.4-1887f4f.0 → 0.0.4-39f8920.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.
@@ -11,9 +11,10 @@ import {
11
11
  type Libp2pComponents,
12
12
  type PeerExchangeQueryResult,
13
13
  PubsubTopic,
14
+ ShardInfo,
14
15
  Tags
15
16
  } from "@waku/interfaces";
16
- import { encodeRelayShard, Logger } from "@waku/utils";
17
+ import { decodeRelayShard, encodeRelayShard, Logger } from "@waku/utils";
17
18
 
18
19
  import { PeerExchangeCodec, WakuPeerExchange } from "./waku_peer_exchange.js";
19
20
 
@@ -198,7 +199,48 @@ export class PeerExchangeDiscovery
198
199
 
199
200
  const hasPeer = await this.components.peerStore.has(peerId);
200
201
  if (hasPeer) {
201
- continue;
202
+ const { hasMultiaddrDiff, hasShardDiff } = await this.checkPeerInfoDiff(
203
+ peerInfo,
204
+ shardInfo
205
+ );
206
+
207
+ if (hasMultiaddrDiff || hasShardDiff) {
208
+ log.info(
209
+ `Peer ${peerId.toString()} has updated multiaddrs or shardInfo, updating`
210
+ );
211
+
212
+ if (hasMultiaddrDiff) {
213
+ log.info(
214
+ `Peer ${peerId.toString()} has updated multiaddrs, updating`
215
+ );
216
+
217
+ await this.components.peerStore.patch(peerId, {
218
+ multiaddrs: peerInfo.multiaddrs
219
+ });
220
+ }
221
+
222
+ if (hasShardDiff && shardInfo) {
223
+ log.info(
224
+ `Peer ${peerId.toString()} has updated shardInfo, updating`
225
+ );
226
+ await this.components.peerStore.merge(peerId, {
227
+ metadata: {
228
+ shardInfo: encodeRelayShard(shardInfo)
229
+ }
230
+ });
231
+
232
+ this.dispatchEvent(
233
+ new CustomEvent<PeerInfo>("peer", {
234
+ detail: {
235
+ id: peerId,
236
+ multiaddrs: peerInfo.multiaddrs
237
+ }
238
+ })
239
+ );
240
+ }
241
+
242
+ continue;
243
+ }
202
244
  }
203
245
 
204
246
  // update the tags for the peer
@@ -213,6 +255,9 @@ export class PeerExchangeDiscovery
213
255
  metadata: {
214
256
  shardInfo: encodeRelayShard(shardInfo)
215
257
  }
258
+ }),
259
+ ...(peerInfo.multiaddrs && {
260
+ multiaddrs: peerInfo.multiaddrs
216
261
  })
217
262
  });
218
263
 
@@ -236,6 +281,37 @@ export class PeerExchangeDiscovery
236
281
  this.queryingPeers.delete(peerIdStr);
237
282
  this.queryAttempts.delete(peerIdStr);
238
283
  }
284
+
285
+ private async checkPeerInfoDiff(
286
+ peerInfo: PeerInfo,
287
+ shardInfo?: ShardInfo
288
+ ): Promise<{ hasMultiaddrDiff: boolean; hasShardDiff: boolean }> {
289
+ const { id: peerId } = peerInfo;
290
+ const peer = await this.components.peerStore.get(peerId);
291
+
292
+ const existingMultiaddrs = peer.addresses.map((a) =>
293
+ a.multiaddr.toString()
294
+ );
295
+ const newMultiaddrs = peerInfo.multiaddrs.map((ma) => ma.toString());
296
+ const hasMultiaddrDiff = existingMultiaddrs.some(
297
+ (ma) => !newMultiaddrs.includes(ma)
298
+ );
299
+
300
+ let hasShardDiff: boolean = false;
301
+ const existingShardInfoBytes = peer.metadata.get("shardInfo");
302
+ if (existingShardInfoBytes) {
303
+ const existingShardInfo = decodeRelayShard(existingShardInfoBytes);
304
+ if (existingShardInfo || shardInfo) {
305
+ hasShardDiff =
306
+ existingShardInfo.clusterId !== shardInfo?.clusterId ||
307
+ existingShardInfo.shards.some(
308
+ (shard) => !shardInfo?.shards.includes(shard)
309
+ );
310
+ }
311
+ }
312
+
313
+ return { hasMultiaddrDiff, hasShardDiff };
314
+ }
239
315
  }
240
316
 
241
317
  export function wakuPeerExchangeDiscovery(