Skip to content

feat: multistream select protocol negotiation error #3237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/interface/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ export class UnsupportedProtocolError extends Error {
}
}

/**
* Thrown when a protocol is not negotiated properly
*/
export class ProtocolNegotiationError extends Error {
static name = 'ProtocolNegotiationError'

constructor (message = 'Protocol negotiation error', options?: ErrorOptions) {
super(message, options)
this.name = 'ProtocolNegotiationError'
}
}

/**
* An invalid or malformed message was encountered during a protocol exchange
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p/test/upgrading/upgrader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('upgrader', () => {
await expect(upgrader.upgradeOutbound(maConn, {
signal: AbortSignal.timeout(100)
})).to.eventually.be.rejected
.with.property('message').that.include('aborted')
.with.property('message').that.include('protocol negotiation failed')
})

it('should not abort if inbound upgrade is successful', async () => {
Expand Down
57 changes: 31 additions & 26 deletions packages/multistream-select/src/select.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UnsupportedProtocolError } from '@libp2p/interface'
import { ProtocolNegotiationError, UnsupportedProtocolError } from '@libp2p/interface'
import { lpStream } from 'it-length-prefixed-stream'
import pDefer from 'p-defer'
import { raceSignal } from 'race-signal'
Expand Down Expand Up @@ -79,38 +79,43 @@ export async function select <Stream extends SelectStream> (stream: Stream, prot
throw new Error('At least one protocol must be specified')
}

options.log.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol)
const p1 = uint8ArrayFromString(`${PROTOCOL_ID}\n`)
const p2 = uint8ArrayFromString(`${protocol}\n`)
await multistream.writeAll(lp, [p1, p2], options)
try {
options.log.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol)
const p1 = uint8ArrayFromString(`${PROTOCOL_ID}\n`)
const p2 = uint8ArrayFromString(`${protocol}\n`)
await multistream.writeAll(lp, [p1, p2], options)

options.log.trace('select: reading multistream-select header')
let response = await multistream.readString(lp, options)
options.log.trace('select: read "%s"', response)

// Read the protocol response if we got the protocolId in return
if (response === PROTOCOL_ID) {
options.log.trace('select: reading protocol response')
response = await multistream.readString(lp, options)
options.log.trace('select: reading multistream-select header')
let response = await multistream.readString(lp, options)
options.log.trace('select: read "%s"', response)
}

// We're done
if (response === protocol) {
return { stream: lp.unwrap(), protocol }
}

// We haven't gotten a valid ack, try the other protocols
for (const protocol of protocols) {
options.log.trace('select: write "%s"', protocol)
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options)
options.log.trace('select: reading protocol response')
const response = await multistream.readString(lp, options)
options.log.trace('select: read "%s" for "%s"', response, protocol)
// Read the protocol response if we got the protocolId in return
if (response === PROTOCOL_ID) {
options.log.trace('select: reading protocol response')
response = await multistream.readString(lp, options)
options.log.trace('select: read "%s"', response)
}

// We're done
if (response === protocol) {
return { stream: lp.unwrap(), protocol }
}

// We haven't gotten a valid ack, try the other protocols
for (const protocol of protocols) {
options.log.trace('select: write "%s"', protocol)
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options)
options.log.trace('select: reading protocol response')
const response = await multistream.readString(lp, options)
options.log.trace('select: read "%s" for "%s"', response, protocol)

if (response === protocol) {
return { stream: lp.unwrap(), protocol }
}
}
} catch (err) {
options.log.error('select: error negotiating protocol', err)
throw new ProtocolNegotiationError('protocol negotiation failed', { cause: err })
}

throw new UnsupportedProtocolError('protocol selection failed')
Expand Down
Loading