libp2p 0.35.7 → 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.
- package/dist/index.min.js +55 -0
- 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 +5 -5
- 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 +6 -6
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/metrics/index.d.ts +9 -4
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/metrics/tracked-map.d.ts +6 -1
- package/dist/src/metrics/tracked-map.d.ts.map +1 -1
- 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 +27 -28
- package/dist/src/peer-store/proto-book.d.ts.map +1 -1
- package/dist/src/peer-store/store.d.ts +73 -0
- package/dist/src/peer-store/store.d.ts.map +1 -0
- package/dist/src/peer-store/types.d.ts +202 -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 +18 -25
- package/src/circuit/auto-relay.js +19 -19
- package/src/connection-manager/auto-dialler.js +9 -4
- package/src/connection-manager/index.js +39 -22
- 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 +22 -10
- package/src/identify/index.js +28 -23
- package/src/index.js +33 -27
- package/src/metrics/index.js +15 -8
- package/src/metrics/tracked-map.js +45 -13
- package/src/peer-routing.js +4 -1
- package/src/peer-store/address-book.js +242 -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
|
@@ -4,6 +4,7 @@ const debug = require('debug')
|
|
|
4
4
|
const mergeOptions = require('merge-options')
|
|
5
5
|
// @ts-ignore retimer does not have types
|
|
6
6
|
const retimer = require('retimer')
|
|
7
|
+
const all = require('it-all')
|
|
7
8
|
|
|
8
9
|
const log = Object.assign(debug('libp2p:connection-manager:auto-dialler'), {
|
|
9
10
|
error: debug('libp2p:connection-manager:auto-dialler:err')
|
|
@@ -50,14 +51,16 @@ class AutoDialler {
|
|
|
50
51
|
/**
|
|
51
52
|
* Starts the auto dialer
|
|
52
53
|
*/
|
|
53
|
-
start () {
|
|
54
|
+
async start () {
|
|
54
55
|
if (!this._options.enabled) {
|
|
55
56
|
log('not enabled')
|
|
56
57
|
return
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
this._running = true
|
|
60
|
-
this._autoDial()
|
|
61
|
+
this._autoDial().catch(err => {
|
|
62
|
+
log.error('could start autodial', err)
|
|
63
|
+
})
|
|
61
64
|
log('started')
|
|
62
65
|
}
|
|
63
66
|
|
|
@@ -84,8 +87,10 @@ class AutoDialler {
|
|
|
84
87
|
return
|
|
85
88
|
}
|
|
86
89
|
|
|
87
|
-
// Sort peers on
|
|
88
|
-
|
|
90
|
+
// Sort peers on whether we know protocols of public keys for them
|
|
91
|
+
// TODO: assuming the `peerStore.getPeers()` order is stable this will mean
|
|
92
|
+
// we keep trying to connect to the same peers?
|
|
93
|
+
const peers = (await all(this._libp2p.peerStore.getPeers()))
|
|
89
94
|
.sort((a, b) => {
|
|
90
95
|
if (b.protocols && b.protocols.length && (!a.protocols || !a.protocols.length)) {
|
|
91
96
|
return 1
|
|
@@ -87,14 +87,22 @@ class ConnectionManager extends EventEmitter {
|
|
|
87
87
|
*
|
|
88
88
|
* @type {Map<string, number>}
|
|
89
89
|
*/
|
|
90
|
-
this._peerValues = trackedMap(
|
|
90
|
+
this._peerValues = trackedMap({
|
|
91
|
+
component: METRICS_COMPONENT,
|
|
92
|
+
metric: METRICS_PEER_VALUES,
|
|
93
|
+
metrics: this._libp2p.metrics
|
|
94
|
+
})
|
|
91
95
|
|
|
92
96
|
/**
|
|
93
97
|
* Map of connections per peer
|
|
94
98
|
*
|
|
95
99
|
* @type {Map<string, Connection[]>}
|
|
96
100
|
*/
|
|
97
|
-
this.connections = trackedMap(
|
|
101
|
+
this.connections = trackedMap({
|
|
102
|
+
component: METRICS_COMPONENT,
|
|
103
|
+
metric: METRICS_PEER_CONNECTIONS,
|
|
104
|
+
metrics: this._libp2p.metrics
|
|
105
|
+
})
|
|
98
106
|
|
|
99
107
|
this._started = false
|
|
100
108
|
this._timer = null
|
|
@@ -187,19 +195,22 @@ class ConnectionManager extends EventEmitter {
|
|
|
187
195
|
*
|
|
188
196
|
* @private
|
|
189
197
|
*/
|
|
190
|
-
_checkMetrics () {
|
|
198
|
+
async _checkMetrics () {
|
|
191
199
|
if (this._libp2p.metrics) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
200
|
+
try {
|
|
201
|
+
const movingAverages = this._libp2p.metrics.global.movingAverages
|
|
202
|
+
// @ts-ignore moving averages object types
|
|
203
|
+
const received = movingAverages.dataReceived[this._options.movingAverageInterval].movingAverage()
|
|
204
|
+
await this._checkMaxLimit('maxReceivedData', received)
|
|
205
|
+
// @ts-ignore moving averages object types
|
|
206
|
+
const sent = movingAverages.dataSent[this._options.movingAverageInterval].movingAverage()
|
|
207
|
+
await this._checkMaxLimit('maxSentData', sent)
|
|
208
|
+
const total = received + sent
|
|
209
|
+
await this._checkMaxLimit('maxData', total)
|
|
210
|
+
log('metrics update', total)
|
|
211
|
+
} finally {
|
|
212
|
+
this._timer = retimer(this._checkMetrics, this._options.pollInterval)
|
|
213
|
+
}
|
|
203
214
|
}
|
|
204
215
|
}
|
|
205
216
|
|
|
@@ -207,9 +218,8 @@ class ConnectionManager extends EventEmitter {
|
|
|
207
218
|
* Tracks the incoming connection and check the connection limit
|
|
208
219
|
*
|
|
209
220
|
* @param {Connection} connection
|
|
210
|
-
* @returns {void}
|
|
211
221
|
*/
|
|
212
|
-
onConnect (connection) {
|
|
222
|
+
async onConnect (connection) {
|
|
213
223
|
const peerId = connection.remotePeer
|
|
214
224
|
const peerIdStr = peerId.toB58String()
|
|
215
225
|
const storedConn = this.connections.get(peerIdStr)
|
|
@@ -222,13 +232,13 @@ class ConnectionManager extends EventEmitter {
|
|
|
222
232
|
this.connections.set(peerIdStr, [connection])
|
|
223
233
|
}
|
|
224
234
|
|
|
225
|
-
this._libp2p.peerStore.keyBook.set(peerId, peerId.pubKey)
|
|
235
|
+
await this._libp2p.peerStore.keyBook.set(peerId, peerId.pubKey)
|
|
226
236
|
|
|
227
237
|
if (!this._peerValues.has(peerIdStr)) {
|
|
228
238
|
this._peerValues.set(peerIdStr, this._options.defaultPeerValue)
|
|
229
239
|
}
|
|
230
240
|
|
|
231
|
-
this._checkMaxLimit('maxConnections', this.size)
|
|
241
|
+
await this._checkMaxLimit('maxConnections', this.size)
|
|
232
242
|
}
|
|
233
243
|
|
|
234
244
|
/**
|
|
@@ -296,6 +306,9 @@ class ConnectionManager extends EventEmitter {
|
|
|
296
306
|
*/
|
|
297
307
|
_onLatencyMeasure (summary) {
|
|
298
308
|
this._checkMaxLimit('maxEventLoopDelay', summary.avgMs)
|
|
309
|
+
.catch(err => {
|
|
310
|
+
log.error(err)
|
|
311
|
+
})
|
|
299
312
|
}
|
|
300
313
|
|
|
301
314
|
/**
|
|
@@ -305,12 +318,12 @@ class ConnectionManager extends EventEmitter {
|
|
|
305
318
|
* @param {string} name - The name of the field to check limits for
|
|
306
319
|
* @param {number} value - The current value of the field
|
|
307
320
|
*/
|
|
308
|
-
_checkMaxLimit (name, value) {
|
|
321
|
+
async _checkMaxLimit (name, value) {
|
|
309
322
|
const limit = this._options[name]
|
|
310
323
|
log('checking limit of %s. current value: %d of %d', name, value, limit)
|
|
311
324
|
if (value > limit) {
|
|
312
325
|
log('%s: limit exceeded: %s, %d', this._peerId, name, value)
|
|
313
|
-
this._maybeDisconnectOne()
|
|
326
|
+
await this._maybeDisconnectOne()
|
|
314
327
|
}
|
|
315
328
|
}
|
|
316
329
|
|
|
@@ -320,7 +333,7 @@ class ConnectionManager extends EventEmitter {
|
|
|
320
333
|
*
|
|
321
334
|
* @private
|
|
322
335
|
*/
|
|
323
|
-
_maybeDisconnectOne () {
|
|
336
|
+
async _maybeDisconnectOne () {
|
|
324
337
|
if (this._options.minConnections < this.connections.size) {
|
|
325
338
|
const peerValues = Array.from(new Map([...this._peerValues.entries()].sort((a, b) => a[1] - b[1])))
|
|
326
339
|
log('%s: sorted peer values: %j', this._peerId, peerValues)
|
|
@@ -331,7 +344,11 @@ class ConnectionManager extends EventEmitter {
|
|
|
331
344
|
log('%s: closing a connection to %j', this._peerId, peerId)
|
|
332
345
|
for (const connections of this.connections.values()) {
|
|
333
346
|
if (connections[0].remotePeer.toB58String() === peerId) {
|
|
334
|
-
connections[0].close()
|
|
347
|
+
connections[0].close().catch(err => {
|
|
348
|
+
log.error(err)
|
|
349
|
+
})
|
|
350
|
+
// TODO: should not need to invoke this manually
|
|
351
|
+
this.onDisconnect(connections[0])
|
|
335
352
|
break
|
|
336
353
|
}
|
|
337
354
|
}
|
|
@@ -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
|
@@ -30,8 +30,8 @@ const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets'
|
|
|
30
30
|
/**
|
|
31
31
|
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
32
32
|
* @typedef {import('peer-id')} PeerId
|
|
33
|
-
* @typedef {import('../peer-store')} PeerStore
|
|
34
|
-
* @typedef {import('../peer-store/
|
|
33
|
+
* @typedef {import('../peer-store/types').PeerStore} PeerStore
|
|
34
|
+
* @typedef {import('../peer-store/types').Address} Address
|
|
35
35
|
* @typedef {import('../transport-manager')} TransportManager
|
|
36
36
|
*/
|
|
37
37
|
|
|
@@ -88,10 +88,18 @@ class Dialer {
|
|
|
88
88
|
this.tokens = [...new Array(maxParallelDials)].map((_, index) => index)
|
|
89
89
|
|
|
90
90
|
/** @type {Map<string, PendingDial>} */
|
|
91
|
-
this._pendingDials = trackedMap(
|
|
91
|
+
this._pendingDials = trackedMap({
|
|
92
|
+
component: METRICS_COMPONENT,
|
|
93
|
+
metric: METRICS_PENDING_DIALS,
|
|
94
|
+
metrics
|
|
95
|
+
})
|
|
92
96
|
|
|
93
97
|
/** @type {Map<string, { resolve: (value: any) => void, reject: (err: Error) => void}>} */
|
|
94
|
-
this._pendingDialTargets = trackedMap(
|
|
98
|
+
this._pendingDialTargets = trackedMap({
|
|
99
|
+
component: METRICS_COMPONENT,
|
|
100
|
+
metric: METRICS_PENDING_DIAL_TARGETS,
|
|
101
|
+
metrics
|
|
102
|
+
})
|
|
95
103
|
|
|
96
104
|
for (const [key, value] of Object.entries(resolvers)) {
|
|
97
105
|
Multiaddr.resolvers.set(key, value)
|
|
@@ -192,10 +200,10 @@ class Dialer {
|
|
|
192
200
|
const { id, multiaddrs } = getPeer(peer)
|
|
193
201
|
|
|
194
202
|
if (multiaddrs) {
|
|
195
|
-
this.peerStore.addressBook.add(id, multiaddrs)
|
|
203
|
+
await this.peerStore.addressBook.add(id, multiaddrs)
|
|
196
204
|
}
|
|
197
205
|
|
|
198
|
-
let knownAddrs = this.peerStore.addressBook.getMultiaddrsForPeer(id, this.addressSorter) || []
|
|
206
|
+
let knownAddrs = await this.peerStore.addressBook.getMultiaddrsForPeer(id, this.addressSorter) || []
|
|
199
207
|
|
|
200
208
|
// If received a multiaddr to dial, it should be the first to use
|
|
201
209
|
// But, if we know other multiaddrs for the peer, we should try them too.
|
|
@@ -215,7 +223,7 @@ class Dialer {
|
|
|
215
223
|
const supportedAddrs = addrs.filter(a => this.transportManager.transportForMultiaddr(a))
|
|
216
224
|
|
|
217
225
|
if (supportedAddrs.length > this.maxAddrsToDial) {
|
|
218
|
-
this.peerStore.delete(id)
|
|
226
|
+
await this.peerStore.delete(id)
|
|
219
227
|
throw errCode(new Error('dial with more addresses than allowed'), codes.ERR_TOO_MANY_ADDRESSES)
|
|
220
228
|
}
|
|
221
229
|
|
|
@@ -252,14 +260,18 @@ class Dialer {
|
|
|
252
260
|
|
|
253
261
|
// Combine the timeout signal and options.signal, if provided
|
|
254
262
|
const timeoutController = new TimeoutController(this.timeout)
|
|
255
|
-
// this controller will potentially be used while dialing lots of
|
|
256
|
-
// peers so prevent MaxListenersExceededWarning appearing in the console
|
|
257
|
-
setMaxListeners && setMaxListeners(Infinity, timeoutController.signal)
|
|
258
263
|
|
|
259
264
|
const signals = [timeoutController.signal]
|
|
260
265
|
options.signal && signals.push(options.signal)
|
|
261
266
|
const signal = anySignal(signals)
|
|
262
267
|
|
|
268
|
+
// this signal will potentially be used while dialing lots of
|
|
269
|
+
// peers so prevent MaxListenersExceededWarning appearing in the console
|
|
270
|
+
try {
|
|
271
|
+
// fails on node < 15.4
|
|
272
|
+
setMaxListeners && setMaxListeners(Infinity, signal)
|
|
273
|
+
} catch {}
|
|
274
|
+
|
|
263
275
|
const pendingDial = {
|
|
264
276
|
dialRequest,
|
|
265
277
|
controller: timeoutController,
|
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
|
/**
|
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')
|
|
@@ -112,7 +111,7 @@ const { updateSelfPeerRecord } = require('./record/utils')
|
|
|
112
111
|
* @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
|
|
113
112
|
* @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
|
|
114
113
|
* @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
|
|
115
|
-
* @property {PeerStoreOptions
|
|
114
|
+
* @property {PeerStoreOptions} [peerStore]
|
|
116
115
|
* @property {import('./transport-manager').TransportManagerOptions} [transportManager]
|
|
117
116
|
* @property {Libp2pConfig} [config]
|
|
118
117
|
*
|
|
@@ -172,13 +171,11 @@ class Libp2p extends EventEmitter {
|
|
|
172
171
|
this.metrics = metrics
|
|
173
172
|
}
|
|
174
173
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
})
|
|
181
|
-
: new PeerStore({ peerId: this.peerId })
|
|
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
|
+
})
|
|
182
179
|
|
|
183
180
|
// Addresses {listen, announce, noAnnounce}
|
|
184
181
|
this.addresses = this._options.addresses
|
|
@@ -290,7 +287,6 @@ class Libp2p extends EventEmitter {
|
|
|
290
287
|
|
|
291
288
|
// Add the identify service since we can multiplex
|
|
292
289
|
this.identifyService = new IdentifyService({ libp2p: this })
|
|
293
|
-
this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
|
|
294
290
|
}
|
|
295
291
|
|
|
296
292
|
// Attach private network protector
|
|
@@ -356,6 +352,10 @@ class Libp2p extends EventEmitter {
|
|
|
356
352
|
async start () {
|
|
357
353
|
log('libp2p is starting')
|
|
358
354
|
|
|
355
|
+
if (this.identifyService) {
|
|
356
|
+
await this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
359
|
try {
|
|
360
360
|
await this._onStarting()
|
|
361
361
|
await this._onDidStart()
|
|
@@ -380,9 +380,13 @@ class Libp2p extends EventEmitter {
|
|
|
380
380
|
try {
|
|
381
381
|
this._isStarted = false
|
|
382
382
|
|
|
383
|
+
if (this.identifyService) {
|
|
384
|
+
await this.identifyService.stop()
|
|
385
|
+
}
|
|
386
|
+
|
|
383
387
|
this.relay && this.relay.stop()
|
|
384
388
|
this.peerRouting.stop()
|
|
385
|
-
this._autodialler.stop()
|
|
389
|
+
await this._autodialler.stop()
|
|
386
390
|
await (this._dht && this._dht.stop())
|
|
387
391
|
|
|
388
392
|
for (const service of this._discovery.values()) {
|
|
@@ -393,7 +397,6 @@ class Libp2p extends EventEmitter {
|
|
|
393
397
|
|
|
394
398
|
this._discovery = new Map()
|
|
395
399
|
|
|
396
|
-
await this.peerStore.stop()
|
|
397
400
|
await this.connectionManager.stop()
|
|
398
401
|
|
|
399
402
|
await Promise.all([
|
|
@@ -499,7 +502,7 @@ class Libp2p extends EventEmitter {
|
|
|
499
502
|
if (!connection) {
|
|
500
503
|
connection = await this.dialer.connectToPeer(peer, options)
|
|
501
504
|
} else if (multiaddrs) {
|
|
502
|
-
this.peerStore.addressBook.add(id, multiaddrs)
|
|
505
|
+
await this.peerStore.addressBook.add(id, multiaddrs)
|
|
503
506
|
}
|
|
504
507
|
|
|
505
508
|
return connection
|
|
@@ -579,14 +582,14 @@ class Libp2p extends EventEmitter {
|
|
|
579
582
|
* @param {string[]|string} protocols
|
|
580
583
|
* @param {(props: HandlerProps) => void} handler
|
|
581
584
|
*/
|
|
582
|
-
handle (protocols, handler) {
|
|
585
|
+
async handle (protocols, handler) {
|
|
583
586
|
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
584
587
|
protocols.forEach(protocol => {
|
|
585
588
|
this.upgrader.protocols.set(protocol, handler)
|
|
586
589
|
})
|
|
587
590
|
|
|
588
591
|
// Add new protocols to self protocols in the Protobook
|
|
589
|
-
this.peerStore.protoBook.add(this.peerId, protocols)
|
|
592
|
+
await this.peerStore.protoBook.add(this.peerId, protocols)
|
|
590
593
|
}
|
|
591
594
|
|
|
592
595
|
/**
|
|
@@ -595,14 +598,14 @@ class Libp2p extends EventEmitter {
|
|
|
595
598
|
*
|
|
596
599
|
* @param {string[]|string} protocols
|
|
597
600
|
*/
|
|
598
|
-
unhandle (protocols) {
|
|
601
|
+
async unhandle (protocols) {
|
|
599
602
|
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
600
603
|
protocols.forEach(protocol => {
|
|
601
604
|
this.upgrader.protocols.delete(protocol)
|
|
602
605
|
})
|
|
603
606
|
|
|
604
607
|
// Remove protocols from self protocols in the Protobook
|
|
605
|
-
this.peerStore.protoBook.remove(this.peerId, protocols)
|
|
608
|
+
await this.peerStore.protoBook.remove(this.peerId, protocols)
|
|
606
609
|
}
|
|
607
610
|
|
|
608
611
|
async _onStarting () {
|
|
@@ -613,11 +616,8 @@ class Libp2p extends EventEmitter {
|
|
|
613
616
|
// Manage your NATs
|
|
614
617
|
this.natManager.start()
|
|
615
618
|
|
|
616
|
-
// Start PeerStore
|
|
617
|
-
await this.peerStore.start()
|
|
618
|
-
|
|
619
619
|
if (this._config.pubsub.enabled) {
|
|
620
|
-
this.pubsub && this.pubsub.start()
|
|
620
|
+
this.pubsub && await this.pubsub.start()
|
|
621
621
|
}
|
|
622
622
|
|
|
623
623
|
// DHT subsystem
|
|
@@ -631,6 +631,10 @@ class Libp2p extends EventEmitter {
|
|
|
631
631
|
|
|
632
632
|
// Start metrics if present
|
|
633
633
|
this.metrics && this.metrics.start()
|
|
634
|
+
|
|
635
|
+
if (this.identifyService) {
|
|
636
|
+
await this.identifyService.start()
|
|
637
|
+
}
|
|
634
638
|
}
|
|
635
639
|
|
|
636
640
|
/**
|
|
@@ -643,17 +647,19 @@ class Libp2p extends EventEmitter {
|
|
|
643
647
|
|
|
644
648
|
this.peerStore.on('peer', peerId => {
|
|
645
649
|
this.emit('peer:discovery', peerId)
|
|
646
|
-
this._maybeConnect(peerId)
|
|
650
|
+
this._maybeConnect(peerId).catch(err => {
|
|
651
|
+
log.error(err)
|
|
652
|
+
})
|
|
647
653
|
})
|
|
648
654
|
|
|
649
655
|
// Once we start, emit any peers we may have already discovered
|
|
650
656
|
// TODO: this should be removed, as we already discovered these peers in the past
|
|
651
|
-
for (const peer of this.peerStore.
|
|
657
|
+
for await (const peer of this.peerStore.getPeers()) {
|
|
652
658
|
this.emit('peer:discovery', peer.id)
|
|
653
659
|
}
|
|
654
660
|
|
|
655
661
|
this.connectionManager.start()
|
|
656
|
-
this._autodialler.start()
|
|
662
|
+
await this._autodialler.start()
|
|
657
663
|
|
|
658
664
|
// Peer discovery
|
|
659
665
|
await this._setupPeerDiscovery()
|
|
@@ -677,8 +683,8 @@ class Libp2p extends EventEmitter {
|
|
|
677
683
|
return
|
|
678
684
|
}
|
|
679
685
|
|
|
680
|
-
peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs)
|
|
681
|
-
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))
|
|
682
688
|
}
|
|
683
689
|
|
|
684
690
|
/**
|
package/src/metrics/index.js
CHANGED
|
@@ -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.
|
|
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.
|
|
95
|
+
return this._systems
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
updateComponentMetric (component, metric, value) {
|
|
99
|
-
if (!this.
|
|
100
|
-
this.
|
|
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
|
|
104
|
-
|
|
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
|
/**
|