Skip to content

Commit

Permalink
Guard against rpc port collisions (#3083)
Browse files Browse the repository at this point in the history
* Add guard for port collions in CLI params

* Add test

* Remove only

* Remove process exit
  • Loading branch information
acolytec3 authored Oct 4, 2023
1 parent a41a672 commit aaf9c8e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const args: ClientOpts = yargs(hideBin(process.argv))
})
.option('wsPort', {
describe: 'WS-RPC server listening port',
default: 8545,
default: 8546,
})
.option('wsAddr', {
describe: 'WS-RPC server listening address',
Expand All @@ -158,7 +158,7 @@ const args: ClientOpts = yargs(hideBin(process.argv))
.option('wsEnginePort', {
describe: 'WS-RPC server listening port for Engine namespace',
number: true,
default: 8551,
default: 8552,
})
.option('wsEngineAddr', {
describe: 'WS-RPC server listening interface address for Engine namespace',
Expand Down Expand Up @@ -369,7 +369,22 @@ const args: ClientOpts = yargs(hideBin(process.argv))
})
.completion()
// strict() ensures that yargs throws when an invalid arg is provided
.strict().argv
.strict()
.check((argv, _options) => {
const usedPorts = new Set()
let collision = false
if (argv.ws === true) {
usedPorts.add(argv.wsPort)
if (!usedPorts.has(argv.wsEnginePort)) {
usedPorts.add(argv.wsEnginePort)
}
}
if (argv.rpc === true && usedPorts.has(argv.rpcPort)) collision = true
if (argv.rpcEngine === true && usedPorts.has(argv.rpcEnginePort)) collision = true

if (collision) throw new Error('cannot reuse ports between RPC instances')
return true
}).argv

/**
* Initializes and returns the databases needed for the client
Expand Down
15 changes: 15 additions & 0 deletions packages/client/test/cli/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ describe('[CLI]', () => {
}
await clientRunHelper(cliArgs, onData, true)
}, 30000)
it('should throw error if the same port is assigned to multiple RPC servers', async () => {
const cliArgs = ['--ws', '--rpc', '--rpcPort=8546']
const onData = async (
message: string,
child: ChildProcessWithoutNullStreams,
resolve: Function
) => {
if (message.includes('cannot reuse')) {
assert.ok(true, 'cannot reuse ports between HTTP and WS RPCs')
}
child.kill(9)
resolve(undefined)
}
await clientRunHelper(cliArgs, onData, true)
}, 30000)
// engine rpc tests
it('should start engine rpc and provide endpoint', async () => {
const cliArgs = ['--rpcEngine', '--port=30310', '--dev=poa']
Expand Down

0 comments on commit aaf9c8e

Please sign in to comment.