libp2p 0.35.5 → 0.35.9-rc.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.
- package/README.md +2 -2
- package/dist/src/circuit/auto-relay.d.ts +8 -10
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/index.d.ts +1 -2
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/content-routing/utils.d.ts +3 -3
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
- package/dist/src/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/dialer/index.d.ts +20 -16
- package/dist/src/dialer/index.d.ts.map +1 -1
- package/dist/src/identify/index.d.ts +4 -4
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/index.d.ts +14 -9
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/metrics/stats.d.ts +3 -1
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/tracked-map.d.ts +3 -0
- package/dist/src/metrics/tracked-map.d.ts.map +1 -0
- package/dist/src/peer-routing.d.ts +1 -1
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-store/address-book.d.ts +28 -84
- package/dist/src/peer-store/address-book.d.ts.map +1 -1
- package/dist/src/peer-store/index.d.ts +25 -94
- package/dist/src/peer-store/index.d.ts.map +1 -1
- package/dist/src/peer-store/key-book.d.ts +31 -15
- package/dist/src/peer-store/key-book.d.ts.map +1 -1
- package/dist/src/peer-store/metadata-book.d.ts +33 -29
- package/dist/src/peer-store/metadata-book.d.ts.map +1 -1
- package/dist/src/peer-store/pb/peer.d.ts +222 -0
- package/dist/src/peer-store/proto-book.d.ts +28 -29
- package/dist/src/peer-store/proto-book.d.ts.map +1 -1
- package/dist/src/peer-store/store.d.ts +46 -0
- package/dist/src/peer-store/store.d.ts.map +1 -0
- package/dist/src/peer-store/types.d.ts +199 -0
- package/dist/src/peer-store/types.d.ts.map +1 -0
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/record/peer-record/index.d.ts +2 -2
- package/dist/src/record/peer-record/index.d.ts.map +1 -1
- package/dist/src/registrar.d.ts +5 -5
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/package.json +21 -23
- package/src/circuit/auto-relay.js +19 -19
- package/src/connection-manager/auto-dialler.js +9 -4
- package/src/connection-manager/index.js +33 -31
- package/src/content-routing/utils.js +4 -4
- package/src/dht/dht-peer-routing.js +2 -6
- package/src/dialer/dial-request.js +4 -1
- package/src/dialer/index.js +24 -20
- package/src/identify/index.js +28 -23
- package/src/index.js +43 -35
- package/src/metrics/stats.js +1 -0
- package/src/metrics/tracked-map.js +68 -0
- package/src/peer-routing.js +11 -1
- package/src/peer-store/address-book.js +238 -227
- package/src/peer-store/index.js +77 -110
- package/src/peer-store/key-book.js +99 -54
- package/src/peer-store/metadata-book.js +171 -101
- package/src/peer-store/pb/peer.d.ts +222 -0
- package/src/peer-store/pb/peer.js +643 -0
- package/src/peer-store/pb/peer.proto +31 -0
- package/src/peer-store/proto-book.js +171 -105
- package/src/peer-store/store.js +250 -0
- package/src/peer-store/types.ts +245 -0
- package/src/ping/index.js +6 -0
- package/src/record/peer-record/index.js +1 -1
- package/src/record/utils.js +1 -1
- package/src/registrar.js +4 -4
- package/src/upgrader.js +3 -1
- package/dist/src/peer-store/book.d.ts +0 -81
- package/dist/src/peer-store/book.d.ts.map +0 -1
- package/dist/src/peer-store/persistent/consts.d.ts +0 -6
- package/dist/src/peer-store/persistent/consts.d.ts.map +0 -1
- package/dist/src/peer-store/persistent/index.d.ts +0 -129
- package/dist/src/peer-store/persistent/index.d.ts.map +0 -1
- package/dist/src/peer-store/persistent/pb/address-book.d.ts +0 -198
- package/dist/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
- package/src/peer-store/book.js +0 -124
- package/src/peer-store/persistent/consts.js +0 -15
- package/src/peer-store/persistent/index.js +0 -408
- package/src/peer-store/persistent/pb/address-book.d.ts +0 -198
- package/src/peer-store/persistent/pb/address-book.js +0 -522
- package/src/peer-store/persistent/pb/address-book.proto +0 -27
- package/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
- package/src/peer-store/persistent/pb/proto-book.js +0 -157
- package/src/peer-store/persistent/pb/proto-book.proto +0 -5
|
@@ -12,7 +12,7 @@ const LatencyMonitor = require('./latency-monitor')
|
|
|
12
12
|
const retimer = require('retimer')
|
|
13
13
|
|
|
14
14
|
const { EventEmitter } = require('events')
|
|
15
|
-
|
|
15
|
+
const trackedMap = require('../metrics/tracked-map')
|
|
16
16
|
const PeerId = require('peer-id')
|
|
17
17
|
|
|
18
18
|
const {
|
|
@@ -34,7 +34,7 @@ const defaultOptions = {
|
|
|
34
34
|
|
|
35
35
|
const METRICS_COMPONENT = 'connection-manager'
|
|
36
36
|
const METRICS_PEER_CONNECTIONS = 'peer-connections'
|
|
37
|
-
const
|
|
37
|
+
const METRICS_PEER_VALUES = 'peer-values'
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
40
|
* @typedef {import('../')} Libp2p
|
|
@@ -87,14 +87,14 @@ class ConnectionManager extends EventEmitter {
|
|
|
87
87
|
*
|
|
88
88
|
* @type {Map<string, number>}
|
|
89
89
|
*/
|
|
90
|
-
this._peerValues =
|
|
90
|
+
this._peerValues = trackedMap(METRICS_COMPONENT, METRICS_PEER_VALUES, this._libp2p.metrics)
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
93
|
* Map of connections per peer
|
|
94
94
|
*
|
|
95
95
|
* @type {Map<string, Connection[]>}
|
|
96
96
|
*/
|
|
97
|
-
this.connections =
|
|
97
|
+
this.connections = trackedMap(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, this._libp2p.metrics)
|
|
98
98
|
|
|
99
99
|
this._started = false
|
|
100
100
|
this._timer = null
|
|
@@ -164,8 +164,6 @@ class ConnectionManager extends EventEmitter {
|
|
|
164
164
|
|
|
165
165
|
await Promise.all(tasks)
|
|
166
166
|
this.connections.clear()
|
|
167
|
-
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, 0)
|
|
168
|
-
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_ALL_CONNECTIONS, 0)
|
|
169
167
|
}
|
|
170
168
|
|
|
171
169
|
/**
|
|
@@ -189,19 +187,22 @@ class ConnectionManager extends EventEmitter {
|
|
|
189
187
|
*
|
|
190
188
|
* @private
|
|
191
189
|
*/
|
|
192
|
-
_checkMetrics () {
|
|
190
|
+
async _checkMetrics () {
|
|
193
191
|
if (this._libp2p.metrics) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
192
|
+
try {
|
|
193
|
+
const movingAverages = this._libp2p.metrics.global.movingAverages
|
|
194
|
+
// @ts-ignore moving averages object types
|
|
195
|
+
const received = movingAverages.dataReceived[this._options.movingAverageInterval].movingAverage()
|
|
196
|
+
await this._checkMaxLimit('maxReceivedData', received)
|
|
197
|
+
// @ts-ignore moving averages object types
|
|
198
|
+
const sent = movingAverages.dataSent[this._options.movingAverageInterval].movingAverage()
|
|
199
|
+
await this._checkMaxLimit('maxSentData', sent)
|
|
200
|
+
const total = received + sent
|
|
201
|
+
await this._checkMaxLimit('maxData', total)
|
|
202
|
+
log('metrics update', total)
|
|
203
|
+
} finally {
|
|
204
|
+
this._timer = retimer(this._checkMetrics, this._options.pollInterval)
|
|
205
|
+
}
|
|
205
206
|
}
|
|
206
207
|
}
|
|
207
208
|
|
|
@@ -209,9 +210,8 @@ class ConnectionManager extends EventEmitter {
|
|
|
209
210
|
* Tracks the incoming connection and check the connection limit
|
|
210
211
|
*
|
|
211
212
|
* @param {Connection} connection
|
|
212
|
-
* @returns {void}
|
|
213
213
|
*/
|
|
214
|
-
onConnect (connection) {
|
|
214
|
+
async onConnect (connection) {
|
|
215
215
|
const peerId = connection.remotePeer
|
|
216
216
|
const peerIdStr = peerId.toB58String()
|
|
217
217
|
const storedConn = this.connections.get(peerIdStr)
|
|
@@ -222,17 +222,15 @@ class ConnectionManager extends EventEmitter {
|
|
|
222
222
|
storedConn.push(connection)
|
|
223
223
|
} else {
|
|
224
224
|
this.connections.set(peerIdStr, [connection])
|
|
225
|
-
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, this.connections.size)
|
|
226
|
-
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_ALL_CONNECTIONS, this.size)
|
|
227
225
|
}
|
|
228
226
|
|
|
229
|
-
this._libp2p.peerStore.keyBook.set(peerId, peerId.pubKey)
|
|
227
|
+
await this._libp2p.peerStore.keyBook.set(peerId, peerId.pubKey)
|
|
230
228
|
|
|
231
229
|
if (!this._peerValues.has(peerIdStr)) {
|
|
232
230
|
this._peerValues.set(peerIdStr, this._options.defaultPeerValue)
|
|
233
231
|
}
|
|
234
232
|
|
|
235
|
-
this._checkMaxLimit('maxConnections', this.size)
|
|
233
|
+
await this._checkMaxLimit('maxConnections', this.size)
|
|
236
234
|
}
|
|
237
235
|
|
|
238
236
|
/**
|
|
@@ -255,9 +253,6 @@ class ConnectionManager extends EventEmitter {
|
|
|
255
253
|
|
|
256
254
|
this._libp2p.metrics && this._libp2p.metrics.onPeerDisconnected(connection.remotePeer)
|
|
257
255
|
}
|
|
258
|
-
|
|
259
|
-
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PEER_CONNECTIONS, this.connections.size)
|
|
260
|
-
this._libp2p.metrics && this._libp2p.metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_ALL_CONNECTIONS, this.size)
|
|
261
256
|
}
|
|
262
257
|
|
|
263
258
|
/**
|
|
@@ -303,6 +298,9 @@ class ConnectionManager extends EventEmitter {
|
|
|
303
298
|
*/
|
|
304
299
|
_onLatencyMeasure (summary) {
|
|
305
300
|
this._checkMaxLimit('maxEventLoopDelay', summary.avgMs)
|
|
301
|
+
.catch(err => {
|
|
302
|
+
log.error(err)
|
|
303
|
+
})
|
|
306
304
|
}
|
|
307
305
|
|
|
308
306
|
/**
|
|
@@ -312,12 +310,12 @@ class ConnectionManager extends EventEmitter {
|
|
|
312
310
|
* @param {string} name - The name of the field to check limits for
|
|
313
311
|
* @param {number} value - The current value of the field
|
|
314
312
|
*/
|
|
315
|
-
_checkMaxLimit (name, value) {
|
|
313
|
+
async _checkMaxLimit (name, value) {
|
|
316
314
|
const limit = this._options[name]
|
|
317
315
|
log('checking limit of %s. current value: %d of %d', name, value, limit)
|
|
318
316
|
if (value > limit) {
|
|
319
317
|
log('%s: limit exceeded: %s, %d', this._peerId, name, value)
|
|
320
|
-
this._maybeDisconnectOne()
|
|
318
|
+
await this._maybeDisconnectOne()
|
|
321
319
|
}
|
|
322
320
|
}
|
|
323
321
|
|
|
@@ -327,7 +325,7 @@ class ConnectionManager extends EventEmitter {
|
|
|
327
325
|
*
|
|
328
326
|
* @private
|
|
329
327
|
*/
|
|
330
|
-
_maybeDisconnectOne () {
|
|
328
|
+
async _maybeDisconnectOne () {
|
|
331
329
|
if (this._options.minConnections < this.connections.size) {
|
|
332
330
|
const peerValues = Array.from(new Map([...this._peerValues.entries()].sort((a, b) => a[1] - b[1])))
|
|
333
331
|
log('%s: sorted peer values: %j', this._peerId, peerValues)
|
|
@@ -338,7 +336,11 @@ class ConnectionManager extends EventEmitter {
|
|
|
338
336
|
log('%s: closing a connection to %j', this._peerId, peerId)
|
|
339
337
|
for (const connections of this.connections.values()) {
|
|
340
338
|
if (connections[0].remotePeer.toB58String() === peerId) {
|
|
341
|
-
connections[0].close()
|
|
339
|
+
connections[0].close().catch(err => {
|
|
340
|
+
log.error(err)
|
|
341
|
+
})
|
|
342
|
+
// TODO: should not need to invoke this manually
|
|
343
|
+
this.onDisconnect(connections[0])
|
|
342
344
|
break
|
|
343
345
|
}
|
|
344
346
|
}
|
|
@@ -14,12 +14,12 @@ const take = require('it-take')
|
|
|
14
14
|
* Store the multiaddrs from every peer in the passed peer store
|
|
15
15
|
*
|
|
16
16
|
* @param {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>} source
|
|
17
|
-
* @param {import('../peer-store')} peerStore
|
|
17
|
+
* @param {import('../peer-store/types').PeerStore} peerStore
|
|
18
18
|
*/
|
|
19
|
-
function storeAddresses (source, peerStore) {
|
|
20
|
-
|
|
19
|
+
async function * storeAddresses (source, peerStore) {
|
|
20
|
+
yield * map(source, async (peer) => {
|
|
21
21
|
// ensure we have the addresses for a given peer
|
|
22
|
-
peerStore.addressBook.add(peer.id, peer.multiaddrs)
|
|
22
|
+
await peerStore.addressBook.add(peer.id, peer.multiaddrs)
|
|
23
23
|
|
|
24
24
|
return peer
|
|
25
25
|
})
|
|
@@ -27,12 +27,8 @@ class DHTPeerRouting {
|
|
|
27
27
|
*/
|
|
28
28
|
async findPeer (peerId, options = {}) {
|
|
29
29
|
for await (const event of this._dht.findPeer(peerId, options)) {
|
|
30
|
-
if (event.name === '
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (peer) {
|
|
34
|
-
return peer
|
|
35
|
-
}
|
|
30
|
+
if (event.name === 'FINAL_PEER') {
|
|
31
|
+
return event.peer
|
|
36
32
|
}
|
|
37
33
|
}
|
|
38
34
|
|
|
@@ -63,7 +63,10 @@ class DialRequest {
|
|
|
63
63
|
tokens.forEach(token => tokenHolder.push(token))
|
|
64
64
|
const dialAbortControllers = this.addrs.map(() => {
|
|
65
65
|
const controller = new AbortController()
|
|
66
|
-
|
|
66
|
+
try {
|
|
67
|
+
// fails on node < 15.4
|
|
68
|
+
setMaxListeners && setMaxListeners(Infinity, controller.signal)
|
|
69
|
+
} catch {}
|
|
67
70
|
|
|
68
71
|
return controller
|
|
69
72
|
})
|
package/src/dialer/index.js
CHANGED
|
@@ -9,11 +9,12 @@ const { Multiaddr } = require('multiaddr')
|
|
|
9
9
|
const { TimeoutController } = require('timeout-abort-controller')
|
|
10
10
|
const { AbortError } = require('abortable-iterator')
|
|
11
11
|
const { anySignal } = require('any-signal')
|
|
12
|
-
|
|
12
|
+
// @ts-expect-error setMaxListeners is missing from the types
|
|
13
|
+
const { setMaxListeners } = require('events')
|
|
13
14
|
const DialRequest = require('./dial-request')
|
|
14
15
|
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
|
|
15
16
|
const getPeer = require('../get-peer')
|
|
16
|
-
|
|
17
|
+
const trackedMap = require('../metrics/tracked-map')
|
|
17
18
|
const { codes } = require('../errors')
|
|
18
19
|
const {
|
|
19
20
|
DIAL_TIMEOUT,
|
|
@@ -29,8 +30,8 @@ const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets'
|
|
|
29
30
|
/**
|
|
30
31
|
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
31
32
|
* @typedef {import('peer-id')} PeerId
|
|
32
|
-
* @typedef {import('../peer-store')} PeerStore
|
|
33
|
-
* @typedef {import('../peer-store/
|
|
33
|
+
* @typedef {import('../peer-store/types').PeerStore} PeerStore
|
|
34
|
+
* @typedef {import('../peer-store/types').Address} Address
|
|
34
35
|
* @typedef {import('../transport-manager')} TransportManager
|
|
35
36
|
*/
|
|
36
37
|
|
|
@@ -55,8 +56,8 @@ const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets'
|
|
|
55
56
|
* @property {Multiaddr[]} addrs
|
|
56
57
|
*
|
|
57
58
|
* @typedef PendingDial
|
|
58
|
-
* @property {
|
|
59
|
-
* @property {TimeoutController} controller
|
|
59
|
+
* @property {import('./dial-request')} dialRequest
|
|
60
|
+
* @property {import('timeout-abort-controller').TimeoutController} controller
|
|
60
61
|
* @property {Promise<Connection>} promise
|
|
61
62
|
* @property {function():void} destroy
|
|
62
63
|
*/
|
|
@@ -85,9 +86,12 @@ class Dialer {
|
|
|
85
86
|
this.timeout = dialTimeout
|
|
86
87
|
this.maxDialsPerPeer = maxDialsPerPeer
|
|
87
88
|
this.tokens = [...new Array(maxParallelDials)].map((_, index) => index)
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
this.
|
|
89
|
+
|
|
90
|
+
/** @type {Map<string, PendingDial>} */
|
|
91
|
+
this._pendingDials = trackedMap(METRICS_COMPONENT, METRICS_PENDING_DIALS, metrics)
|
|
92
|
+
|
|
93
|
+
/** @type {Map<string, { resolve: (value: any) => void, reject: (err: Error) => void}>} */
|
|
94
|
+
this._pendingDialTargets = trackedMap(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, metrics)
|
|
91
95
|
|
|
92
96
|
for (const [key, value] of Object.entries(resolvers)) {
|
|
93
97
|
Multiaddr.resolvers.set(key, value)
|
|
@@ -111,9 +115,6 @@ class Dialer {
|
|
|
111
115
|
pendingTarget.reject(new AbortError('Dialer was destroyed'))
|
|
112
116
|
}
|
|
113
117
|
this._pendingDialTargets.clear()
|
|
114
|
-
|
|
115
|
-
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIALS, 0)
|
|
116
|
-
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, 0)
|
|
117
118
|
}
|
|
118
119
|
|
|
119
120
|
/**
|
|
@@ -163,7 +164,6 @@ class Dialer {
|
|
|
163
164
|
const id = `${(parseInt(String(Math.random() * 1e9), 10)).toString() + Date.now()}`
|
|
164
165
|
const cancellablePromise = new Promise((resolve, reject) => {
|
|
165
166
|
this._pendingDialTargets.set(id, { resolve, reject })
|
|
166
|
-
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, this._pendingDialTargets.size)
|
|
167
167
|
})
|
|
168
168
|
|
|
169
169
|
try {
|
|
@@ -175,7 +175,6 @@ class Dialer {
|
|
|
175
175
|
return dialTarget
|
|
176
176
|
} finally {
|
|
177
177
|
this._pendingDialTargets.delete(id)
|
|
178
|
-
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIAL_TARGETS, this._pendingDialTargets.size)
|
|
179
178
|
}
|
|
180
179
|
}
|
|
181
180
|
|
|
@@ -193,10 +192,10 @@ class Dialer {
|
|
|
193
192
|
const { id, multiaddrs } = getPeer(peer)
|
|
194
193
|
|
|
195
194
|
if (multiaddrs) {
|
|
196
|
-
this.peerStore.addressBook.add(id, multiaddrs)
|
|
195
|
+
await this.peerStore.addressBook.add(id, multiaddrs)
|
|
197
196
|
}
|
|
198
197
|
|
|
199
|
-
let knownAddrs = this.peerStore.addressBook.getMultiaddrsForPeer(id, this.addressSorter) || []
|
|
198
|
+
let knownAddrs = await this.peerStore.addressBook.getMultiaddrsForPeer(id, this.addressSorter) || []
|
|
200
199
|
|
|
201
200
|
// If received a multiaddr to dial, it should be the first to use
|
|
202
201
|
// But, if we know other multiaddrs for the peer, we should try them too.
|
|
@@ -216,7 +215,7 @@ class Dialer {
|
|
|
216
215
|
const supportedAddrs = addrs.filter(a => this.transportManager.transportForMultiaddr(a))
|
|
217
216
|
|
|
218
217
|
if (supportedAddrs.length > this.maxAddrsToDial) {
|
|
219
|
-
this.peerStore.delete(id)
|
|
218
|
+
await this.peerStore.delete(id)
|
|
220
219
|
throw errCode(new Error('dial with more addresses than allowed'), codes.ERR_TOO_MANY_ADDRESSES)
|
|
221
220
|
}
|
|
222
221
|
|
|
@@ -253,10 +252,18 @@ class Dialer {
|
|
|
253
252
|
|
|
254
253
|
// Combine the timeout signal and options.signal, if provided
|
|
255
254
|
const timeoutController = new TimeoutController(this.timeout)
|
|
255
|
+
|
|
256
256
|
const signals = [timeoutController.signal]
|
|
257
257
|
options.signal && signals.push(options.signal)
|
|
258
258
|
const signal = anySignal(signals)
|
|
259
259
|
|
|
260
|
+
// this signal will potentially be used while dialing lots of
|
|
261
|
+
// peers so prevent MaxListenersExceededWarning appearing in the console
|
|
262
|
+
try {
|
|
263
|
+
// fails on node < 15.4
|
|
264
|
+
setMaxListeners && setMaxListeners(Infinity, signal)
|
|
265
|
+
} catch {}
|
|
266
|
+
|
|
260
267
|
const pendingDial = {
|
|
261
268
|
dialRequest,
|
|
262
269
|
controller: timeoutController,
|
|
@@ -264,13 +271,10 @@ class Dialer {
|
|
|
264
271
|
destroy: () => {
|
|
265
272
|
timeoutController.clear()
|
|
266
273
|
this._pendingDials.delete(dialTarget.id)
|
|
267
|
-
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIALS, this._pendingDials.size)
|
|
268
274
|
}
|
|
269
275
|
}
|
|
270
276
|
this._pendingDials.set(dialTarget.id, pendingDial)
|
|
271
277
|
|
|
272
|
-
this._metrics && this._metrics.updateComponentMetric(METRICS_COMPONENT, METRICS_PENDING_DIALS, this._pendingDials.size)
|
|
273
|
-
|
|
274
278
|
return pendingDial
|
|
275
279
|
}
|
|
276
280
|
|
package/src/identify/index.js
CHANGED
|
@@ -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.
|
|
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.
|
|
211
|
-
this.peerStore.metadataBook.
|
|
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.
|
|
227
|
-
this.peerStore.metadataBook.
|
|
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)
|
|
@@ -268,7 +273,7 @@ class IdentifyService {
|
|
|
268
273
|
}
|
|
269
274
|
|
|
270
275
|
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
|
271
|
-
const protocols = this.peerStore.protoBook.get(this.peerId)
|
|
276
|
+
const protocols = await this.peerStore.protoBook.get(this.peerId)
|
|
272
277
|
|
|
273
278
|
const message = Message.Identify.encode({
|
|
274
279
|
protocolVersion: this._host.protocolVersion,
|
|
@@ -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,14 @@ 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
|
+
await this.peerStore.protoBook.set(id, message.protocols)
|
|
342
347
|
}
|
|
343
348
|
|
|
344
349
|
/**
|