libp2p 0.46.19 → 0.46.20

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.
Files changed (67) hide show
  1. package/dist/index.min.js +22 -22
  2. package/dist/src/components.d.ts +3 -1
  3. package/dist/src/components.d.ts.map +1 -1
  4. package/dist/src/components.js +4 -0
  5. package/dist/src/components.js.map +1 -1
  6. package/dist/src/connection/index.d.ts +3 -1
  7. package/dist/src/connection/index.d.ts.map +1 -1
  8. package/dist/src/connection/index.js +17 -11
  9. package/dist/src/connection/index.js.map +1 -1
  10. package/dist/src/connection-manager/auto-dial.d.ts +3 -1
  11. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  12. package/dist/src/connection-manager/auto-dial.js +20 -20
  13. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  14. package/dist/src/connection-manager/connection-pruner.d.ts +3 -1
  15. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  16. package/dist/src/connection-manager/connection-pruner.js +8 -8
  17. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  18. package/dist/src/connection-manager/dial-queue.d.ts +3 -1
  19. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  20. package/dist/src/connection-manager/dial-queue.js +26 -23
  21. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  22. package/dist/src/connection-manager/index.d.ts +3 -1
  23. package/dist/src/connection-manager/index.d.ts.map +1 -1
  24. package/dist/src/connection-manager/index.js +21 -18
  25. package/dist/src/connection-manager/index.js.map +1 -1
  26. package/dist/src/connection-manager/utils.d.ts +2 -1
  27. package/dist/src/connection-manager/utils.d.ts.map +1 -1
  28. package/dist/src/connection-manager/utils.js +2 -4
  29. package/dist/src/connection-manager/utils.js.map +1 -1
  30. package/dist/src/identify/consts.d.ts +1 -1
  31. package/dist/src/identify/identify.d.ts.map +1 -1
  32. package/dist/src/identify/identify.js +28 -21
  33. package/dist/src/identify/identify.js.map +1 -1
  34. package/dist/src/identify/index.d.ts +2 -1
  35. package/dist/src/identify/index.d.ts.map +1 -1
  36. package/dist/src/identify/index.js.map +1 -1
  37. package/dist/src/index.d.ts +21 -1
  38. package/dist/src/index.d.ts.map +1 -1
  39. package/dist/src/index.js.map +1 -1
  40. package/dist/src/libp2p.d.ts +2 -1
  41. package/dist/src/libp2p.d.ts.map +1 -1
  42. package/dist/src/libp2p.js +20 -16
  43. package/dist/src/libp2p.js.map +1 -1
  44. package/dist/src/ping/index.d.ts.map +1 -1
  45. package/dist/src/ping/index.js +9 -3
  46. package/dist/src/ping/index.js.map +1 -1
  47. package/dist/src/upgrader.d.ts +3 -1
  48. package/dist/src/upgrader.d.ts.map +1 -1
  49. package/dist/src/upgrader.js +37 -31
  50. package/dist/src/upgrader.js.map +1 -1
  51. package/dist/src/version.d.ts +1 -1
  52. package/dist/src/version.js +1 -1
  53. package/package.json +21 -21
  54. package/src/components.ts +8 -1
  55. package/src/connection/index.ts +20 -13
  56. package/src/connection-manager/auto-dial.ts +22 -22
  57. package/src/connection-manager/connection-pruner.ts +10 -10
  58. package/src/connection-manager/dial-queue.ts +28 -25
  59. package/src/connection-manager/index.ts +23 -20
  60. package/src/connection-manager/utils.ts +5 -7
  61. package/src/identify/identify.ts +30 -23
  62. package/src/identify/index.ts +2 -1
  63. package/src/index.ts +22 -1
  64. package/src/libp2p.ts +21 -18
  65. package/src/ping/index.ts +10 -3
  66. package/src/upgrader.ts +39 -32
  67. package/src/version.ts +1 -1
@@ -1,16 +1,13 @@
1
- import { logger } from '@libp2p/logger'
2
1
  import { PeerMap, PeerSet } from '@libp2p/peer-collections'
3
2
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
3
  import { PeerJobQueue } from '../utils/peer-job-queue.js'
5
4
  import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE, AUTO_DIAL_INTERVAL, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PEER_RETRY_THRESHOLD, AUTO_DIAL_PRIORITY, LAST_DIAL_FAILURE_KEY, MIN_CONNECTIONS } from './constants.js'
