libp2p 0.35.4 → 0.35.8-bf1fc32

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 (93) hide show
  1. package/README.md +2 -2
  2. package/dist/index.min.js +55 -0
  3. package/dist/src/circuit/auto-relay.d.ts +8 -10
  4. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  5. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  6. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  7. package/dist/src/connection-manager/index.d.ts +1 -2
  8. package/dist/src/connection-manager/index.d.ts.map +1 -1
  9. package/dist/src/content-routing/utils.d.ts +3 -3
  10. package/dist/src/content-routing/utils.d.ts.map +1 -1
  11. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  12. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  13. package/dist/src/dialer/index.d.ts +20 -16
  14. package/dist/src/dialer/index.d.ts.map +1 -1
  15. package/dist/src/identify/index.d.ts +4 -4
  16. package/dist/src/identify/index.d.ts.map +1 -1
  17. package/dist/src/index.d.ts +14 -9
  18. package/dist/src/index.d.ts.map +1 -1
  19. package/dist/src/metrics/index.d.ts +9 -4
  20. package/dist/src/metrics/index.d.ts.map +1 -1
  21. package/dist/src/metrics/stats.d.ts +3 -1
  22. package/dist/src/metrics/stats.d.ts.map +1 -1
  23. package/dist/src/metrics/tracked-map.d.ts +8 -0
  24. package/dist/src/metrics/tracked-map.d.ts.map +1 -0
  25. package/dist/src/peer-routing.d.ts +1 -1
  26. package/dist/src/peer-routing.d.ts.map +1 -1
  27. package/dist/src/peer-store/address-book.d.ts +28 -84
  28. package/dist/src/peer-store/address-book.d.ts.map +1 -1
  29. package/dist/src/peer-store/index.d.ts +25 -94
  30. package/dist/src/peer-store/index.d.ts.map +1 -1
  31. package/dist/src/peer-store/key-book.d.ts +31 -15
  32. package/dist/src/peer-store/key-book.d.ts.map +1 -1
  33. package/dist/src/peer-store/metadata-book.d.ts +33 -29
  34. package/dist/src/peer-store/metadata-book.d.ts.map +1 -1
  35. package/dist/src/peer-store/pb/peer.d.ts +222 -0
  36. package/dist/src/peer-store/proto-book.d.ts +27 -28
  37. package/dist/src/peer-store/proto-book.d.ts.map +1 -1
  38. package/dist/src/peer-store/store.d.ts +73 -0
  39. package/dist/src/peer-store/store.d.ts.map +1 -0
  40. package/dist/src/peer-store/types.d.ts +202 -0
  41. package/dist/src/peer-store/types.d.ts.map +1 -0
  42. package/dist/src/ping/index.d.ts.map +1 -1
  43. package/dist/src/record/peer-record/index.d.ts +2 -2
  44. package/dist/src/record/peer-record/index.d.ts.map +1 -1
  45. package/dist/src/registrar.d.ts +5 -5
  46. package/dist/src/registrar.d.ts.map +1 -1
  47. package/dist/src/upgrader.d.ts.map +1 -1
  48. package/package.json +26 -30
  49. package/src/circuit/auto-relay.js +19 -19
  50. package/src/connection-manager/auto-dialler.js +9 -4
  51. package/src/connection-manager/index.js +41 -31
  52. package/src/content-routing/utils.js +4 -4
  53. package/src/dht/dht-peer-routing.js +2 -6
  54. package/src/dialer/dial-request.js +4 -1
  55. package/src/dialer/index.js +33 -21
  56. package/src/identify/index.js +28 -23
  57. package/src/index.js +43 -35
  58. package/src/metrics/index.js +15 -8
  59. package/src/metrics/stats.js +1 -0
  60. package/src/metrics/tracked-map.js +94 -0
  61. package/src/peer-routing.js +11 -1
  62. package/src/peer-store/address-book.js +242 -227
  63. package/src/peer-store/index.js +77 -110
  64. package/src/peer-store/key-book.js +99 -54
  65. package/src/peer-store/metadata-book.js +171 -101
  66. package/src/peer-store/pb/peer.d.ts +222 -0
  67. package/src/peer-store/pb/peer.js +643 -0
  68. package/src/peer-store/pb/peer.proto +31 -0
  69. package/src/peer-store/proto-book.js +171 -105
  70. package/src/peer-store/store.js +250 -0
  71. package/src/peer-store/types.ts +245 -0
  72. package/src/ping/index.js +6 -0
  73. package/src/record/peer-record/index.js +1 -1
  74. package/src/record/utils.js +1 -1
  75. package/src/registrar.js +4 -4
  76. package/src/upgrader.js +3 -1
  77. package/dist/src/peer-store/book.d.ts +0 -81
  78. package/dist/src/peer-store/book.d.ts.map +0 -1
  79. package/dist/src/peer-store/persistent/consts.d.ts +0 -6
  80. package/dist/src/peer-store/persistent/consts.d.ts.map +0 -1
  81. package/dist/src/peer-store/persistent/index.d.ts +0 -129
  82. package/dist/src/peer-store/persistent/index.d.ts.map +0 -1
  83. package/dist/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  84. package/dist/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  85. package/src/peer-store/book.js +0 -124
  86. package/src/peer-store/persistent/consts.js +0 -15
  87. package/src/peer-store/persistent/index.js +0 -408
  88. package/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  89. package/src/peer-store/persistent/pb/address-book.js +0 -522
  90. package/src/peer-store/persistent/pb/address-book.proto +0 -27
  91. package/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  92. package/src/peer-store/persistent/pb/proto-book.js +0 -157
  93. package/src/peer-store/persistent/pb/proto-book.proto +0 -5
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,7 +28,6 @@ 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')
@@ -48,6 +47,8 @@ const { updateSelfPeerRecord } = require('./record/utils')
48
47
  * @typedef {import('libp2p-interfaces/src/pubsub').PubsubOptions} PubsubOptions
