Skip to content

Commit f64c73b

Browse files
Alan Shawjacobheun
Alan Shaw
authored andcommitted
refactor: blacklist to denylist (libp2p#341)
"denylist" more adequately describes what this list is for. BREAKING CHANGE: Constructor options `blacklistTTL` and `blackListAttempts` have been renamed to `denyTTL` and `denyAttempts`. The error code from errors thrown when dial is currently denied has changed from `ERR_BLACKLISTED` to `ERR_DENIED`. License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent a386bb7 commit f64c73b

11 files changed

+70
-74
lines changed

CHANGELOG.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Bug Fixes
66

7-
* clear blacklist for peer when connection is established ([#340](https://github.com/libp2p/js-libp2p-switch/issues/340)) ([f306cba](https://github.com/libp2p/js-libp2p-switch/commit/f306cba))
7+
* clear denylist for peer when connection is established ([#340](https://github.com/libp2p/js-libp2p-switch/issues/340)) ([f306cba](https://github.com/libp2p/js-libp2p-switch/commit/f306cba))
88
* dont blindly add observed addresses to our list ([#337](https://github.com/libp2p/js-libp2p-switch/issues/337)) ([f879cfc](https://github.com/libp2p/js-libp2p-switch/commit/f879cfc))
99

1010

@@ -72,7 +72,7 @@
7272

7373
### Bug Fixes
7474

75-
* dont blacklist good peers ([#319](https://github.com/libp2p/js-libp2p-switch/issues/319)) ([f31663f](https://github.com/libp2p/js-libp2p-switch/commit/f31663f))
75+
* dont denylist good peers ([#319](https://github.com/libp2p/js-libp2p-switch/issues/319)) ([f31663f](https://github.com/libp2p/js-libp2p-switch/commit/f31663f))
7676
* revert to try each ([#320](https://github.com/libp2p/js-libp2p-switch/issues/320)) ([805d1ad](https://github.com/libp2p/js-libp2p-switch/commit/805d1ad))
7777

7878

@@ -1067,6 +1067,3 @@ using the connection returned via the callback.
10671067

10681068
<a name="0.1.0"></a>
10691069
# 0.1.0 (2015-07-19)
1070-
1071-
1072-

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ const sw = new switch(peerInfo , peerBook [, options])
6262

6363
If defined, `options` should be an object with the following keys and respective values:
6464

65-
- `blacklistTTL`: - number of ms a peer should not be dialable to after it errors. Each successive blacklisting will increase the ttl from the base value. Defaults to 5 minutes
66-
- `blackListAttempts`: - number of blacklists before a peer
67-
is permanently blacklisted. Defaults to 5.
65+
- `denyTTL`: - number of ms a peer should not be dialable to after it errors. Each successive deny will increase the TTL from the base value. Defaults to 5 minutes
66+
- `denyAttempts`: - number of times a peer can be denied before they are permanently denied. Defaults to 5.
6867
- `maxParallelDials`: - number of concurrent dials the switch should allow. Defaults to `100`
6968
- `maxColdCalls`: - number of queued cold calls that are allowed. Defaults to `50`
7069
- `dialTimeout`: - number of ms a dial to a peer should be allowed to run. Defaults to `30000` (30 seconds)

src/connection/manager.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ 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)
39+
// Clear the denylist of the peer
40+
this.switch.dialer.clearDenylist(connection.theirPeerInfo)
4141
} else {
4242
connection.once('muxed', () => {
4343
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
44-
// Clear the blacklist of the peer
45-
this.switch.dialer.clearBlacklist(connection.theirPeerInfo)
44+
// Clear the denylist of the peer
45+
this.switch.dialer.clearDenylist(connection.theirPeerInfo)
4646
})
4747
}
4848
}

src/constants.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

33
module.exports = {
4-
BLACK_LIST_TTL: 5 * 60 * 1e3, // How long before an errored peer can be dialed again
5-
BLACK_LIST_ATTEMPTS: 5, // Num of unsuccessful dials before a peer is permanently blacklisted
4+
DENY_TTL: 5 * 60 * 1e3, // How long before an errored peer can be dialed again
5+
DENY_ATTEMPTS: 5, // Num of unsuccessful dials before a peer is permanently denied
66
DIAL_TIMEOUT: 30e3, // How long in ms a dial attempt is allowed to take
77
MAX_COLD_CALLS: 50, // How many dials w/o protocols that can be queued
88
MAX_PARALLEL_DIALS: 100, // Maximum allowed concurrent dials

src/dialer/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const DialQueueManager = require('./queueManager')
44
const getPeerInfo = require('../get-peer-info')
55
const {
6-
BLACK_LIST_ATTEMPTS,
7-
BLACK_LIST_TTL,
6+
DENY_ATTEMPTS,
7+
DENY_TTL,
88
MAX_COLD_CALLS,
99
MAX_PARALLEL_DIALS,
1010
PRIORITY_HIGH,
@@ -59,11 +59,11 @@ module.exports = function (_switch) {
5959
}
6060

6161
/**
62-
* Clears the blacklist for a given peer
62+
* Clears the denylist for a given peer
6363
* @param {PeerInfo} peerInfo
6464
*/
65-
function clearBlacklist (peerInfo) {
66-
dialQueueManager.clearBlacklist(peerInfo)
65+
function clearDenylist (peerInfo) {
66+
dialQueueManager.clearDenylist(peerInfo)
6767
}
6868

6969
/**
@@ -110,9 +110,9 @@ module.exports = function (_switch) {
110110
connect,
111111
dial,
112112
dialFSM,
113-
clearBlacklist,
114-
BLACK_LIST_ATTEMPTS: isNaN(_switch._options.blackListAttempts) ? BLACK_LIST_ATTEMPTS : _switch._options.blackListAttempts,
115-
BLACK_LIST_TTL: isNaN(_switch._options.blacklistTTL) ? BLACK_LIST_TTL : _switch._options.blacklistTTL,
113+
clearDenylist,
114+
DENY_ATTEMPTS: isNaN(_switch._options.denyAttempts) ? DENY_ATTEMPTS : _switch._options.denyAttempts,
115+
DENY_TTL: isNaN(_switch._options.denyTTL) ? DENY_TTL : _switch._options.denyTTL,
116116
MAX_COLD_CALLS: isNaN(_switch._options.maxColdCalls) ? MAX_COLD_CALLS : _switch._options.maxColdCalls,
117117
MAX_PARALLEL_DIALS: isNaN(_switch._options.maxParallelDials) ? MAX_PARALLEL_DIALS : _switch._options.maxParallelDials
118118
}

src/dialer/queue.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const ConnectionFSM = require('../connection')
4-
const { DIAL_ABORTED, ERR_BLACKLISTED } = require('../errors')
4+
const { DIAL_ABORTED, ERR_DENIED } = require('../errors')
55
const nextTick = require('async/nextTick')
66
const once = require('once')
77
const debug = require('debug')
@@ -68,8 +68,8 @@ class Queue {
6868
this.id = peerId
6969
this.switch = _switch
7070
this._queue = []
71-
this.blackListed = null
72-
this.blackListCount = 0
71+
this.denylisted = null
72+
this.denylistCount = 0
7373
this.isRunning = false
7474
this.onStopped = onStopped
7575
}
@@ -86,7 +86,7 @@ class Queue {
8686
*/
8787
add (protocol, useFSM, callback) {
8888
if (!this.isDialAllowed()) {
89-
return nextTick(callback, ERR_BLACKLISTED())
89+
return nextTick(callback, ERR_DENIED())
9090
}
9191
this._queue.push({ protocol, useFSM, callback })
9292
}
@@ -96,10 +96,10 @@ class Queue {
9696
* @returns {boolean}
9797
*/
9898
isDialAllowed () {
99-
if (this.blackListed) {
100-
// If the blacklist ttl has passed, reset it
101-
if (Date.now() > this.blackListed) {
102-
this.blackListed = null
99+
if (this.denylisted) {
100+
// If the deny ttl has passed, reset it
101+
if (Date.now() > this.denylisted) {
102+
this.denylisted = null
103103
return true
104104
}
105105
// Dial is not allowed
@@ -146,25 +146,25 @@ class Queue {
146146
}
147147

148148
/**
149-
* Marks the queue as blacklisted. The queue will be immediately aborted.
149+
* Marks the queue as denylisted. The queue will be immediately aborted.
150150
* @returns {void}
151151
*/
152-
blacklist () {
153-
this.blackListCount++
152+
denylist () {
153+
this.denylistCount++
154154

155-
if (this.blackListCount >= this.switch.dialer.BLACK_LIST_ATTEMPTS) {
156-
this.blackListed = Infinity
155+
if (this.denylistCount >= this.switch.dialer.DENY_ATTEMPTS) {
156+
this.denylisted = Infinity
157157
return
158158
}
159159

160-
let ttl = this.switch.dialer.BLACK_LIST_TTL * Math.pow(this.blackListCount, 3)
160+
let ttl = this.switch.dialer.DENY_TTL * Math.pow(this.denylistCount, 3)
161161
const minTTL = ttl * 0.9
162162
const maxTTL = ttl * 1.1
163163

164164
// Add a random jitter of 20% to the ttl
165165
ttl = Math.floor(Math.random() * (maxTTL - minTTL) + minTTL)
166166

167-
this.blackListed = Date.now() + ttl
167+
this.denylisted = Date.now() + ttl
168168
this.abort()
169169
}
170170

@@ -244,11 +244,11 @@ class Queue {
244244
// depending on the error.
245245
connectionFSM.once('error', (err) => {
246246
queuedDial.callback(err)
247-
// Dont blacklist peers we have identified and that we are connected to
247+
// Dont denylist peers we have identified and that we are connected to
248248
if (peerInfo.protocols.size > 0 && peerInfo.isConnected()) {
249249
return
250250
}
251-
this.blacklist()
251+
this.denylist()
252252
})
253253

254254
connectionFSM.once('close', () => {
@@ -257,14 +257,14 @@ class Queue {
257257

258258
// If we're not muxed yet, add listeners
259259
connectionFSM.once('muxed', () => {
260-
this.blackListCount = 0 // reset blacklisting on good connections
260+
this.denylistCount = 0 // reset denylisting on good connections
261261
queuedDial.connection = connectionFSM
262262
createConnectionWithProtocol(queuedDial)
263263
next()
264264
})
265265

266266
connectionFSM.once('unmuxed', () => {
267-
this.blackListCount = 0
267+
this.denylistCount = 0
268268
queuedDial.connection = connectionFSM
269269
createConnectionWithProtocol(queuedDial)
270270
next()

src/dialer/queueManager.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ class DialQueueManager {
2727

2828
/**
2929
* Runs through all queues, aborts and removes them if they
30-
* are no longer valid. A queue that is blacklisted indefinitely,
30+
* are no longer valid. A queue that is denylisted indefinitely,
3131
* is considered no longer valid.
3232
* @private
3333
*/
3434
_clean () {
3535
const queues = Object.values(this._queues)
3636
queues.forEach(dialQueue => {
37-
// Clear if the queue has reached max blacklist
38-
if (dialQueue.blackListed === Infinity) {
37+
// Clear if the queue has reached max denylist
38+
if (dialQueue.denylisted === Infinity) {
3939
dialQueue.abort()
4040
delete this._queues[dialQueue.id]
4141
return
4242
}
4343

44-
// Keep track of blacklisted queues
45-
if (dialQueue.blackListed) return
44+
// Keep track of denylisted queues
45+
if (dialQueue.denylisted) return
4646

4747
// Clear if peer is no longer active
4848
// To avoid reallocating memory, dont delete queues of
@@ -125,7 +125,7 @@ class DialQueueManager {
125125

126126
// If we're already connected to the peer, start the queue now
127127
// While it might cause queues to go over the max parallel amount,
128-
// it avoids blocking peers we're already connected to
128+
// it avoids denying peers we're already connected to
129129
if (peerInfo.isConnected()) {
130130
targetQueue.start()
131131
return
@@ -184,13 +184,13 @@ class DialQueueManager {
184184
}
185185

186186
/**
187-
* Will remove the `peerInfo` from the dial blacklist
187+
* Will remove the `peerInfo` from the dial denylist
188188
* @param {PeerInfo} peerInfo
189189
*/
190-
clearBlacklist (peerInfo) {
190+
clearDenylist (peerInfo) {
191191
const queue = this.getQueue(peerInfo)
192-
queue.blackListed = null
193-
queue.blackListCount = 0
192+
queue.denylisted = null
193+
queue.denylistCount = 0
194194
}
195195

196196
/**

src/errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const errCode = require('err-code')
55
module.exports = {
66
CONNECTION_FAILED: (err) => errCode(err, 'CONNECTION_FAILED'),
77
DIAL_ABORTED: () => errCode('Dial was aborted', 'DIAL_ABORTED'),
8-
ERR_BLACKLISTED: () => errCode('Dial is currently blacklisted for this peer', 'ERR_BLACKLISTED'),
8+
ERR_DENIED: () => errCode('Dial is currently denied for this peer', 'ERR_DENIED'),
99
DIAL_SELF: () => errCode('A node cannot dial itself', 'DIAL_SELF'),
1010
INVALID_STATE_TRANSITION: (err) => errCode(err, 'INVALID_STATE_TRANSITION'),
1111
NO_TRANSPORTS_REGISTERED: () => errCode('No transports registered, dial not possible', 'NO_TRANSPORTS_REGISTERED'),

test/circuit-relay.node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const utils = require('./utils')
2020
const createInfos = utils.createInfos
2121
const Swarm = require('../src')
2222
const switchOptions = {
23-
blacklistTTL: 0 // nullifies blacklisting
23+
denyTTL: 0 // nullifies denylisting
2424
}
2525

2626
describe(`circuit`, function () {

test/dial-fsm.node.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('dialFSM', () => {
113113
protocol = '/error/1.0.0'
114114
switchC.handle(protocol, () => { })
115115

116-
switchA.dialer.clearBlacklist(switchC._peerInfo)
116+
switchA.dialer.clearDenylist(switchC._peerInfo)
117117
switchA.dialFSM(switchC._peerInfo, protocol, (err, connFSM) => {
118118
expect(err).to.not.exist()
119119
connFSM.once('error', (err) => {
@@ -124,34 +124,34 @@ describe('dialFSM', () => {
124124
})
125125
})
126126

127-
it('should error when the peer is blacklisted', (done) => {
127+
it('should error when the peer is denylisted', (done) => {
128128
protocol = '/error/1.0.0'
129129
switchC.handle(protocol, () => { })
130130

131-
switchA.dialer.clearBlacklist(switchC._peerInfo)
131+
switchA.dialer.clearDenylist(switchC._peerInfo)
132132
switchA.dialFSM(switchC._peerInfo, protocol, (err, connFSM) => {
133133
expect(err).to.not.exist()
134134
connFSM.once('error', () => {
135-
// dial with the blacklist
135+
// dial with the denylist
136136
switchA.dialFSM(switchC._peerInfo, protocol, (err) => {
137137
expect(err).to.exist()
138-
expect(err.code).to.eql('ERR_BLACKLISTED')
138+
expect(err.code).to.eql('ERR_DENIED')
139139
done()
140140
})
141141
})
142142
})
143143
})
144144

145-
it('should not blacklist a peer that was successfully connected', (done) => {
146-
protocol = '/noblacklist/1.0.0'
145+
it('should not denylist a peer that was successfully connected', (done) => {
146+
protocol = '/nodenylist/1.0.0'
147147
switchB.handle(protocol, () => { })
148148

149-
switchA.dialer.clearBlacklist(switchB._peerInfo)
149+
switchA.dialer.clearDenylist(switchB._peerInfo)
150150
switchA.dialFSM(switchB._peerInfo, protocol, (err, connFSM) => {
151151
expect(err).to.not.exist()
152152
connFSM.once('connection', () => {
153153
connFSM.once('close', () => {
154-
// peer should not be blacklisted
154+
// peer should not be denylisted
155155
switchA.dialFSM(switchB._peerInfo, protocol, (err, conn) => {
156156
expect(err).to.not.exist()
157157
conn.once('close', done)
@@ -163,7 +163,7 @@ describe('dialFSM', () => {
163163
})
164164
})
165165

166-
it('should clear the blacklist for a peer that connected to us', (done) => {
166+
it('should clear the denylist for a peer that connected to us', (done) => {
167167
series([
168168
// Attempt to dial the peer that's not listening
169169
(cb) => switchC.dial(switchDialOnly._peerInfo, (err) => {

0 commit comments

Comments
 (0)