libp2p 2.1.1 → 2.1.2-0c5957836

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.
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable max-depth */
2
- import { TimeoutError, DialError, setMaxListeners } from '@libp2p/interface'
2
+ import { TimeoutError, DialError, setMaxListeners, AbortError } from '@libp2p/interface'
3
3
  import { PeerMap } from '@libp2p/peer-collections'
4
4
  import { defaultAddressSort } from '@libp2p/utils/address-sort'
5
5
  import { PriorityQueue, type PriorityQueueJobOptions } from '@libp2p/utils/priority-queue'
@@ -103,7 +103,9 @@ export class DialQueue {
103
103
  })
104
104
  // a started job errored
105
105
  this.queue.addEventListener('error', (event) => {
106
- this.log.error('error in dial queue', event.detail)
106
+ if (event.detail.name !== AbortError.name) {
107
+ this.log.error('error in dial queue - %e', event.detail)
108
+ }
107
109
  })
108
110
  }
109
111
 
@@ -1,4 +1,4 @@
1
- import { InvalidParametersError, NotStartedError, start, stop } from '@libp2p/interface'
1
+ import { InvalidMultiaddrError, InvalidParametersError, InvalidPeerIdError, NotStartedError, start, stop } from '@libp2p/interface'
2
2
  import { PeerMap } from '@libp2p/peer-collections'
3
3
  import { defaultAddressSort } from '@libp2p/utils/address-sort'
4
4
  import { RateLimiter } from '@libp2p/utils/rate-limiter'
