libp2p 2.7.3-e2f49432b → 2.7.4-0555339ba

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.
@@ -203,7 +203,7 @@ export class DialQueue {
203
203
 
204
204
  options.onProgress?.(new CustomProgressEvent('dial-queue:add-to-dial-queue'))
205
205
  return this.queue.add(async (options) => {
206
- options?.onProgress?.(new CustomProgressEvent('dial-queue:start-dial'))
206
+ options.onProgress?.(new CustomProgressEvent('dial-queue:start-dial'))
207
207
  // create abort conditions - need to do this before `calculateMultiaddrs` as
208
208
  // we may be about to resolve a dns addr which can time out
209
209
  const signal = anySignal([
@@ -212,103 +212,145 @@ export class DialQueue {
212
212
  ])
213
213
  setMaxListeners(Infinity, signal)
214
214
 
215
- let addrsToDial: Address[]
216
-
217
215
  try {
218
- // load addresses from address book, resolve and dnsaddrs, filter
219
- // undialables, add peer IDs, etc
220
- addrsToDial = await this.calculateMultiaddrs(peerId, options?.multiaddrs, {
221
- ...options,
222
- signal
223
- })
216
+ return await this.dialPeer(options, signal)
217
+ } finally {
218
+ // clean up abort signals/controllers
219
+ signal.clear()
220
+ }
221
+ }, {
222
+ peerId,
223
+ priority: options.priority ?? DEFAULT_DIAL_PRIORITY,
224
+ multiaddrs: new Set(multiaddrs.map(ma => ma.toString())),
225
+ signal: options.signal ?? AbortSignal.timeout(this.dialTimeout),
226
+ onProgress: options.onProgress
227
+ })
228
+ }
224
229
 
225
- options?.onProgress?.(new CustomProgressEvent<Address[]>('dial-queue:calculated-addresses', addrsToDial))
230
+ private async dialPeer (options: DialQueueJobOptions, signal: AbortSignal): Promise<Connection> {
231
+ const peerId = options.peerId
232
+ const multiaddrs = options.multiaddrs
233
+ const failedMultiaddrs = new Set<string>()
226
234
 
227
- addrsToDial.map(({ multiaddr }) => multiaddr.toString()).forEach(addr => {
228
- options?.multiaddrs.add(addr)
229
- })
230
- } catch (err) {
231
- signal.clear()
232
- throw err
235
+ // if we have no multiaddrs, only a peer id, set a flag so we will look the
236
+ // peer up in the peer routing to obtain multiaddrs
237
+ let forcePeerLookup = options.multiaddrs.size === 0
238
+
239
+ let dialed = 0
240
+ let dialIteration = 0
241
+ const errors: Error[] = []
242
+
243
+ this.log('starting dial to %p', peerId)
244
+
245
+ // repeat this operation in case addresses are added to the dial while we
246
+ // resolve multiaddrs, etc
247
+ while (forcePeerLookup || multiaddrs.size > 0) {
248
+ dialIteration++
249
+
250
+ // only perform peer lookup once
251
+ forcePeerLookup = false
252
+
253
+ // the addresses we will dial
254
+ const addrsToDial: Address[] = []
255
+
256
+ // copy the addresses into a new set
257
+ const addrs = new Set(options.multiaddrs)
258
+
259
+ // empty the old set - subsequent dial attempts for the same peer id may
260
+ // add more addresses to try
261
+ multiaddrs.clear()
262
+
263
+ this.log('calculating addrs to dial %p from %s', peerId, [...addrs])
264
+
265
+ // load addresses from address book, resolve and dnsaddrs, filter
266
+ // undialables, add peer IDs, etc
267
+ const calculatedAddrs = await this.calculateMultiaddrs(peerId, addrs, {
268
+ ...options,
269
+ signal
270
+ })
271
+
272
+ for (const addr of calculatedAddrs) {
273
+ // skip any addresses we have previously failed to dial
274
+ if (failedMultiaddrs.has(addr.multiaddr.toString())) {
275
+ this.log.trace('skipping previously failed multiaddr %a while dialing %p', addr.multiaddr, peerId)
276
+ continue
277
+ }
278
+
279
+ addrsToDial.push(addr)
233
280
  }
234
281
 
235
- try {
236
- let dialed = 0
237
- const errors: Error[] = []
282
+ this.log('%s dial to %p with %s', dialIteration === 1 ? 'starting' : 'continuing', peerId, addrsToDial.map(ma => ma.multiaddr.toString()))
238
283
 
239
- for (const address of addrsToDial) {
240
- if (dialed === this.maxPeerAddrsToDial) {
241
- this.log('dialed maxPeerAddrsToDial (%d) addresses for %p, not trying any others', dialed, peerId)
284
+ options?.onProgress?.(new CustomProgressEvent<Address[]>('dial-queue:calculated-addresses', addrsToDial))
242
285
 
243
- throw new DialError('Peer had more than maxPeerAddrsToDial')
244
- }
286
+ for (const address of addrsToDial) {
287
+ if (dialed === this.maxPeerAddrsToDial) {
288
+ this.log('dialed maxPeerAddrsToDial (%d) addresses for %p, not trying any others', dialed, options.peerId)
245
289
 
246
- dialed++
290
+ throw new DialError('Peer had more than maxPeerAddrsToDial')
291
+ }
247
292
 
293
+ dialed++
294
+
295
+ try {
296
+ // try to dial the address
297
+ const conn = await this.components.transportManager.dial(address.multiaddr, {
298
+ ...options,
299
+ signal
300
+ })
301
+
302
+ this.log('dial to %a succeeded', address.multiaddr)
303
+
304
+ // record the successful dial and the address
248
305
  try {
249
- const conn = await this.components.transportManager.dial(address.multiaddr, {
250
- ...options,
251
- signal
306
+ await this.components.peerStore.merge(conn.remotePeer, {
307
+ multiaddrs: [
308
+ conn.remoteAddr
309
+ ],
310
+ metadata: {
311
+ [LAST_DIAL_SUCCESS_KEY]: uint8ArrayFromString(Date.now().toString())
312
+ }
252
313
  })
314
+ } catch (err: any) {
315
+ this.log.error('could not update last dial failure key for %p', peerId, err)
316
+ }
317
+
318
+ // dial successful, return the connection
319
+ return conn
320
+ } catch (err: any) {
321
+ this.log.error('dial failed to %a', address.multiaddr, err)
253
322
 
254
- this.log('dial to %a succeeded', address.multiaddr)
323
+ // ensure we don't dial it again in this attempt
324
+ failedMultiaddrs.add(address.multiaddr.toString())
255
325
 
256
- // record the successful dial and the address
326
+ if (peerId != null) {
327
+ // record the failed dial
257
328
  try {
258
- await this.components.peerStore.merge(conn.remotePeer, {
259
- multiaddrs: [
260
- conn.remoteAddr
261
- ],
329
+ await this.components.peerStore.merge(peerId, {
262
330
  metadata: {
263
- [LAST_DIAL_SUCCESS_KEY]: uint8ArrayFromString(Date.now().toString())
331
+ [LAST_DIAL_FAILURE_KEY]: uint8ArrayFromString(Date.now().toString())
264
332
  }
265
333
  })
266
334
  } catch (err: any) {
267
335
  this.log.error('could not update last dial failure key for %p', peerId, err)
268
336
  }
337
+ }
269
338
 
270
- return conn
271
- } catch (err: any) {
272
- this.log.error('dial failed to %a', address.multiaddr, err)
273
-
274
- if (peerId != null) {
275
- // record the failed dial
276
- try {
277
- await this.components.peerStore.merge(peerId, {
278
- metadata: {
279
- [LAST_DIAL_FAILURE_KEY]: uint8ArrayFromString(Date.now().toString())
280
- }
281
- })
282
- } catch (err: any) {
283
- this.log.error('could not update last dial failure key for %p', peerId, err)
284
- }
285
- }
286
-
287
- // the user/dial timeout/shutdown controller signal aborted
288
- if (signal.aborted) {
289
- throw new TimeoutError(err.message)
290
- }
291
-
292
- errors.push(err)
339
+ // the user/dial timeout/shutdown controller signal aborted
340
+ if (signal.aborted) {
341
+ throw new TimeoutError(err.message)
293
342
  }
294
- }
295
343
 
296
- if (errors.length === 1) {
297
- throw errors[0]
344
+ errors.push(err)
298
345
  }
299
-
300
- throw new AggregateError(errors, 'All multiaddr dials failed')
301
- } finally {
302
- // clean up abort signals/controllers
303
- signal.clear()
304
346
  }
305
- }, {
306
- peerId,
307
- priority: options.priority ?? DEFAULT_DIAL_PRIORITY,
308
- multiaddrs: new Set(multiaddrs.map(ma => ma.toString())),
309
- signal: options.signal ?? AbortSignal.timeout(this.dialTimeout),
310
- onProgress: options.onProgress
311
- })
347
+ }
348
+
349
+ if (errors.length === 1) {
350
+ throw errors[0]
351
+ }
352
+
353
+ throw new AggregateError(errors, 'All multiaddr dials failed')
312
354
  }
313
355
 
314
356
  // eslint-disable-next-line complexity
@@ -358,8 +400,10 @@ export class DialQueue {
358
400
  isCertified: false
359
401
  })))
360
402
  } catch (err: any) {
361
- if (err.name !== 'NoPeerRoutersError') {
362
- this.log.error('looking up multiaddrs for %p in the peer routing failed', peerId, err)
403
+ if (err.name === 'NoPeerRoutersError') {
404
+ this.log('no peer routers configured', peerId)
405
+ } else {
406
+ this.log.error('looking up multiaddrs for %p in the peer routing failed - %e', peerId, err)
363
407
  }
364
408
  }
365
409
  }
package/src/errors.ts CHANGED
@@ -59,6 +59,20 @@ export class DialDeniedError extends Error {
59
59
  }
60
60
  }
