Skip to content

Commit

Permalink
fix: catch errors during identify (#1138)
Browse files Browse the repository at this point in the history
Sometimes identify can be started while we are shutting down (for
example) - we should just log these errors.
  • Loading branch information
achingbrain authored Jan 21, 2022
1 parent a4bba35 commit 12f1bb0
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/identify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,25 @@ class IdentifyService {
* @returns {Promise<void>}
*/
async _handleIdentify ({ connection, stream }) {
let publicKey = new Uint8Array(0)
if (this.peerId.pubKey) {
publicKey = this.peerId.pubKey.bytes
}
try {
let publicKey = new Uint8Array(0)
if (this.peerId.pubKey) {
publicKey = this.peerId.pubKey.bytes
}

const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
const protocols = await this.peerStore.protoBook.get(this.peerId)
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
const protocols = await this.peerStore.protoBook.get(this.peerId)

const message = Message.Identify.encode({
protocolVersion: this._host.protocolVersion,
agentVersion: this._host.agentVersion,
publicKey,
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
signedPeerRecord,
observedAddr: connection.remoteAddr.bytes,
protocols
}).finish()
const message = Message.Identify.encode({
protocolVersion: this._host.protocolVersion,
agentVersion: this._host.agentVersion,
publicKey,
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
signedPeerRecord,
observedAddr: connection.remoteAddr.bytes,
protocols
}).finish()

try {
await pipe(
[message],
lp.encode(),
Expand Down Expand Up @@ -343,7 +343,11 @@ class IdentifyService {
}

// Update the protocols
await this.peerStore.protoBook.set(id, message.protocols)
try {
await this.peerStore.protoBook.set(id, message.protocols)
} catch (/** @type {any} */ err) {
log.error('received invalid protocols', err)
}
}

/**
Expand Down

0 comments on commit 12f1bb0

Please sign in to comment.