Skip to content

Commit

Permalink
Rework rlpx-simulator.spec.ts tests / Fix devp2p/genesis/wallet Cover…
Browse files Browse the repository at this point in the history
…age Reporting (#3760)

* Rework rlpx-simulator.spec.ts tests

* Fix twoPeerMsgExchange3

* Fix test

* Fix type errors

* Restore tests to be more similar to previous format

* Increase test timeout

* Fix devp2p coverage reporting

* Fix genesis codecov reporting

* Update wallet codecov reporting command

---------

Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com>
  • Loading branch information
scorbajio and holgerd77 authored Oct 31, 2024
1 parent dc7169c commit 987a855
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 68 deletions.
2 changes: 1 addition & 1 deletion packages/devp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
128 changes: 69 additions & 59 deletions packages/devp2p/test/integration/rlpx-simulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
})
})
})
20 changes: 14 additions & 6 deletions packages/devp2p/test/integration/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/genesis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 987a855

Please sign in to comment.