libp2p 0.39.5 → 0.40.0-030dbc8
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 +14 -2
- package/dist/src/address-manager/index.d.ts +18 -2
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +19 -6
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/circuit/auto-relay.d.ts +2 -2
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +12 -12
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/index.d.ts +13 -3
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +1 -1
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +29 -34
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts +22 -7
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +26 -17
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/components.d.ts +110 -0
- package/dist/src/components.d.ts.map +1 -0
- package/dist/src/components.js +199 -0
- package/dist/src/components.js.map +1 -0
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -11
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +9 -2
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +6 -6
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/connection-manager/dialer/index.d.ts +12 -6
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +12 -17
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +41 -17
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +80 -81
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +7 -2
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +4 -4
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +1 -1
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/errors.d.ts +2 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +1 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/index.d.ts +7 -3
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +18 -6
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +24 -35
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/index.d.ts +13 -3
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +41 -41
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js +23 -34
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/index.d.ts +13 -12
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts +2 -8
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +5 -3
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +24 -29
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/keychain/index.d.ts +6 -2
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +19 -19
- package/dist/src/keychain/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +1 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +49 -73
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/nat-manager.d.ts +9 -2
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +3 -3
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-record-updater.d.ts +11 -2
- package/dist/src/peer-record-updater.d.ts.map +1 -1
- package/dist/src/peer-record-updater.js +10 -10
- package/dist/src/peer-record-updater.js.map +1 -1
- package/dist/src/peer-routing.d.ts +6 -2
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +4 -4
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts +7 -3
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +3 -3
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/index.d.ts +2 -17
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +4 -1
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +2 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +4 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts +9 -3
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +6 -6
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +9 -3
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +5 -6
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +17 -5
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +55 -75
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +39 -40
- package/src/address-manager/index.ts +34 -10
- package/src/circuit/README.md +6 -6
- package/src/circuit/auto-relay.ts +15 -15
- package/src/circuit/index.ts +16 -5
- package/src/circuit/pb/index.ts +29 -35
- package/src/circuit/transport.ts +49 -20
- package/src/components.ts +332 -0
- package/src/config.ts +0 -11
- package/src/connection-manager/auto-dialler.ts +17 -9
- package/src/connection-manager/dialer/index.ts +26 -25
- package/src/connection-manager/index.ts +136 -93
- package/src/content-routing/index.ts +13 -7
- package/src/errors.ts +2 -1
- package/src/fetch/index.ts +28 -10
- package/src/fetch/pb/proto.ts +24 -35
- package/src/identify/index.ts +57 -53
- package/src/identify/pb/message.ts +23 -35
- package/src/index.ts +12 -11
- package/src/insecure/index.ts +6 -3
- package/src/insecure/pb/proto.ts +24 -29
- package/src/keychain/index.ts +27 -22
- package/src/libp2p.ts +50 -79
- package/src/nat-manager.ts +14 -6
- package/src/peer-record-updater.ts +23 -13
- package/src/peer-routing.ts +12 -7
- package/src/ping/index.ts +12 -7
- package/src/pnet/index.ts +5 -1
- package/src/pubsub/dummy-pubsub.ts +3 -1
- package/src/registrar.ts +17 -10
- package/src/transport-manager.ts +16 -10
- package/src/upgrader.ts +78 -87
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts +0 -16
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/auto-dialer.js +0 -43
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +0 -1
- package/dist/src/metrics/index.d.ts +0 -93
- package/dist/src/metrics/index.d.ts.map +0 -1
- package/dist/src/metrics/index.js +0 -234
- package/dist/src/metrics/index.js.map +0 -1
- package/dist/src/metrics/moving-average.d.ts +0 -14
- package/dist/src/metrics/moving-average.d.ts.map +0 -1
- package/dist/src/metrics/moving-average.js +0 -40
- package/dist/src/metrics/moving-average.js.map +0 -1
- package/dist/src/metrics/stats.d.ts +0 -87
- package/dist/src/metrics/stats.d.ts.map +0 -1
- package/dist/src/metrics/stats.js +0 -183
- package/dist/src/metrics/stats.js.map +0 -1
- package/src/connection-manager/dialer/auto-dialer.ts +0 -65
- package/src/metrics/index.ts +0 -311
- package/src/metrics/moving-average.ts +0 -53
- package/src/metrics/stats.ts +0 -238
|
@@ -2,8 +2,6 @@ import { logger } from '@libp2p/logger'
|
|
|
2
2
|
import errCode from 'err-code'
|
|
3
3
|
import mergeOptions from 'merge-options'
|
|
4
4
|
import { LatencyMonitor, SummaryObject } from './latency-monitor.js'
|
|
5
|
-
// @ts-expect-error retimer does not have types
|
|
6
|
-
import retimer from 'retimer'
|
|
7
5
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
8
6
|
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
9
7
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
@@ -11,14 +9,16 @@ import { codes } from '../errors.js'
|
|
|
11
9
|
import { isPeerId, PeerId } from '@libp2p/interface-peer-id'
|
|
12
10
|
import { setMaxListeners } from 'events'
|
|
13
11
|
import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection'
|
|
14
|
-
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
15
|
-
import { Components, Initializable } from '@libp2p/components'
|
|
12
|
+
import type { ConnectionManager, Dialer } from '@libp2p/interface-connection-manager'
|
|
16
13
|
import * as STATUS from '@libp2p/interface-connection/status'
|
|
17
|
-
import type { AddressSorter } from '@libp2p/interface-peer-store'
|
|
18
|
-
import
|
|
14
|
+
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store'
|
|
15
|
+
import { multiaddr, Multiaddr, Resolver } from '@multiformats/multiaddr'
|
|
19
16
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
20
17
|
import { TimeoutController } from 'timeout-abort-controller'
|
|
21
18
|
import { KEEP_ALIVE } from '@libp2p/interface-peer-store/tags'
|
|
19
|
+
import { RateLimiterMemory } from 'rate-limiter-flexible'
|
|
20
|
+
import type { Metrics } from '@libp2p/interface-metrics'
|
|
21
|
+
import type { Upgrader } from '@libp2p/interface-transport'
|
|
22
22
|
|
|
23
23
|
const log = logger('libp2p:connection-manager')
|
|
24
24
|
|
|
@@ -31,11 +31,11 @@ const defaultOptions: Partial<ConnectionManagerInit> = {
|
|
|
31
31
|
maxEventLoopDelay: Infinity,
|
|
32
32
|
pollInterval: 2000,
|
|
33
33
|
autoDialInterval: 10000,
|
|
34
|
-
movingAverageInterval: 60000
|
|
34
|
+
movingAverageInterval: 60000,
|
|
35
|
+
inboundConnectionThreshold: 5,
|
|
36
|
+
maxIncomingPendingConnections: 10
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
const METRICS_SYSTEM = 'libp2p'
|
|
38
|
-
const METRICS_COMPONENT = 'connection-manager'
|
|
39
39
|
const STARTUP_RECONNECT_TIMEOUT = 60000
|
|
40
40
|
|
|
41
41
|
export interface ConnectionManagerInit {
|
|
@@ -132,6 +132,30 @@ export interface ConnectionManagerInit {
|
|
|
132
132
|
* tagged with KEEP_ALIVE up to this timeout in ms. (default: 60000)
|
|
133
133
|
*/
|
|
134
134
|
startupReconnectTimeout?: number
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A list of multiaddrs that will always be allowed (except if they are in the
|
|
138
|
+
* deny list) to open connections to this node even if we've reached maxConnections
|
|
139
|
+
*/
|
|
140
|
+
allow?: string[]
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* A list of multiaddrs that will never be allowed to open connections to
|
|
144
|
+
* this node under any circumstances
|
|
145
|
+
*/
|
|
146
|
+
deny?: string[]
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* If more than this many connections are opened per second by a single
|
|
150
|
+
* host, reject subsequent connections
|
|
151
|
+
*/
|
|
152
|
+
inboundConnectionThreshold?: number
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The maximum number of parallel incoming connections allowed that have yet to
|
|
156
|
+
* complete the connection upgrade - e.g. choosing connection encryption, muxer, etc
|
|
157
|
+
*/
|
|
158
|
+
maxIncomingPendingConnections?: number
|
|
135
159
|
}
|
|
136
160
|
|
|
137
161
|
export interface ConnectionManagerEvents {
|
|
@@ -139,21 +163,32 @@ export interface ConnectionManagerEvents {
|
|
|
139
163
|
'peer:disconnect': CustomEvent<PeerId>
|
|
140
164
|
}
|
|
141
165
|
|
|
166
|
+
export interface DefaultConnectionManagerComponents {
|
|
167
|
+
peerId: PeerId
|
|
168
|
+
metrics?: Metrics
|
|
169
|
+
upgrader: Upgrader
|
|
170
|
+
peerStore: PeerStore
|
|
171
|
+
dialer: Dialer
|
|
172
|
+
}
|
|
173
|
+
|
|
142
174
|
/**
|
|
143
175
|
* Responsible for managing known connections.
|
|
144
176
|
*/
|
|
145
|
-
export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable
|
|
146
|
-
private components
|
|
177
|
+
export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable {
|
|
178
|
+
private readonly components: DefaultConnectionManagerComponents
|
|
147
179
|
private readonly opts: Required<ConnectionManagerInit>
|
|
148
180
|
private readonly connections: Map<string, Connection[]>
|
|
149
181
|
private started: boolean
|
|
150
|
-
private timer?: ReturnType<retimer>
|
|
151
182
|
private readonly latencyMonitor: LatencyMonitor
|
|
152
183
|
private readonly startupReconnectTimeout: number
|
|
153
184
|
private connectOnStartupController?: TimeoutController
|
|
154
185
|
private readonly dialTimeout: number
|
|
186
|
+
private readonly allow: Multiaddr[]
|
|
187
|
+
private readonly deny: Multiaddr[]
|
|
188
|
+
private readonly inboundConnectionRateLimiter: RateLimiterMemory
|
|
189
|
+
private incomingPendingConnections: number
|
|
155
190
|
|
|
156
|
-
constructor (init: ConnectionManagerInit) {
|
|
191
|
+
constructor (components: DefaultConnectionManagerComponents, init: ConnectionManagerInit) {
|
|
157
192
|
super()
|
|
158
193
|
|
|
159
194
|
this.opts = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
|
|
@@ -164,13 +199,14 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
164
199
|
|
|
165
200
|
log('options: %o', this.opts)
|
|
166
201
|
|
|
202
|
+
this.components = components
|
|
203
|
+
|
|
167
204
|
/**
|
|
168
205
|
* Map of connections per peer
|
|
169
206
|
*/
|
|
170
207
|
this.connections = new Map()
|
|
171
208
|
|
|
172
209
|
this.started = false
|
|
173
|
-
this._checkMetrics = this._checkMetrics.bind(this)
|
|
174
210
|
|
|
175
211
|
this.latencyMonitor = new LatencyMonitor({
|
|
176
212
|
latencyCheckIntervalMs: init.pollInterval,
|
|
@@ -187,18 +223,30 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
187
223
|
|
|
188
224
|
this.startupReconnectTimeout = init.startupReconnectTimeout ?? STARTUP_RECONNECT_TIMEOUT
|
|
189
225
|
this.dialTimeout = init.dialTimeout ?? 30000
|
|
226
|
+
|
|
227
|
+
this.allow = (init.allow ?? []).map(ma => multiaddr(ma))
|
|
228
|
+
this.deny = (init.deny ?? []).map(ma => multiaddr(ma))
|
|
229
|
+
|
|
230
|
+
this.inboundConnectionRateLimiter = new RateLimiterMemory({
|
|
231
|
+
points: this.opts.inboundConnectionThreshold,
|
|
232
|
+
duration: 1
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
this.incomingPendingConnections = 0
|
|
190
236
|
}
|
|
191
237
|
|
|
192
|
-
|
|
193
|
-
this.
|
|
238
|
+
isStarted () {
|
|
239
|
+
return this.started
|
|
240
|
+
}
|
|
194
241
|
|
|
242
|
+
/**
|
|
243
|
+
* Starts the Connection Manager. If Metrics are not enabled on libp2p
|
|
244
|
+
* only event loop and connection limits will be monitored.
|
|
245
|
+
*/
|
|
246
|
+
async start () {
|
|
195
247
|
// track inbound/outbound connections
|
|
196
|
-
this.components.
|
|
197
|
-
|
|
198
|
-
component: METRICS_COMPONENT,
|
|
199
|
-
metric: 'connections',
|
|
200
|
-
label: 'direction',
|
|
201
|
-
value: () => {
|
|
248
|
+
this.components.metrics?.registerMetricGroup('libp2p_connection_manager_connections', {
|
|
249
|
+
calculate: () => {
|
|
202
250
|
const metric = {
|
|
203
251
|
inbound: 0,
|
|
204
252
|
outbound: 0
|
|
@@ -219,12 +267,9 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
219
267
|
})
|
|
220
268
|
|
|
221
269
|
// track total number of streams per protocol
|
|
222
|
-
this.components.
|
|
223
|
-
system: METRICS_SYSTEM,
|
|
224
|
-
component: METRICS_COMPONENT,
|
|
225
|
-
metric: 'protocol-streams-total',
|
|
270
|
+
this.components.metrics?.registerMetricGroup('libp2p_protocol_streams_total', {
|
|
226
271
|
label: 'protocol',
|
|
227
|
-
|
|
272
|
+
calculate: () => {
|
|
228
273
|
const metric: Record<string, number> = {}
|
|
229
274
|
|
|
230
275
|
for (const conns of this.connections.values()) {
|
|
@@ -242,12 +287,9 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
242
287
|
})
|
|
243
288
|
|
|
244
289
|
// track 90th percentile of streams per protocol
|
|
245
|
-
this.components.
|
|
246
|
-
system: METRICS_SYSTEM,
|
|
247
|
-
component: METRICS_COMPONENT,
|
|
248
|
-
metric: 'protocol-streams-per-connection-90th-percentile',
|
|
290
|
+
this.components.metrics?.registerMetricGroup('libp2p_connection_manager_protocol_streams_per_connection_90th_percentile', {
|
|
249
291
|
label: 'protocol',
|
|
250
|
-
|
|
292
|
+
calculate: () => {
|
|
251
293
|
const allStreams: Record<string, number[]> = {}
|
|
252
294
|
|
|
253
295
|
for (const conns of this.connections.values()) {
|
|
@@ -279,20 +321,6 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
279
321
|
return metric
|
|
280
322
|
}
|
|
281
323
|
})
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
isStarted () {
|
|
285
|
-
return this.started
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Starts the Connection Manager. If Metrics are not enabled on libp2p
|
|
290
|
-
* only event loop and connection limits will be monitored.
|
|
291
|
-
*/
|
|
292
|
-
async start () {
|
|
293
|
-
if (this.components.getMetrics() != null) {
|
|
294
|
-
this.timer = this.timer ?? retimer(this._checkMetrics, this.opts.pollInterval)
|
|
295
|
-
}
|
|
296
324
|
|
|
297
325
|
// latency monitor
|
|
298
326
|
this.latencyMonitor.start()
|
|
@@ -304,16 +332,16 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
304
332
|
}
|
|
305
333
|
|
|
306
334
|
async afterStart () {
|
|
307
|
-
this.components.
|
|
308
|
-
this.components.
|
|
335
|
+
this.components.upgrader.addEventListener('connection', this.onConnect)
|
|
336
|
+
this.components.upgrader.addEventListener('connectionEnd', this.onDisconnect)
|
|
309
337
|
|
|
310
338
|
// re-connect to any peers with the KEEP_ALIVE tag
|
|
311
339
|
void Promise.resolve()
|
|
312
340
|
.then(async () => {
|
|
313
341
|
const keepAlivePeers: PeerId[] = []
|
|
314
342
|
|
|
315
|
-
for (const peer of await this.components.
|
|
316
|
-
const tags = await this.components.
|
|
343
|
+
for (const peer of await this.components.peerStore.all()) {
|
|
344
|
+
const tags = await this.components.peerStore.getTags(peer.id)
|
|
317
345
|
const hasKeepAlive = tags.filter(tag => tag.name === KEEP_ALIVE).length > 0
|
|
318
346
|
|
|
319
347
|
if (hasKeepAlive) {
|
|
@@ -351,16 +379,14 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
351
379
|
async beforeStop () {
|
|
352
380
|
// if we are still dialing KEEP_ALIVE peers, abort those dials
|
|
353
381
|
this.connectOnStartupController?.abort()
|
|
354
|
-
this.components.
|
|
355
|
-
this.components.
|
|
382
|
+
this.components.upgrader.removeEventListener('connection', this.onConnect)
|
|
383
|
+
this.components.upgrader.removeEventListener('connectionEnd', this.onDisconnect)
|
|
356
384
|
}
|
|
357
385
|
|
|
358
386
|
/**
|
|
359
387
|
* Stops the Connection Manager
|
|
360
388
|
*/
|
|
361
389
|
async stop () {
|
|
362
|
-
this.timer?.clear()
|
|
363
|
-
|
|
364
390
|
this.latencyMonitor.removeEventListener('data', this._onLatencyMeasure)
|
|
365
391
|
this.latencyMonitor.stop()
|
|
366
392
|
|
|
@@ -392,31 +418,6 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
392
418
|
this.connections.clear()
|
|
393
419
|
}
|
|
394
420
|
|
|
395
|
-
/**
|
|
396
|
-
* Checks the libp2p metrics to determine if any values have exceeded
|
|
397
|
-
* the configured maximums.
|
|
398
|
-
*
|
|
399
|
-
* @private
|
|
400
|
-
*/
|
|
401
|
-
async _checkMetrics () {
|
|
402
|
-
const metrics = this.components.getMetrics()
|
|
403
|
-
|
|
404
|
-
if (metrics != null) {
|
|
405
|
-
try {
|
|
406
|
-
const movingAverages = metrics.getGlobal().getMovingAverages()
|
|
407
|
-
const received = movingAverages.dataReceived[this.opts.movingAverageInterval].movingAverage
|
|
408
|
-
await this._checkMaxLimit('maxReceivedData', received)
|
|
409
|
-
const sent = movingAverages.dataSent[this.opts.movingAverageInterval].movingAverage
|
|
410
|
-
await this._checkMaxLimit('maxSentData', sent)
|
|
411
|
-
const total = received + sent
|
|
412
|
-
await this._checkMaxLimit('maxData', total)
|
|
413
|
-
log.trace('metrics update', total)
|
|
414
|
-
} finally {
|
|
415
|
-
this.timer = retimer(this._checkMetrics, this.opts.pollInterval)
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
421
|
onConnect (evt: CustomEvent<Connection>) {
|
|
421
422
|
void this._onConnect(evt).catch(err => {
|
|
422
423
|
log.error(err)
|
|
@@ -446,7 +447,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
446
447
|
}
|
|
447
448
|
|
|
448
449
|
if (peerId.publicKey != null) {
|
|
449
|
-
await this.components.
|
|
450
|
+
await this.components.peerStore.keyBook.set(peerId, peerId.publicKey)
|
|
450
451
|
}
|
|
451
452
|
|
|
452
453
|
const numConnections = this.getConnections().length
|
|
@@ -476,8 +477,6 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
476
477
|
} else if (storedConn != null) {
|
|
477
478
|
this.connections.delete(peerId)
|
|
478
479
|
this.dispatchEvent(new CustomEvent<Connection>('peer:disconnect', { detail: connection }))
|
|
479
|
-
|
|
480
|
-
this.components.getMetrics()?.onPeerDisconnected(connection.remotePeer)
|
|
481
480
|
}
|
|
482
481
|
}
|
|
483
482
|
|
|
@@ -518,7 +517,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
518
517
|
}
|
|
519
518
|
|
|
520
519
|
try {
|
|
521
|
-
const connection = await this.components.
|
|
520
|
+
const connection = await this.components.dialer.dial(peerId, options)
|
|
522
521
|
let peerConnections = this.connections.get(peerId.toString())
|
|
523
522
|
|
|
524
523
|
if (peerConnections == null) {
|
|
@@ -597,8 +596,8 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
597
596
|
const limit = this.opts[name]
|
|
598
597
|
log.trace('checking limit of %s. current value: %d of %d', name, value, limit)
|
|
599
598
|
if (value > limit) {
|
|
600
|
-
log('%s: limit exceeded: %p, %d/%d, pruning %d connection(s)', this.components.
|
|
601
|
-
await this.
|
|
599
|
+
log('%s: limit exceeded: %p, %d/%d, pruning %d connection(s)', this.components.peerId, name, value, limit, toPrune)
|
|
600
|
+
await this._pruneConnections(toPrune)
|
|
602
601
|
}
|
|
603
602
|
}
|
|
604
603
|
|
|
@@ -606,13 +605,8 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
606
605
|
* If we have more connections than our maximum, select some excess connections
|
|
607
606
|
* to prune based on peer value
|
|
608
607
|
*/
|
|
609
|
-
async
|
|
608
|
+
async _pruneConnections (toPrune: number) {
|
|
610
609
|
const connections = this.getConnections()
|
|
611
|
-
|
|
612
|
-
if (connections.length <= this.opts.minConnections || toPrune < 1) {
|
|
613
|
-
return
|
|
614
|
-
}
|
|
615
|
-
|
|
616
610
|
const peerValues = new PeerMap<number>()
|
|
617
611
|
|
|
618
612
|
// work out peer values
|
|
@@ -623,7 +617,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
623
617
|
continue
|
|
624
618
|
}
|
|
625
619
|
|
|
626
|
-
const tags = await this.components.
|
|
620
|
+
const tags = await this.components.peerStore.getTags(remotePeer)
|
|
627
621
|
|
|
628
622
|
// sum all tag values
|
|
629
623
|
peerValues.set(remotePeer, tags.reduce((acc, curr) => {
|
|
@@ -677,6 +671,55 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
677
671
|
}
|
|
678
672
|
|
|
679
673
|
async acceptIncomingConnection (maConn: MultiaddrConnection): Promise<boolean> {
|
|
680
|
-
|
|
674
|
+
// check deny list
|
|
675
|
+
const denyConnection = this.deny.some(ma => {
|
|
676
|
+
return maConn.remoteAddr.toString().startsWith(ma.toString())
|
|
677
|
+
})
|
|
678
|
+
|
|
679
|
+
if (denyConnection) {
|
|
680
|
+
log('connection from %s refused - connection remote address was in deny list', maConn.remoteAddr)
|
|
681
|
+
return false
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// check allow list
|
|
685
|
+
const allowConnection = this.allow.some(ma => {
|
|
686
|
+
return maConn.remoteAddr.toString().startsWith(ma.toString())
|
|
687
|
+
})
|
|
688
|
+
|
|
689
|
+
if (allowConnection) {
|
|
690
|
+
this.incomingPendingConnections++
|
|
691
|
+
|
|
692
|
+
return true
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// check pending connections
|
|
696
|
+
if (this.incomingPendingConnections === this.opts.maxIncomingPendingConnections) {
|
|
697
|
+
log('connection from %s refused - incomingPendingConnections exceeded by peer %s', maConn.remoteAddr)
|
|
698
|
+
return false
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
if (maConn.remoteAddr.isThinWaistAddress()) {
|
|
702
|
+
const host = maConn.remoteAddr.nodeAddress().address
|
|
703
|
+
|
|
704
|
+
try {
|
|
705
|
+
await this.inboundConnectionRateLimiter.consume(host, 1)
|
|
706
|
+
} catch {
|
|
707
|
+
log('connection from %s refused - inboundConnectionThreshold exceeded by host %s', host, maConn.remoteAddr)
|
|
708
|
+
return false
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
if (this.getConnections().length < this.opts.maxConnections) {
|
|
713
|
+
this.incomingPendingConnections++
|
|
714
|
+
|
|
715
|
+
return true
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
log('connection from %s refused - maxConnections exceeded', maConn.remoteAddr)
|
|
719
|
+
return false
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
afterUpgradeInbound () {
|
|
723
|
+
this.incomingPendingConnections--
|
|
681
724
|
}
|
|
682
725
|
}
|
|
@@ -12,18 +12,24 @@ import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
|
12
12
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
13
13
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
14
14
|
import type { CID } from 'multiformats/cid'
|
|
15
|
-
import type {
|
|
15
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
16
|
+
import type { DualDHT } from '@libp2p/interface-dht'
|
|
16
17
|
|
|
17
18
|
export interface CompoundContentRoutingInit {
|
|
18
19
|
routers: ContentRouting[]
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
export interface CompoundContentRoutingComponents {
|
|
23
|
+
peerStore: PeerStore
|
|
24
|
+
dht?: DualDHT
|
|
25
|
+
}
|
|
26
|
+
|
|
21
27
|
export class CompoundContentRouting implements ContentRouting, Startable {
|
|
22
28
|
private readonly routers: ContentRouting[]
|
|
23
29
|
private started: boolean
|
|
24
|
-
private readonly components:
|
|
30
|
+
private readonly components: CompoundContentRoutingComponents
|
|
25
31
|
|
|
26
|
-
constructor (components:
|
|
32
|
+
constructor (components: CompoundContentRoutingComponents, init: CompoundContentRoutingInit) {
|
|
27
33
|
this.routers = init.routers ?? []
|
|
28
34
|
this.started = false
|
|
29
35
|
this.components = components
|
|
@@ -53,7 +59,7 @@ export class CompoundContentRouting implements ContentRouting, Startable {
|
|
|
53
59
|
merge(
|
|
54
60
|
...this.routers.map(router => router.findProviders(key, options))
|
|
55
61
|
),
|
|
56
|
-
(source) => storeAddresses(source, this.components.
|
|
62
|
+
(source) => storeAddresses(source, this.components.peerStore),
|
|
57
63
|
(source) => uniquePeers(source),
|
|
58
64
|
(source) => requirePeers(source)
|
|
59
65
|
)
|
|
@@ -79,7 +85,7 @@ export class CompoundContentRouting implements ContentRouting, Startable {
|
|
|
79
85
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
|
80
86
|
}
|
|
81
87
|
|
|
82
|
-
const dht = this.components.
|
|
88
|
+
const dht = this.components.dht
|
|
83
89
|
|
|
84
90
|
if (dht != null) {
|
|
85
91
|
await drain(dht.put(key, value, options))
|
|
@@ -95,7 +101,7 @@ export class CompoundContentRouting implements ContentRouting, Startable {
|
|
|
95
101
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
|
|
96
102
|
}
|
|
97
103
|
|
|
98
|
-
const dht = this.components.
|
|
104
|
+
const dht = this.components.dht
|
|
99
105
|
|
|
100
106
|
if (dht != null) {
|
|
101
107
|
for await (const event of dht.get(key, options)) {
|
|
@@ -121,7 +127,7 @@ export class CompoundContentRouting implements ContentRouting, Startable {
|
|
|
121
127
|
}
|
|
122
128
|
|
|
123
129
|
let gotValues = 0
|
|
124
|
-
const dht = this.components.
|
|
130
|
+
const dht = this.components.dht
|
|
125
131
|
|
|
126
132
|
if (dht != null) {
|
|
127
133
|
for await (const event of dht.get(key, options)) {
|
package/src/errors.ts
CHANGED
|
@@ -73,5 +73,6 @@ export enum codes {
|
|
|
73
73
|
ERR_ALREADY_SUCCEEDED = 'ERR_ALREADY_SUCCEEDED',
|
|
74
74
|
ERR_NO_HANDLER_FOR_PROTOCOL = 'ERR_NO_HANDLER_FOR_PROTOCOL',
|
|
75
75
|
ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS',
|
|
76
|
-
ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS'
|
|
76
|
+
ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS',
|
|
77
|
+
ERR_CONNECTION_DENIED = 'ERR_CONNECTION_DENIED'
|
|
77
78
|
}
|
package/src/fetch/index.ts
CHANGED
|
@@ -7,14 +7,16 @@ import { PROTOCOL_NAME, PROTOCOL_VERSION } from './constants.js'
|
|
|
7
7
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
8
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
9
9
|
import type { Stream } from '@libp2p/interface-connection'
|
|
10
|
-
import type { IncomingStreamData } from '@libp2p/interface-registrar'
|
|
11
|
-
import type { Components } from '@libp2p/components'
|
|
10
|
+
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
12
11
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
13
12
|
import { abortableDuplex } from 'abortable-iterator'
|
|
14
13
|
import { pipe } from 'it-pipe'
|
|
15
14
|
import first from 'it-first'
|
|
16
15
|
import { TimeoutController } from 'timeout-abort-controller'
|
|
17
16
|
import { setMaxListeners } from 'events'
|
|
17
|
+
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
|
|
18
|
+
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
|
|
19
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
18
20
|
|
|
19
21
|
const log = logger('libp2p:fetch')
|
|
20
22
|
|
|
@@ -38,6 +40,11 @@ export interface LookupFunction {
|
|
|
38
40
|
(key: string): Promise<Uint8Array | null>
|
|
39
41
|
}
|
|
40
42
|
|
|
43
|
+
export interface FetchServiceComponents {
|
|
44
|
+
registrar: Registrar
|
|
45
|
+
connectionManager: ConnectionManager
|
|
46
|
+
}
|
|
47
|
+
|
|
41
48
|
/**
|
|
42
49
|
* A simple libp2p protocol for requesting a value corresponding to a key from a peer.
|
|
43
50
|
* Developers can register one or more lookup function for retrieving the value corresponding to
|
|
@@ -46,12 +53,12 @@ export interface LookupFunction {
|
|
|
46
53
|
*/
|
|
47
54
|
export class FetchService implements Startable {
|
|
48
55
|
public readonly protocol: string
|
|
49
|
-
private readonly components:
|
|
56
|
+
private readonly components: FetchServiceComponents
|
|
50
57
|
private readonly lookupFunctions: Map<string, LookupFunction>
|
|
51
58
|
private started: boolean
|
|
52
59
|
private readonly init: FetchServiceInit
|
|
53
60
|
|
|
54
|
-
constructor (components:
|
|
61
|
+
constructor (components: FetchServiceComponents, init: FetchServiceInit) {
|
|
55
62
|
this.started = false
|
|
56
63
|
this.components = components
|
|
57
64
|
this.protocol = `/${init.protocolPrefix ?? 'libp2p'}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
|
|
@@ -61,7 +68,7 @@ export class FetchService implements Startable {
|
|
|
61
68
|
}
|
|
62
69
|
|
|
63
70
|
async start () {
|
|
64
|
-
await this.components.
|
|
71
|
+
await this.components.registrar.handle(this.protocol, (data) => {
|
|
65
72
|
void this.handleMessage(data)
|
|
66
73
|
.catch(err => {
|
|
67
74
|
log.error(err)
|
|
@@ -77,7 +84,7 @@ export class FetchService implements Startable {
|
|
|
77
84
|
}
|
|
78
85
|
|
|
79
86
|
async stop () {
|
|
80
|
-
await this.components.
|
|
87
|
+
await this.components.registrar.unhandle(this.protocol)
|
|
81
88
|
this.started = false
|
|
82
89
|
}
|
|
83
90
|
|
|
@@ -91,13 +98,14 @@ export class FetchService implements Startable {
|
|
|
91
98
|
async fetch (peer: PeerId, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
|
|
92
99
|
log('dialing %s to %p', this.protocol, peer)
|
|
93
100
|
|
|
94
|
-
const connection = await this.components.
|
|
101
|
+
const connection = await this.components.connectionManager.openConnection(peer, options)
|
|
95
102
|
let timeoutController
|
|
96
103
|
let signal = options.signal
|
|
97
104
|
let stream: Stream | undefined
|
|
98
105
|
|
|
99
106
|
// create a timeout if no abort signal passed
|
|
100
107
|
if (signal == null) {
|
|
108
|
+
log('using default timeout of %d ms', this.init.timeout)
|
|
101
109
|
timeoutController = new TimeoutController(this.init.timeout)
|
|
102
110
|
signal = timeoutController.signal
|
|
103
111
|
|
|
@@ -108,13 +116,15 @@ export class FetchService implements Startable {
|
|
|
108
116
|
}
|
|
109
117
|
|
|
110
118
|
try {
|
|
111
|
-
stream = await connection.newStream(
|
|
119
|
+
stream = await connection.newStream(this.protocol, {
|
|
112
120
|
signal
|
|
113
121
|
})
|
|
114
122
|
|
|
115
123
|
// make stream abortable
|
|
116
124
|
const source = abortableDuplex(stream, signal)
|
|
117
125
|
|
|
126
|
+
log('fetch %s', key)
|
|
127
|
+
|
|
118
128
|
const result = await pipe(
|
|
119
129
|
[FetchRequest.encode({ identifier: key })],
|
|
120
130
|
lp.encode(),
|
|
@@ -131,16 +141,20 @@ export class FetchService implements Startable {
|
|
|
131
141
|
|
|
132
142
|
switch (response.status) {
|
|
133
143
|
case (FetchResponse.StatusCode.OK): {
|
|
144
|
+
log('received status for %s ok', key)
|
|
134
145
|
return response.data
|
|
135
146
|
}
|
|
136
147
|
case (FetchResponse.StatusCode.NOT_FOUND): {
|
|
148
|
+
log('received status for %s not found', key)
|
|
137
149
|
return null
|
|
138
150
|
}
|
|
139
151
|
case (FetchResponse.StatusCode.ERROR): {
|
|
140
|
-
|
|
152
|
+
log('received status for %s error', key)
|
|
153
|
+
const errmsg = uint8arrayToString(response.data)
|
|
141
154
|
throw errCode(new Error('Error in fetch protocol response: ' + errmsg), codes.ERR_INVALID_PARAMETERS)
|
|
142
155
|
}
|
|
143
156
|
default: {
|
|
157
|
+
log('received status for %s unknown', key)
|
|
144
158
|
throw errCode(new Error('Unknown response status'), codes.ERR_INVALID_MESSAGE)
|
|
145
159
|
}
|
|
146
160
|
}
|
|
@@ -184,14 +198,18 @@ export class FetchService implements Startable {
|
|
|
184
198
|
let response: FetchResponse
|
|
185
199
|
const lookup = self._getLookupFunction(request.identifier)
|
|
186
200
|
if (lookup != null) {
|
|
201
|
+
log('look up data with identifier %s', request.identifier)
|
|
187
202
|
const data = await lookup(request.identifier)
|
|
188
203
|
if (data != null) {
|
|
204
|
+
log('sending status for %s ok', request.identifier)
|
|
189
205
|
response = { status: FetchResponse.StatusCode.OK, data }
|
|
190
206
|
} else {
|
|
207
|
+
log('sending status for %s not found', request.identifier)
|
|
191
208
|
response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }
|
|
192
209
|
}
|
|
193
210
|
} else {
|
|
194
|
-
|
|
211
|
+
log('sending status for %s error', request.identifier)
|
|
212
|
+
const errmsg = uint8arrayFromString(`No lookup function registered for key: ${request.identifier}`)
|
|
195
213
|
response = { status: FetchResponse.StatusCode.ERROR, data: errmsg }
|
|
196
214
|
}
|
|
197
215
|
|