Skip to content

Commit

Permalink
fix: should not prune a peer that is in the allow list once the conne…
Browse files Browse the repository at this point in the history
…ction limit is reached (#1515)
  • Loading branch information
maschad committed Dec 8, 2022
1 parent d6b4fd3 commit 7d17a3b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,15 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven

for (const connection of sortedConnections) {
log('too many connections open - closing a connection to %p', connection.remotePeer)
toClose.push(connection)
// check allow list
const connectionInAllowList = this.allow.some((ma) => {
return ma.getPeerId() === connection.remotePeer.toString()
})

// Connections in the allow list should be excluded from pruning
if (!connectionInAllowList) {
toClose.push(connection)
}

if (toClose.length === toPrune) {
break
Expand Down
65 changes: 65 additions & 0 deletions test/connection-manager/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,71 @@ describe('Connection Manager', () => {
expect(lowestSpy).to.have.property('callCount', 1)
})

it('should not close connection that is on the allowlist when pruning', async () => {
const max = 5
const remoteAddrPeerId = await createEd25519PeerId()

libp2p = await createNode({
config: createBaseOptions({
connectionManager: {
maxIncomingConnections: max,
minIncomingConnections: 0,
allow: [
'/ip4/83.13.55.32'
]
}
}),
started: false
})

await libp2p.start()

const connectionManager = libp2p.connectionManager as DefaultConnectionManager
const connectionManagerMaybeDisconnectOneSpy = sinon.spy(connectionManager, '_pruneConnections')
const spies = new Map<number, sinon.SinonSpy<[], Promise<void>>>()

// Max out connections
for (let i = 1; i < max; i++) {
const connection = mockConnection(mockMultiaddrConnection(mockDuplex(), await createEd25519PeerId()))
const spy = sinon.spy(connection, 'close')
const value = i * 10
spies.set(value, spy)
await libp2p.peerStore.tagPeer(connection.remotePeer, 'test-tag', {
value
})
await connectionManager._onConnect(new CustomEvent('connection', { detail: connection }))
}

// Connect to the peer on the allowed list
const connection = mockConnection(mockMultiaddrConnection(mockDuplex(), remoteAddrPeerId))

// Tag that allowed peer with lowest value
const value = 0 * 10
await libp2p.peerStore.tagPeer(connection.remotePeer, 'test-tag', {
value
})

await connectionManager._onConnect(new CustomEvent('connection', { detail: connection }))

// get the lowest value
const lowest = Array.from(spies.keys()).sort((a, b) => {
if (a > b) {
return 1
}

if (a < b) {
return -1
}

return 0
})[0]
const lowestSpy = spies.get(lowest)

expect(connectionManagerMaybeDisconnectOneSpy.callCount).to.equal(0)
// expect lowest value spy NOT to be called since the peer is in the allow list
expect(lowestSpy).to.have.property('callCount', 0)
})

it('should close connection when the maximum incoming has been reached even without tags', async () => {
const max = 5
libp2p = await createNode({
Expand Down

0 comments on commit 7d17a3b

Please sign in to comment.