@@ -57,17 +57,34 @@ export interface ConnectionManagerInit {
57
57
  /**
58
58
  * How long a dial attempt is allowed to take, including DNS resolution
59
59
  * of the multiaddr, opening a socket and upgrading it to a Connection.
60
+ *
61
+ * @default 5000
60
62
  */
61
63
  dialTimeout?: number
62
64
 
63
65
  /**
64
- * When a new inbound connection is opened, the upgrade process (e.g. protect,
65
- * encrypt, multiplex etc) must complete within this number of ms.
66
+ * When a new incoming connection is opened, the upgrade process (e.g.
67
+ * protect, encrypt, multiplex etc) must complete within this number of ms.
66
68
  *
67
- * @default 30000
69
+ * @default 3000
68
70
  */
69
71
  inboundUpgradeTimeout?: number
70
72
 
73
+ /**
74
+ * When a new outbound connection is opened, the upgrade process (e.g.
75
+ * protect, encrypt, multiplex etc) must complete within this number of ms.
76
+ *
77
+ * @default 3000
78
+ */
79
+ outboundUpgradeTimeout?: number
80
+
81
+ /**
82
+ * Protocol negotiation must complete within this number of ms
83
+ *
84
+ * @default 2000
85
+ */
86
+ protocolNegotiationTimeout?: number
87
+
71
88
  /**
72
89
  * Multiaddr resolvers to use when dialling
73
90
  */
@@ -164,7 +181,6 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
164
181
  private readonly deny: Multiaddr[]
165
182
  private readonly maxIncomingPendingConnections: number
166
183
  private incomingPendingConnections: number
167
- private outboundPendingConnections: number
168
184
  private readonly maxConnections: number
169
185
 
170
186
  public readonly dialQueue: DialQueue
@@ -175,6 +191,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
175
191
  private readonly metrics?: Metrics
176
192
  private readonly events: TypedEventTarget<Libp2pEvents>
177
193
  private readonly log: Logger
194
+ private readonly peerId: PeerId
178
195
 
179
196
  constructor (components: DefaultConnectionManagerComponents, init: ConnectionManagerInit = {}) {
180
197
  this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections
@@ -189,6 +206,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
189
206
  this.connections = new PeerMap()
190
207
 
191
208
  this.started = false
209
+ this.peerId = components.peerId
192
210
  this.peerStore = components.peerStore
193
211
  this.metrics = components.metrics
194
212
  this.events = components.events
@@ -203,7 +221,6 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
203
221
  this.allow = (init.allow ?? []).map(ma => multiaddr(ma))
204
222
  this.deny = (init.deny ?? []).map(ma => multiaddr(ma))
205
223
 
206
- this.outboundPendingConnections = 0
207
224
  this.incomingPendingConnections = 0
208
225
  this.maxIncomingPendingConnections = init.maxIncomingPendingConnections ?? defaultOptions.maxIncomingPendingConnections
209
226
 
@@ -266,8 +283,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
266
283
  const metric = {
267
284
  inbound: 0,
268
285
  'inbound pending': this.incomingPendingConnections,
269
- outbound: 0,
270
- 'outbound pending': this.outboundPendingConnections
286
+ outbound: 0
271
287
  }
272
288
 
273
289
  for (const conns of this.connections.values()) {
@@ -468,54 +484,67 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
468
484
 
469
485
  options.signal?.throwIfAborted()
470
486
 
471
- try {
472
- this.outboundPendingConnections++
487
+ const { peerId } = getPeerAddress(peerIdOrMultiaddr)
473
488
 
474
- const { peerId } = getPeerAddress(peerIdOrMultiaddr)
489
+ if (this.peerId.equals(peerId)) {
490
+ throw new InvalidPeerIdError('Can not dial self')
491
+ }
475
492
 
476
- if (peerId != null && options.force !== true) {
477
- this.log('dial %p', peerId)
478
- const existingConnection = this.getConnections(peerId)
479
- .find(conn => conn.limits == null)
493
+ if (peerId != null && options.force !== true) {
494
+ this.log('dial %p', peerId)
495
+ const existingConnection = this.getConnections(peerId)
496
+ .find(conn => conn.limits == null)
480
497
 
481
- if (existingConnection != null) {
482
- this.log('had an existing non-limited connection to %p', peerId)
498
+ if (existingConnection != null) {
499
+ this.log('had an existing non-limited connection to %p', peerId)
483
500
 
484
- options.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
485
- return existingConnection
486
- }
501
+ options.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
502
+ return existingConnection
487
503
  }
504
+ }
488
505
 
489
- const connection = await this.dialQueue.dial(peerIdOrMultiaddr, {
490
- ...options,
491
- priority: options.priority ?? DEFAULT_DIAL_PRIORITY
492
- })
493
- let peerConnections = this.connections.get(connection.remotePeer)
506
+ const connection = await this.dialQueue.dial(peerIdOrMultiaddr, {
507
+ ...options,
508
+ priority: options.priority ?? DEFAULT_DIAL_PRIORITY
509
+ })
494
510
 
495
- if (peerConnections == null) {
496
- peerConnections = []
497
- this.connections.set(connection.remotePeer, peerConnections)
498
- }
511
+ if (connection.remotePeer.equals(this.peerId)) {
512
+ const err = new InvalidPeerIdError('Can not dial self')
513
+ connection.abort(err)
514
+ throw err
515
+ }
499
516
 
500
- // we get notified of connections via the Upgrader emitting "connection"
501
- // events, double check we aren't already tracking this connection before
502
- // storing it
503
- let trackedConnection = false
517
+ let peerConnections = this.connections.get(connection.remotePeer)
504
518
 
505
- for (const conn of peerConnections) {
506
- if (conn.id === connection.id) {
507
- trackedConnection = true
508
- }
519
+ if (peerConnections == null) {
520
+ peerConnections = []
521
+ this.connections.set(connection.remotePeer, peerConnections)
522
+ }
523
+
524
+ // we get notified of connections via the Upgrader emitting "connection"
525
+ // events, double check we aren't already tracking this connection before
526
+ // storing it
527
+ let trackedConnection = false
528
+
529
+ for (const conn of peerConnections) {
530
+ if (conn.id === connection.id) {
531
+ trackedConnection = true
509
532
  }
510
533
 
511
- if (!trackedConnection) {
512
- peerConnections.push(connection)
534
+ // make sure we don't already have a connection to this multiaddr
535
+ if (options.force !== true && conn.id !== connection.id && conn.remoteAddr.equals(connection.remoteAddr)) {
536
+ connection.abort(new InvalidMultiaddrError('Duplicate multiaddr connection'))
537
+
538
+ // return the existing connection
539
+ return conn
513
540
  }
541
+ }
514
542
 
515
- return connection
516
- } finally {
517
- this.outboundPendingConnections--
543
+ if (!trackedConnection) {
544
+ peerConnections.push(connection)
518
545
  }
546
+
547
+ return connection
519
548
  }
520
549
 
521
550
  async closeConnections (peerId: PeerId, options: AbortOptions = {}): Promise<void> {
package/src/libp2p.ts CHANGED
@@ -110,7 +110,8 @@ export class Libp2p<T extends ServiceMap = ServiceMap> extends TypedEventEmitter
110
110
  this.components.upgrader = new DefaultUpgrader(this.components, {
111
111
  connectionEncrypters: (init.connectionEncrypters ?? []).map((fn, index) => this.configureComponent(`connection-encryption-${index}`, fn(this.components))),
112
112
  streamMuxers: (init.streamMuxers ?? []).map((fn, index) => this.configureComponent(`stream-muxers-${index}`, fn(this.components))),
113
- inboundUpgradeTimeout: init.connectionManager?.inboundUpgradeTimeout
113
+ inboundUpgradeTimeout: init.connectionManager?.inboundUpgradeTimeout,
114
+ outboundUpgradeTimeout: init.connectionManager?.outboundUpgradeTimeout
114
115
  })
115
116
 
116
117
  // Setup the transport manager