Skip to content

Commit f306cba

Browse files
authored
fix: clear blacklist for peer when connection is established (libp2p#340)
1 parent f879cfc commit f306cba

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/connection/manager.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ class ConnectionManager {
3636
this.switch.emit('connection:start', connection.theirPeerInfo)
3737
if (connection.getState() === 'MUXED') {
3838
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
39+
// Clear the blacklist of the peer
40+
this.switch.dialer.clearBlacklist(connection.theirPeerInfo)
3941
} else {
40-
connection.once('muxed', () => this.switch.emit('peer-mux-established', connection.theirPeerInfo))
42+
connection.once('muxed', () => {
43+
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
44+
// Clear the blacklist of the peer
45+
this.switch.dialer.clearBlacklist(connection.theirPeerInfo)
46+
})
4147
}
4248
}
4349
}

src/dialer/queue.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ class Queue {
8282
* @param {string} protocol
8383
* @param {boolean} useFSM If callback should use a ConnectionFSM instead
8484
* @param {function(Error, Connection)} callback
85+
* @returns {void}
8586
*/
8687
add (protocol, useFSM, callback) {
8788
if (!this.isDialAllowed()) {
88-
nextTick(callback, ERR_BLACKLISTED())
89+
return nextTick(callback, ERR_BLACKLISTED())
8990
}
9091
this._queue.push({ protocol, useFSM, callback })
9192
}

test/dial-fsm.node.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ chai.use(dirtyChai)
1010
const sinon = require('sinon')
1111
const PeerBook = require('peer-book')
1212
const parallel = require('async/parallel')
13+
const series = require('async/series')
1314
const WS = require('libp2p-websockets')
1415
const TCP = require('libp2p-tcp')
1516
const secio = require('libp2p-secio')
@@ -25,16 +26,18 @@ describe('dialFSM', () => {
2526
let switchA
2627
let switchB
2728
let switchC
29+
let switchDialOnly
2830
let peerAId
2931
let peerBId
3032
let protocol
3133

32-
before((done) => createInfos(3, (err, infos) => {
34+
before((done) => createInfos(4, (err, infos) => {
3335
expect(err).to.not.exist()
3436

3537
const peerA = infos[0]
3638
const peerB = infos[1]
3739
const peerC = infos[2]
40+
const peerDialOnly = infos[3]
3841

3942
peerAId = peerA.id.toB58String()
4043
peerBId = peerB.id.toB58String()
@@ -48,22 +51,27 @@ describe('dialFSM', () => {
4851
switchA = new Switch(peerA, new PeerBook())
4952
switchB = new Switch(peerB, new PeerBook())
5053
switchC = new Switch(peerC, new PeerBook())
54+
switchDialOnly = new Switch(peerDialOnly, new PeerBook())
5155

5256
switchA.transport.add('tcp', new TCP())
5357
switchB.transport.add('tcp', new TCP())
5458
switchC.transport.add('ws', new WS())
59+
switchDialOnly.transport.add('ws', new WS())
5560

5661
switchA.connection.crypto(secio.tag, secio.encrypt)
5762
switchB.connection.crypto(secio.tag, secio.encrypt)
5863
switchC.connection.crypto(secio.tag, secio.encrypt)
64+
switchDialOnly.connection.crypto(secio.tag, secio.encrypt)
5965

6066
switchA.connection.addStreamMuxer(multiplex)
6167
switchB.connection.addStreamMuxer(multiplex)
6268
switchC.connection.addStreamMuxer(multiplex)
69+
switchDialOnly.connection.addStreamMuxer(multiplex)
6370

6471
switchA.connection.reuse()
6572
switchB.connection.reuse()
6673
switchC.connection.reuse()
74+
switchDialOnly.connection.reuse()
6775

6876
parallel([
6977
(cb) => switchA.start(cb),
@@ -155,6 +163,30 @@ describe('dialFSM', () => {
155163
})
156164
})
157165

166+
it('should clear the blacklist for a peer that connected to us', (done) => {
167+
series([
168+
// Attempt to dial the peer that's not listening
169+
(cb) => switchC.dial(switchDialOnly._peerInfo, (err) => {
170+
expect(err).to.exist()
171+
cb()
172+
}),
173+
// Dial from the dial only peer
174+
(cb) => switchDialOnly.dial(switchC._peerInfo, (err) => {
175+
expect(err).to.not.exist()
176+
// allow time for muxing to occur
177+
setTimeout(cb, 100)
178+
}),
179+
// "Dial" to the dial only peer, this should reuse the existing connection
180+
(cb) => switchC.dial(switchDialOnly._peerInfo, (err) => {
181+
expect(err).to.not.exist()
182+
cb()
183+
})
184+
], (err) => {
185+
expect(err).to.not.exist()
186+
done()
187+
})
188+
})
189+
158190
it('should emit a `closed` event when closed', (done) => {
159191
protocol = '/closed/1.0.0'
160192
switchB.handle(protocol, () => { })

0 commit comments

Comments
 (0)