libp2p 0.35.8 → 0.36.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/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/config.d.ts +2 -0
- package/dist/src/config.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/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/dialer/index.d.ts +12 -7
- package/dist/src/dialer/index.d.ts.map +1 -1
- package/dist/src/errors.d.ts +2 -0
- package/dist/src/fetch/constants.d.ts +2 -0
- package/dist/src/fetch/constants.d.ts.map +1 -0
- package/dist/src/fetch/index.d.ts +76 -0
- package/dist/src/fetch/index.d.ts.map +1 -0
- package/dist/src/fetch/proto.d.ts +134 -0
- 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 +116 -7
- 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 +30 -84
- package/dist/src/peer-store/address-book.d.ts.map +1 -1
- package/dist/src/peer-store/index.d.ts +28 -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/types.d.ts +90 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/upgrader.d.ts +8 -2
- package/dist/src/upgrader.d.ts.map +1 -1
- package/package.json +23 -26
- package/src/circuit/auto-relay.js +52 -38
- package/src/config.js +2 -0
- package/src/connection-manager/auto-dialler.js +20 -8
- package/src/connection-manager/index.js +42 -22
- package/src/content-routing/utils.js +4 -4
- package/src/dialer/dial-request.js +4 -1
- package/src/dialer/index.js +38 -8
- package/src/errors.js +2 -0
- package/src/fetch/README.md +36 -0
- package/src/fetch/constants.js +6 -0
- package/src/fetch/index.js +159 -0
- package/src/fetch/proto.d.ts +134 -0
- package/src/fetch/proto.js +333 -0
- package/src/fetch/proto.proto +15 -0
- package/src/identify/index.js +47 -38
- package/src/index.js +72 -27
- package/src/metrics/index.js +15 -8
- package/src/metrics/tracked-map.js +40 -14
- package/src/peer-routing.js +4 -1
- package/src/peer-store/address-book.js +254 -226
- package/src/peer-store/index.js +79 -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 +263 -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/types.ts +98 -0
- package/src/upgrader.js +32 -2
- 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,10 +28,10 @@ const TransportManager = require('./transport-manager')
|
|
|
28
28
|
const Upgrader = require('./upgrader')
|
|
29
29
|
const PeerStore = require('./peer-store')
|
|
30
30
|
const PubsubAdapter = require('./pubsub-adapter')
|
|
31
|
-
const PersistentPeerStore = require('./peer-store/persistent')
|
|
32
31
|
const Registrar = require('./registrar')
|
|
33
32
|
const ping = require('./ping')
|
|
34
33
|
const IdentifyService = require('./identify')
|
|
34
|
+
const FetchService = require('./fetch')
|
|
35
35
|
const NatManager = require('./nat-manager')
|
|
36
36
|
const { updateSelfPeerRecord } = require('./record/utils')
|
|
37
37
|
|
|
@@ -48,6 +48,7 @@ const { updateSelfPeerRecord } = require('./record/utils')
|
|
|
48
48
|
* @typedef {import('libp2p-interfaces/src/pubsub').PubsubOptions} PubsubOptions
|
|
49
49
|
* @typedef {import('interface-datastore').Datastore} Datastore
|
|
50
50
|
* @typedef {import('./pnet')} Protector
|
|
51
|
+
* @typedef {import('./types').ConnectionGater} ConnectionGater
|
|
51
52
|
* @typedef {Object} PersistentPeerStoreOptions
|
|
52
53
|
* @property {number} [threshold]
|
|
53
54
|
*/
|
|
@@ -106,13 +107,14 @@ const { updateSelfPeerRecord } = require('./record/utils')
|
|
|
106
107
|
* @property {Libp2pModules} modules libp2p modules to use
|
|
107
108
|
* @property {import('./address-manager').AddressManagerOptions} [addresses]
|
|
108
109
|
* @property {import('./connection-manager').ConnectionManagerOptions} [connectionManager]
|
|
110
|
+
* @property {Partial<import('./types').ConnectionGater>} [connectionGater]
|
|
109
111
|
* @property {Datastore} [datastore]
|
|
110
112
|
* @property {import('./dialer').DialerOptions} [dialer]
|
|
111
113
|
* @property {import('./identify/index').HostProperties} [host] libp2p host
|
|
112
114
|
* @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
|
|
113
115
|
* @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
|
|
114
116
|
* @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
|
|
115
|
-
* @property {PeerStoreOptions
|
|
117
|
+
* @property {PeerStoreOptions} [peerStore]
|
|
116
118
|
* @property {import('./transport-manager').TransportManagerOptions} [transportManager]
|
|
117
119
|
* @property {Libp2pConfig} [config]
|
|
118
120
|
*
|
|
@@ -172,13 +174,26 @@ class Libp2p extends EventEmitter {
|
|
|
172
174
|
this.metrics = metrics
|
|
173
175
|
}
|
|
174
176
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
:
|
|
177
|
+
/** @type {ConnectionGater} */
|
|
178
|
+
this.connectionGater = {
|
|
179
|
+
denyDialPeer: async () => Promise.resolve(false),
|
|
180
|
+
denyDialMultiaddr: async () => Promise.resolve(false),
|
|
181
|
+
denyInboundConnection: async () => Promise.resolve(false),
|
|
182
|
+
denyOutboundConnection: async () => Promise.resolve(false),
|
|
183
|
+
denyInboundEncryptedConnection: async () => Promise.resolve(false),
|
|
184
|
+
denyOutboundEncryptedConnection: async () => Promise.resolve(false),
|
|
185
|
+
denyInboundUpgradedConnection: async () => Promise.resolve(false),
|
|
186
|
+
denyOutboundUpgradedConnection: async () => Promise.resolve(false),
|
|
187
|
+
filterMultiaddrForPeer: async () => Promise.resolve(true),
|
|
188
|
+
...this._options.connectionGater
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** @type {import('./peer-store/types').PeerStore} */
|
|
192
|
+
this.peerStore = new PeerStore({
|
|
193
|
+
peerId: this.peerId,
|
|
194
|
+
datastore: (this.datastore && this._options.peerStore.persistence) ? this.datastore : new MemoryDatastore(),
|
|
195
|
+
addressFilter: this.connectionGater.filterMultiaddrForPeer
|
|
196
|
+
})
|
|
182
197
|
|
|
183
198
|
// Addresses {listen, announce, noAnnounce}
|
|
184
199
|
this.addresses = this._options.addresses
|
|
@@ -222,6 +237,7 @@ class Libp2p extends EventEmitter {
|
|
|
222
237
|
|
|
223
238
|
// Setup the Upgrader
|
|
224
239
|
this.upgrader = new Upgrader({
|
|
240
|
+
connectionGater: this.connectionGater,
|
|
225
241
|
localPeer: this.peerId,
|
|
226
242
|
metrics: this.metrics,
|
|
227
243
|
onConnection: (connection) => this.connectionManager.onConnect(connection),
|
|
@@ -264,6 +280,7 @@ class Libp2p extends EventEmitter {
|
|
|
264
280
|
|
|
265
281
|
this.dialer = new Dialer({
|
|
266
282
|
transportManager: this.transportManager,
|
|
283
|
+
connectionGater: this.connectionGater,
|
|
267
284
|
peerStore: this.peerStore,
|
|
268
285
|
metrics: this.metrics,
|
|
269
286
|
...this._options.dialer
|
|
@@ -290,7 +307,6 @@ class Libp2p extends EventEmitter {
|
|
|
290
307
|
|
|
291
308
|
// Add the identify service since we can multiplex
|
|
292
309
|
this.identifyService = new IdentifyService({ libp2p: this })
|
|
293
|
-
this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
|
|
294
310
|
}
|
|
295
311
|
|
|
296
312
|
// Attach private network protector
|
|
@@ -327,6 +343,8 @@ class Libp2p extends EventEmitter {
|
|
|
327
343
|
ping.mount(this)
|
|
328
344
|
|
|
329
345
|
this._onDiscoveryPeer = this._onDiscoveryPeer.bind(this)
|
|
346
|
+
|
|
347
|
+
this.fetchService = new FetchService(this)
|
|
330
348
|
}
|
|
331
349
|
|
|
332
350
|
/**
|
|
@@ -356,6 +374,14 @@ class Libp2p extends EventEmitter {
|
|
|
356
374
|
async start () {
|
|
357
375
|
log('libp2p is starting')
|
|
358
376
|
|
|
377
|
+
if (this.identifyService) {
|
|
378
|
+
await this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (this.fetchService) {
|
|
382
|
+
await this.handle(FetchService.PROTOCOL, this.fetchService.handleMessage)
|
|
383
|
+
}
|
|
384
|
+
|
|
359
385
|
try {
|
|
360
386
|
await this._onStarting()
|
|
361
387
|
await this._onDidStart()
|
|
@@ -380,9 +406,13 @@ class Libp2p extends EventEmitter {
|
|
|
380
406
|
try {
|
|
381
407
|
this._isStarted = false
|
|
382
408
|
|
|
409
|
+
if (this.identifyService) {
|
|
410
|
+
await this.identifyService.stop()
|
|
411
|
+
}
|
|
412
|
+
|
|
383
413
|
this.relay && this.relay.stop()
|
|
384
414
|
this.peerRouting.stop()
|
|
385
|
-
this._autodialler.stop()
|
|
415
|
+
await this._autodialler.stop()
|
|
386
416
|
await (this._dht && this._dht.stop())
|
|
387
417
|
|
|
388
418
|
for (const service of this._discovery.values()) {
|
|
@@ -393,7 +423,6 @@ class Libp2p extends EventEmitter {
|
|
|
393
423
|
|
|
394
424
|
this._discovery = new Map()
|
|
395
425
|
|
|
396
|
-
await this.peerStore.stop()
|
|
397
426
|
await this.connectionManager.stop()
|
|
398
427
|
|
|
399
428
|
await Promise.all([
|
|
@@ -404,6 +433,8 @@ class Libp2p extends EventEmitter {
|
|
|
404
433
|
await this.natManager.stop()
|
|
405
434
|
await this.transportManager.close()
|
|
406
435
|
|
|
436
|
+
this.unhandle(FetchService.PROTOCOL)
|
|
437
|
+
|
|
407
438
|
ping.unmount(this)
|
|
408
439
|
this.dialer.destroy()
|
|
409
440
|
} catch (/** @type {any} */ err) {
|
|
@@ -499,7 +530,7 @@ class Libp2p extends EventEmitter {
|
|
|
499
530
|
if (!connection) {
|
|
500
531
|
connection = await this.dialer.connectToPeer(peer, options)
|
|
501
532
|
} else if (multiaddrs) {
|
|
502
|
-
this.peerStore.addressBook.add(id, multiaddrs)
|
|
533
|
+
await this.peerStore.addressBook.add(id, multiaddrs)
|
|
503
534
|
}
|
|
504
535
|
|
|
505
536
|
return connection
|
|
@@ -556,6 +587,17 @@ class Libp2p extends EventEmitter {
|
|
|
556
587
|
)
|
|
557
588
|
}
|
|
558
589
|
|
|
590
|
+
/**
|
|
591
|
+
* Sends a request to fetch the value associated with the given key from the given peer.
|
|
592
|
+
*
|
|
593
|
+
* @param {PeerId|Multiaddr} peer
|
|
594
|
+
* @param {string} key
|
|
595
|
+
* @returns {Promise<Uint8Array | null>}
|
|
596
|
+
*/
|
|
597
|
+
fetch (peer, key) {
|
|
598
|
+
return this.fetchService.fetch(peer, key)
|
|
599
|
+
}
|
|
600
|
+
|
|
559
601
|
/**
|
|
560
602
|
* Pings the given peer in order to obtain the operation latency.
|
|
561
603
|
*
|
|
@@ -579,14 +621,14 @@ class Libp2p extends EventEmitter {
|
|
|
579
621
|
* @param {string[]|string} protocols
|
|
580
622
|
* @param {(props: HandlerProps) => void} handler
|
|
581
623
|
*/
|
|
582
|
-
handle (protocols, handler) {
|
|
624
|
+
async handle (protocols, handler) {
|
|
583
625
|
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
584
626
|
protocols.forEach(protocol => {
|
|
585
627
|
this.upgrader.protocols.set(protocol, handler)
|
|
586
628
|
})
|
|
587
629
|
|
|
588
630
|
// Add new protocols to self protocols in the Protobook
|
|
589
|
-
this.peerStore.protoBook.add(this.peerId, protocols)
|
|
631
|
+
await this.peerStore.protoBook.add(this.peerId, protocols)
|
|
590
632
|
}
|
|
591
633
|
|
|
592
634
|
/**
|
|
@@ -595,14 +637,14 @@ class Libp2p extends EventEmitter {
|
|
|
595
637
|
*
|
|
596
638
|
* @param {string[]|string} protocols
|
|
597
639
|
*/
|
|
598
|
-
unhandle (protocols) {
|
|
640
|
+
async unhandle (protocols) {
|
|
599
641
|
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
600
642
|
protocols.forEach(protocol => {
|
|
601
643
|
this.upgrader.protocols.delete(protocol)
|
|
602
644
|
})
|
|
603
645
|
|
|
604
646
|
// Remove protocols from self protocols in the Protobook
|
|
605
|
-
this.peerStore.protoBook.remove(this.peerId, protocols)
|
|
647
|
+
await this.peerStore.protoBook.remove(this.peerId, protocols)
|
|
606
648
|
}
|
|
607
649
|
|
|
608
650
|
async _onStarting () {
|
|
@@ -613,11 +655,8 @@ class Libp2p extends EventEmitter {
|
|
|
613
655
|
// Manage your NATs
|
|
614
656
|
this.natManager.start()
|
|
615
657
|
|
|
616
|
-
// Start PeerStore
|
|
617
|
-
await this.peerStore.start()
|
|
618
|
-
|
|
619
658
|
if (this._config.pubsub.enabled) {
|
|
620
|
-
this.pubsub && this.pubsub.start()
|
|
659
|
+
this.pubsub && await this.pubsub.start()
|
|
621
660
|
}
|
|
622
661
|
|
|
623
662
|
// DHT subsystem
|
|
@@ -631,6 +670,10 @@ class Libp2p extends EventEmitter {
|
|
|
631
670
|
|
|
632
671
|
// Start metrics if present
|
|
633
672
|
this.metrics && this.metrics.start()
|
|
673
|
+
|
|
674
|
+
if (this.identifyService) {
|
|
675
|
+
await this.identifyService.start()
|
|
676
|
+
}
|
|
634
677
|
}
|
|
635
678
|
|
|
636
679
|
/**
|
|
@@ -643,17 +686,19 @@ class Libp2p extends EventEmitter {
|
|
|
643
686
|
|
|
644
687
|
this.peerStore.on('peer', peerId => {
|
|
645
688
|
this.emit('peer:discovery', peerId)
|
|
646
|
-
this._maybeConnect(peerId)
|
|
689
|
+
this._maybeConnect(peerId).catch(err => {
|
|
690
|
+
log.error(err)
|
|
691
|
+
})
|
|
647
692
|
})
|
|
648
693
|
|
|
649
694
|
// Once we start, emit any peers we may have already discovered
|
|
650
695
|
// TODO: this should be removed, as we already discovered these peers in the past
|
|
651
|
-
for (const peer of this.peerStore.
|
|
696
|
+
for await (const peer of this.peerStore.getPeers()) {
|
|
652
697
|
this.emit('peer:discovery', peer.id)
|
|
653
698
|
}
|
|
654
699
|
|
|
655
700
|
this.connectionManager.start()
|
|
656
|
-
this._autodialler.start()
|
|
701
|
+
await this._autodialler.start()
|
|
657
702
|
|
|
658
703
|
// Peer discovery
|
|
659
704
|
await this._setupPeerDiscovery()
|
|
@@ -677,8 +722,8 @@ class Libp2p extends EventEmitter {
|
|
|
677
722
|
return
|
|
678
723
|
}
|
|
679
724
|
|
|
680
|
-
peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs)
|
|
681
|
-
peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols)
|
|
725
|
+
peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
|
|
726
|
+
peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
|
|
682
727
|
}
|
|
683
728
|
|
|
684
729
|
/**
|
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
|
/**
|
|
@@ -6,18 +6,27 @@
|
|
|
6
6
|
*/
|
|
7
7
|
class TrackedMap extends Map {
|
|
8
8
|
/**
|
|
9
|
-
* @param {
|
|
10
|
-
* @param {string}
|
|
11
|
-
* @param {
|
|
9
|
+
* @param {object} options
|
|
10
|
+
* @param {string} options.system
|
|
11
|
+
* @param {string} options.component
|
|
12
|
+
* @param {string} options.metric
|
|
13
|
+
* @param {import('.')} options.metrics
|
|
12
14
|
*/
|
|
13
|
-
constructor (
|
|
15
|
+
constructor (options) {
|
|
14
16
|
super()
|
|
15
17
|
|
|
18
|
+
const { system, component, metric, metrics } = options
|
|
19
|
+
this._system = system
|
|
16
20
|
this._component = component
|
|
17
|
-
this.
|
|
21
|
+
this._metric = metric
|
|
18
22
|
this._metrics = metrics
|
|
19
23
|
|
|
20
|
-
this._metrics.updateComponentMetric(
|
|
24
|
+
this._metrics.updateComponentMetric({
|
|
25
|
+
system: this._system,
|
|
26
|
+
component: this._component,
|
|
27
|
+
metric: this._metric,
|
|
28
|
+
value: this.size
|
|
29
|
+
})
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
/**
|
|
@@ -26,7 +35,12 @@ class TrackedMap extends Map {
|
|
|
26
35
|
*/
|
|
27
36
|
set (key, value) {
|
|
28
37
|
super.set(key, value)
|
|
29
|
-
this._metrics.updateComponentMetric(
|
|
38
|
+
this._metrics.updateComponentMetric({
|
|
39
|
+
system: this._system,
|
|
40
|
+
component: this._component,
|
|
41
|
+
metric: this._metric,
|
|
42
|
+
value: this.size
|
|
43
|
+
})
|
|
30
44
|
return this
|
|
31
45
|
}
|
|
32
46
|
|
|
@@ -35,31 +49,43 @@ class TrackedMap extends Map {
|
|
|
35
49
|
*/
|
|
36
50
|
delete (key) {
|
|
37
51
|
const deleted = super.delete(key)
|
|
38
|
-
this._metrics.updateComponentMetric(
|
|
52
|
+
this._metrics.updateComponentMetric({
|
|
53
|
+
system: this._system,
|
|
54
|
+
component: this._component,
|
|
55
|
+
metric: this._metric,
|
|
56
|
+
value: this.size
|
|
57
|
+
})
|
|
39
58
|
return deleted
|
|
40
59
|
}
|
|
41
60
|
|
|
42
61
|
clear () {
|
|
43
62
|
super.clear()
|
|
44
63
|
|
|
45
|
-
this._metrics.updateComponentMetric(
|
|
64
|
+
this._metrics.updateComponentMetric({
|
|
65
|
+
system: this._system,
|
|
66
|
+
component: this._component,
|
|
67
|
+
metric: this._metric,
|
|
68
|
+
value: this.size
|
|
69
|
+
})
|
|
46
70
|
}
|
|
47
71
|
}
|
|
48
72
|
|
|
49
73
|
/**
|
|
50
74
|
* @template K
|
|
51
75
|
* @template V
|
|
52
|
-
* @param {
|
|
53
|
-
* @param {string}
|
|
54
|
-
* @param {
|
|
76
|
+
* @param {object} options
|
|
77
|
+
* @param {string} [options.system]
|
|
78
|
+
* @param {string} options.component
|
|
79
|
+
* @param {string} options.metric
|
|
80
|
+
* @param {import('.')} [options.metrics]
|
|
55
81
|
* @returns {Map<K, V>}
|
|
56
82
|
*/
|
|
57
|
-
module.exports = (component,
|
|
83
|
+
module.exports = ({ system = 'libp2p', component, metric, metrics }) => {
|
|
58
84
|
/** @type {Map<K, V>} */
|
|
59
85
|
let map
|
|
60
86
|
|
|
61
87
|
if (metrics) {
|
|
62
|
-
map = new TrackedMap(component,
|
|
88
|
+
map = new TrackedMap({ system, component, metric, metrics })
|
|
63
89
|
} else {
|
|
64
90
|
map = new Map()
|
|
65
91
|
}
|
package/src/peer-routing.js
CHANGED
|
@@ -154,7 +154,10 @@ class PeerRouting {
|
|
|
154
154
|
const controller = new TimeoutController(options.timeout)
|
|
155
155
|
// this controller will potentially be used while dialing lots of
|
|
156
156
|
// peers so prevent MaxListenersExceededWarning appearing in the console
|
|
157
|
-
|
|
157
|
+
try {
|
|
158
|
+
// fails on node < 15.4
|
|
159
|
+
setMaxListeners && setMaxListeners(Infinity, controller.signal)
|
|
160
|
+
} catch {}
|
|
158
161
|
|
|
159
162
|
options.signal = controller.signal
|
|
160
163
|
}
|