Skip to content

Commit

Permalink
Update-ts-sim-test (#120)
Browse files Browse the repository at this point in the history
* move Dockerfile to ts-sim-test/

* move main.ts to ts-sim-test

* update test name in Dockerfile

* update ts-sim-test

* fix spacing
  • Loading branch information
ScottyPoi authored Dec 6, 2023
1 parent 8e8e03b commit 4866b70
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ import {
import { decodeENR } from '../../utils.js'

const client_enr_tag = async (test: Test, client: IClient) => {
const clients: any = {
trin: 't 0.1.1',
fluffy: 'f 0.0.1',
ultralight: 'u 0.0.1',
}

const res = await client.rpc.request('discv5_nodeInfo', [])
const nodeInfo = res.result
const enr = decodeENR(nodeInfo.enr)
if (!nodeInfo.enr || !nodeInfo.nodeId) {
test.fatal(`Expected response not received: ${res.error}`)
}
if (clients[client.kind] !== enr.c.slice(0, 7)) {
test.fatal(`Expected client type ${clients[client.kind]}, got ${enr.c}`)
if (client.kind[0] !== enr.c[0]) {
test.fatal(`Expected client type to begin with: ${client.kind[0]}, got ${enr.c}`)
}
}

Expand All @@ -33,8 +27,15 @@ const two_client_demo = async (test: Test, client_a: IClient, client_b: IClient)
const res2 = await client_b.rpc.request('discv5_nodeInfo', [])
const nodeInfo1 = res1.result
const nodeInfo2 = res2.result
if (!nodeInfo1.enr || !nodeInfo1.nodeId || !nodeInfo2.enr || !nodeInfo2.nodeId) {
test.fatal(`Expected response not received: \n ${res1} \n ${res2}`)
}
const ping1 = await client_a.rpc.request('portal_historyPing', [nodeInfo2.enr])
const ping2 = await client_b.rpc.request('portal_historyPing', [nodeInfo1.enr])
console.log({
ping1,
ping2,
})
if (!ping1.result || !ping2.result) {
test.fatal(`Expected response not received: ${ping1.error} ${ping2.error}`)
}
Expand All @@ -53,34 +54,49 @@ const n_client_demo = async (test: Test, clients: IClient[]) => {
}
}
for await (const [i, client] of clients.entries()) {
const res = await client.rpc.request('portal_historyPing', [
clientsInfo[(i + 1) % clients.length][1].enr,
])
if (!res.result) {
test.fatal(`${clientsInfo[(i + 1) % clients.length][0]}: Expected response not received`)
for await (const [_i, _client] of clients.entries()) {
if (i === _i) {
continue
}
const res = await client.rpc.request('portal_historyPing', [clientsInfo[_i][1].enr])
if (!res.result) {
test.fatal(`ping {${client} --> ${clientsInfo[_i]}}: Expected response not received`)
}
}
}
await new Promise((resolve) => setTimeout(resolve, 1000))
const routingTables = await Promise.all(
clients.map(async (client) => {
const res = await client.rpc.request('portal_historyRoutingTableInfo', [])
return [client.kind, res.result]
}),
)
if (routingTables.some((table) => !table)) {
if (routingTables.some((table) => !table || !table[1].buckets || !table[1].localNodeId)) {
test.fatal(`Expected response not received`)
}
const errors: string[] = []
const passing: string[] = []
for (const [client, table] of routingTables) {
const expected = clientsInfo.map(([_, c]) => [_, c.nodeId]).filter(([k, _]) => k !== client)
const peers = Object.values(table.buckets).flat()
const expected = clientsInfo.filter(([k, _]) => k !== client).map(([_, c]) => c.nodeId)
const peers = Object.values(table.buckets).flat() as string[]
if (peers.length < expected.length) {
errors.push(
`${client}: expected ${JSON.stringify(expected)} peers, got ${JSON.stringify(peers)}`,
`${client}: ${peers.length} peers (fail)\n expected: [${expected.join(
`,\n `,
)}] \n got: [${peers.join(
`,\n `,
)}] \n------\n(${client}): portal_historyRoutingTableInfo => ${JSON.stringify(table, null, 2)}`,
)
} else {
passing.push(
`${client}: ${peers.length} peers (pass)\n${' '.repeat(4)} [${peers
.map((p) => `${p.startsWith('0x') ? `${p.slice(0, 10)}` : `0x${p.slice(0, 8)}`}...`)
.join(', ')}]`,
)
}
}
if (errors.length > 0) {
test.fatal(errors.join('\n'))
test.fatal([...passing, ...errors].join('\n'))
}
}

Expand Down Expand Up @@ -114,7 +130,7 @@ const run_all_client_tests = async (test: Test) => {

const main = async () => {
const suite = new Suite(
'ts-sim-demo',
'ts-sim-test',
'The RPC-compatibility test suite runs a set of RPC related tests against a running node. It tests client implementations of the JSON-RPC API for conformance with the portal network API specification.',
)
suite.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ FROM node:18

RUN npm i -g npm@9.8.1

WORKDIR /ts-sim-demo
WORKDIR /ts-sim-test

COPY ./hivesim-ts/ ./
COPY ./hivesim-ts/src ./src
COPY ./hivesim-ts/package*.json ./
COPY ./hivesim-ts/src/simulators ./src/simulators
COPY ./hivesim-ts/src/simulators/ts-sim-demo ./src/simulators/ts-sim-demo
COPY ./hivesim-ts/src/simulators/ts-sim-test ./src/simulators/ts-sim-test
COPY ./hivesim-ts/tsconfig.json ./
COPY ./hivesim-ts/eslint.cjs ./
COPY ./hivesim-ts/prettier.config.js ./
Expand All @@ -22,5 +22,5 @@ RUN npm run build
COPY . .
RUN pwd
RUN ls -la
CMD node ./dist/simulators/ts-sim-demo/main.js
CMD node ./dist/simulators/ts-sim-test/main.js

0 comments on commit 4866b70

Please sign in to comment.