diff --git a/packages/devp2p/package.json b/packages/devp2p/package.json index 6630684dc5..463ed430fe 100644 --- a/packages/devp2p/package.json +++ b/packages/devp2p/package.json @@ -46,7 +46,7 @@ "scripts": { "build": "../../config/cli/ts-build.sh", "clean": "../../config/cli/clean-package.sh", - "coverage": "../../config/cli/coverage.sh", + "coverage": "DEBUG=ethjs npx vitest run --coverage.enabled --coverage.reporter=lcov", "docs:build": "typedoc --options typedoc.cjs", "examples": "tsx ../../scripts/examples-runner.ts -- devp2p", "examples:build": "npx embedme README.md", diff --git a/packages/devp2p/test/integration/rlpx-simulator.spec.ts b/packages/devp2p/test/integration/rlpx-simulator.spec.ts index 1613a9ae13..caa576a77d 100644 --- a/packages/devp2p/test/integration/rlpx-simulator.spec.ts +++ b/packages/devp2p/test/integration/rlpx-simulator.spec.ts @@ -6,76 +6,63 @@ import { DISCONNECT_REASON } from '../../src/types.js' import * as util from './util.js' describe('RLPx simulator tests', () => { - it('RLPX: add working node', () => { + it('RLPX: add working node', async () => { const basePort = 40404 - const rlpxs = util.initTwoPeerRLPXSetup(undefined, undefined, undefined, basePort) - - rlpxs[0].events.on('peer:added', async (peer) => { - assert.equal( - peer['_port'], - basePort + 1, - 'should have added peer on peer:added after successful handshake', - ) - assert.equal(rlpxs[0].getPeers().length, 1, 'peer list length should be 1') - assert.equal(rlpxs[0]._getOpenSlots(), 9, 'should have maxPeers - 1 open slots left') - await util.delay(500) - util.destroyRLPXs(rlpxs) + const { rlpxs, peer } = util.initTwoPeerRLPXSetup(undefined, undefined, undefined, basePort + 1) + rlpxs[0]['_dpt']!.addPeer(peer).catch(() => { + throw new Error('Peering failed') }) - }) - - it('RLPX: ban node with missing tcp port', () => { - const rlpxs = util.initTwoPeerRLPXSetup(undefined, undefined, undefined, 40444) - - rlpxs[0].events.on('peer:added', async () => { - const peer = { - id: hexToBytes('0xabcd'), - address: '127.0.0.1', - udpPort: 30308, - tcpPort: null, - } - assert.notOk( - rlpxs[0]['_dpt']!['_banlist'].has(peer), - 'should not be in ban list before bad peer discovered', - ) - rlpxs[0]['_dpt']!.events.emit('peer:new', peer) - assert.ok( - rlpxs[0]['_dpt']!['_banlist'].has(peer), - 'should be in ban list after bad peer discovered', - ) - await util.delay(500) - util.destroyRLPXs(rlpxs) + await new Promise((resolve) => { + rlpxs[0].events.on('peer:added', async (peer) => { + assert.equal( + peer['_port'], + basePort + 1, + 'should have added peer on peer:added after successful handshake', + ) + assert.equal(rlpxs[0].getPeers().length, 1, 'peer list length should be 1') + assert.equal(rlpxs[0]._getOpenSlots(), 9, 'should have maxPeers - 1 open slots left') + await util.delay(500) + util.destroyRLPXs(rlpxs) + resolve(undefined) + }) }) }) - - it('RLPX: remove node', () => { - const rlpxs = util.initTwoPeerRLPXSetup(undefined, undefined, undefined, 40504) - - try { - rlpxs[0].events.once('peer:added', (peer) => { - rlpxs[0].disconnect(peer['_remoteId']) - }) - rlpxs[0].events.once('peer:removed', async (_, reason: any) => { - assert.equal( - reason, - DISCONNECT_REASON.CLIENT_QUITTING, - 'should close with CLIENT_QUITTING disconnect reason', + it('RLPX: ban node with missing tcp port', async () => { + const { rlpxs, peer } = util.initTwoPeerRLPXSetup(undefined, undefined, undefined, 40444) + rlpxs[0]['_dpt']!.addPeer(peer).catch(() => { + throw new Error('Peering failed') + }) + await new Promise((resolve) => { + rlpxs[0].events.on('peer:added', async () => { + const peer = { + id: hexToBytes('0xabcd'), + address: '127.0.0.1', + udpPort: 30308, + tcpPort: null, + } + assert.notOk( + rlpxs[0]['_dpt']!['_banlist'].has(peer), + 'should not be in ban list before bad peer discovered', + ) + rlpxs[0]['_dpt']!.events.emit('peer:new', peer) + assert.ok( + rlpxs[0]['_dpt']!['_banlist'].has(peer), + 'should be in ban list after bad peer discovered', ) - assert.equal(rlpxs[0]._getOpenSlots(), 10, 'should have maxPeers open slots left') await util.delay(500) util.destroyRLPXs(rlpxs) + resolve(undefined) }) - } catch (err) { - assert.fail(`An unexpected error occurred: ${err}`) - } + }) }) - - it('RLPX: test peer queue / refill connections', () => { + it('RLPX: test peer queue / refill connections', async () => { const basePort = 60661 const rlpxs = util.getTestRLPXs(3, 1, basePort) const peer = { address: util.localhost, udpPort: basePort + 1, tcpPort: basePort + 1 } rlpxs[0]['_dpt']!.addPeer(peer) - try { + await new Promise((resolve) => { rlpxs[0].events.on('peer:added', async (peer) => { + //@ts-ignore if ((peer['_socket'] as any)._peername.port === basePort + 1) { assert.equal(rlpxs[0]['_peersQueue'].length, 0, 'peers queue should contain no peers') const peer2 = { @@ -90,10 +77,33 @@ describe('RLPx simulator tests', () => { if ((peer['_socket'] as any)._peername.port === basePort + 2) { assert.equal(rlpxs[0]['_peersQueue'].length, 0, 'peers queue should contain no peers') util.destroyRLPXs(rlpxs) + resolve(undefined) } }) - } catch (err) { - assert.fail(`An unexpected error occurred: ${err}`) - } + }) + }, 30000) + it('RLPX: remove node', async () => { + const { rlpxs, peer } = util.initTwoPeerRLPXSetup(undefined, undefined, undefined, 40504) + rlpxs[0] + ['_dpt']!.addPeer(peer) + .then((peer1) => { + rlpxs[0].disconnect(peer1['id']!) + }) + .catch((e) => { + throw new Error(`Peering failed: ${e}: ${e.stack}`) + }) + await new Promise((resolve) => { + rlpxs[0].events.once('peer:removed', async (_, reason: any) => { + assert.equal( + reason, + DISCONNECT_REASON.CLIENT_QUITTING, + 'should close with CLIENT_QUITTING disconnect reason', + ) + assert.equal(rlpxs[0]._getOpenSlots(), 10, 'should have maxPeers open slots left') + await util.delay(500) + util.destroyRLPXs(rlpxs) + }) + resolve(undefined) + }) }) }) diff --git a/packages/devp2p/test/integration/util.ts b/packages/devp2p/test/integration/util.ts index 235b5f05d7..78426c108a 100644 --- a/packages/devp2p/test/integration/util.ts +++ b/packages/devp2p/test/integration/util.ts @@ -97,18 +97,23 @@ export function getTestRLPXs( return rlpxs } +type TestRLPXs = { + rlpxs: RLPx[] + peer: { + address: string + udpPort: number + tcpPort: number + } +} export function initTwoPeerRLPXSetup( maxPeers?: any, capabilities?: any, common?: Object | Common, basePort = 30306, -): RLPx[] { +): TestRLPXs { const rlpxs = getTestRLPXs(2, maxPeers, basePort, capabilities, common) const peer = { address: localhost, udpPort: basePort + 1, tcpPort: basePort + 1 } - rlpxs[0]['_dpt']!.addPeer(peer).catch(() => { - /* Silently catch rejections here since not an actual test error */ - }) - return rlpxs + return { rlpxs, peer } } export function destroyRLPXs(rlpxs: any) { @@ -135,7 +140,10 @@ export function twoPeerMsgExchange3( common?: Object | Common, basePort = 30306, ) { - const rlpxs = initTwoPeerRLPXSetup(null, capabilities, common, basePort) + const { rlpxs, peer } = initTwoPeerRLPXSetup(null, capabilities, common, basePort) + rlpxs[0]['_dpt']!.addPeer(peer).catch(() => { + throw new Error('Peering failed') + }) rlpxs[0].events.on('peer:added', function (peer: any) { const protocol = peer.getProtocols()[0] opts.sendMessage(rlpxs, protocol) diff --git a/packages/genesis/package.json b/packages/genesis/package.json index df7ac6dbd0..4954784ac5 100644 --- a/packages/genesis/package.json +++ b/packages/genesis/package.json @@ -45,7 +45,7 @@ "scripts": { "build": "../../config/cli/ts-build.sh", "clean": "../../config/cli/clean-package.sh", - "coverage": "../../config/cli/coverage.sh", + "coverage": "DEBUG=ethjs npx vitest run --coverage.enabled --coverage.reporter=lcov", "examples": "tsx ../../scripts/examples-runner.ts -- genesis", "examples:build": "npx embedme README.md", "docs:build": "typedoc --options typedoc.cjs", diff --git a/packages/wallet/package.json b/packages/wallet/package.json index f1c7dbc98e..a6797e4b14 100644 --- a/packages/wallet/package.json +++ b/packages/wallet/package.json @@ -34,7 +34,7 @@ "scripts": { "build": "../../config/cli/ts-build.sh", "clean": "../../config/cli/clean-package.sh", - "coverage": "c8 --all --reporter=lcov --reporter=text npm run test:node", + "coverage": "DEBUG=ethjs npx vitest run --coverage.enabled --coverage.reporter=lcov", "docs:build": "npx typedoc --options typedoc.cjs", "examples": "tsx ../../scripts/examples-runner.ts -- wallet", "examples:build": "npx embedme README.md",