libp2p 0.35.7 → 0.35.8-9b22c6e

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 (100) hide show
  1. package/dist/index.min.js +55 -0
  2. package/dist/src/circuit/auto-relay.d.ts +8 -10
  3. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  4. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  5. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  6. package/dist/src/connection-manager/index.d.ts +1 -2
  7. package/dist/src/connection-manager/index.d.ts.map +1 -1
  8. package/dist/src/content-routing/utils.d.ts +3 -3
  9. package/dist/src/content-routing/utils.d.ts.map +1 -1
  10. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  11. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  12. package/dist/src/dialer/index.d.ts +5 -5
  13. package/dist/src/dialer/index.d.ts.map +1 -1
  14. package/dist/src/fetch/constants.d.ts +2 -0
  15. package/dist/src/fetch/constants.d.ts.map +1 -0
  16. package/dist/src/fetch/index.d.ts +76 -0
  17. package/dist/src/fetch/index.d.ts.map +1 -0
  18. package/dist/src/fetch/proto.d.ts +134 -0
  19. package/dist/src/identify/index.d.ts +4 -4
  20. package/dist/src/identify/index.d.ts.map +1 -1
  21. package/dist/src/index.d.ts +16 -6
  22. package/dist/src/index.d.ts.map +1 -1
  23. package/dist/src/metrics/index.d.ts +9 -4
  24. package/dist/src/metrics/index.d.ts.map +1 -1
  25. package/dist/src/metrics/tracked-map.d.ts +6 -1
  26. package/dist/src/metrics/tracked-map.d.ts.map +1 -1
  27. package/dist/src/peer-routing.d.ts +1 -1
  28. package/dist/src/peer-routing.d.ts.map +1 -1
  29. package/dist/src/peer-store/address-book.d.ts +28 -84
  30. package/dist/src/peer-store/address-book.d.ts.map +1 -1
  31. package/dist/src/peer-store/index.d.ts +25 -94
  32. package/dist/src/peer-store/index.d.ts.map +1 -1
  33. package/dist/src/peer-store/key-book.d.ts +31 -15
  34. package/dist/src/peer-store/key-book.d.ts.map +1 -1
  35. package/dist/src/peer-store/metadata-book.d.ts +33 -29
  36. package/dist/src/peer-store/metadata-book.d.ts.map +1 -1
  37. package/dist/src/peer-store/pb/peer.d.ts +222 -0
  38. package/dist/src/peer-store/proto-book.d.ts +27 -28
  39. package/dist/src/peer-store/proto-book.d.ts.map +1 -1
  40. package/dist/src/peer-store/store.d.ts +73 -0
  41. package/dist/src/peer-store/store.d.ts.map +1 -0
  42. package/dist/src/peer-store/types.d.ts +202 -0
  43. package/dist/src/peer-store/types.d.ts.map +1 -0
  44. package/dist/src/ping/index.d.ts.map +1 -1
  45. package/dist/src/record/peer-record/index.d.ts +2 -2
  46. package/dist/src/record/peer-record/index.d.ts.map +1 -1
  47. package/dist/src/registrar.d.ts +5 -5
  48. package/dist/src/registrar.d.ts.map +1 -1
  49. package/dist/src/upgrader.d.ts.map +1 -1
  50. package/package.json +24 -28
  51. package/src/circuit/auto-relay.js +52 -38
  52. package/src/connection-manager/auto-dialler.js +23 -9
  53. package/src/connection-manager/index.js +42 -22
  54. package/src/content-routing/utils.js +4 -4
  55. package/src/dht/dht-peer-routing.js +2 -6
  56. package/src/dialer/dial-request.js +4 -1
  57. package/src/dialer/index.js +22 -10
  58. package/src/fetch/README.md +36 -0
  59. package/src/fetch/constants.js +6 -0
  60. package/src/fetch/index.js +159 -0
  61. package/src/fetch/proto.d.ts +134 -0
  62. package/src/fetch/proto.js +333 -0
  63. package/src/fetch/proto.proto +15 -0
  64. package/src/identify/index.js +47 -38
  65. package/src/index.js +53 -27
  66. package/src/metrics/index.js +15 -8
  67. package/src/metrics/tracked-map.js +45 -13
  68. package/src/peer-routing.js +4 -1
  69. package/src/peer-store/address-book.js +242 -227
  70. package/src/peer-store/index.js +77 -110
  71. package/src/peer-store/key-book.js +99 -54
  72. package/src/peer-store/metadata-book.js +171 -101
  73. package/src/peer-store/pb/peer.d.ts +222 -0
  74. package/src/peer-store/pb/peer.js +643 -0
  75. package/src/peer-store/pb/peer.proto +31 -0
  76. package/src/peer-store/proto-book.js +171 -105
  77. package/src/peer-store/store.js +250 -0
  78. package/src/peer-store/types.ts +245 -0
  79. package/src/ping/index.js +6 -0
  80. package/src/record/peer-record/index.js +1 -1
  81. package/src/record/utils.js +1 -1
  82. package/src/registrar.js +4 -4
  83. package/src/upgrader.js +3 -2
  84. package/dist/src/peer-store/book.d.ts +0 -81
  85. package/dist/src/peer-store/book.d.ts.map +0 -1
  86. package/dist/src/peer-store/persistent/consts.d.ts +0 -6
  87. package/dist/src/peer-store/persistent/consts.d.ts.map +0 -1
  88. package/dist/src/peer-store/persistent/index.d.ts +0 -129
  89. package/dist/src/peer-store/persistent/index.d.ts.map +0 -1
  90. package/dist/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  91. package/dist/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  92. package/src/peer-store/book.js +0 -124
  93. package/src/peer-store/persistent/consts.js +0 -15
  94. package/src/peer-store/persistent/index.js +0 -408
  95. package/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  96. package/src/peer-store/persistent/pb/address-book.js +0 -522
  97. package/src/peer-store/persistent/pb/address-book.proto +0 -27
  98. package/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  99. package/src/peer-store/persistent/pb/proto-book.js +0 -157
  100. package/src/peer-store/persistent/pb/proto-book.proto +0 -5