61
61
 
62
+ export class UnsupportedListenAddressError extends Error {
63
+ constructor (message = 'No transport was configured to listen on this address') {
64
+ super(message)
65
+ this.name = 'UnsupportedListenAddressError'
66
+ }
67
+ }
68
+
69
+ export class UnsupportedListenAddressesError extends Error {
70
+ constructor (message = 'Configured listen addresses could not be listened on') {
71
+ super(message)
72
+ this.name = 'UnsupportedListenAddressesError'
73
+ }
74
+ }
75
+
62
76
  export class NoValidAddressesError extends Error {
63
77
  constructor (message = 'No valid addresses') {
64
78
  super(message)
@@ -2,7 +2,7 @@ import { FaultTolerance, InvalidParametersError, NotStartedError } from '@libp2p
2
2
  import { trackedMap } from '@libp2p/utils/tracked-map'
3
3
  import { IP4, IP6 } from '@multiformats/multiaddr-matcher'
4
4
  import { CustomProgressEvent } from 'progress-events'
5
- import { NoValidAddressesError, TransportUnavailableError } from './errors.js'
5
+ import { TransportUnavailableError, UnsupportedListenAddressError, UnsupportedListenAddressesError } from './errors.js'
6
6
  import type { Libp2pEvents, ComponentLogger, Logger, Connection, TypedEventTarget, Metrics, Startable, Listener, Transport, Upgrader } from '@libp2p/interface'
7
7
  import type { AddressManager, TransportManager, TransportManagerDialOptions } from '@libp2p/interface-internal'
8
8
  import type { Multiaddr } from '@multiformats/multiaddr'
@@ -25,7 +25,7 @@ interface IPStats {
25
25
  }
26
26
 
27
27
  interface ListenStats {
28
- unsupportedAddresses: Set<string>
28
+ errors: Map<string, Error>
29
29
  ipv4: IPStats
30
30
  ipv6: IPStats
31
31
  }
@@ -207,9 +207,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
207
207
  // track IPv4/IPv6 results - if we succeed on IPv4 but all IPv6 attempts
208
208
  // fail then we are probably on a network without IPv6 support
209
209
  const listenStats: ListenStats = {
210
- unsupportedAddresses: new Set(
211
- addrs.map(ma => ma.toString())
212
- ),
210
+ errors: new Map(),
213
211
  ipv4: {
214
212
  success: 0,
215
213
  attempts: 0
@@ -220,6 +218,10 @@ export class DefaultTransportManager implements TransportManager, Startable {
220
218
  }
221
219
  }
222
220
 
221
+ addrs.forEach(ma => {
222
+ listenStats.errors.set(ma.toString(), new UnsupportedListenAddressError())
223
+ })
224
+
223
225
  const tasks: Array<Promise<void>> = []
224
226
 
225
227
  for (const [key, transport] of this.transports.entries()) {
@@ -269,7 +271,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
269
271
  tasks.push(
270
272
  listener.listen(addr)
271
273
  .then(() => {
272
- listenStats.unsupportedAddresses.delete(addr.toString())
274
+ listenStats.errors.delete(addr.toString())
273
275
 
274
276
  if (IP4.matches(addr)) {
275
277
  listenStats.ipv4.success++
@@ -280,6 +282,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
280
282
  }
281
283
  }, (err) => {
282
284
  this.log.error('transport %s could not listen on address %a - %e', key, addr, err)
285
+ listenStats.errors.set(addr.toString(), err)
283
286
  throw err
284
287
  })
285
288
  )
@@ -308,9 +311,13 @@ export class DefaultTransportManager implements TransportManager, Startable {
308
311
  }
309
312
 
310
313
  // if a configured address was not able to be listened on, throw an error
311
- throw new NoValidAddressesError(`No configured transport could listen on these addresses, please remove them from your config: ${[
312
- ...listenStats.unsupportedAddresses
313
- ].join(', ')}`)
314
+ throw new UnsupportedListenAddressesError(`Some configured addresses failed to be listened on, you may need to remove one or more listen addresses from your configuration or set \`transportManager.faultTolerance\` to NO_FATAL:\n${
315
+ [...listenStats.errors.entries()].map(([addr, err]) => {
316
+ return `
317
+ ${addr}: ${`${err.stack ?? err}`.split('\n').join('\n ')}
318
+ `
319
+ }).join('')
320
+ }`)
314
321
  }
315
322
 
316
323
  private ipv6Unsupported (listenStats: ListenStats): boolean {
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '2.7.3-e2f49432b'
1
+ export const version = '2.7.4-0555339ba'
2
2
  export const name = 'js-libp2p'