-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathrpc.spec.ts
More file actions
112 lines (107 loc) · 3.73 KB
/
rpc.spec.ts
File metadata and controls
112 lines (107 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { bytesToHex } from '@ethereumjs/util'
import { MemoryLevel } from 'memory-level'
import { assert, describe, it } from 'vitest'
import { EthereumClient } from '../../src/client.ts'
import { Config } from '../../src/config.ts'
import { METHOD_NOT_FOUND } from '../../src/rpc/error-code.ts'
import { RPCManager } from '../../src/rpc/index.ts'
import {
MethodConfig,
createRPCServer,
createRPCServerListener,
createWsRPCServerListener,
} from '../../src/util/rpc.ts'
import { getRPCClient, setupChain } from '../rpc/helpers.ts'
import { powData } from '../testdata/geth-genesis/pow.ts'
describe('[Util/RPC]', () => {
it('should return enabled RPC servers', async () => {
const config = new Config({ accountCache: 10000, storageCache: 1000 })
const client = await EthereumClient.create({ config, metaDB: new MemoryLevel() })
const manager = new RPCManager(client, config)
const { logger } = config
for (const methodConfig of Object.values(MethodConfig)) {
for (const rpcDebug of ['', 'eth']) {
const { server } = createRPCServer(manager, {
methodConfig,
rpcDebug,
logger,
rpcDebugVerbose: '',
})
const httpServer = createRPCServerListener({
server,
withEngineMiddleware: { jwtSecret: new Uint8Array(32) },
maxPayload: '15mb',
})
const wsServer = createWsRPCServerListener({
server,
withEngineMiddleware: { jwtSecret: new Uint8Array(32) },
})
const req = { id: 1, method: 'eth_getCanonicalHeadBlock', params: [] }
const resp = {
id: 1,
result: { test: bytesToHex(new Uint8Array(64).fill(1)) },
}
const reqBulk = [req, req]
const respBulk = [resp, { id: 2, error: { err0: '456' } }]
// Valid
server.emit('request', req)
server.emit('response', req, resp)
server.emit('response', reqBulk, respBulk)
// Invalid
server.emit('response', req, []) // empty
server.emit('response', [req], respBulk) // mismatch length
assert.isTrue(
httpServer !== undefined && wsServer !== undefined,
'should return http and ws servers',
)
}
}
})
it('should not throw if rpcDebugVerbose string is undefined', async () => {
const config = new Config({ accountCache: 10000, storageCache: 1000 })
const client = await EthereumClient.create({ config, metaDB: new MemoryLevel() })
const manager = new RPCManager(client, config)
const { logger } = config
const methodConfig = Object.values(MethodConfig)[0]
const { server } = createRPCServer(manager, {
methodConfig,
rpcDebug: 'eth',
logger,
rpcDebugVerbose: undefined as any,
})
const httpServer = createRPCServerListener({
server,
withEngineMiddleware: { jwtSecret: new Uint8Array(32) },
maxPayload: '15mb',
})
const wsServer = createWsRPCServerListener({
server,
withEngineMiddleware: { jwtSecret: new Uint8Array(32) },
})
assert.isTrue(
httpServer !== undefined && wsServer !== undefined,
'should return http and ws servers',
)
})
})
describe('[Util/RPC/Engine eth methods]', async () => {
const { server } = await setupChain(powData, 'pow')
const rpc = getRPCClient(server)
const methods = [
'eth_blockNumber',
'eth_call',
'eth_chainId',
'eth_getCode',
'eth_getBlockByHash',
'eth_getBlockByNumber',
'eth_getLogs',
'eth_sendRawTransaction',
'eth_syncing',
]
for (const method of methods) {
it(`should have method ${method}`, async () => {
const res = await rpc.request(method, [])
assert.notEqual(res.error?.code, METHOD_NOT_FOUND, `should have ${method}`)
})
}
})