libp2p 0.45.9-5315f7bc → 0.45.9-57c32721
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 +24 -24
- package/dist/src/address-manager/utils.d.ts.map +1 -1
- package/dist/src/address-manager/utils.js.map +1 -1
- package/dist/src/autonat/index.d.ts +20 -0
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +20 -0
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/index.d.ts +35 -0
- package/dist/src/circuit-relay/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/index.js +35 -0
- package/dist/src/circuit-relay/index.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts +1 -1
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +21 -19
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/index.d.ts +11 -0
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/index.js +47 -20
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +1 -0
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +6 -0
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +22 -8
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
- package/dist/src/connection/index.d.ts +19 -10
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +52 -28
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +3 -1
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +2 -2
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/constants.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +5 -10
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +1 -1
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +11 -5
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/fetch/constants.d.ts.map +1 -1
- package/dist/src/fetch/constants.js.map +1 -1
- package/dist/src/fetch/index.js +4 -4
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/consts.d.ts.map +1 -1
- package/dist/src/identify/consts.js.map +1 -1
- package/dist/src/identify/identify.d.ts +5 -4
- package/dist/src/identify/identify.d.ts.map +1 -1
- package/dist/src/identify/identify.js +63 -69
- package/dist/src/identify/identify.js.map +1 -1
- package/dist/src/identify/index.d.ts +22 -3
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +3 -3
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +2 -2
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/ping/constants.d.ts.map +1 -1
- package/dist/src/ping/constants.js.map +1 -1
- package/dist/src/ping/index.d.ts +1 -0
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +31 -20
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +3 -0
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +2 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +36 -25
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +28 -31
- package/src/address-manager/utils.ts +0 -1
- package/src/autonat/index.ts +21 -0
- package/src/circuit-relay/index.ts +36 -0
- package/src/circuit-relay/server/index.ts +24 -21
- package/src/circuit-relay/transport/index.ts +61 -22
- package/src/circuit-relay/transport/listener.ts +2 -0
- package/src/circuit-relay/transport/reservation-store.ts +33 -10
- package/src/connection/index.ts +78 -36
- package/src/connection-manager/auto-dial.ts +3 -1
- package/src/connection-manager/connection-pruner.ts +2 -2
- package/src/connection-manager/constants.ts +0 -1
- package/src/connection-manager/dial-queue.ts +8 -14
- package/src/connection-manager/index.ts +11 -5
- package/src/fetch/constants.ts +0 -1
- package/src/fetch/index.ts +4 -4
- package/src/identify/consts.ts +0 -1
- package/src/identify/identify.ts +73 -79
- package/src/identify/index.ts +26 -2
- package/src/libp2p.ts +4 -4
- package/src/ping/constants.ts +0 -1
- package/src/ping/index.ts +38 -20
- package/src/transport-manager.ts +4 -0
- package/src/upgrader.ts +40 -26
- package/src/version.ts +1 -1
|
@@ -5,7 +5,7 @@ import { peerIdFromBytes, peerIdFromString } from '@libp2p/peer-id'
|
|
|
5
5
|
import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
|
|
6
6
|
import * as mafmt from '@multiformats/mafmt'
|
|
7
7
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
8
|
-
import { pbStream } from 'it-
|
|
8
|
+
import { pbStream } from 'it-protobuf-stream'
|
|
9
9
|
import { MAX_CONNECTIONS } from '../../connection-manager/constants.js'
|
|
10
10
|
import { codes } from '../../errors.js'
|
|
11
11
|
import { CIRCUIT_PROTO_CODE, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js'
|
|
@@ -85,11 +85,25 @@ export interface CircuitRelayTransportInit extends RelayStoreInit {
|
|
|
85
85
|
* should be set to the same value (default: 300)
|
|
86
86
|
*/
|
|
87
87
|
maxOutboundStopStreams?: number
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Incoming STOP requests (e.g. when a remote peer wants to dial us via a relay)
|
|
91
|
+
* must finish the initial protocol negotiation within this timeout in ms
|
|
92
|
+
* (default: 30000)
|
|
93
|
+
*/
|
|
94
|
+
stopTimeout?: number
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* When creating a reservation it must complete within this number of ms
|
|
98
|
+
* (default: 10000)
|
|
99
|
+
*/
|
|
100
|
+
reservationCompletionTimeout?: number
|
|
88
101
|
}
|
|
89
102
|
|
|
90
103
|
const defaults = {
|
|
91
104
|
maxInboundStopStreams: MAX_CONNECTIONS,
|
|
92
|
-
maxOutboundStopStreams: MAX_CONNECTIONS
|
|
105
|
+
maxOutboundStopStreams: MAX_CONNECTIONS,
|
|
106
|
+
stopTimeout: 30000
|
|
93
107
|
}
|
|
94
108
|
|
|
95
109
|
class CircuitRelayTransport implements Transport {
|
|
@@ -104,6 +118,7 @@ class CircuitRelayTransport implements Transport {
|
|
|
104
118
|
private readonly reservationStore: ReservationStore
|
|
105
119
|
private readonly maxInboundStopStreams: number
|
|
106
120
|
private readonly maxOutboundStopStreams?: number
|
|
121
|
+
private readonly stopTimeout: number
|
|
107
122
|
private started: boolean
|
|
108
123
|
|
|
109
124
|
constructor (components: CircuitRelayTransportComponents, init: CircuitRelayTransportInit) {
|
|
@@ -116,6 +131,7 @@ class CircuitRelayTransport implements Transport {
|
|
|
116
131
|
this.connectionGater = components.connectionGater
|
|
117
132
|
this.maxInboundStopStreams = init.maxInboundStopStreams ?? defaults.maxInboundStopStreams
|
|
118
133
|
this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams
|
|
134
|
+
this.stopTimeout = init.stopTimeout ?? defaults.stopTimeout
|
|
119
135
|
|
|
120
136
|
if (init.discoverRelays != null && init.discoverRelays > 0) {
|
|
121
137
|
this.discovery = new RelayDiscovery(components)
|
|
@@ -148,11 +164,13 @@ class CircuitRelayTransport implements Transport {
|
|
|
148
164
|
|
|
149
165
|
await this.registrar.handle(RELAY_V2_STOP_CODEC, (data) => {
|
|
150
166
|
void this.onStop(data).catch(err => {
|
|
151
|
-
log.error(err)
|
|
167
|
+
log.error('error while handling STOP protocol', err)
|
|
168
|
+
data.stream.abort(err)
|
|
152
169
|
})
|
|
153
170
|
}, {
|
|
154
171
|
maxInboundStreams: this.maxInboundStopStreams,
|
|
155
|
-
maxOutboundStreams: this.maxOutboundStopStreams
|
|
172
|
+
maxOutboundStreams: this.maxOutboundStopStreams,
|
|
173
|
+
runOnTransientConnection: true
|
|
156
174
|
})
|
|
157
175
|
|
|
158
176
|
this.started = true
|
|
@@ -244,7 +262,7 @@ class CircuitRelayTransport implements Transport {
|
|
|
244
262
|
try {
|
|
245
263
|
const pbstr = pbStream(stream)
|
|
246
264
|
const hopstr = pbstr.pb(HopMessage)
|
|
247
|
-
hopstr.write({
|
|
265
|
+
await hopstr.write({
|
|
248
266
|
type: HopMessage.Type.CONNECT,
|
|
249
267
|
peer: {
|
|
250
268
|
id: destinationPeer.toBytes(),
|
|
@@ -258,16 +276,16 @@ class CircuitRelayTransport implements Transport {
|
|
|
258
276
|
throw new CodeError(`failed to connect via relay with status ${status?.status?.toString() ?? 'undefined'}`, codes.ERR_HOP_REQUEST_FAILED)
|
|
259
277
|
}
|
|
260
278
|
|
|
261
|
-
// TODO: do something with limit and transient connection
|
|
262
|
-
|
|
263
279
|
const maConn = streamToMaConnection({
|
|
264
280
|
stream: pbstr.unwrap(),
|
|
265
281
|
remoteAddr: ma,
|
|
266
282
|
localAddr: relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerId.toString()}`)
|
|
267
283
|
})
|
|
268
284
|
|
|
269
|
-
log('new outbound connection %a', maConn.remoteAddr)
|
|
270
|
-
return await this.upgrader.upgradeOutbound(maConn
|
|
285
|
+
log('new outbound transient connection %a', maConn.remoteAddr)
|
|
286
|
+
return await this.upgrader.upgradeOutbound(maConn, {
|
|
287
|
+
transient: true
|
|
288
|
+
})
|
|
271
289
|
} catch (err) {
|
|
272
290
|
log.error(`Circuit relay dial to destination ${destinationPeer.toString()} via relay ${connection.remotePeer.toString()} failed`, err)
|
|
273
291
|
disconnectOnFailure && await connection.close()
|
|
@@ -303,49 +321,70 @@ class CircuitRelayTransport implements Transport {
|
|
|
303
321
|
* An incoming STOP request means a remote peer wants to dial us via a relay
|
|
304
322
|
*/
|
|
305
323
|
async onStop ({ connection, stream }: IncomingStreamData): Promise<void> {
|
|
306
|
-
const
|
|
307
|
-
const
|
|
308
|
-
|
|
324
|
+
const signal = AbortSignal.timeout(this.stopTimeout)
|
|
325
|
+
const pbstr = pbStream(stream).pb(StopMessage)
|
|
326
|
+
const request = await pbstr.read({
|
|
327
|
+
signal
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
log('new circuit relay v2 stop stream from %p with type %s', connection.remotePeer, request.type)
|
|
309
331
|
|
|
310
332
|
if (request?.type === undefined) {
|
|
333
|
+
log.error('type was missing from circuit v2 stop protocol request from %s', connection.remotePeer)
|
|
334
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }, {
|
|
335
|
+
signal
|
|
336
|
+
})
|
|
337
|
+
await stream.close()
|
|
311
338
|
return
|
|
312
339
|
}
|
|
313
340
|
|
|
314
|
-
const stopstr = pbstr.pb(StopMessage)
|
|
315
|
-
log('new circuit relay v2 stop stream from %p', connection.remotePeer)
|
|
316
|
-
|
|
317
341
|
// Validate the STOP request has the required input
|
|
318
342
|
if (request.type !== StopMessage.Type.CONNECT) {
|
|
319
343
|
log.error('invalid stop connect request via peer %p', connection.remotePeer)
|
|
320
|
-
|
|
344
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE }, {
|
|
345
|
+
signal
|
|
346
|
+
})
|
|
347
|
+
await stream.close()
|
|
321
348
|
return
|
|
322
349
|
}
|
|
323
350
|
|
|
324
351
|
if (!isValidStop(request)) {
|
|
325
352
|
log.error('invalid stop connect request via peer %p', connection.remotePeer)
|
|
326
|
-
|
|
353
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }, {
|
|
354
|
+
signal
|
|
355
|
+
})
|
|
356
|
+
await stream.close()
|
|
327
357
|
return
|
|
328
358
|
}
|
|
329
359
|
|
|
330
360
|
const remotePeerId = peerIdFromBytes(request.peer.id)
|
|
331
361
|
|
|
332
362
|
if ((await this.connectionGater.denyInboundRelayedConnection?.(connection.remotePeer, remotePeerId)) === true) {
|
|
333
|
-
|
|
363
|
+
log.error('connection gater denied inbound relayed connection from %p', connection.remotePeer)
|
|
364
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, {
|
|
365
|
+
signal
|
|
366
|
+
})
|
|
367
|
+
await stream.close()
|
|
334
368
|
return
|
|
335
369
|
}
|
|
336
370
|
|
|
337
|
-
|
|
371
|
+
log.trace('sending success response to %p', connection.remotePeer)
|
|
372
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.OK }, {
|
|
373
|
+
signal
|
|
374
|
+
})
|
|
338
375
|
|
|
339
376
|
const remoteAddr = connection.remoteAddr.encapsulate(`/p2p-circuit/p2p/${remotePeerId.toString()}`)
|
|
340
377
|
const localAddr = this.addressManager.getAddresses()[0]
|
|
341
378
|
const maConn = streamToMaConnection({
|
|
342
|
-
stream: pbstr.unwrap(),
|
|
379
|
+
stream: pbstr.unwrap().unwrap(),
|
|
343
380
|
remoteAddr,
|
|
344
381
|
localAddr
|
|
345
382
|
})
|
|
346
383
|
|
|
347
|
-
log('new inbound connection %
|
|
348
|
-
await this.upgrader.upgradeInbound(maConn
|
|
384
|
+
log('new inbound transient connection %a', maConn.remoteAddr)
|
|
385
|
+
await this.upgrader.upgradeInbound(maConn, {
|
|
386
|
+
transient: true
|
|
387
|
+
})
|
|
349
388
|
log('%s connection %a upgraded', 'inbound', maConn.remoteAddr)
|
|
350
389
|
}
|
|
351
390
|
}
|
|
@@ -95,6 +95,8 @@ class CircuitRelayTransportListener extends EventEmitter<ListenerEvents> impleme
|
|
|
95
95
|
#removeRelayPeer (peerId: PeerId): void {
|
|
96
96
|
const had = this.listeningAddrs.has(peerId)
|
|
97
97
|
|
|
98
|
+
log('relay peer removed %p - had reservation', peerId, had)
|
|
99
|
+
|
|
98
100
|
this.listeningAddrs.delete(peerId)
|
|
99
101
|
|
|
100
102
|
if (had) {
|
|
@@ -2,13 +2,13 @@ import { EventEmitter } from '@libp2p/interface/events'
|
|
|
2
2
|
import { logger } from '@libp2p/logger'
|
|
3
3
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
4
4
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
5
|
-
import { pbStream } from 'it-
|
|
5
|
+
import { pbStream } from 'it-protobuf-stream'
|
|
6
6
|
import { PeerJobQueue } from '../../utils/peer-job-queue.js'
|
|
7
7
|
import { DEFAULT_RESERVATION_CONCURRENCY, RELAY_TAG, RELAY_V2_HOP_CODEC } from '../constants.js'
|
|
8
8
|
import { HopMessage, Status } from '../pb/index.js'
|
|
9
9
|
import { getExpirationMilliseconds } from '../utils.js'
|
|
10
10
|
import type { Reservation } from '../pb/index.js'
|
|
11
|
-
import type { Libp2pEvents } from '@libp2p/interface'
|
|
11
|
+
import type { Libp2pEvents, AbortOptions } from '@libp2p/interface'
|
|
12
12
|
import type { Connection } from '@libp2p/interface/connection'
|
|
13
13
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
14
14
|
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
@@ -55,6 +55,12 @@ export interface RelayStoreInit {
|
|
|
55
55
|
* Limit the number of potential relays we will dial (default: 100)
|
|
56
56
|
*/
|
|
57
57
|
maxReservationQueueLength?: number
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* When creating a reservation it must complete within this number of ms
|
|
61
|
+
* (default: 5000)
|
|
62
|
+
*/
|
|
63
|
+
reservationCompletionTimeout?: number
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
export type RelayType = 'discovered' | 'configured'
|
|
@@ -80,6 +86,7 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
|
|
|
80
86
|
private readonly reservations: PeerMap<RelayEntry>
|
|
81
87
|
private readonly maxDiscoveredRelays: number
|
|
82
88
|
private readonly maxReservationQueueLength: number
|
|
89
|
+
private readonly reservationCompletionTimeout: number
|
|
83
90
|
private started: boolean
|
|
84
91
|
|
|
85
92
|
constructor (components: RelayStoreComponents, init?: RelayStoreInit) {
|
|
@@ -93,6 +100,7 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
|
|
|
93
100
|
this.reservations = new PeerMap()
|
|
94
101
|
this.maxDiscoveredRelays = init?.discoverRelays ?? 0
|
|
95
102
|
this.maxReservationQueueLength = init?.maxReservationQueueLength ?? 100
|
|
103
|
+
this.reservationCompletionTimeout = init?.reservationCompletionTimeout ?? 10000
|
|
96
104
|
this.started = false
|
|
97
105
|
|
|
98
106
|
// ensure we don't listen on multiple relays simultaneously
|
|
@@ -117,12 +125,12 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
|
|
|
117
125
|
}
|
|
118
126
|
|
|
119
127
|
async stop (): Promise<void> {
|
|
128
|
+
this.reserveQueue.clear()
|
|
120
129
|
this.reservations.forEach(({ timeout }) => {
|
|
121
130
|
clearTimeout(timeout)
|
|
122
131
|
})
|
|
123
132
|
this.reservations.clear()
|
|
124
|
-
|
|
125
|
-
this.started = true
|
|
133
|
+
this.started = false
|
|
126
134
|
}
|
|
127
135
|
|
|
128
136
|
/**
|
|
@@ -175,14 +183,20 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
|
|
|
175
183
|
return
|
|
176
184
|
}
|
|
177
185
|
|
|
178
|
-
const
|
|
186
|
+
const signal = AbortSignal.timeout(this.reservationCompletionTimeout)
|
|
187
|
+
|
|
188
|
+
const connection = await this.connectionManager.openConnection(peerId, {
|
|
189
|
+
signal
|
|
190
|
+
})
|
|
179
191
|
|
|
180
192
|
if (connection.remoteAddr.protoNames().includes('p2p-circuit')) {
|
|
181
193
|
log('not creating reservation over relayed connection')
|
|
182
194
|
return
|
|
183
195
|
}
|
|
184
196
|
|
|
185
|
-
const reservation = await this.#createReservation(connection
|
|
197
|
+
const reservation = await this.#createReservation(connection, {
|
|
198
|
+
signal
|
|
199
|
+
})
|
|
186
200
|
|
|
187
201
|
log('created reservation on relay peer %p', peerId)
|
|
188
202
|
|
|
@@ -220,6 +234,13 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
|
|
|
220
234
|
} catch (err) {
|
|
221
235
|
log.error('could not reserve slot on %p', peerId, err)
|
|
222
236
|
|
|
237
|
+
// cancel the renewal timeout if it's been set
|
|
238
|
+
const reservation = this.reservations.get(peerId)
|
|
239
|
+
|
|
240
|
+
if (reservation != null) {
|
|
241
|
+
clearTimeout(reservation.timeout)
|
|
242
|
+
}
|
|
243
|
+
|
|
223
244
|
// if listening failed, remove the reservation
|
|
224
245
|
this.reservations.delete(peerId)
|
|
225
246
|
}
|
|
@@ -236,22 +257,24 @@ export class ReservationStore extends EventEmitter<ReservationStoreEvents> imple
|
|
|
236
257
|
return this.reservations.get(peerId)?.reservation
|
|
237
258
|
}
|
|
238
259
|
|
|
239
|
-
async #createReservation (connection: Connection): Promise<Reservation> {
|
|
260
|
+
async #createReservation (connection: Connection, options: AbortOptions): Promise<Reservation> {
|
|
261
|
+
options.signal?.throwIfAborted()
|
|
262
|
+
|
|
240
263
|
log('requesting reservation from %p', connection.remotePeer)
|
|
241
264
|
const stream = await connection.newStream(RELAY_V2_HOP_CODEC)
|
|
242
265
|
const pbstr = pbStream(stream)
|
|
243
266
|
const hopstr = pbstr.pb(HopMessage)
|
|
244
|
-
hopstr.write({ type: HopMessage.Type.RESERVE })
|
|
267
|
+
await hopstr.write({ type: HopMessage.Type.RESERVE }, options)
|
|
245
268
|
|
|
246
269
|
let response: HopMessage
|
|
247
270
|
|
|
248
271
|
try {
|
|
249
|
-
response = await hopstr.read()
|
|
272
|
+
response = await hopstr.read(options)
|
|
250
273
|
} catch (err: any) {
|
|
251
274
|
log.error('error parsing reserve message response from %p because', connection.remotePeer, err)
|
|
252
275
|
throw err
|
|
253
276
|
} finally {
|
|
254
|
-
stream.close()
|
|
277
|
+
await stream.close()
|
|
255
278
|
}
|
|
256
279
|
|
|
257
280
|
if (response.status === Status.OK && (response.reservation != null)) {
|
package/src/connection/index.ts
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
|
+
import { setMaxListeners } from 'events'
|
|
1
2
|
import { symbol } from '@libp2p/interface/connection'
|
|
2
|
-
import { OPEN, CLOSING, CLOSED } from '@libp2p/interface/connection/status'
|
|
3
3
|
import { CodeError } from '@libp2p/interface/errors'
|
|
4
4
|
import { logger } from '@libp2p/logger'
|
|
5
5
|
import type { AbortOptions } from '@libp2p/interface'
|
|
6
|
-
import type {
|
|
6
|
+
import type { Direction, Connection, Stream, ConnectionTimeline, ConnectionStatus, NewStreamOptions } from '@libp2p/interface/connection'
|
|
7
7
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
8
8
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
9
|
|
|
10
10
|
const log = logger('libp2p:connection')
|
|
11
11
|
|
|
12
|
+
const CLOSE_TIMEOUT = 500
|
|
13
|
+
|
|
12
14
|
interface ConnectionInit {
|
|
13
15
|
remoteAddr: Multiaddr
|
|
14
16
|
remotePeer: PeerId
|
|
15
17
|
newStream: (protocols: string[], options?: AbortOptions) => Promise<Stream>
|
|
16
|
-
close: () => Promise<void>
|
|
18
|
+
close: (options?: AbortOptions) => Promise<void>
|
|
19
|
+
abort: (err: Error) => void
|
|
17
20
|
getStreams: () => Stream[]
|
|
18
|
-
|
|
21
|
+
status: ConnectionStatus
|
|
22
|
+
direction: Direction
|
|
23
|
+
timeline: ConnectionTimeline
|
|
24
|
+
multiplexer?: string
|
|
25
|
+
encryption?: string
|
|
26
|
+
transient?: boolean
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
/**
|
|
@@ -38,10 +46,12 @@ export class ConnectionImpl implements Connection {
|
|
|
38
46
|
*/
|
|
39
47
|
public readonly remotePeer: PeerId
|
|
40
48
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
public
|
|
49
|
+
public direction: Direction
|
|
50
|
+
public timeline: ConnectionTimeline
|
|
51
|
+
public multiplexer?: string
|
|
52
|
+
public encryption?: string
|
|
53
|
+
public status: ConnectionStatus
|
|
54
|
+
public transient: boolean
|
|
45
55
|
|
|
46
56
|
/**
|
|
47
57
|
* User provided tags
|
|
@@ -52,39 +62,42 @@ export class ConnectionImpl implements Connection {
|
|
|
52
62
|
/**
|
|
53
63
|
* Reference to the new stream function of the multiplexer
|
|
54
64
|
*/
|
|
55
|
-
private readonly _newStream: (protocols: string[], options?:
|
|
65
|
+
private readonly _newStream: (protocols: string[], options?: NewStreamOptions) => Promise<Stream>
|
|
56
66
|
|
|
57
67
|
/**
|
|
58
68
|
* Reference to the close function of the raw connection
|
|
59
69
|
*/
|
|
60
|
-
private readonly _close: () => Promise<void>
|
|
70
|
+
private readonly _close: (options?: AbortOptions) => Promise<void>
|
|
71
|
+
|
|
72
|
+
private readonly _abort: (err: Error) => void
|
|
61
73
|
|
|
62
74
|
/**
|
|
63
75
|
* Reference to the getStreams function of the muxer
|
|
64
76
|
*/
|
|
65
77
|
private readonly _getStreams: () => Stream[]
|
|
66
78
|
|
|
67
|
-
private _closing: boolean
|
|
68
|
-
|
|
69
79
|
/**
|
|
70
80
|
* An implementation of the js-libp2p connection.
|
|
71
81
|
* Any libp2p transport should use an upgrader to return this connection.
|
|
72
82
|
*/
|
|
73
83
|
constructor (init: ConnectionInit) {
|
|
74
|
-
const { remoteAddr, remotePeer, newStream, close,
|
|
84
|
+
const { remoteAddr, remotePeer, newStream, close, abort, getStreams } = init
|
|
75
85
|
|
|
76
86
|
this.id = `${(parseInt(String(Math.random() * 1e9))).toString(36)}${Date.now()}`
|
|
77
87
|
this.remoteAddr = remoteAddr
|
|
78
88
|
this.remotePeer = remotePeer
|
|
79
|
-
this.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
this.direction = init.direction
|
|
90
|
+
this.status = 'open'
|
|
91
|
+
this.timeline = init.timeline
|
|
92
|
+
this.multiplexer = init.multiplexer
|
|
93
|
+
this.encryption = init.encryption
|
|
94
|
+
this.transient = init.transient ?? false
|
|
95
|
+
|
|
83
96
|
this._newStream = newStream
|
|
84
97
|
this._close = close
|
|
98
|
+
this._abort = abort
|
|
85
99
|
this._getStreams = getStreams
|
|
86
100
|
this.tags = []
|
|
87
|
-
this._closing = false
|
|
88
101
|
}
|
|
89
102
|
|
|
90
103
|
readonly [Symbol.toStringTag] = 'Connection'
|
|
@@ -101,12 +114,12 @@ export class ConnectionImpl implements Connection {
|
|
|
101
114
|
/**
|
|
102
115
|
* Create a new stream from this connection
|
|
103
116
|
*/
|
|
104
|
-
async newStream (protocols: string | string[], options?:
|
|
105
|
-
if (this.
|
|
117
|
+
async newStream (protocols: string | string[], options?: NewStreamOptions): Promise<Stream> {
|
|
118
|
+
if (this.status === 'closing') {
|
|
106
119
|
throw new CodeError('the connection is being closed', 'ERR_CONNECTION_BEING_CLOSED')
|
|
107
120
|
}
|
|
108
121
|
|
|
109
|
-
if (this.
|
|
122
|
+
if (this.status === 'closed') {
|
|
110
123
|
throw new CodeError('the connection is closed', 'ERR_CONNECTION_CLOSED')
|
|
111
124
|
}
|
|
112
125
|
|
|
@@ -114,9 +127,13 @@ export class ConnectionImpl implements Connection {
|
|
|
114
127
|
protocols = [protocols]
|
|
115
128
|
}
|
|
116
129
|
|
|
130
|
+
if (this.transient && options?.runOnTransientConnection !== true) {
|
|
131
|
+
throw new CodeError('Cannot open protocol stream on transient connection', 'ERR_TRANSIENT_CONNECTION')
|
|
132
|
+
}
|
|
133
|
+
|
|
117
134
|
const stream = await this._newStream(protocols, options)
|
|
118
135
|
|
|
119
|
-
stream.
|
|
136
|
+
stream.direction = 'outbound'
|
|
120
137
|
|
|
121
138
|
return stream
|
|
122
139
|
}
|
|
@@ -125,7 +142,7 @@ export class ConnectionImpl implements Connection {
|
|
|
125
142
|
* Add a stream when it is opened to the registry
|
|
126
143
|
*/
|
|
127
144
|
addStream (stream: Stream): void {
|
|
128
|
-
stream.
|
|
145
|
+
stream.direction = 'inbound'
|
|
129
146
|
}
|
|
130
147
|
|
|
131
148
|
/**
|
|
@@ -138,27 +155,52 @@ export class ConnectionImpl implements Connection {
|
|
|
138
155
|
/**
|
|
139
156
|
* Close the connection
|
|
140
157
|
*/
|
|
141
|
-
async close (): Promise<void> {
|
|
142
|
-
if (this.
|
|
158
|
+
async close (options: AbortOptions = {}): Promise<void> {
|
|
159
|
+
if (this.status === 'closed' || this.status === 'closing') {
|
|
143
160
|
return
|
|
144
161
|
}
|
|
145
162
|
|
|
146
|
-
this.
|
|
163
|
+
log('closing connection to %a', this.remoteAddr)
|
|
164
|
+
|
|
165
|
+
this.status = 'closing'
|
|
166
|
+
|
|
167
|
+
options.signal = options?.signal ?? AbortSignal.timeout(CLOSE_TIMEOUT)
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
// fails on node < 15.4
|
|
171
|
+
setMaxListeners?.(Infinity, options.signal)
|
|
172
|
+
} catch { }
|
|
147
173
|
|
|
148
|
-
// close all streams - this can throw if we're not multiplexed
|
|
149
174
|
try {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
175
|
+
// close all streams gracefully - this can throw if we're not multiplexed
|
|
176
|
+
await Promise.all(
|
|
177
|
+
this.streams.map(async s => s.close(options))
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
// Close raw connection
|
|
181
|
+
await this._close(options)
|
|
182
|
+
|
|
183
|
+
this.timeline.close = Date.now()
|
|
184
|
+
this.status = 'closed'
|
|
185
|
+
} catch (err: any) {
|
|
186
|
+
log.error('error encountered during graceful close of connection to %a', this.remoteAddr, err)
|
|
187
|
+
this.abort(err)
|
|
153
188
|
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
abort (err: Error): void {
|
|
192
|
+
log.error('aborting connection to %a due to error', this.remoteAddr, err)
|
|
193
|
+
|
|
194
|
+
this.status = 'closing'
|
|
195
|
+
this.streams.forEach(s => { s.abort(err) })
|
|
196
|
+
|
|
197
|
+
log.error('all streams aborted', this.streams.length)
|
|
154
198
|
|
|
155
|
-
//
|
|
156
|
-
this.
|
|
157
|
-
await this._close()
|
|
158
|
-
this._closing = false
|
|
199
|
+
// Abort raw connection
|
|
200
|
+
this._abort(err)
|
|
159
201
|
|
|
160
|
-
this.
|
|
161
|
-
this.
|
|
202
|
+
this.timeline.close = Date.now()
|
|
203
|
+
this.status = 'closed'
|
|
162
204
|
}
|
|
163
205
|
}
|
|
164
206
|
|
|
@@ -113,7 +113,9 @@ export class AutoDial implements Startable {
|
|
|
113
113
|
|
|
114
114
|
// Already has enough connections
|
|
115
115
|
if (numConnections >= this.minConnections) {
|
|
116
|
-
|
|
116
|
+
if (this.minConnections > 0) {
|
|
117
|
+
log.trace('have enough connections %d/%d', numConnections, this.minConnections)
|
|
118
|
+
}
|
|
117
119
|
return
|
|
118
120
|
}
|
|
119
121
|
|
|
@@ -106,8 +106,8 @@ export class ConnectionPruner {
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
// if the peers have an equal tag value then we want to close short-lived connections first
|
|
109
|
-
const connectionALifespan = a.
|
|
110
|
-
const connectionBLifespan = b.
|
|
109
|
+
const connectionALifespan = a.timeline.open
|
|
110
|
+
const connectionBLifespan = b.timeline.open
|
|
111
111
|
|
|
112
112
|
if (connectionALifespan < connectionBLifespan) {
|
|
113
113
|
return 1
|
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
MAX_PEER_ADDRS_TO_DIAL
|
|
17
17
|
} from './constants.js'
|
|
18
18
|
import { combineSignals, resolveMultiaddrs } from './utils.js'
|
|
19
|
-
import type { AddressSorter, AbortOptions } from '@libp2p/interface'
|
|
19
|
+
import type { AddressSorter, AbortOptions, PendingDial } from '@libp2p/interface'
|
|
20
20
|
import type { Connection } from '@libp2p/interface/connection'
|
|
21
21
|
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
|
|
22
22
|
import type { Metric, Metrics } from '@libp2p/interface/metrics'
|
|
@@ -26,16 +26,6 @@ import type { TransportManager } from '@libp2p/interface-internal/transport-mana
|
|
|
26
26
|
|
|
27
27
|
const log = logger('libp2p:connection-manager:dial-queue')
|
|
28
28
|
|
|
29
|
-
export type PendingDialStatus = 'queued' | 'active' | 'error' | 'success'
|
|
30
|
-
|
|
31
|
-
export interface PendingDial {
|
|
32
|
-
id: string
|
|
33
|
-
status: PendingDialStatus
|
|
34
|
-
peerId?: PeerId
|
|
35
|
-
multiaddrs: Multiaddr[]
|
|
36
|
-
promise: Promise<Connection>
|
|
37
|
-
}
|
|
38
|
-
|
|
39
29
|
export interface PendingDialTarget {
|
|
40
30
|
resolve: (value: any) => void
|
|
41
31
|
reject: (err: Error) => void
|
|
@@ -45,6 +35,10 @@ export interface DialOptions extends AbortOptions {
|
|
|
45
35
|
priority?: number
|
|
46
36
|
}
|
|
47
37
|
|
|
38
|
+
interface PendingDialInternal extends PendingDial {
|
|
39
|
+
promise: Promise<Connection>
|
|
40
|
+
}
|
|
41
|
+
|
|
48
42
|
interface DialerInit {
|
|
49
43
|
addressSorter?: AddressSorter
|
|
50
44
|
maxParallelDials?: number
|
|
@@ -74,7 +68,7 @@ interface DialQueueComponents {
|
|
|
74
68
|
}
|
|
75
69
|
|
|
76
70
|
export class DialQueue {
|
|
77
|
-
public pendingDials:
|
|
71
|
+
public pendingDials: PendingDialInternal[]
|
|
78
72
|
public queue: PQueue
|
|
79
73
|
private readonly peerId: PeerId
|
|
80
74
|
private readonly peerStore: PeerStore
|
|
@@ -218,7 +212,7 @@ export class DialQueue {
|
|
|
218
212
|
|
|
219
213
|
log('creating dial target for', addrsToDial.map(({ multiaddr }) => multiaddr.toString()))
|
|
220
214
|
// @ts-expect-error .promise property is set below
|
|
221
|
-
const pendingDial:
|
|
215
|
+
const pendingDial: PendingDialInternal = {
|
|
222
216
|
id: randomId(),
|
|
223
217
|
status: 'queued',
|
|
224
218
|
peerId,
|
|
@@ -394,7 +388,7 @@ export class DialQueue {
|
|
|
394
388
|
return sortedGatedAddrs
|
|
395
389
|
}
|
|
396
390
|
|
|
397
|
-
private async performDial (pendingDial:
|
|
391
|
+
private async performDial (pendingDial: PendingDialInternal, options: DialOptions = {}): Promise<Connection> {
|
|
398
392
|
const dialAbortControllers: Array<(AbortController | undefined)> = pendingDial.multiaddrs.map(() => new AbortController())
|
|
399
393
|
|
|
400
394
|
try {
|
|
@@ -273,7 +273,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
273
273
|
|
|
274
274
|
for (const conns of this.connections.values()) {
|
|
275
275
|
for (const conn of conns) {
|
|
276
|
-
if (conn.
|
|
276
|
+
if (conn.direction === 'inbound') {
|
|
277
277
|
metric.inbound++
|
|
278
278
|
} else {
|
|
279
279
|
metric.outbound++
|
|
@@ -294,7 +294,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
294
294
|
for (const conns of this.connections.values()) {
|
|
295
295
|
for (const conn of conns) {
|
|
296
296
|
for (const stream of conn.streams) {
|
|
297
|
-
const key = `${stream.
|
|
297
|
+
const key = `${stream.direction} ${stream.protocol ?? 'unnegotiated'}`
|
|
298
298
|
|
|
299
299
|
metric[key] = (metric[key] ?? 0) + 1
|
|
300
300
|
}
|
|
@@ -316,7 +316,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
316
316
|
const streams: Record<string, number> = {}
|
|
317
317
|
|
|
318
318
|
for (const stream of conn.streams) {
|
|
319
|
-
const key = `${stream.
|
|
319
|
+
const key = `${stream.direction} ${stream.protocol ?? 'unnegotiated'}`
|
|
320
320
|
|
|
321
321
|
streams[key] = (streams[key] ?? 0) + 1
|
|
322
322
|
}
|
|
@@ -488,6 +488,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
488
488
|
throw new CodeError('Not started', codes.ERR_NODE_NOT_STARTED)
|
|
489
489
|
}
|
|
490
490
|
|
|
491
|
+
options.signal?.throwIfAborted()
|
|
492
|
+
|
|
491
493
|
const { peerId } = getPeerAddress(peerIdOrMultiaddr)
|
|
492
494
|
|
|
493
495
|
if (peerId != null) {
|
|
@@ -530,12 +532,16 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
530
532
|
return connection
|
|
531
533
|
}
|
|
532
534
|
|
|
533
|
-
async closeConnections (peerId: PeerId): Promise<void> {
|
|
535
|
+
async closeConnections (peerId: PeerId, options: AbortOptions = {}): Promise<void> {
|
|
534
536
|
const connections = this.connections.get(peerId) ?? []
|
|
535
537
|
|
|
536
538
|
await Promise.all(
|
|
537
539
|
connections.map(async connection => {
|
|
538
|
-
|
|
540
|
+
try {
|
|
541
|
+
await connection.close(options)
|
|
542
|
+
} catch (err: any) {
|
|
543
|
+
connection.abort(err)
|
|
544
|
+
}
|
|
539
545
|
})
|
|
540
546
|
)
|
|
541
547
|
}
|
package/src/fetch/constants.ts
CHANGED