@@ -77,8 +77,6 @@ class IdentifyService {
77
77
  ...libp2p._options.host
78
78
  }
79
79
 
80
- this.peerStore.metadataBook.set(this.peerId, 'AgentVersion', uint8ArrayFromString(this._host.agentVersion))
81
- this.peerStore.metadataBook.set(this.peerId, 'ProtocolVersion', uint8ArrayFromString(this._host.protocolVersion))
82
80
  // When a new connection happens, trigger identify
83
81
  this.connectionManager.on('peer:connect', (connection) => {
84
82
  this.identify(connection).catch(log.error)
@@ -87,18 +85,27 @@ class IdentifyService {
87
85
  // When self multiaddrs change, trigger identify-push
88
86
  this.peerStore.on('change:multiaddrs', ({ peerId }) => {
89
87
  if (peerId.toString() === this.peerId.toString()) {
90
- this.pushToPeerStore()
88
+ this.pushToPeerStore().catch(err => log.error(err))
91
89
  }
92
90
  })
93
91
 
94
92
  // When self protocols change, trigger identify-push
95
93
  this.peerStore.on('change:protocols', ({ peerId }) => {
96
94
  if (peerId.toString() === this.peerId.toString()) {
97
- this.pushToPeerStore()
95
+ this.pushToPeerStore().catch(err => log.error(err))
98
96
  }
99
97
  })
100
98
  }
101
99
 
100
+ async start () {
101
+ await this.peerStore.metadataBook.setValue(this.peerId, 'AgentVersion', uint8ArrayFromString(this._host.agentVersion))
102
+ await this.peerStore.metadataBook.setValue(this.peerId, 'ProtocolVersion', uint8ArrayFromString(this._host.protocolVersion))
103
+ }
104
+
105
+ async stop () {
106
+
107
+ }
108
+
102
109
  /**
103
110
  * Send an Identify Push update to the list of connections
104
111
  *
@@ -108,7 +115,7 @@ class IdentifyService {
108
115
  async push (connections) {
109
116
  const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
110
117
  const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.bytes)
111
- const protocols = this.peerStore.protoBook.get(this.peerId) || []
118
+ const protocols = await this.peerStore.protoBook.get(this.peerId)
112
119
 
113
120
  const pushes = connections.map(async connection => {
114
121
  try {
@@ -135,10 +142,8 @@ class IdentifyService {
135
142
 
136
143
  /**
137
144
  * Calls `push` for all peers in the `peerStore` that are connected
138
- *
139
- * @returns {void}
140
145
  */
141
- pushToPeerStore () {
146
+ async pushToPeerStore () {
142
147
  // Do not try to push if libp2p node is not running
143
148
  if (!this._libp2p.isStarted()) {
144
149
  return
@@ -146,13 +151,13 @@ class IdentifyService {
146
151
 
147
152
  const connections = []
148
153
  let connection
149
- for (const peer of this.peerStore.peers.values()) {
154
+ for await (const peer of this.peerStore.getPeers()) {
150
155
  if (peer.protocols.includes(this.identifyPushProtocolStr) && (connection = this.connectionManager.get(peer.id))) {
151
156
  connections.push(connection)
152
157
  }
153
158
  }
154
159
 
155
- this.push(connections)
160
+ await this.push(connections)
156
161
  }
157
162
 
158
163
  /**
@@ -205,10 +210,10 @@ class IdentifyService {
205
210
 
206
211
  try {
207
212
  const envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
208
- if (this.peerStore.addressBook.consumePeerRecord(envelope)) {
209
- this.peerStore.protoBook.set(id, protocols)
210
- this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
211
- this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
213
+ if (await this.peerStore.addressBook.consumePeerRecord(envelope)) {
214
+ await this.peerStore.protoBook.set(id, protocols)
215
+ await this.peerStore.metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
216
+ await this.peerStore.metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
212
217
  return
213
218
  }
214
219
  } catch (/** @type {any} */ err) {
@@ -217,14 +222,14 @@ class IdentifyService {
217
222
 
218
223
  // LEGACY: Update peers data in PeerStore
219
224
  try {
220
- this.peerStore.addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr)))
225
+ await this.peerStore.addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr)))
221
226
  } catch (/** @type {any} */ err) {
222
227
  log.error('received invalid addrs', err)
223
228
  }
224
229
 
225
- this.peerStore.protoBook.set(id, protocols)
226
- this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
227
- this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
230
+ await this.peerStore.protoBook.set(id, protocols)
231
+ await this.peerStore.metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
232
+ await this.peerStore.metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
228
233
 
229
234
  // TODO: Add and score our observed addr
230
235
  log('received observed address of %s', cleanObservedAddr)
@@ -262,25 +267,25 @@ class IdentifyService {
262
267
  * @returns {Promise<void>}
263
268
  */
264
269
  async _handleIdentify ({ connection, stream }) {
265
- let publicKey = new Uint8Array(0)
266
- if (this.peerId.pubKey) {
267
- publicKey = this.peerId.pubKey.bytes
268
- }
270
+ try {
271
+ let publicKey = new Uint8Array(0)
272
+ if (this.peerId.pubKey) {
273
+ publicKey = this.peerId.pubKey.bytes
274
+ }
269
275
 
270
- const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
271
- const protocols = this.peerStore.protoBook.get(this.peerId) || []
276
+ const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
277
+ const protocols = await this.peerStore.protoBook.get(this.peerId)
272
278
 
273
- const message = Message.Identify.encode({
274
- protocolVersion: this._host.protocolVersion,
275
- agentVersion: this._host.agentVersion,
276
- publicKey,
277
- listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
278
- signedPeerRecord,
279
- observedAddr: connection.remoteAddr.bytes,
280
- protocols
281
- }).finish()
279
+ const message = Message.Identify.encode({
280
+ protocolVersion: this._host.protocolVersion,
281
+ agentVersion: this._host.agentVersion,
282
+ publicKey,
283
+ listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
284
+ signedPeerRecord,
285
+ observedAddr: connection.remoteAddr.bytes,
286
+ protocols
287
+ }).finish()
282
288
 
283
- try {
284
289
  await pipe(
285
290
  [message],
286
291
  lp.encode(),
@@ -321,8 +326,8 @@ class IdentifyService {
321
326
 
322
327
  try {
323
328
  const envelope = await Envelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
324
- if (this.peerStore.addressBook.consumePeerRecord(envelope)) {
325
- this.peerStore.protoBook.set(id, message.protocols)
329
+ if (await this.peerStore.addressBook.consumePeerRecord(envelope)) {
330
+ await this.peerStore.protoBook.set(id, message.protocols)
326
331
  return
327
332
  }
328
333
  } catch (/** @type {any} */ err) {
@@ -331,14 +336,18 @@ class IdentifyService {
331
336
 
332
337
  // LEGACY: Update peers data in PeerStore
333
338
  try {
334
- this.peerStore.addressBook.set(id,
339
+ await this.peerStore.addressBook.set(id,
335
340
  message.listenAddrs.map((addr) => new Multiaddr(addr)))
336
341
  } catch (/** @type {any} */ err) {
337
342
  log.error('received invalid addrs', err)
338
343
  }
339
344
 
340
345
  // Update the protocols
341
- this.peerStore.protoBook.set(id, message.protocols)
346
+ try {
347
+ await this.peerStore.protoBook.set(id, message.protocols)
348
+ } catch (/** @type {any} */ err) {
349
+ log.error('received invalid protocols', err)
350
+ }
342
351
  }
343
352
 
344
353
  /**
package/src/index.js CHANGED
@@ -9,7 +9,7 @@ const { EventEmitter } = require('events')
9
9
  const errCode = require('err-code')
10
10
  const PeerId = require('peer-id')
11
11
  const { Multiaddr } = require('multiaddr')
12
-
12
+ const { MemoryDatastore } = require('datastore-core/memory')
13
13
  const PeerRouting = require('./peer-routing')
14
14
  const ContentRouting = require('./content-routing')
15
15
  const getPeer = require('./get-peer')
@@ -28,10 +28,10 @@ const TransportManager = require('./transport-manager')
28
28
  const Upgrader = require('./upgrader')
29
29
  const PeerStore = require('./peer-store')
30
30
  const PubsubAdapter = require('./pubsub-adapter')
31
- const PersistentPeerStore = require('./peer-store/persistent')
32
31
  const Registrar = require('./registrar')
33
32
  const ping = require('./ping')
34
33
  const IdentifyService = require('./identify')
34
+ const FetchService = require('./fetch')
35
35
  const NatManager = require('./nat-manager')
36
36
  const { updateSelfPeerRecord } = require('./record/utils')
37
37
 
@@ -112,7 +112,7 @@ const { updateSelfPeerRecord } = require('./record/utils')
112
112
  * @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
113
113
  * @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
114
114
  * @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
115
- * @property {PeerStoreOptions & PersistentPeerStoreOptions} [peerStore]
115
+ * @property {PeerStoreOptions} [peerStore]
116
116
  * @property {import('./transport-manager').TransportManagerOptions} [transportManager]
117
117
  * @property {Libp2pConfig} [config]
118
118
  *
@@ -172,13 +172,11 @@ class Libp2p extends EventEmitter {
172
172
  this.metrics = metrics
173
173
  }
174
174
 
175
- this.peerStore = (this.datastore && this._options.peerStore.persistence)
176
- ? new PersistentPeerStore({
177
- peerId: this.peerId,
178
- datastore: this.datastore,
179
- ...this._options.peerStore
180
- })
181
- : new PeerStore({ peerId: this.peerId })
175
+ /** @type {import('./peer-store/types').PeerStore} */
176
+ this.peerStore = new PeerStore({
177
+ peerId: this.peerId,
178
+ datastore: (this.datastore && this._options.peerStore.persistence) ? this.datastore : new MemoryDatastore()
179
+ })
182
180
 
183
181
  // Addresses {listen, announce, noAnnounce}
184
182
  this.addresses = this._options.addresses
@@ -290,7 +288,6 @@ class Libp2p extends EventEmitter {
290
288
 
291
289
  // Add the identify service since we can multiplex
292
290
  this.identifyService = new IdentifyService({ libp2p: this })
293
- this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
294
291
  }
295
292
 
296
293
  // Attach private network protector
@@ -327,6 +324,8 @@ class Libp2p extends EventEmitter {
327
324
  ping.mount(this)
328
325
 
329
326
  this._onDiscoveryPeer = this._onDiscoveryPeer.bind(this)
327
+
328
+ this.fetchService = new FetchService(this)
330
329
  }
331
330
 
332
331
  /**
@@ -356,6 +355,14 @@ class Libp2p extends EventEmitter {
356
355
  async start () {
357
356
  log('libp2p is starting')
358
357
 
358
+ if (this.identifyService) {
359
+ await this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
360
+ }
361
+
362
+ if (this.fetchService) {
363
+ await this.handle(FetchService.PROTOCOL, this.fetchService.handleMessage)
364
+ }
365
+
359
366
  try {
360
367
  await this._onStarting()
361
368
  await this._onDidStart()
@@ -380,9 +387,13 @@ class Libp2p extends EventEmitter {
380
387
  try {
381
388
  this._isStarted = false
382
389
 
390
+ if (this.identifyService) {
391
+ await this.identifyService.stop()
392
+ }
393
+
383
394
  this.relay && this.relay.stop()
384
395
  this.peerRouting.stop()
385
- this._autodialler.stop()
396
+ await this._autodialler.stop()
386
397
  await (this._dht && this._dht.stop())
387
398
 
388
399
  for (const service of this._discovery.values()) {
@@ -393,7 +404,6 @@ class Libp2p extends EventEmitter {
393
404
 
394
405
  this._discovery = new Map()
395
406
 
396
- await this.peerStore.stop()
397
407
  await this.connectionManager.stop()
398
408
 
399
409
  await Promise.all([
@@ -404,6 +414,8 @@ class Libp2p extends EventEmitter {
404
414
  await this.natManager.stop()
405
415
  await this.transportManager.close()
406
416
 
417
+ this.unhandle(FetchService.PROTOCOL)
418
+
407
419
  ping.unmount(this)
408
420
  this.dialer.destroy()
409
421
  } catch (/** @type {any} */ err) {
@@ -499,7 +511,7 @@ class Libp2p extends EventEmitter {
499
511
  if (!connection) {
500
512
  connection = await this.dialer.connectToPeer(peer, options)
501
513
  } else if (multiaddrs) {
502
- this.peerStore.addressBook.add(id, multiaddrs)
514
+ await this.peerStore.addressBook.add(id, multiaddrs)
503
515
  }
504
516
 
505
517
  return connection
@@ -556,6 +568,17 @@ class Libp2p extends EventEmitter {
556
568
  )
557
569
  }
558
570
 
571
+ /**
572
+ * Sends a request to fetch the value associated with the given key from the given peer.
573
+ *
574
+ * @param {PeerId|Multiaddr} peer
575
+ * @param {string} key
576
+ * @returns {Promise<Uint8Array | null>}
577
+ */
578
+ fetch (peer, key) {
579
+ return this.fetchService.fetch(peer, key)
580
+ }
581
+
559
582
  /**
560
583
  * Pings the given peer in order to obtain the operation latency.
561
584
  *
@@ -579,14 +602,14 @@ class Libp2p extends EventEmitter {
579
602
  * @param {string[]|string} protocols
580
603
  * @param {(props: HandlerProps) => void} handler
581
604
  */
582
- handle (protocols, handler) {
605
+ async handle (protocols, handler) {
583
606
  protocols = Array.isArray(protocols) ? protocols : [protocols]
584
607
  protocols.forEach(protocol => {
585
608
  this.upgrader.protocols.set(protocol, handler)
586
609
  })
587
610
 
588
611
  // Add new protocols to self protocols in the Protobook
589
- this.peerStore.protoBook.add(this.peerId, protocols)
612
+ await this.peerStore.protoBook.add(this.peerId, protocols)
590
613
  }
591
614
 
592
615
  /**
@@ -595,14 +618,14 @@ class Libp2p extends EventEmitter {
595
618
  *
596
619
  * @param {string[]|string} protocols
597
620
  */
598
- unhandle (protocols) {
621
+ async unhandle (protocols) {
599
622
  protocols = Array.isArray(protocols) ? protocols : [protocols]
600
623
  protocols.forEach(protocol => {
601
624
  this.upgrader.protocols.delete(protocol)
602
625
  })
603
626
 
604
627
  // Remove protocols from self protocols in the Protobook
605
- this.peerStore.protoBook.remove(this.peerId, protocols)
628
+ await this.peerStore.protoBook.remove(this.peerId, protocols)
606
629
  }
607
630
 
608
631
  async _onStarting () {
@@ -613,11 +636,8 @@ class Libp2p extends EventEmitter {
613
636
  // Manage your NATs
614
637
  this.natManager.start()
615
638
 
616
- // Start PeerStore
617
- await this.peerStore.start()
618
-
619
639
  if (this._config.pubsub.enabled) {
620
- this.pubsub && this.pubsub.start()
640
+ this.pubsub && await this.pubsub.start()
621
641
  }
622
642
 
623
643
  // DHT subsystem
@@ -631,6 +651,10 @@ class Libp2p extends EventEmitter {
631
651
 
632
652
  // Start metrics if present
633
653
  this.metrics && this.metrics.start()
654
+
655
+ if (this.identifyService) {
656
+ await this.identifyService.start()
657
+ }
634
658
  }
635
659
 
636
660
  /**
@@ -643,17 +667,19 @@ class Libp2p extends EventEmitter {
643
667
 
644
668
  this.peerStore.on('peer', peerId => {
645
669
  this.emit('peer:discovery', peerId)
646
- this._maybeConnect(peerId)
670
+ this._maybeConnect(peerId).catch(err => {
671
+ log.error(err)
672
+ })
647
673
  })
648
674
 
649
675
  // Once we start, emit any peers we may have already discovered
650
676
  // TODO: this should be removed, as we already discovered these peers in the past
651
- for (const peer of this.peerStore.peers.values()) {
677
+ for await (const peer of this.peerStore.getPeers()) {
652
678
  this.emit('peer:discovery', peer.id)
653
679
  }
654
680
 
655
681
  this.connectionManager.start()
656
- this._autodialler.start()
682
+ await this._autodialler.start()
657
683
 
658
684
  // Peer discovery
659
685
  await this._setupPeerDiscovery()
@@ -677,8 +703,8 @@ class Libp2p extends EventEmitter {
677
703
  return
678
704
  }
679
705
 
680
- peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs)
681
- peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols)
706
+ peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
707
+ peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
682
708
  }
683
709
 
684
710
  /**
@@ -44,7 +44,7 @@ class Metrics {
44
44
  this._oldPeers = oldPeerLRU(this._options.maxOldPeersRetention)
45
45
  this._running = false
46
46
  this._onMessage = this._onMessage.bind(this)
47
- this._componentMetrics = new Map()
47
+ this._systems = new Map()
48
48
  }
49
49
 
50
50
  /**
@@ -89,19 +89,26 @@ class Metrics {
89
89
  }
90
90
 
91
91
  /**
92
- * @returns {Map}
92
+ * @returns {Map<string, Map<string, Map<string, any>>>}
93
93
  */
94
94
  getComponentMetrics () {
95
- return this._componentMetrics
95
+ return this._systems
96
96
  }
97
97
 
98
- updateComponentMetric (component, metric, value) {
99
- if (!this._componentMetrics.has(component)) {
100
- this._componentMetrics.set(component, new Map())
98
+ updateComponentMetric ({ system = 'libp2p', component, metric, value }) {
99
+ if (!this._systems.has(system)) {
100
+ this._systems.set(system, new Map())
101
101
  }
102
102
 
103
- const map = this._componentMetrics.get(component)
104
- map.set(metric, value)
103
+ const systemMetrics = this._systems.get(system)
104
+
105
+ if (!systemMetrics.has(component)) {
106
+ systemMetrics.set(component, new Map())
107
+ }
108
+
109
+ const componentMetrics = systemMetrics.get(component)
110
+
111
+ componentMetrics.set(metric, value)
105
112
  }
106
113
 
107
114
  /**
@@ -6,18 +6,27 @@
6
6
  */
7
7
  class TrackedMap extends Map {
8
8
  /**
9
- * @param {string} component
10
- * @param {string} name
11
- * @param {import('.')} metrics
9
+ * @param {object} options
10
+ * @param {string} options.system
11
+ * @param {string} options.component
12
+ * @param {string} options.metric
13
+ * @param {import('.')} options.metrics
12
14
  */
13
- constructor (component, name, metrics) {
15
+ constructor (options) {
14
16
  super()
15
17
 
18
+ const { system, component, metric, metrics } = options
19
+ this._system = system
16
20
  this._component = component
17
- this._name = name
21
+ this._metric = metric
18
22
  this._metrics = metrics
19
23
 
20
- this._metrics.updateComponentMetric(this._component, this._name, this.size)
24
+ this._metrics.updateComponentMetric({
25
+ system: this._system,
26
+ component: this._component,
27
+ metric: this._metric,
28
+ value: this.size
29
+ })
21
30
  }
22
31
 
23
32
  /**
@@ -26,7 +35,12 @@ class TrackedMap extends Map {
26
35
  */
27
36
  set (key, value) {
28
37
  super.set(key, value)
29
- this._metrics.updateComponentMetric(this._component, this._name, this.size)
38
+ this._metrics.updateComponentMetric({
39
+ system: this._system,
40
+ component: this._component,
41
+ metric: this._metric,
42
+ value: this.size
43
+ })
30
44
  return this
31
45
  }
32
46
 
@@ -35,25 +49,43 @@ class TrackedMap extends Map {
35
49
  */
36
50
  delete (key) {
37
51
  const deleted = super.delete(key)
38
- this._metrics.updateComponentMetric(this._component, this._name, this.size)
52
+ this._metrics.updateComponentMetric({
53
+ system: this._system,
54
+ component: this._component,
55
+ metric: this._metric,
56
+ value: this.size
57
+ })
39
58
  return deleted
40
59
  }
60
+
61
+ clear () {
62
+ super.clear()
63
+
64
+ this._metrics.updateComponentMetric({
65
+ system: this._system,
66
+ component: this._component,
67
+ metric: this._metric,
68
+ value: this.size
69
+ })
70
+ }
41
71
  }
42
72
 
43
73
  /**
44
74
  * @template K
45
75
  * @template V
46
- * @param {string} component
47
- * @param {string} name
48
- * @param {import('.')} [metrics]
76
+ * @param {object} options
77
+ * @param {string} [options.system]
78
+ * @param {string} options.component
79
+ * @param {string} options.metric
80
+ * @param {import('.')} [options.metrics]
49
81
  * @returns {Map<K, V>}
50
82
  */
51
- module.exports = (component, name, metrics) => {
83
+ module.exports = ({ system = 'libp2p', component, metric, metrics }) => {
52
84
  /** @type {Map<K, V>} */
53
85
  let map
54
86
 
55
87
  if (metrics) {
56
- map = new TrackedMap(component, name, metrics)
88
+ map = new TrackedMap({ system, component, metric, metrics })
57
89
  } else {
58
90
  map = new Map()
59
91
  }
@@ -154,7 +154,10 @@ class PeerRouting {
154
154
  const controller = new TimeoutController(options.timeout)
155
155
  // this controller will potentially be used while dialing lots of
156
156
  // peers so prevent MaxListenersExceededWarning appearing in the console
157
- setMaxListeners && setMaxListeners(Infinity, controller.signal)
157
+ try {
158
+ // fails on node < 15.4
159
+ setMaxListeners && setMaxListeners(Infinity, controller.signal)
160
+ } catch {}
158
161
 
159
162
  options.signal = controller.signal
160
163
  }