49
48
  * @typedef {import('interface-datastore').Datastore} Datastore
50
49
  * @typedef {import('./pnet')} Protector
50
+ * @typedef {Object} PersistentPeerStoreOptions
51
+ * @property {number} [threshold]
51
52
  */
52
53
 
53
54
  /**
@@ -110,7 +111,7 @@ const { updateSelfPeerRecord } = require('./record/utils')
110
111
  * @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
111
112
  * @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
112
113
  * @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
113
- * @property {PeerStoreOptions & import('./peer-store/persistent').PersistentPeerStoreOptions} [peerStore]
114
+ * @property {PeerStoreOptions} [peerStore]
114
115
  * @property {import('./transport-manager').TransportManagerOptions} [transportManager]
115
116
  * @property {Libp2pConfig} [config]
116
117
  *
@@ -161,13 +162,20 @@ class Libp2p extends EventEmitter {
161
162
  this.peerId = this._options.peerId
162
163
  this.datastore = this._options.datastore
163
164
 
164
- this.peerStore = (this.datastore && this._options.peerStore.persistence)
165
- ? new PersistentPeerStore({
166
- peerId: this.peerId,
167
- datastore: this.datastore,
168
- ...this._options.peerStore
165
+ // Create Metrics
166
+ if (this._options.metrics.enabled) {
167
+ const metrics = new Metrics({
168
+ ...this._options.metrics
169
169
  })
170
- : new PeerStore({ peerId: this.peerId })
170
+
171
+ this.metrics = metrics
172
+ }
173
+
174
+ /** @type {import('./peer-store/types').PeerStore} */
175
+ this.peerStore = new PeerStore({
176
+ peerId: this.peerId,
177
+ datastore: (this.datastore && this._options.peerStore.persistence) ? this.datastore : new MemoryDatastore()
178
+ })
171
179
 
172
180
  // Addresses {listen, announce, noAnnounce}
173
181
  this.addresses = this._options.addresses
@@ -195,15 +203,6 @@ class Libp2p extends EventEmitter {
195
203
  autoDialInterval: this._options.connectionManager.autoDialInterval
196
204
  })
197
205
 
198
- // Create Metrics
199
- if (this._options.metrics.enabled) {
200
- const metrics = new Metrics({
201
- ...this._options.metrics
202
- })
203
-
204
- this.metrics = metrics
205
- }
206
-
207
206
  // Create keychain
