From 58523f91516b6ea7bd69996c03b152af05227500 Mon Sep 17 00:00:00 2001 From: Amir Date: Mon, 21 Oct 2024 13:16:03 -0700 Subject: [PATCH 1/9] Rework rlpx-simulator.spec.ts tests --- .../test/integration/rlpx-simulator.spec.ts | 138 +++++++++--------- packages/devp2p/test/integration/util.ts | 5 +- 2 files changed, 73 insertions(+), 70 deletions(-) diff --git a/packages/devp2p/test/integration/rlpx-simulator.spec.ts b/packages/devp2p/test/integration/rlpx-simulator.spec.ts index 1613a9ae13..8f9cb13d8b 100644 --- a/packages/devp2p/test/integration/rlpx-simulator.spec.ts +++ b/packages/devp2p/test/integration/rlpx-simulator.spec.ts @@ -6,94 +6,100 @@ 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') + }) + 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: 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) + 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', + ) + 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']) + 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']) }) - rlpxs[0].events.once('peer:removed', async (_, reason: any) => { + .catch((e) => { + throw new Error(`Peering failed: ${e}: ${e.stack}`) + }) + await new Promise((resolve) => { + rlpxs[0].events.once('peer:removed', (_, 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) }) - } catch (err) { - assert.fail(`An unexpected error occurred: ${err}`) - } + resolve(undefined) + }) }) - - 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) => { - if ((peer['_socket'] as any)._peername.port === basePort + 1) { - assert.equal(rlpxs[0]['_peersQueue'].length, 0, 'peers queue should contain no peers') - const peer2 = { - address: util.localhost, - udpPort: basePort + 2, - tcpPort: basePort + 2, - } - rlpxs[0]['_dpt']!.addPeer(peer2) - await util.delay(500) - assert.equal(rlpxs[0]['_peersQueue'].length, 1, 'peers queue should contain one peer') + assert.equal(peer._socket._peername.port, basePort + 1) + assert.equal(rlpxs[0]['_peersQueue'].length, 0, 'peers queue should contain no peers') + const peer2 = { + address: util.localhost, + udpPort: basePort + 2, + tcpPort: basePort + 2, } - if ((peer['_socket'] as any)._peername.port === basePort + 2) { + rlpxs[0]['_dpt']!.addPeer(peer2).then((peer) => { + assert.equal(rlpxs[0]['_peersQueue'].length, 1, 'peers queue should contain one peer') + assert.equal(peer.tcpPort, 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}`) - } - }) + }) + }, 10000) }) diff --git a/packages/devp2p/test/integration/util.ts b/packages/devp2p/test/integration/util.ts index 235b5f05d7..d59f063cae 100644 --- a/packages/devp2p/test/integration/util.ts +++ b/packages/devp2p/test/integration/util.ts @@ -105,10 +105,7 @@ export function initTwoPeerRLPXSetup( ): RLPx[] { 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) { From 29fa66cc6a2ef40761657b0a6f434e434ddebed9 Mon Sep 17 00:00:00 2001 From: Amir Date: Mon, 21 Oct 2024 15:27:37 -0700 Subject: [PATCH 2/9] Fix twoPeerMsgExchange3 --- packages/devp2p/test/integration/util.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/devp2p/test/integration/util.ts b/packages/devp2p/test/integration/util.ts index d59f063cae..f169169301 100644 --- a/packages/devp2p/test/integration/util.ts +++ b/packages/devp2p/test/integration/util.ts @@ -132,7 +132,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) From 055f677c3404d2e1e002e23915586d9e37a58011 Mon Sep 17 00:00:00 2001 From: Amir Date: Mon, 28 Oct 2024 16:59:44 -0700 Subject: [PATCH 3/9] Fix test --- packages/devp2p/test/integration/rlpx-simulator.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/devp2p/test/integration/rlpx-simulator.spec.ts b/packages/devp2p/test/integration/rlpx-simulator.spec.ts index 8f9cb13d8b..209c4c0682 100644 --- a/packages/devp2p/test/integration/rlpx-simulator.spec.ts +++ b/packages/devp2p/test/integration/rlpx-simulator.spec.ts @@ -95,7 +95,6 @@ describe('RLPx simulator tests', () => { rlpxs[0]['_dpt']!.addPeer(peer2).then((peer) => { assert.equal(rlpxs[0]['_peersQueue'].length, 1, 'peers queue should contain one peer') assert.equal(peer.tcpPort, basePort + 2) - assert.equal(rlpxs[0]['_peersQueue'].length, 0, 'peers queue should contain no peers') util.destroyRLPXs(rlpxs) resolve(undefined) }) From 414be158c24e4b71d025f9a164b39169dc35ecc9 Mon Sep 17 00:00:00 2001 From: Amir Date: Mon, 28 Oct 2024 17:18:15 -0700 Subject: [PATCH 4/9] Fix type errors --- .../devp2p/test/integration/rlpx-simulator.spec.ts | 3 ++- packages/devp2p/test/integration/util.ts | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/devp2p/test/integration/rlpx-simulator.spec.ts b/packages/devp2p/test/integration/rlpx-simulator.spec.ts index 209c4c0682..e83b80c587 100644 --- a/packages/devp2p/test/integration/rlpx-simulator.spec.ts +++ b/packages/devp2p/test/integration/rlpx-simulator.spec.ts @@ -60,7 +60,7 @@ describe('RLPx simulator tests', () => { rlpxs[0] ['_dpt']!.addPeer(peer) .then((peer1) => { - rlpxs[0].disconnect(peer1['id']) + rlpxs[0].disconnect(peer1['id']!) }) .catch((e) => { throw new Error(`Peering failed: ${e}: ${e.stack}`) @@ -85,6 +85,7 @@ describe('RLPx simulator tests', () => { rlpxs[0]['_dpt']!.addPeer(peer) await new Promise((resolve) => { rlpxs[0].events.on('peer:added', async (peer) => { + //@ts-ignore assert.equal(peer._socket._peername.port, basePort + 1) assert.equal(rlpxs[0]['_peersQueue'].length, 0, 'peers queue should contain no peers') const peer2 = { diff --git a/packages/devp2p/test/integration/util.ts b/packages/devp2p/test/integration/util.ts index f169169301..78426c108a 100644 --- a/packages/devp2p/test/integration/util.ts +++ b/packages/devp2p/test/integration/util.ts @@ -97,12 +97,20 @@ 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 } return { rlpxs, peer } From 79ea02091a57fa1fac40c976f3d394056d2b3d04 Mon Sep 17 00:00:00 2001 From: Amir Date: Mon, 28 Oct 2024 17:33:20 -0700 Subject: [PATCH 5/9] Restore tests to be more similar to previous format --- .../test/integration/rlpx-simulator.spec.ts | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/devp2p/test/integration/rlpx-simulator.spec.ts b/packages/devp2p/test/integration/rlpx-simulator.spec.ts index e83b80c587..745f9563f6 100644 --- a/packages/devp2p/test/integration/rlpx-simulator.spec.ts +++ b/packages/devp2p/test/integration/rlpx-simulator.spec.ts @@ -66,13 +66,14 @@ describe('RLPx simulator tests', () => { throw new Error(`Peering failed: ${e}: ${e.stack}`) }) await new Promise((resolve) => { - rlpxs[0].events.once('peer:removed', (_, reason: any) => { + 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) @@ -86,19 +87,22 @@ describe('RLPx simulator tests', () => { await new Promise((resolve) => { rlpxs[0].events.on('peer:added', async (peer) => { //@ts-ignore - assert.equal(peer._socket._peername.port, basePort + 1) - assert.equal(rlpxs[0]['_peersQueue'].length, 0, 'peers queue should contain no peers') - const peer2 = { - address: util.localhost, - udpPort: basePort + 2, - tcpPort: basePort + 2, - } - rlpxs[0]['_dpt']!.addPeer(peer2).then((peer) => { + if ((peer['_socket'] as any)._peername.port === basePort + 1) { + assert.equal(rlpxs[0]['_peersQueue'].length, 0, 'peers queue should contain no peers') + const peer2 = { + address: util.localhost, + udpPort: basePort + 2, + tcpPort: basePort + 2, + } + rlpxs[0]['_dpt']!.addPeer(peer2) + await util.delay(500) assert.equal(rlpxs[0]['_peersQueue'].length, 1, 'peers queue should contain one peer') - assert.equal(peer.tcpPort, basePort + 2) + } + 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) - }) + } }) }) }, 10000) From 83422fb279566e977b08c181c9141fdc435c98e2 Mon Sep 17 00:00:00 2001 From: Amir Date: Mon, 28 Oct 2024 21:53:10 -0700 Subject: [PATCH 6/9] Increase test timeout --- .../test/integration/rlpx-simulator.spec.ts | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/devp2p/test/integration/rlpx-simulator.spec.ts b/packages/devp2p/test/integration/rlpx-simulator.spec.ts index 745f9563f6..caa576a77d 100644 --- a/packages/devp2p/test/integration/rlpx-simulator.spec.ts +++ b/packages/devp2p/test/integration/rlpx-simulator.spec.ts @@ -55,30 +55,6 @@ describe('RLPx simulator tests', () => { }) }) }) - 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) - }) - }) it('RLPX: test peer queue / refill connections', async () => { const basePort = 60661 const rlpxs = util.getTestRLPXs(3, 1, basePort) @@ -105,5 +81,29 @@ describe('RLPx simulator tests', () => { } }) }) - }, 10000) + }, 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) + }) + }) }) From 492c062d973dc3d56ce16b58dbbed6bc8a0acc4d Mon Sep 17 00:00:00 2001 From: Amir Date: Wed, 30 Oct 2024 12:42:12 -0700 Subject: [PATCH 7/9] Fix devp2p coverage reporting --- packages/devp2p/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 6980be507623d3f5adc5063cfd965ff13e91eb6f Mon Sep 17 00:00:00 2001 From: Amir Date: Wed, 30 Oct 2024 12:48:05 -0700 Subject: [PATCH 8/9] Fix genesis codecov reporting --- packages/genesis/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 98201ce48787b4dabf94c6a8163d1c7ce589cdb3 Mon Sep 17 00:00:00 2001 From: Amir Date: Wed, 30 Oct 2024 12:59:54 -0700 Subject: [PATCH 9/9] Update wallet codecov reporting command --- packages/wallet/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",