libp2p 0.35.4 → 0.35.8-12f1bb0
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/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 +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/index.d.ts +9 -4
- package/dist/src/metrics/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 +8 -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 +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 +28 -32
- package/src/circuit/auto-relay.js +19 -19
- package/src/connection-manager/auto-dialler.js +9 -4
- package/src/connection-manager/index.js +41 -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 +33 -21
- package/src/identify/index.js +47 -38
- package/src/index.js +43 -35
- package/src/metrics/index.js +15 -8
- package/src/metrics/stats.js +1 -0
- package/src/metrics/tracked-map.js +94 -0
- package/src/peer-routing.js +11 -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
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)
|
|
@@ -262,25 +267,25 @@ class IdentifyService {
|
|
|
262
267
|
* @returns {Promise<void>}
|
|
263
268
|
*/
|
|
264
269
|
async _handleIdentify ({ connection, stream }) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
270
|
+
try {
|
|
271
|
+
let publicKey = new Uint8Array(0)
|
|
272
|
+
if (this.peerId.pubKey) {
|
|
273
|
+
publicKey = this.peerId.pubKey.bytes
|
|
274
|
+
}
|
|
269
275
|
|
|
270
|
-
|
|
271
|
-
|
|
276
|
+
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
|
277
|
+
const protocols = await this.peerStore.protoBook.get(this.peerId)
|
|
272
278
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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
|
-
|
|
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,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
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
/**
|
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
|
/**
|
package/src/metrics/stats.js
CHANGED
|
@@ -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
|
+
}
|
package/src/peer-routing.js
CHANGED
|
@@ -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
|
-
|
|
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(
|