208
207
  if (this._options.keychain && this._options.keychain.datastore) {
209
208
  log('creating keychain')
@@ -288,7 +287,6 @@ class Libp2p extends EventEmitter {
288
287
 
289
288
  // Add the identify service since we can multiplex
290
289
  this.identifyService = new IdentifyService({ libp2p: this })
291
- this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
292
290
  }
293
291
 
294
292
  // Attach private network protector
@@ -354,6 +352,10 @@ class Libp2p extends EventEmitter {
354
352
  async start () {
355
353
  log('libp2p is starting')
356
354
 
355
+ if (this.identifyService) {
356
+ await this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
357
+ }
358
+
357
359
  try {
358
360
  await this._onStarting()
359
361
  await this._onDidStart()
@@ -378,9 +380,13 @@ class Libp2p extends EventEmitter {
378
380
  try {
379
381
  this._isStarted = false
380
382
 
383
+ if (this.identifyService) {
384
+ await this.identifyService.stop()
385
+ }
386
+
381
387
  this.relay && this.relay.stop()
382
388
  this.peerRouting.stop()
383
- this._autodialler.stop()
389
+ await this._autodialler.stop()
384
390
  await (this._dht && this._dht.stop())
385
391
 
386
392
  for (const service of this._discovery.values()) {
@@ -391,7 +397,6 @@ class Libp2p extends EventEmitter {
391
397
 
392
398
  this._discovery = new Map()
393
399
 
394
- await this.peerStore.stop()
395
400
  await this.connectionManager.stop()
396
401
 
397
402
  await Promise.all([
@@ -497,7 +502,7 @@ class Libp2p extends EventEmitter {
497
502
  if (!connection) {
498
503
  connection = await this.dialer.connectToPeer(peer, options)
499
504
  } else if (multiaddrs) {
500
- this.peerStore.addressBook.add(id, multiaddrs)
505
+ await this.peerStore.addressBook.add(id, multiaddrs)
501
506
  }
502
507
 
503
508
  return connection
@@ -577,14 +582,14 @@ class Libp2p extends EventEmitter {
577
582
  * @param {string[]|string} protocols
578
583
  * @param {(props: HandlerProps) => void} handler
579
584
  */
580
- handle (protocols, handler) {
585
+ async handle (protocols, handler) {
581
586
  protocols = Array.isArray(protocols) ? protocols : [protocols]
582
587
  protocols.forEach(protocol => {
583
588
  this.upgrader.protocols.set(protocol, handler)
584
589
  })
585
590
 
586
591
  // Add new protocols to self protocols in the Protobook
587
- this.peerStore.protoBook.add(this.peerId, protocols)
592
+ await this.peerStore.protoBook.add(this.peerId, protocols)
588
593
  }
589
594
 
590
595
  /**
@@ -593,14 +598,14 @@ class Libp2p extends EventEmitter {
593
598
  *
594
599
  * @param {string[]|string} protocols
595
600
  */
596
- unhandle (protocols) {
601
+ async unhandle (protocols) {
597
602
  protocols = Array.isArray(protocols) ? protocols : [protocols]
598
603
  protocols.forEach(protocol => {
599
604
  this.upgrader.protocols.delete(protocol)
600
605
  })
601
606
 
602
607
  // Remove protocols from self protocols in the Protobook
603
- this.peerStore.protoBook.remove(this.peerId, protocols)
608
+ await this.peerStore.protoBook.remove(this.peerId, protocols)
604
609
  }
605
610
 
606
611
  async _onStarting () {
@@ -611,11 +616,8 @@ class Libp2p extends EventEmitter {
611
616
  // Manage your NATs
612
617
  this.natManager.start()
613
618
 
614
- // Start PeerStore
615
- await this.peerStore.start()
616
-
617
619
  if (this._config.pubsub.enabled) {
618
- this.pubsub && this.pubsub.start()
620
+ this.pubsub && await this.pubsub.start()
619
621
  }
620
622
 
621
623
  // DHT subsystem
@@ -629,6 +631,10 @@ class Libp2p extends EventEmitter {
629
631
 
630
632
  // Start metrics if present
631
633
  this.metrics && this.metrics.start()
634
+
635
+ if (this.identifyService) {
636
+ await this.identifyService.start()
637
+ }
632
638
  }
633
639
 
634
640
  /**
@@ -641,17 +647,19 @@ class Libp2p extends EventEmitter {
641
647
 
642
648
  this.peerStore.on('peer', peerId => {
643
649
  this.emit('peer:discovery', peerId)
644
- this._maybeConnect(peerId)
650
+ this._maybeConnect(peerId).catch(err => {
651
+ log.error(err)
652
+ })
645
653
  })
646
654
 
647
655
  // Once we start, emit any peers we may have already discovered
648
656
  // TODO: this should be removed, as we already discovered these peers in the past
649
- for (const peer of this.peerStore.peers.values()) {
657
+ for await (const peer of this.peerStore.getPeers()) {
650
658
  this.emit('peer:discovery', peer.id)
651
659
  }
652
660
 
653
661
  this.connectionManager.start()
654
- this._autodialler.start()
662
+ await this._autodialler.start()
655
663
 
656
664
  // Peer discovery
657
665
  await this._setupPeerDiscovery()
@@ -675,8 +683,8 @@ class Libp2p extends EventEmitter {
675
683
  return
676
684
  }
677
685
 
678
- peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs)
679
- peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols)
686
+ peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
687
+ peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
680
688
  }
681
689
 
682
690
  /**
@@ -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
  /**
@@ -8,6 +8,7 @@ const retimer = require('retimer')
8
8
 
9
9
  /**
10
10
  * @typedef {import('@vascosantos/moving-average').IMovingAverage} IMovingAverage
11
+ * @typedef {import('bignumber.js').BigNumber} Big
11
12
  */
12
13
 
13
14
  class Stats extends EventEmitter {
@@ -0,0 +1,94 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * @template K
5
+ * @template V
6
+ */
7
+ class TrackedMap extends Map {
8
+ /**
9
+ * @param {object} options
10
+ * @param {string} options.system
11
+ * @param {string} options.component
12
+ * @param {string} options.metric
13
+ * @param {import('.')} options.metrics
14
+ */
15
+ constructor (options) {
16
+ super()
17
+
18
+ const { system, component, metric, metrics } = options
19
+ this._system = system
20
+ this._component = component
21
+ this._metric = metric
22
+ this._metrics = metrics
23
+
24
+ this._metrics.updateComponentMetric({
25
+ system: this._system,
26
+ component: this._component,
27
+ metric: this._metric,
28
+ value: this.size
29
+ })
30
+ }
31
+
32
+ /**
33
+ * @param {K} key
34
+ * @param {V} value
35
+ */
36
+ set (key, value) {
37
+ super.set(key, value)
38
+ this._metrics.updateComponentMetric({
39
+ system: this._system,
40
+ component: this._component,
41
+ metric: this._metric,
42
+ value: this.size
43
+ })
44
+ return this
45
+ }
46
+
47
+ /**
48
+ * @param {K} key
49
+ */
50
+ delete (key) {
51
+ const deleted = super.delete(key)
52
+ this._metrics.updateComponentMetric({
53
+ system: this._system,
54
+ component: this._component,
55
+ metric: this._metric,
56
+ value: this.size
57
+ })
58
+ return deleted
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
+ }
71
+ }
72
+
73
+ /**
74
+ * @template K
75
+ * @template V
76
+ * @param {object} options
77
+ * @param {string} [options.system]
78
+ * @param {string} options.component
79
+ * @param {string} options.metric
80
+ * @param {import('.')} [options.metrics]
81
+ * @returns {Map<K, V>}
82
+ */
83
+ module.exports = ({ system = 'libp2p', component, metric, metrics }) => {
84
+ /** @type {Map<K, V>} */
85
+ let map
86
+
87
+ if (metrics) {
88
+ map = new TrackedMap({ system, component, metric, metrics })
89
+ } else {
90
+ map = new Map()
91
+ }
92
+
93
+ return map
94
+ }
@@ -24,6 +24,8 @@ const {
24
24
  // @ts-ignore module with no types
25
25
  } = require('set-delayed-interval')
26
26
  const { DHTPeerRouting } = require('./dht/dht-peer-routing')
27
+ // @ts-expect-error setMaxListeners is missing from the types
28
+ const { setMaxListeners } = require('events')
27
29
 
28
30
  /**
29
31
  * @typedef {import('peer-id')} PeerId
@@ -149,7 +151,15 @@ class PeerRouting {
149
151
  }
150
152
 
151
153
  if (options.timeout) {
152
- options.signal = new TimeoutController(options.timeout).signal
154
+ const controller = new TimeoutController(options.timeout)
155
+ // this controller will potentially be used while dialing lots of
156
+ // peers so prevent MaxListenersExceededWarning appearing in the console
157
+ try {
158
+ // fails on node < 15.4
159
+ setMaxListeners && setMaxListeners(Infinity, controller.signal)
160
+ } catch {}
161
+
162
+ options.signal = controller.signal
153
163
  }
154
164
 
155
165
  yield * pipe(