-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathrpc.spec.ts
More file actions
202 lines (187 loc) · 6.17 KB
/
rpc.spec.ts
File metadata and controls
202 lines (187 loc) · 6.17 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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: Config.RPC_ETH_MAXPAYLOAD_DEFAULT,
})
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: Config.RPC_ENGINE_MAXPAYLOAD_DEFAULT,
})
const wsServer = createWsRPCServerListener({
server,
withEngineMiddleware: { jwtSecret: new Uint8Array(32) },
})
assert.isTrue(
httpServer !== undefined && wsServer !== undefined,
'should return http and ws servers',
)
})
it('should reject oversized RPC payloads', async () => {
const config = new Config({
accountCache: 10000,
storageCache: 1000,
rpcEthMaxPayload: '1kb',
rpcEngineMaxPayload: '10mb',
})
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 ethHttpServer = createRPCServerListener({
server,
withEngineMiddleware: undefined,
maxPayload: config.rpcEthMaxPayload,
})
const engineHttpServer = createRPCServerListener({
server,
withEngineMiddleware: undefined,
maxPayload: config.rpcEngineMaxPayload,
})
const ethPort = 8545
const enginePort = 8551
ethHttpServer.listen(ethPort)
engineHttpServer.listen(enginePort)
const oversizedEthPayload = JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'eth_getBlockByNumber',
params: ['latest', true],
data: 'eth'.repeat(2500),
})
const oversizedEnginePayload = JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'engine_newPayloadV2',
params: [
{
baseFeePerGas: '0x',
blockHash: '0x',
blockNumber: '0x',
extraData: '0x'.repeat(2500),
feeRecipient: '0x',
gasLimit: '0x',
gasUsed: '0x',
logsBloom: '0x',
parentHash: '0x',
prevRandao: '0x',
receiptsRoot: '0x',
stateRoot: '0x',
timestamp: '0x',
transactions: [],
},
],
})
const resEth = await fetch(`http://localhost:${ethPort}`, {
method: 'POST',
body: oversizedEthPayload,
headers: { 'Content-Type': 'application/json' },
})
const resEngine = await fetch(`http://localhost:${enginePort}`, {
method: 'POST',
body: oversizedEnginePayload,
headers: {
'Content-Type': 'application/json',
},
})
assert.strictEqual(
resEth.status,
413,
'ETH server should reject oversized payload with 413 status',
)
assert.strictEqual(resEngine.status, 200, 'ENGINE server should accept oversized payload')
})
})
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}`)
})
}
})