Skip to content
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

fix: upgrader should not need muxers #517

Merged
merged 3 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
77 changes: 57 additions & 20 deletions src/upgrader.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Upgrader {
async upgradeInbound (maConn) {
let encryptedConn
let remotePeer
let muxedConnection
let upgradedConn
let Muxer
let cryptoProtocol
let setPeer
Expand Down Expand Up @@ -94,7 +94,11 @@ class Upgrader {
} = await this._encryptInbound(this.localPeer, protectedConn, this.cryptos))

// Multiplex the connection
;({ stream: muxedConnection, Muxer } = await this._multiplexInbound(encryptedConn, this.muxers))
if (this.muxers.size) {
({ stream: upgradedConn, Muxer } = await this._multiplexInbound(encryptedConn, this.muxers))
} else {
upgradedConn = encryptedConn
}
} catch (err) {
log.error('Failed to upgrade inbound connection', err)
await maConn.close(err)
Expand All @@ -112,7 +116,7 @@ class Upgrader {
cryptoProtocol,
direction: 'inbound',
maConn,
muxedConnection,
upgradedConn,
Muxer,
remotePeer
})
Expand All @@ -134,7 +138,7 @@ class Upgrader {

let encryptedConn
let remotePeer
let muxedConnection
let upgradedConn
let cryptoProtocol
let Muxer
let setPeer
Expand Down Expand Up @@ -164,7 +168,11 @@ class Upgrader {
} = await this._encryptOutbound(this.localPeer, protectedConn, remotePeerId, this.cryptos))

// Multiplex the connection
;({ stream: muxedConnection, Muxer } = await this._multiplexOutbound(encryptedConn, this.muxers))
if (this.muxers.size) {
({ stream: upgradedConn, Muxer } = await this._multiplexOutbound(encryptedConn, this.muxers))
} else {
upgradedConn = encryptedConn
}
} catch (err) {
log.error('Failed to upgrade outbound connection', err)
await maConn.close(err)
Expand All @@ -182,7 +190,7 @@ class Upgrader {
cryptoProtocol,
direction: 'outbound',
maConn,
muxedConnection,
upgradedConn,
Muxer,
remotePeer
})
Expand All @@ -195,7 +203,7 @@ class Upgrader {
* @param {string} cryptoProtocol The crypto protocol that was negotiated
* @param {string} direction One of ['inbound', 'outbound']
* @param {MultiaddrConnection} maConn The transport layer connection
* @param {*} muxedConnection A duplex connection returned from multiplexer selection
* @param {*} upgradedConn A duplex connection returned from multiplexer selection
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
* @param {Muxer} Muxer The muxer to be used for muxing
* @param {PeerId} remotePeer The peer the connection is with
* @returns {Connection}
Expand All @@ -204,10 +212,50 @@ class Upgrader {
cryptoProtocol,
direction,
maConn,
muxedConnection,
upgradedConn,
Muxer,
remotePeer
}) {
const _timeline = maConn.timeline
maConn.timeline = new Proxy(_timeline, {
set: (...args) => {
if (args[1] === 'close' && args[2] && !_timeline.close) {
connection.stat.status = 'closed'
this.onConnectionEnd(connection)
}

return Reflect.set(...args)
}
})

if (!Muxer) {
// Create the connection
maConn.timeline.upgraded = Date.now()
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved

const connection = new Connection({
localAddr: maConn.localAddr,
remoteAddr: maConn.remoteAddr,
localPeer: this.localPeer,
remotePeer: remotePeer,
stat: {
direction,
timeline: maConn.timeline,
encryption: cryptoProtocol
},
newStream: () => {
throw errCode(new Error('connection is not multiplexed'), 'ERR_CONNECTION_NOT_MULTIPLEXED')
},
getStreams: () => {
throw errCode(new Error('connection is not multiplexed'), 'ERR_CONNECTION_NOT_MULTIPLEXED')
},
close: err => maConn.close(err)
})

this.onConnection(connection)

return connection
}

// Create the muxer
const muxer = new Muxer({
// Run anytime a remote stream is created
Expand Down Expand Up @@ -244,20 +292,9 @@ class Upgrader {
}

// Pipe all data through the muxer
pipe(muxedConnection, muxer, muxedConnection)
pipe(upgradedConn, muxer, upgradedConn)

maConn.timeline.upgraded = Date.now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just set this right after we created the timeline Proxy, that way we don't need to set it here and in the !Muxer if statement above.

const _timeline = maConn.timeline
maConn.timeline = new Proxy(_timeline, {
set: (...args) => {
if (args[1] === 'close' && args[2] && !_timeline.close) {
connection.stat.status = 'closed'
this.onConnectionEnd(connection)
}

return Reflect.set(...args)
}
})

// Create the connection
const connection = new Connection({
Expand Down
22 changes: 22 additions & 0 deletions test/upgrading/upgrader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,28 @@ describe('Upgrader', () => {
expect(result).to.eql([hello])
})

it('should upgrade with only crypto', async () => {
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })

// No available muxers
const muxers = new Map()
sinon.stub(localUpgrader, 'muxers').value(muxers)
sinon.stub(remoteUpgrader, 'muxers').value(muxers)

const cryptos = new Map([[Crypto.protocol, Crypto]])
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)

const connections = await Promise.all([
localUpgrader.upgradeOutbound(outbound),
remoteUpgrader.upgradeInbound(inbound)
])

expect(connections).to.have.length(2)

await expect(connections[0].newStream('/echo/1.0.0')).to.be.rejected()
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I test the errors with multiplexer operations in the connection?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to add a quick test. They should also throw errors with codes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to also add a test for closing. It's going to the MultiaddrConn so it should be fine, but I think having it for avoid regressions in the future would be good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added to this test. I think it is better than create a new test


it('should use a private connection protector when provided', async () => {
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })

Expand Down