6
- import type { Libp2pEvents } from '@libp2p/interface'
5
+ import type { Libp2pEvents, Logger, ComponentLogger } from '@libp2p/interface'
7
6
  import type { TypedEventTarget } from '@libp2p/interface/events'
8
7
  import type { PeerStore } from '@libp2p/interface/peer-store'
9
8
  import type { Startable } from '@libp2p/interface/startable'
10
9
  import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
11
10
 
12
- const log = logger('libp2p:connection-manager:auto-dial')
13
-
14
11
  interface AutoDialInit {
15
12
  minConnections?: number
16
13
  maxQueueLength?: number
@@ -25,6 +22,7 @@ interface AutoDialComponents {
25
22
  connectionManager: ConnectionManager
26
23
  peerStore: PeerStore
27
24
  events: TypedEventTarget<Libp2pEvents>
25
+ logger: ComponentLogger
28
26
  }
29
27
 
30
28
  const defaultOptions = {
@@ -50,6 +48,7 @@ export class AutoDial implements Startable {
50
48
  private autoDialInterval?: ReturnType<typeof setInterval>
51
49
  private started: boolean
52
50
  private running: boolean
51
+ readonly #log: Logger
53
52
 
54
53
  /**
55
54
  * Proactively tries to connect to known peers stored in the PeerStore.
@@ -65,20 +64,21 @@ export class AutoDial implements Startable {
65
64
  this.autoDialMaxQueueLength = init.maxQueueLength ?? defaultOptions.maxQueueLength
66
65
  this.autoDialPeerRetryThresholdMs = init.autoDialPeerRetryThreshold ?? defaultOptions.autoDialPeerRetryThreshold
67
66
  this.autoDialDiscoveredPeersDebounce = init.autoDialDiscoveredPeersDebounce ?? defaultOptions.autoDialDiscoveredPeersDebounce
67
+ this.#log = components.logger.forComponent('libp2p:connection-manager:auto-dial')
68
68
  this.started = false
69
69
  this.running = false
70
70
  this.queue = new PeerJobQueue({
71
71
  concurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency
72
72
  })
73
73
  this.queue.addListener('error', (err) => {
74
- log.error('error during auto-dial', err)
74
+ this.#log.error('error during auto-dial', err)
75
75
  })
76
76
 
77
77
  // check the min connection limit whenever a peer disconnects
78
78
  components.events.addEventListener('connection:close', () => {
79
79
  this.autoDial()
80
80
  .catch(err => {
81
- log.error(err)
81
+ this.#log.error(err)
82
82
  })
83
83
  })
84
84
 
@@ -93,7 +93,7 @@ export class AutoDial implements Startable {
93
93
  debounce = setTimeout(() => {
94
94
  this.autoDial()
95
95
  .catch(err => {
96
- log.error(err)
96
+ this.#log.error(err)
97
97
  })
98
98
  }, this.autoDialDiscoveredPeersDebounce)
99
99
  })
@@ -107,7 +107,7 @@ export class AutoDial implements Startable {
107
107
  this.autoDialInterval = setTimeout(() => {
108
108
  this.autoDial()
109
109
  .catch(err => {
110
- log.error('error while autodialing', err)
110
+ this.#log.error('error while autodialing', err)
111
111
  })
112
112
  }, this.autoDialIntervalMs)
113
113
  this.started = true
@@ -116,7 +116,7 @@ export class AutoDial implements Startable {
116
116
  afterStart (): void {
117
117
  this.autoDial()
118
118
  .catch(err => {
119
- log.error('error while autodialing', err)
119
+ this.#log.error('error while autodialing', err)
120
120
  })
121
121
  }
122
122
 
@@ -139,24 +139,24 @@ export class AutoDial implements Startable {
139
139
  // Already has enough connections
140
140
  if (numConnections >= this.minConnections) {
141
141
  if (this.minConnections > 0) {
142
- log.trace('have enough connections %d/%d', numConnections, this.minConnections)
142
+ this.#log.trace('have enough connections %d/%d', numConnections, this.minConnections)
143
143
  }
144
144
  return
145
145
  }
146
146
 
147
147
  if (this.queue.size > this.autoDialMaxQueueLength) {
148
- log('not enough connections %d/%d but auto dial queue is full', numConnections, this.minConnections)
148
+ this.#log('not enough connections %d/%d but auto dial queue is full', numConnections, this.minConnections)
149
149
  return
150
150
  }
151
151
 
152
152
  if (this.running) {
153
- log('not enough connections %d/%d - but skipping autodial as it is already running', numConnections, this.minConnections)
153
+ this.#log('not enough connections %d/%d - but skipping autodial as it is already running', numConnections, this.minConnections)
154
154
  return
155
155
  }
156
156
 
157
157
  this.running = true
158
158
 
159
- log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
159
+ this.#log('not enough connections %d/%d - will dial peers to increase the number of connections', numConnections, this.minConnections)
160
160
 
161
161
  const dialQueue = new PeerSet(
162
162
  // @ts-expect-error boolean filter removes falsy peer IDs
@@ -172,25 +172,25 @@ export class AutoDial implements Startable {
172
172
  (peer) => {
173
173
  // Remove peers without addresses
174
174
  if (peer.addresses.length === 0) {
175
- log.trace('not autodialing %p because they have no addresses', peer.id)
175
+ this.#log.trace('not autodialing %p because they have no addresses', peer.id)
176
176
  return false
177
177
  }
178
178
 
179
179
  // remove peers we are already connected to
180
180
  if (connections.has(peer.id)) {
181
- log.trace('not autodialing %p because they are already connected', peer.id)
181
+ this.#log.trace('not autodialing %p because they are already connected', peer.id)
182
182
  return false
183
183
  }
184
184
 
185
185
  // remove peers we are already dialling
186
186
  if (dialQueue.has(peer.id)) {
187
- log.trace('not autodialing %p because they are already being dialed', peer.id)
187
+ this.#log.trace('not autodialing %p because they are already being dialed', peer.id)
188
188
  return false
189
189
  }
190
190
 
191
191
  // remove peers already in the autodial queue
192
192
  if (this.queue.hasJob(peer.id)) {
193
- log.trace('not autodialing %p because they are already being autodialed', peer.id)
193
+ this.#log.trace('not autodialing %p because they are already being autodialed', peer.id)
194
194
  return false
195
195
  }
196
196
 
@@ -249,7 +249,7 @@ export class AutoDial implements Startable {
249
249
  return Date.now() - lastDialFailureTimestamp > this.autoDialPeerRetryThresholdMs
250
250
  })
251
251
 
252
- log('selected %d/%d peers to dial', peersThatHaveNotFailed.length, peers.length)
252
+ this.#log('selected %d/%d peers to dial', peersThatHaveNotFailed.length, peers.length)
253
253
 
254
254
  for (const peer of peersThatHaveNotFailed) {
255
255
  this.queue.add(async () => {
@@ -257,19 +257,19 @@ export class AutoDial implements Startable {
257
257
 
258
258
  // Check to see if we still need to auto dial
259
259
  if (numConnections >= this.minConnections) {
260
- log('got enough connections now %d/%d', numConnections, this.minConnections)
260
+ this.#log('got enough connections now %d/%d', numConnections, this.minConnections)
261
261
  this.queue.clear()
262
262
  return
263
263
  }
264
264
 
265
- log('connecting to a peerStore stored peer %p', peer.id)
265
+ this.#log('connecting to a peerStore stored peer %p', peer.id)
266
266
  await this.connectionManager.openConnection(peer.id, {
267
267
  priority: this.autoDialPriority
268
268
  })
269
269
  }, {
270
270
  peerId: peer.id
271
271
  }).catch(err => {
272
- log.error('could not connect to peerStore stored peer', err)
272
+ this.#log.error('could not connect to peerStore stored peer', err)
273
273
  })
274
274
  }
275
275
 
@@ -279,7 +279,7 @@ export class AutoDial implements Startable {
279
279
  this.autoDialInterval = setTimeout(() => {
280
280
  this.autoDial()
281
281
  .catch(err => {
282
- log.error('error while autodialing', err)
282
+ this.#log.error('error while autodialing', err)
283
283
  })
284
284
  }, this.autoDialIntervalMs)
285
285
  }
@@ -1,14 +1,11 @@
1
- import { logger } from '@libp2p/logger'
2
1
  import { PeerMap } from '@libp2p/peer-collections'
3
2
  import { MAX_CONNECTIONS } from './constants.js'
4
- import type { Libp2pEvents } from '@libp2p/interface'
3
+ import type { Libp2pEvents, Logger, ComponentLogger } from '@libp2p/interface'
5
4
  import type { TypedEventTarget } from '@libp2p/interface/events'
6
5
  import type { PeerStore } from '@libp2p/interface/peer-store'
7
6
  import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
8
7
  import type { Multiaddr } from '@multiformats/multiaddr'
9
8
 
10
- const log = logger('libp2p:connection-manager:connection-pruner')
11
-
12
9
  interface ConnectionPrunerInit {
13
10
  maxConnections?: number
14
11
  allow?: Multiaddr[]
@@ -18,6 +15,7 @@ interface ConnectionPrunerComponents {
18
15
  connectionManager: ConnectionManager
19
16
  peerStore: PeerStore
20
17
  events: TypedEventTarget<Libp2pEvents>
18
+ logger: ComponentLogger
21
19
  }
22
20
 
23
21
  const defaultOptions = {
@@ -34,6 +32,7 @@ export class ConnectionPruner {
34
32
  private readonly peerStore: PeerStore
35
33
  private readonly allow: Multiaddr[]
36
34
  private readonly events: TypedEventTarget<Libp2pEvents>
35
+ readonly #log: Logger
37
36
 
38
37
  constructor (components: ConnectionPrunerComponents, init: ConnectionPrunerInit = {}) {
39
38
  this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections
@@ -41,12 +40,13 @@ export class ConnectionPruner {
41
40
  this.connectionManager = components.connectionManager
42
41
  this.peerStore = components.peerStore
43
42
  this.events = components.events
43
+ this.#log = components.logger.forComponent('libp2p:connection-manager:connection-pruner')
44
44
 
45
45
  // check the max connection limit whenever a peer connects
46
46
  components.events.addEventListener('connection:open', () => {
47
47
  this.maybePruneConnections()
48
48
  .catch(err => {
49
- log.error(err)
49
+ this.#log.error(err)
50
50
  })
51
51
  })
52
52
  }
@@ -60,12 +60,12 @@ export class ConnectionPruner {
60
60
  const numConnections = connections.length
61
61
  const toPrune = Math.max(numConnections - this.maxConnections, 0)
62
62
 
63
- log('checking max connections limit %d/%d', numConnections, this.maxConnections)
63
+ this.#log('checking max connections limit %d/%d', numConnections, this.maxConnections)
64
64
  if (numConnections <= this.maxConnections) {
65
65
  return
66
66
  }
67
67
 
68
- log('max connections limit exceeded %d/%d, pruning %d connection(s)', numConnections, this.maxConnections, toPrune)
68
+ this.#log('max connections limit exceeded %d/%d, pruning %d connection(s)', numConnections, this.maxConnections, toPrune)
69
69
  const peerValues = new PeerMap<number>()
70
70
 
71
71
  // work out peer values
@@ -87,7 +87,7 @@ export class ConnectionPruner {
87
87
  }, 0))
88
88
  } catch (err: any) {
89
89
  if (err.code !== 'ERR_NOT_FOUND') {
90
- log.error('error loading peer tags', err)
90
+ this.#log.error('error loading peer tags', err)
91
91
  }
92
92
  }
93
93
  }
@@ -124,7 +124,7 @@ export class ConnectionPruner {
124
124
  const toClose = []
125
125
 
126
126
  for (const connection of sortedConnections) {
127
- log('too many connections open - closing a connection to %p', connection.remotePeer)
127
+ this.#log('too many connections open - closing a connection to %p', connection.remotePeer)
128
128
  // check allow list
129
129
  const connectionInAllowList = this.allow.some((ma) => {
130
130
  return connection.remoteAddr.toString().startsWith(ma.toString())
@@ -146,7 +146,7 @@ export class ConnectionPruner {
146
146
  try {
147
147
  await connection.close()
148
148
  } catch (err) {
149
- log.error(err)
149
+ this.#log.error(err)
150
150
  }
151
151
  })
152
152
  )
@@ -1,6 +1,5 @@
1
1
  import { AbortError, CodeError } from '@libp2p/interface/errors'
2
2
  import { setMaxListeners } from '@libp2p/interface/events'
3
- import { logger } from '@libp2p/logger'
4
3
  import { PeerMap } from '@libp2p/peer-collections'
5
4
  import { defaultAddressSort } from '@libp2p/utils/address-sort'
6
5
  import { type Multiaddr, type Resolver, resolvers } from '@multiformats/multiaddr'
@@ -19,7 +18,7 @@ import {
19
18
  LAST_DIAL_FAILURE_KEY
20
19
  } from './constants.js'
21
20
  import { combineSignals, resolveMultiaddrs } from './utils.js'
22
- import type { AddressSorter, AbortOptions, PendingDial } from '@libp2p/interface'
21
+ import type { AddressSorter, AbortOptions, PendingDial, ComponentLogger, Logger } from '@libp2p/interface'
23
22
  import type { Connection } from '@libp2p/interface/connection'
24
23
  import type { ConnectionGater } from '@libp2p/interface/connection-gater'
25
24
  import type { Metric, Metrics } from '@libp2p/interface/metrics'
@@ -27,8 +26,6 @@ import type { PeerId } from '@libp2p/interface/peer-id'
27
26
  import type { Address, PeerStore } from '@libp2p/interface/peer-store'
28
27
  import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
29
28
 
30
- const log = logger('libp2p:connection-manager:dial-queue')
31
-
32
29
  export interface PendingDialTarget {
33
30
  resolve(value: any): void
34
31
  reject(err: Error): void
@@ -70,6 +67,7 @@ interface DialQueueComponents {
70
67
  peerStore: PeerStore
71
68
  transportManager: TransportManager
72
69
  connectionGater: ConnectionGater
70
+ logger: ComponentLogger
73
71
  }
74
72
 
75
73
  export class DialQueue {
@@ -87,6 +85,7 @@ export class DialQueue {
87
85
  private readonly pendingDialCount?: Metric
88
86
  private readonly shutDownController: AbortController
89
87
  private readonly connections: PeerMap<Connection[]>
88
+ readonly #log: Logger
90
89
 
91
90
  constructor (components: DialQueueComponents, init: DialerInit = {}) {
92
91
  this.addressSorter = init.addressSorter ?? defaultOptions.addressSorter
@@ -94,6 +93,7 @@ export class DialQueue {
94
93
  this.maxParallelDialsPerPeer = init.maxParallelDialsPerPeer ?? defaultOptions.maxParallelDialsPerPeer
95
94
  this.dialTimeout = init.dialTimeout ?? defaultOptions.dialTimeout
96
95
  this.connections = init.connections ?? new PeerMap()
96
+ this.#log = components.logger.forComponent('libp2p:connection-manager:dial-queue')
97
97
 
98
98
  this.peerId = components.peerId
99
99
  this.peerStore = components.peerStore
@@ -133,7 +133,7 @@ export class DialQueue {
133
133
  })
134
134
  // a started job errored
135
135
  this.queue.on('error', (err) => {
136
- log.error('error in dial queue', err)
136
+ this.#log.error('error in dial queue', err)
137
137
  this.pendingDialCount?.update(this.queue.size)
138
138
  this.inProgressDialCount?.update(this.queue.pending)
139
139
  })
@@ -205,7 +205,7 @@ export class DialQueue {
205
205
  })
206
206
 
207
207
  if (existingConnection != null) {
208
- log('already connected to %a', existingConnection.remoteAddr)
208
+ this.#log('already connected to %a', existingConnection.remoteAddr)
209
209
  return existingConnection
210
210
  }
211
211
 
@@ -226,12 +226,12 @@ export class DialQueue {
226
226
  })
227
227
 
228
228
  if (existingDial != null) {
229
- log('joining existing dial target for %p', peerId)
229
+ this.#log('joining existing dial target for %p', peerId)
230
230
  signal.clear()
231
231
  return existingDial.promise
232
232
  }
233
233
 
234
- log('creating dial target for', addrsToDial.map(({ multiaddr }) => multiaddr.toString()))
234
+ this.#log('creating dial target for', addrsToDial.map(({ multiaddr }) => multiaddr.toString()))
235
235
  // @ts-expect-error .promise property is set below
236
236
  const pendingDial: PendingDialInternal = {
237
237
  id: randomId(),
@@ -252,7 +252,7 @@ export class DialQueue {
252
252
  signal.clear()
253
253
  })
254
254
  .catch(async err => {
255
- log.error('dial failed to %s', pendingDial.multiaddrs.map(ma => ma.toString()).join(', '), err)
255
+ this.#log.error('dial failed to %s', pendingDial.multiaddrs.map(ma => ma.toString()).join(', '), err)
256
256
 
257
257
  if (peerId != null) {
258
258
  // record the last failed dial
@@ -263,7 +263,7 @@ export class DialQueue {
263
263
  }
264
264
  })
265
265
  } catch (err: any) {
266
- log.error('could not update last dial failure key for %p', peerId, err)
266
+ this.#log.error('could not update last dial failure key for %p', peerId, err)
267
267
  }
268
268
  }
269
269
 
@@ -294,12 +294,12 @@ export class DialQueue {
294
294
  })
295
295
 
296
296
  if (existingConnection != null) {
297
- log('already connected to %a', existingConnection.remoteAddr)
297
+ this.#log('already connected to %a', existingConnection.remoteAddr)
298
298
  await connection.close()
299
299
  return existingConnection
300
300
  }
301
301
 
302
- log('connection opened to %a', connection.remoteAddr)
302
+ this.#log('connection opened to %a', connection.remoteAddr)
303
303
  return connection
304
304
  }
305
305
 
@@ -334,11 +334,11 @@ export class DialQueue {
334
334
 
335
335
  // if just a peer id was passed, load available multiaddrs for this peer from the address book
336
336
  if (addrs.length === 0) {
337
- log('loading multiaddrs for %p', peerId)
337
+ this.#log('loading multiaddrs for %p', peerId)
338
338
  try {
339
339
  const peer = await this.peerStore.get(peerId)
340
340
  addrs.push(...peer.addresses)
341
- log('loaded multiaddrs for %p', peerId, addrs.map(({ multiaddr }) => multiaddr.toString()))
341
+ this.#log('loaded multiaddrs for %p', peerId, addrs.map(({ multiaddr }) => multiaddr.toString()))
342
342
  } catch (err: any) {
343
343
  if (err.code !== codes.ERR_NOT_FOUND) {
344
344
  throw err
@@ -350,7 +350,10 @@ export class DialQueue {
350
350
  // resolve addresses - this can result in a one-to-many translation when dnsaddrs are resolved
351
351
  let resolvedAddresses = (await Promise.all(
352
352
  addrs.map(async addr => {
353
- const result = await resolveMultiaddrs(addr.multiaddr, options)
353
+ const result = await resolveMultiaddrs(addr.multiaddr, {
354
+ ...options,
355
+ log: this.#log
356
+ })
354
357
 
355
358
  if (result.length === 1 && result[0].equals(addr.multiaddr)) {
356
359
  return addr
@@ -422,8 +425,8 @@ export class DialQueue {
422
425
  const dedupedMultiaddrs = [...dedupedAddrs.values()]
423
426
 
424
427
  if (dedupedMultiaddrs.length === 0 || dedupedMultiaddrs.length > this.maxPeerAddrsToDial) {
425
- log('addresses for %p before filtering', peerId ?? 'unknown peer', resolvedAddresses.map(({ multiaddr }) => multiaddr.toString()))
426
- log('addresses for %p after filtering', peerId ?? 'unknown peer', dedupedMultiaddrs.map(({ multiaddr }) => multiaddr.toString()))
428
+ this.#log('addresses for %p before filtering', peerId ?? 'unknown peer', resolvedAddresses.map(({ multiaddr }) => multiaddr.toString()))
429
+ this.#log('addresses for %p after filtering', peerId ?? 'unknown peer', dedupedMultiaddrs.map(({ multiaddr }) => multiaddr.toString()))
427
430
  }
428
431
 
429
432
  // make sure we actually have some addresses to dial
@@ -467,7 +470,7 @@ export class DialQueue {
467
470
  concurrency: this.maxParallelDialsPerPeer
468
471
  })
469
472
  peerDialQueue.on('error', (err) => {
470
- log.error('error dialling', err)
473
+ this.#log.error('error dialling', err)
471
474
  })
472
475
 
473
476
  const conn = await Promise.any(pendingDial.multiaddrs.map(async (addr, i) => {
@@ -480,13 +483,13 @@ export class DialQueue {
480
483
  // let any signal abort the dial
481
484
  const signal = combineSignals(controller.signal, options.signal)
482
485
  signal.addEventListener('abort', () => {
483
- log('dial to %a aborted', addr)
486
+ this.#log('dial to %a aborted', addr)
484
487
  })
485
488
  const deferred = pDefer<Connection>()
486
489
 
487
490
  await peerDialQueue.add(async () => {
488
491
  if (signal.aborted) {
489
- log('dial to %a was aborted before reaching the head of the peer dial queue', addr)
492
+ this.#log('dial to %a was aborted before reaching the head of the peer dial queue', addr)
490
493
  deferred.reject(new AbortError())
491
494
  return
492
495
  }
@@ -495,7 +498,7 @@ export class DialQueue {
495
498
  await this.queue.add(async () => {
496
499
  try {
497
500
  if (signal.aborted) {
498
- log('dial to %a was aborted before reaching the head of the dial queue', addr)
501
+ this.#log('dial to %a was aborted before reaching the head of the dial queue', addr)
499
502
  deferred.reject(new AbortError())
500
503
  return
501
504
  }
@@ -510,10 +513,10 @@ export class DialQueue {
510
513
 
511
514
  if (controller.signal.aborted) {
512
515
  // another dial succeeded faster than this one
513
- log('multiple dials succeeded, closing superfluous connection')
516
+ this.#log('multiple dials succeeded, closing superfluous connection')
514
517
 
515
518
  conn.close().catch(err => {
516
- log.error('error closing superfluous connection', err)
519
+ this.#log.error('error closing superfluous connection', err)
517
520
  })
518
521
 
519
522
  deferred.reject(new AbortError())
@@ -530,13 +533,13 @@ export class DialQueue {
530
533
  }
531
534
  })
532
535
 
533
- log('dial to %a succeeded', addr)
536
+ this.#log('dial to %a succeeded', addr)
534
537
 
535
538
  // resolve the connection promise
536
539
  deferred.resolve(conn)
537
540
  } catch (err: any) {
538
541
  // something only went wrong if our signal was not aborted
539
- log.error('error during dial of %a', addr, err)
542
+ this.#log.error('error during dial of %a', addr, err)
540
543
  deferred.reject(err)
541
544
  }
542
545
  }, {
@@ -1,6 +1,5 @@
1
1
  import { CodeError } from '@libp2p/interface/errors'
2
2
  import { KEEP_ALIVE } from '@libp2p/interface/peer-store/tags'
3
- import { logger } from '@libp2p/logger'
4
3
  import { PeerMap } from '@libp2p/peer-collections'
5
4
  import { defaultAddressSort } from '@libp2p/utils/address-sort'
6
5
  import { type Multiaddr, type Resolver, multiaddr } from '@multiformats/multiaddr'
@@ -12,7 +11,7 @@ import { AutoDial } from './auto-dial.js'
12
11
  import { ConnectionPruner } from './connection-pruner.js'
13
12
  import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PARALLEL_DIALS_PER_PEER, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
14
13
  import { DialQueue } from './dial-queue.js'
15
- import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions } from '@libp2p/interface'
14
+ import type { PendingDial, AddressSorter, Libp2pEvents, AbortOptions, ComponentLogger, Logger } from '@libp2p/interface'
16
15
  import type { Connection, MultiaddrConnection } from '@libp2p/interface/connection'
17
16
  import type { ConnectionGater } from '@libp2p/interface/connection-gater'
18
17
  import type { TypedEventTarget } from '@libp2p/interface/events'
@@ -23,8 +22,6 @@ import type { Startable } from '@libp2p/interface/startable'
23
22
  import type { ConnectionManager, OpenConnectionOptions } from '@libp2p/interface-internal/connection-manager'
24
23
  import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
25
24
 
26
- const log = logger('libp2p:connection-manager')
27
-
28
25
  const DEFAULT_DIAL_PRIORITY = 50
29
26
 
30
27
  export interface ConnectionManagerInit {
@@ -166,6 +163,7 @@ export interface DefaultConnectionManagerComponents {
166
163
  transportManager: TransportManager
167
164
  connectionGater: ConnectionGater
168
165
  events: TypedEventTarget<Libp2pEvents>
166
+ logger: ComponentLogger
169
167
  }
170
168
 
171
169
  /**
@@ -188,6 +186,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
188
186
  private readonly peerStore: PeerStore
189
187
  private readonly metrics?: Metrics
190
188
  private readonly events: TypedEventTarget<Libp2pEvents>
189
+ readonly #log: Logger
191
190
 
192
191
  constructor (components: DefaultConnectionManagerComponents, init: ConnectionManagerInit = {}) {
193
192
  this.maxConnections = init.maxConnections ?? defaultOptions.maxConnections
@@ -206,6 +205,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
206
205
  this.peerStore = components.peerStore
207
206
  this.metrics = components.metrics
208
207
  this.events = components.events
208
+ this.#log = components.logger.forComponent('libp2p:connection-manager')
209
209
 
210
210
  this.onConnect = this.onConnect.bind(this)
211
211
  this.onDisconnect = this.onDisconnect.bind(this)
@@ -229,7 +229,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
229
229
  this.autoDial = new AutoDial({
230
230
  connectionManager: this,
231
231
  peerStore: components.peerStore,
232
- events: components.events
232
+ events: components.events,
233
+ logger: components.logger
233
234
  }, {
234
235
  minConnections,
235
236
  autoDialConcurrency: init.autoDialConcurrency ?? defaultOptions.autoDialConcurrency,
@@ -241,7 +242,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
241
242
  this.connectionPruner = new ConnectionPruner({
242
243
  connectionManager: this,
243
244
  peerStore: components.peerStore,
244
- events: components.events
245
+ events: components.events,
246
+ logger: components.logger
245
247
  }, {
246
248
  maxConnections: this.maxConnections,
247
249
  allow: this.allow
@@ -252,7 +254,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
252
254
  metrics: components.metrics,
253
255
  peerStore: components.peerStore,
254
256
  transportManager: components.transportManager,
255
- connectionGater: components.connectionGater
257
+ connectionGater: components.connectionGater,
258
+ logger: components.logger
256
259
  }, {
257
260
  addressSorter: init.addressSorter ?? defaultAddressSort,
258
261
  maxParallelDials: init.maxParallelDials ?? MAX_PARALLEL_DIALS,
@@ -356,7 +359,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
356
359
  this.autoDial.start()
357
360
 
358
361
  this.started = true
359
- log('started')
362
+ this.#log('started')
360
363
  }
361
364
 
362
365
  async afterStart (): Promise<void> {
@@ -373,13 +376,13 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
373
376
  keepAlivePeers.map(async peer => {
374
377
  await this.openConnection(peer.id)
375
378
  .catch(err => {
376
- log.error(err)
379
+ this.#log.error(err)
377
380
  })
378
381
  })
379
382
  )
380
383
  })
381
384
  .catch(err => {
382
- log.error(err)
385
+ this.#log.error(err)
383
386
  })
384
387
 
385
388
  this.autoDial.afterStart()
@@ -400,22 +403,22 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
400
403
  try {
401
404
  await connection.close()
402
405
  } catch (err) {
403
- log.error(err)
406
+ this.#log.error(err)
404
407
  }
405
408
  })())
406
409
  }
407
410
  }
408
411
 
409
- log('closing %d connections', tasks.length)
412
+ this.#log('closing %d connections', tasks.length)
410
413
  await Promise.all(tasks)
411
414
  this.connections.clear()
412
415
 
413
- log('stopped')
416
+ this.#log('stopped')
414
417
  }
415
418
 
416
419
  onConnect (evt: CustomEvent<Connection>): void {
417
420
  void this._onConnect(evt).catch(err => {
418
- log.error(err)
421
+ this.#log.error(err)
419
422
  })
420
423
  }
421
424
 
@@ -505,12 +508,12 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
505
508
  const { peerId } = getPeerAddress(peerIdOrMultiaddr)
506
509
 
507
510
  if (peerId != null && options.force !== true) {
508
- log('dial %p', peerId)
511
+ this.#log('dial %p', peerId)
509
512
  const existingConnection = this.getConnections(peerId)
510
513
  .find(conn => !conn.transient)
511
514
 
512
515
  if (existingConnection != null) {
513
- log('had an existing non-transient connection to %p', peerId)
516
+ this.#log('had an existing non-transient connection to %p', peerId)
514
517
 
515
518
  return existingConnection
516
519
  }
@@ -566,7 +569,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
566
569
  })
567
570
 
568
571
  if (denyConnection) {
569
- log('connection from %a refused - connection remote address was in deny list', maConn.remoteAddr)
572
+ this.#log('connection from %a refused - connection remote address was in deny list', maConn.remoteAddr)
570
573
  return false
571
574
  }
572
575
 
@@ -583,7 +586,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
583
586
 
584
587
  // check pending connections
585
588
  if (this.incomingPendingConnections === this.maxIncomingPendingConnections) {
586
- log('connection from %a refused - incomingPendingConnections exceeded by host', maConn.remoteAddr)
589
+ this.#log('connection from %a refused - incomingPendingConnections exceeded by host', maConn.remoteAddr)
587
590
  return false
588
591
  }
589
592
 
@@ -593,7 +596,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
593
596
  try {
594
597
  await this.inboundConnectionRateLimiter.consume(host, 1)
595
598
  } catch {
596
- log('connection from %a refused - inboundConnectionThreshold exceeded by host %s', maConn.remoteAddr, host)
599
+ this.#log('connection from %a refused - inboundConnectionThreshold exceeded by host %s', maConn.remoteAddr, host)
597
600
  return false
598
601
  }
599
602
  }
@@ -604,7 +607,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
604
607
  return true
605
608
  }
606
609
 
607
- log('connection from %a refused - maxConnections exceeded', maConn.remoteAddr)
610
+ this.#log('connection from %a refused - maxConnections exceeded', maConn.remoteAddr)
608
611
  return false
609
612
  }
610
613