Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

fix: clear blacklist for peer when connection is established #340

Merged
merged 1 commit into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/connection/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ class ConnectionManager {
this.switch.emit('connection:start', connection.theirPeerInfo)
if (connection.getState() === 'MUXED') {
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
// Clear the blacklist of the peer
this.switch.dialer.clearBlacklist(connection.theirPeerInfo)
} else {
connection.once('muxed', () => this.switch.emit('peer-mux-established', connection.theirPeerInfo))
connection.once('muxed', () => {
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
// Clear the blacklist of the peer
this.switch.dialer.clearBlacklist(connection.theirPeerInfo)
})
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/dialer/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ class Queue {
* @param {string} protocol
* @param {boolean} useFSM If callback should use a ConnectionFSM instead
* @param {function(Error, Connection)} callback
* @returns {void}
*/
add (protocol, useFSM, callback) {
if (!this.isDialAllowed()) {
nextTick(callback, ERR_BLACKLISTED())
return nextTick(callback, ERR_BLACKLISTED())
}
this._queue.push({ protocol, useFSM, callback })
}
Expand Down
34 changes: 33 additions & 1 deletion test/dial-fsm.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ chai.use(dirtyChai)
const sinon = require('sinon')
const PeerBook = require('peer-book')
const parallel = require('async/parallel')
const series = require('async/series')
const WS = require('libp2p-websockets')
const TCP = require('libp2p-tcp')
const secio = require('libp2p-secio')
Expand All @@ -25,16 +26,18 @@ describe('dialFSM', () => {
let switchA
let switchB
let switchC
let switchDialOnly
let peerAId
let peerBId
let protocol

before((done) => createInfos(3, (err, infos) => {
before((done) => createInfos(4, (err, infos) => {
expect(err).to.not.exist()

const peerA = infos[0]
const peerB = infos[1]
const peerC = infos[2]
const peerDialOnly = infos[3]

peerAId = peerA.id.toB58String()
peerBId = peerB.id.toB58String()
Expand All @@ -48,22 +51,27 @@ describe('dialFSM', () => {
switchA = new Switch(peerA, new PeerBook())
switchB = new Switch(peerB, new PeerBook())
switchC = new Switch(peerC, new PeerBook())
switchDialOnly = new Switch(peerDialOnly, new PeerBook())

switchA.transport.add('tcp', new TCP())
switchB.transport.add('tcp', new TCP())
switchC.transport.add('ws', new WS())
switchDialOnly.transport.add('ws', new WS())

switchA.connection.crypto(secio.tag, secio.encrypt)
switchB.connection.crypto(secio.tag, secio.encrypt)
switchC.connection.crypto(secio.tag, secio.encrypt)
switchDialOnly.connection.crypto(secio.tag, secio.encrypt)

switchA.connection.addStreamMuxer(multiplex)
switchB.connection.addStreamMuxer(multiplex)
switchC.connection.addStreamMuxer(multiplex)
switchDialOnly.connection.addStreamMuxer(multiplex)

switchA.connection.reuse()
switchB.connection.reuse()
switchC.connection.reuse()
switchDialOnly.connection.reuse()

parallel([
(cb) => switchA.start(cb),
Expand Down Expand Up @@ -155,6 +163,30 @@ describe('dialFSM', () => {
})
})

it('should clear the blacklist for a peer that connected to us', (done) => {
series([
// Attempt to dial the peer that's not listening
(cb) => switchC.dial(switchDialOnly._peerInfo, (err) => {
expect(err).to.exist()
cb()
}),
// Dial from the dial only peer
(cb) => switchDialOnly.dial(switchC._peerInfo, (err) => {
expect(err).to.not.exist()
// allow time for muxing to occur
setTimeout(cb, 100)
}),
// "Dial" to the dial only peer, this should reuse the existing connection
(cb) => switchC.dial(switchDialOnly._peerInfo, (err) => {
expect(err).to.not.exist()
cb()
})
], (err) => {
expect(err).to.not.exist()
done()
})
})

it('should emit a `closed` event when closed', (done) => {
protocol = '/closed/1.0.0'
switchB.handle(protocol, () => { })
Expand Down