Skip to content

Commit 87f44a2

Browse files
ryanioholgerd77
authored andcommitted
improve test/rpc/helpers code:
* add more type information * rename `node` to `client` (to reflect changes in prior PR) * rename `blockChainStub` to `mockBlockchain`
1 parent 5a86053 commit 87f44a2

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

packages/client/test/rpc/helpers.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import tape from 'tape'
22
import { Server as RPCServer, HttpServer } from 'jayson/promise'
3-
const request = require('supertest')
43
import Common from '@ethereumjs/common'
54
import { RPCManager as Manager } from '../../lib/rpc'
6-
import * as Logger from '../../lib/logging'
7-
import { blockChain } from './blockChainStub'
5+
import { getLogger } from '../../lib/logging'
6+
import { mockBlockchain } from './mockBlockchain'
87
import { Chain } from '../../lib/blockchain/chain'
98
import { RlpxServer } from '../../lib/net/server/rlpxserver'
10-
import Blockchain from '@ethereumjs/blockchain'
119
import { Config } from '../../lib/config'
10+
import type Blockchain from '@ethereumjs/blockchain'
11+
import type EthereumClient from '../../lib/client'
12+
const request = require('supertest')
1213

1314
const config: any = { loglevel: 'error' }
14-
config.logger = Logger.getLogger(config)
15+
config.logger = getLogger(config)
1516

1617
export function startRPC(methods: any, port: number = 3000) {
1718
const server = new RPCServer(methods)
@@ -24,59 +25,63 @@ export function closeRPC(server: HttpServer) {
2425
server.close()
2526
}
2627

27-
export function createManager(node: any) {
28-
return new Manager(node, config)
28+
export function createManager(client: EthereumClient) {
29+
return new Manager(client, config)
2930
}
3031

31-
export function createNode(nodeConfig?: any) {
32-
const common = nodeConfig?.commonChain ?? new Common({ chain: 'mainnet' })
32+
export function createClient(clientOpts: any = {}) {
33+
const common = clientOpts.commonChain ?? new Common({ chain: 'mainnet' })
3334
const config = new Config({ transports: [], common })
34-
const chain = new Chain({
35-
config,
36-
blockchain: (<unknown>blockChain({})) as Blockchain,
37-
})
35+
const blockchain = (<any>mockBlockchain()) as Blockchain
36+
37+
const chain = new Chain({ config, blockchain })
3838
chain.opened = true
39-
const defaultNodeConfig = {
39+
40+
const defaultClientConfig = {
4041
blockchain: chain,
4142
opened: true,
4243
ethProtocolVersions: [63],
4344
}
44-
const trueNodeConfig = { ...defaultNodeConfig, ...nodeConfig }
45+
const clientConfig = { ...defaultClientConfig, ...clientOpts }
46+
4547
const servers = [
4648
new RlpxServer({
4749
config,
4850
bootnodes: '10.0.0.1:1234,10.0.0.2:1234',
4951
}),
5052
]
51-
return {
53+
54+
const client: any = {
5255
services: [
5356
{
5457
name: 'eth',
55-
chain: trueNodeConfig.blockchain,
58+
chain: clientConfig.blockchain,
5659
pool: { peers: [1, 2, 3] },
5760
protocols: [
5861
{
5962
name: 'eth',
60-
versions: trueNodeConfig.ethProtocolVersions,
63+
versions: clientConfig.ethProtocolVersions,
6164
},
6265
],
6366
},
6467
],
6568
servers,
66-
opened: trueNodeConfig.opened,
69+
opened: clientConfig.opened,
6770
server: (name: string) => {
6871
return servers.find((s) => s.name === name)
6972
},
7073
}
74+
75+
return client as EthereumClient
7176
}
7277

7378
export function baseSetup() {
74-
const manager = createManager(createNode())
79+
const manager = createManager(createClient())
7580
const server = startRPC(manager.getMethods())
7681
return server
7782
}
7883

79-
export function params(method: any, params: any[] = []) {
84+
export function params(method: string, params: Array<any> = []) {
8085
const req = {
8186
jsonrpc: '2.0',
8287
method,
@@ -86,14 +91,20 @@ export function params(method: any, params: any[] = []) {
8691
return req
8792
}
8893

89-
export function baseRequest(t: tape.Test, server: any, req: any, expect: any, expectRes: any) {
94+
export function baseRequest(
95+
t: tape.Test,
96+
server: HttpServer,
97+
req: Object,
98+
expect: number,
99+
expectRes: Function
100+
) {
90101
request(server)
91102
.post('/')
92103
.set('Content-Type', 'application/json')
93104
.send(req)
94105
.expect(expect)
95106
.expect(expectRes)
96-
.end((err: any) => {
107+
.end((err?: Error) => {
97108
closeRPC(server)
98109
t.end(err)
99110
})

packages/client/test/rpc/blockChainStub.ts renamed to packages/client/test/rpc/mockBlockchain.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Block } from '@ethereumjs/block'
22
import { bufferToHex } from 'ethereumjs-util'
33

4-
export function blockChain(options: any) {
4+
export function mockBlockchain(options: any = {}) {
55
const txHash = Buffer.from(
66
'0be3065cf288b071ccff922c1c601e2e5628d488b66e781c260ecee36054a2dc',
77
'hex'
@@ -21,10 +21,10 @@ export function blockChain(options: any) {
2121
}),
2222
}
2323
return {
24-
getBlock: async function (_data: any) {
25-
return Promise.resolve(block)
24+
getBlock: async (_data: any) => {
25+
return block
2626
},
27-
getLatestHeader() {
27+
getLatestHeader: () => {
2828
return Block.fromBlockData().header
2929
},
3030
}

0 commit comments

Comments
 (0)