Skip to content

Commit

Permalink
fix: do not prune a peer that is in the allowlist + fix tests (libp2p…
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Feb 1, 2023
1 parent 442d5fa commit 1c04b61
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
log('too many connections open - closing a connection to %p', connection.remotePeer)
// check allow list
const connectionInAllowList = this.allow.some((ma) => {
return ma.getPeerId() === connection.remotePeer.toString()
return connection.remoteAddr.toString().startsWith(ma.toString())
})

// Connections in the allow list should be excluded from pruning
Expand Down
26 changes: 17 additions & 9 deletions test/connection-manager/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ describe('Connection Manager', () => {
})

it('should not close connection that is on the allowlist when pruning', async () => {
const max = 5
const remoteAddrPeerId = await createEd25519PeerId()
const max = 2
const remoteAddr = multiaddr('/ip4/83.13.55.32/tcp/59283')

libp2p = await createNode({
config: createBaseOptions({
Expand All @@ -193,23 +193,31 @@ describe('Connection Manager', () => {
const spies = new Map<number, sinon.SinonSpy<[], Promise<void>>>()

// Max out connections
for (let i = 1; i < max; i++) {
for (let i = 0; i < max; i++) {
const connection = mockConnection(mockMultiaddrConnection(mockDuplex(), await createEd25519PeerId()))
const spy = sinon.spy(connection, 'close')
const value = i * 10
const value = (i + 1) * 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))
// an outbound connection is opened from an address in the allow list
const remotePeer = await createEd25519PeerId()
const connection = mockConnection(mockMultiaddrConnection({
remoteAddr,
source: [],
sink: async () => {}
}, remotePeer))

const value = 0
const spy = sinon.spy(connection, 'close')
spies.set(value, spy)

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

Expand All @@ -229,7 +237,7 @@ describe('Connection Manager', () => {
})[0]
const lowestSpy = spies.get(lowest)

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

0 comments on commit 1c04b61

Please sign in to comment.