Skip to content

Commit f537b37

Browse files
authored
fix: log upgrade messages on connection (#2281)
To give more scope of where operations are taking place, use the connection logger to log messages about the upgrade process.
1 parent 8c169db commit f537b37

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

packages/libp2p/src/upgrader.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createConnection } from './connection/index.js'
55
import { INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
66
import { codes } from './errors.js'
77
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
8-
import type { Libp2pEvents, AbortOptions, ComponentLogger, Logger, MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions, ConnectionEncrypter, SecuredConnection, ConnectionGater, TypedEventTarget, Metrics, PeerId, PeerStore, StreamMuxer, StreamMuxerFactory, Upgrader, UpgraderOptions } from '@libp2p/interface'
8+
import type { Libp2pEvents, AbortOptions, ComponentLogger, MultiaddrConnection, Connection, Stream, ConnectionProtector, NewStreamOptions, ConnectionEncrypter, SecuredConnection, ConnectionGater, TypedEventTarget, Metrics, PeerId, PeerStore, StreamMuxer, StreamMuxerFactory, Upgrader, UpgraderOptions } from '@libp2p/interface'
99
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
1010

1111
const DEFAULT_PROTOCOL_SELECT_TIMEOUT = 30000
@@ -103,12 +103,10 @@ export class DefaultUpgrader implements Upgrader {
103103
private readonly muxers: Map<string, StreamMuxerFactory>
104104
private readonly inboundUpgradeTimeout: number
105105
private readonly events: TypedEventTarget<Libp2pEvents>
106-
private readonly log: Logger
107106

108107
constructor (components: DefaultUpgraderComponents, init: UpgraderInit) {
109108
this.components = components
110109
this.connectionEncryption = new Map()
111-
this.log = components.logger.forComponent('libp2p:upgrader')
112110

113111
init.connectionEncryption.forEach(encrypter => {
114112
this.connectionEncryption.set(encrypter.protocol, encrypter)
@@ -167,7 +165,7 @@ export class DefaultUpgrader implements Upgrader {
167165

168166
this.components.metrics?.trackMultiaddrConnection(maConn)
169167

170-
this.log('starting the inbound connection upgrade')
168+
maConn.log('starting the inbound connection upgrade')
171169

172170
// Protect
173171
let protectedConn = maConn
@@ -176,7 +174,7 @@ export class DefaultUpgrader implements Upgrader {
176174
const protector = this.components.connectionProtector
177175

178176
if (protector != null) {
179-
this.log('protecting the inbound connection')
177+
maConn.log('protecting the inbound connection')
180178
protectedConn = await protector.protect(maConn)
181179
}
182180
}
@@ -223,13 +221,13 @@ export class DefaultUpgrader implements Upgrader {
223221
upgradedConn = multiplexed.stream
224222
}
225223
} catch (err: any) {
226-
this.log.error('Failed to upgrade inbound connection', err)
224+
maConn.log.error('failed to upgrade inbound connection', err)
227225
throw err
228226
}
229227

230228
await this.shouldBlockConnection(remotePeer, maConn, 'denyInboundUpgradedConnection')
231229

232-
this.log('successfully upgraded inbound connection')
230+
maConn.log('successfully upgraded inbound connection')
233231

234232
return this._createConnection({
235233
cryptoProtocol,
@@ -268,7 +266,7 @@ export class DefaultUpgrader implements Upgrader {
268266

269267
this.components.metrics?.trackMultiaddrConnection(maConn)
270268

271-
this.log('Starting the outbound connection upgrade')
269+
maConn.log('starting the outbound connection upgrade')
272270

273271
// If the transport natively supports encryption, skip connection
274272
// protector and encryption
@@ -321,14 +319,14 @@ export class DefaultUpgrader implements Upgrader {
321319
upgradedConn = multiplexed.stream
322320
}
323321
} catch (err: any) {
324-
this.log.error('Failed to upgrade outbound connection', err)
322+
maConn.log.error('failed to upgrade outbound connection', err)
325323
await maConn.close(err)
326324
throw err
327325
}
328326

329327
await this.shouldBlockConnection(remotePeer, maConn, 'denyOutboundUpgradedConnection')
330328

331-
this.log('Successfully upgraded outbound connection')
329+
maConn.log('successfully upgraded outbound connection')
332330

333331
return this._createConnection({
334332
cryptoProtocol,
@@ -425,7 +423,7 @@ export class DefaultUpgrader implements Upgrader {
425423
this._onStream({ connection, stream: muxedStream, protocol })
426424
})
427425
.catch(async err => {
428-
this.log.error('error handling incoming stream id %s', muxedStream.id, err.message, err.code, err.stack)
426+
connection.log.error('error handling incoming stream id %s', muxedStream.id, err.message, err.code, err.stack)
429427

430428
if (muxedStream.timeline.close == null) {
431429
await muxedStream.close()
@@ -445,7 +443,7 @@ export class DefaultUpgrader implements Upgrader {
445443

446444
try {
447445
if (options.signal == null) {
448-
this.log('No abort signal was passed while trying to negotiate protocols %s falling back to default timeout', protocols)
446+
muxedStream.log('no abort signal was passed while trying to negotiate protocols %s falling back to default timeout', protocols)
449447

450448
const signal = AbortSignal.timeout(DEFAULT_PROTOCOL_SELECT_TIMEOUT)
451449
setMaxListeners(Infinity, signal)
@@ -529,7 +527,7 @@ export class DefaultUpgrader implements Upgrader {
529527
muxer.sink(upgradedConn.source),
530528
upgradedConn.sink(muxer.source)
531529
]).catch(err => {
532-
this.log.error(err)
530+
connection.log.error('error piping data through muxer', err)
533531
})
534532
}
535533

@@ -544,14 +542,14 @@ export class DefaultUpgrader implements Upgrader {
544542
await connection.close()
545543
}
546544
} catch (err: any) {
547-
this.log.error(err)
545+
connection.log.error('error closing connection after timeline close', err)
548546
} finally {
549547
this.events.safeDispatchEvent('connection:close', {
550548
detail: connection
551549
})
552550
}
553551
})().catch(err => {
554-
this.log.error(err)
552+
connection.log.error('error thrown while dispatching connection:close event', err)
555553
})
556554
}
557555

@@ -580,14 +578,14 @@ export class DefaultUpgrader implements Upgrader {
580578
close: async (options?: AbortOptions) => {
581579
// Ensure remaining streams are closed gracefully
582580
if (muxer != null) {
583-
this.log.trace('close muxer')
581+
connection.log.trace('close muxer')
584582
await muxer.close(options)
585583
}
586584

587-
this.log.trace('close maconn')
585+
connection.log.trace('close maconn')
588586
// close the underlying transport
589587
await maConn.close(options)
590-
this.log.trace('closed maconn')
588+
connection.log.trace('closed maconn')
591589
},
592590
abort: (err) => {
593591
maConn.abort(err)
@@ -624,7 +622,7 @@ export class DefaultUpgrader implements Upgrader {
624622
*/
625623
async _encryptInbound (connection: MultiaddrConnection): Promise<CryptoResult> {
626624
const protocols = Array.from(this.connectionEncryption.keys())
627-
this.log('handling inbound crypto protocol selection', protocols)
625+
connection.log('handling inbound crypto protocol selection', protocols)
628626

629627
try {
630628
const { stream, protocol } = await mss.handle(connection, protocols, {
@@ -636,7 +634,7 @@ export class DefaultUpgrader implements Upgrader {
636634
throw new Error(`no crypto module found for ${protocol}`)
637635
}
638636

639-
this.log('encrypting inbound connection using', protocol)
637+
connection.log('encrypting inbound connection using', protocol)
640638

641639
return {
642640
...await encrypter.secureInbound(this.components.peerId, stream),
@@ -653,7 +651,7 @@ export class DefaultUpgrader implements Upgrader {
653651
*/
654652
async _encryptOutbound (connection: MultiaddrConnection, remotePeerId?: PeerId): Promise<CryptoResult> {
655653
const protocols = Array.from(this.connectionEncryption.keys())
656-
this.log('selecting outbound crypto protocol', protocols)
654+
connection.log('selecting outbound crypto protocol', protocols)
657655

658656
try {
659657
connection.log.trace('selecting encrypter from %s', protocols)
@@ -689,7 +687,7 @@ export class DefaultUpgrader implements Upgrader {
689687
*/
690688
async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: MultiaddrConnection, muxerFactory?: StreamMuxerFactory }> {
691689
const protocols = Array.from(muxers.keys())
692-
this.log('outbound selecting muxer %s', protocols)
690+
connection.log('outbound selecting muxer %s', protocols)
693691
try {
694692
connection.log.trace('selecting stream muxer from %s', protocols)
695693

@@ -706,7 +704,7 @@ export class DefaultUpgrader implements Upgrader {
706704

707705
return { stream, muxerFactory }
708706
} catch (err: any) {
709-
connection.log.error('error multiplexing outbound stream', err)
707+
connection.log.error('error multiplexing outbound connection', err)
710708
throw new CodeError(String(err), codes.ERR_MUXER_UNAVAILABLE)
711709
}
712710
}
@@ -717,7 +715,7 @@ export class DefaultUpgrader implements Upgrader {
717715
*/
718716
async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>): Promise<{ stream: MultiaddrConnection, muxerFactory?: StreamMuxerFactory }> {
719717
const protocols = Array.from(muxers.keys())
720-
this.log('inbound handling muxers %s', protocols)
718+
connection.log('inbound handling muxers %s', protocols)
721719
try {
722720
const { stream, protocol } = await mss.handle(connection, protocols, {
723721
log: connection.log
@@ -726,7 +724,7 @@ export class DefaultUpgrader implements Upgrader {
726724

727725
return { stream, muxerFactory }
728726
} catch (err: any) {
729-
this.log.error('error multiplexing inbound stream', err)
727+
connection.log.error('error multiplexing inbound connection', err)
730728
throw new CodeError(String(err), codes.ERR_MUXER_UNAVAILABLE)
731729
}
732730
}

0 commit comments

Comments
 (0)