Skip to content

Commit

Permalink
fix: throw error for non-integer connection limits (libp2p#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Jan 31, 2023
1 parent 4084163 commit 81b5154
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,10 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven

this.opts = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)

if (this.opts.maxConnections < this.opts.minConnections) {
throw errCode(new Error('Connection Manager maxConnections must be greater than minConnections'), codes.ERR_INVALID_PARAMETERS)
}

log('options: %o', this.opts)

this.validateLimits()

this.components = components

/**
Expand Down Expand Up @@ -352,6 +350,20 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
})
}

validateLimits () {
if (this.opts.minConnections < 0 || !Number.isInteger(this.opts.minConnections)) {
throw errCode(new Error('Connection Manager minConnections must be a positive integer'), codes.ERR_INVALID_PARAMETERS)
}

if (this.opts.maxConnections < 0 || !Number.isInteger(this.opts.maxConnections)) {
throw errCode(new Error('Connection Manager maxConnections must be a positive integer'), codes.ERR_INVALID_PARAMETERS)
}

if (this.opts.maxConnections < this.opts.minConnections) {
throw errCode(new Error('Connection Manager maxConnections must be greater than minConnections'), codes.ERR_INVALID_PARAMETERS)
}
}

async beforeStop () {
// if we are still dialing KEEP_ALIVE peers, abort those dials
this.connectOnStartupController?.abort()
Expand Down
37 changes: 37 additions & 0 deletions test/connection-manager/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { Dialer } from '@libp2p/interface-connection-manager'
import type { Connection } from '@libp2p/interface-connection'
import type { Upgrader } from '@libp2p/interface-transport'
import type { PeerStore } from '@libp2p/interface-peer-store'
import { codes } from '../../src/errors.js'

const defaultOptions = {
maxConnections: 10,
Expand Down Expand Up @@ -198,6 +199,42 @@ describe('Connection Manager', () => {
expect(spy).to.have.property('callCount', 1)
})

it('should fail if the connection manager has negative maxConnections limit', async () => {
await expect(createNode({
config: createBaseOptions({
connectionManager: { maxConnections: -5 }
}),
started: false
})).to.eventually.rejected('maxConnections must be greater than 0').with.property('code', codes.ERR_INVALID_PARAMETERS)
})

it('should fail if the connection manager has negative minConnections limit', async () => {
await expect(createNode({
config: createBaseOptions({
connectionManager: { minConnections: -5 }
}),
started: false
})).to.eventually.rejected('minConnections must be greater than 0').with.property('code', codes.ERR_INVALID_PARAMETERS)
})

it('should fail if the connection manager has non-integer maxConnections limit', async () => {
await expect(createNode({
config: createBaseOptions({
connectionManager: { maxConnections: 1.5 }
}),
started: false
})).to.eventually.rejected('maxConnections must be an integer').with.property('code', codes.ERR_INVALID_PARAMETERS)
})

it('should fail if the connection manager has non-integer minConnections limit', async () => {
await expect(createNode({
config: createBaseOptions({
connectionManager: { minConnections: 1.5 }
}),
started: false
})).to.eventually.rejected('minConnections must be an integer').with.property('code', codes.ERR_INVALID_PARAMETERS)
})

it('should fail if the connection manager has mismatched connection limit options', async () => {
await expect(createNode({
config: createBaseOptions({
Expand Down

0 comments on commit 81b5154

Please sign in to comment.