forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.ts
438 lines (412 loc) · 14.3 KB
/
debug.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import {
TypeOutput,
bigIntToHex,
bytesToHex,
createAddressFromString,
hexToBytes,
toType,
} from '@ethereumjs/util'
import { type VM, encodeReceipt, runTx } from '@ethereumjs/vm'
import { INTERNAL_ERROR, INVALID_PARAMS } from '../error-code.js'
import { callWithStackTrace, getBlockByOption } from '../helpers.js'
import { middleware, validators } from '../validation.js'
import type { Chain } from '../../blockchain/index.js'
import type { EthereumClient } from '../../index.js'
import type { FullEthereumService } from '../../service/index.js'
import type { RPCTx } from '../types.js'
import type { Block } from '@ethereumjs/block'
import type { PrefixedHexString } from '@ethereumjs/util'
export interface tracerOpts {
disableStack?: boolean
disableStorage?: boolean
enableMemory?: boolean
enableReturnData?: boolean
tracer?: string
timeout?: string
tracerConfig?: any
}
export interface structLog {
depth: number
gas: number
gasCost: number
op: string
pc: number
stack: string[] | undefined
memory: string[] | undefined
returnData: string[] | undefined
storage: {
[key: string]: string
}
error: boolean | undefined | null
}
/**
* Validate tracer opts to ensure only supports opts are provided
* @param opts a dictionary of {@link tracerOpts}
* @returns a dictionary of valid {@link tracerOpts}
* @throws if invalid tracer options are provided
*/
const validateTracerConfig = (opts: tracerOpts): tracerOpts => {
if (opts.tracerConfig !== undefined) {
throw {
code: INVALID_PARAMS,
message: 'custom tracers and tracer configurations are not implemented',
}
}
if (opts.tracer !== undefined) {
throw {
code: INVALID_PARAMS,
message: 'custom tracers not implemented',
}
}
if (opts.timeout !== undefined) {
throw {
code: INVALID_PARAMS,
message: 'custom tracer timeouts not implemented',
}
}
if (opts.enableReturnData === true) {
throw {
code: INVALID_PARAMS,
message: 'enabling return data not implemented',
}
}
return {
...{ disableStack: false, disableStorage: false, enableMemory: false, enableReturnData: false },
...opts,
}
}
/**
* debug_* RPC module
* @memberof module:rpc/modules
*/
export class Debug {
private service: FullEthereumService
private chain: Chain
private vm: VM
private _rpcDebug: boolean
/**
* Create debug_* RPC module
* @param client Client to which the module binds
*/
constructor(client: EthereumClient, rpcDebug: boolean) {
this.service = client.service as FullEthereumService
this.chain = this.service.chain
this.vm = (this.service as FullEthereumService).execution?.vm
this._rpcDebug = rpcDebug
this.traceTransaction = middleware(
callWithStackTrace(this.traceTransaction.bind(this), this._rpcDebug),
1,
[[validators.hex]],
)
this.traceCall = middleware(callWithStackTrace(this.traceCall.bind(this), this._rpcDebug), 2, [
[validators.transaction()],
[validators.blockOption],
])
this.storageRangeAt = middleware(
callWithStackTrace(this.storageRangeAt.bind(this), this._rpcDebug),
5,
[
[validators.blockHash],
[validators.unsignedInteger],
[validators.address],
[validators.uint256],
[validators.unsignedInteger],
],
)
this.getRawBlock = middleware(
callWithStackTrace(this.getRawBlock.bind(this), this._rpcDebug),
1,
[[validators.blockOption]],
)
this.getRawHeader = middleware(
callWithStackTrace(this.getRawHeader.bind(this), this._rpcDebug),
1,
[[validators.blockOption]],
)
this.getRawReceipts = middleware(
callWithStackTrace(this.getRawReceipts.bind(this), this._rpcDebug),
1,
[[validators.blockOption]],
)
this.getRawTransaction = middleware(
callWithStackTrace(this.getRawTransaction.bind(this), this._rpcDebug),
1,
[[validators.hex]],
)
}
/**
* Returns a call trace for the requested transaction or null if not available
* @param params an array of two parameters:
* 1. string representing the transaction hash
* 2. an optional tracer options object
*/
async traceTransaction(params: [PrefixedHexString, tracerOpts]) {
const [txHash, config] = params
// Validate configuration and parameters
if (!this.service.execution.receiptsManager) {
throw {
message: 'missing receiptsManager',
code: INTERNAL_ERROR,
}
}
const opts = validateTracerConfig(config)
const result = await this.service.execution.receiptsManager.getReceiptByTxHash(
hexToBytes(txHash),
)
if (!result) return null
const [_, blockHash, txIndex] = result
const block = await this.service.chain.getBlock(blockHash)
const parentBlock = await this.service.chain.getBlock(block.header.parentHash)
const tx = block.transactions[txIndex]
// Copy VM so as to not modify state when running transactions being traced
const vmCopy = await this.service.execution.vm.shallowCopy()
await vmCopy.stateManager.setStateRoot(parentBlock.header.stateRoot)
for (let x = 0; x < txIndex; x++) {
// Run all txns in the block prior to the traced transaction
await runTx(vmCopy, { tx: block.transactions[x], block })
}
const trace = {
gas: '',
returnValue: '',
failed: false,
structLogs: [] as structLog[],
}
vmCopy.evm.events?.on('step', async (step, next) => {
const memory = []
let storage = {}
if (opts.disableStorage === false) {
if (!('dumpStorage' in vmCopy.stateManager)) {
throw {
message: 'stateManager has no dumpStorage implementation',
code: INTERNAL_ERROR,
}
}
storage = await vmCopy.stateManager.dumpStorage!(step.address)
}
if (opts.enableMemory === true) {
for (let x = 0; x < step.memoryWordCount; x++) {
const word = bytesToHex(step.memory.slice(x * 32, 32))
memory.push(word)
}
}
const log = {
pc: step.pc,
op: step.opcode.name,
gasCost: step.opcode.fee + Number(step.opcode.dynamicFee),
gas: Number(step.gasLeft),
depth: step.depth,
error: null,
stack: opts.disableStack !== true ? step.stack.map(bigIntToHex) : undefined,
storage,
memory,
returnData: undefined,
}
trace.structLogs.push(log)
next?.()
})
vmCopy.evm.events?.on('afterMessage', (data, next) => {
if (data.execResult.exceptionError !== undefined && trace.structLogs.length > 0) {
// Mark last opcode trace as error if exception occurs
trace.structLogs[trace.structLogs.length - 1].error = true
}
next?.()
})
const res = await runTx(vmCopy, { tx, block })
trace.gas = bigIntToHex(res.totalGasSpent)
trace.failed = res.execResult.exceptionError !== undefined
trace.returnValue = bytesToHex(res.execResult.returnValue)
return trace
}
/**
* Returns a trace of an eth_call within the context of the given block execution using the final state of the parent block
* @param params an array of 3 parameters:
* 1. an {@link RPCTx} object that mirrors the eth_call parameters object
* 2. A block hash or number formatted as a hex prefixed string
* 3. An optional tracer options object
* @returns an execution trace of an {@link eth_call} in the context of a given block execution
* mirroring the output from {@link traceTransaction}
*/
async traceCall(params: [RPCTx, string, tracerOpts]) {
const [callArgs, blockOpt, tracerOpts] = params
// Validate configuration and parameters
if (!this.service.execution.receiptsManager) {
throw {
message: 'missing receiptsManager',
code: INTERNAL_ERROR,
}
}
if (this.vm === undefined) {
throw new Error('missing vm')
}
const opts = validateTracerConfig(tracerOpts)
const block = await getBlockByOption(blockOpt, this.chain)
const parentBlock = await this.service.chain.getBlock(block.header.parentHash)
const vm = await this.vm.shallowCopy()
await vm.stateManager.setStateRoot(parentBlock.header.stateRoot)
const { from, to, gas: gasLimit, gasPrice, value, data } = callArgs
const trace = {
gas: '',
returnValue: '',
failed: false,
structLogs: [] as structLog[],
}
vm.evm.events?.on('step', async (step, next) => {
const memory = []
let storage = {}
if (opts.disableStorage === false) {
if (!('dumpStorage' in vm.stateManager)) {
throw {
message: 'stateManager has no dumpStorage implementation',
code: INTERNAL_ERROR,
}
}
storage = await vm.stateManager.dumpStorage!(step.address)
}
if (opts.enableMemory === true) {
for (let x = 0; x < step.memoryWordCount; x++) {
const word = bytesToHex(step.memory.slice(x * 32, 32))
memory.push(word)
}
}
const log = {
pc: step.pc,
op: step.opcode.name,
gasCost: step.opcode.fee + Number(step.opcode.dynamicFee),
gas: Number(step.gasLeft),
depth: step.depth,
error: null,
stack: opts.disableStack !== true ? step.stack.map(bigIntToHex) : undefined,
storage,
memory,
returnData: undefined,
}
trace.structLogs.push(log)
next?.()
})
vm.evm.events?.on('afterMessage', (data, next) => {
if (data.execResult.exceptionError !== undefined && trace.structLogs.length > 0) {
// Mark last opcode trace as error if exception occurs
trace.structLogs[trace.structLogs.length - 1].error = true
}
next?.()
})
const runCallOpts = {
caller: from !== undefined ? createAddressFromString(from) : undefined,
to: to !== undefined ? createAddressFromString(to) : undefined,
gasLimit: toType(gasLimit, TypeOutput.BigInt),
gasPrice: toType(gasPrice, TypeOutput.BigInt),
value: toType(value, TypeOutput.BigInt),
data: data !== undefined ? hexToBytes(data) : undefined,
}
const res = await vm.evm.runCall(runCallOpts)
trace.gas = bigIntToHex(res.execResult.executionGasUsed)
trace.failed = res.execResult.exceptionError !== undefined
trace.returnValue = bytesToHex(res.execResult.returnValue)
return trace
}
/**
* Returns a limited set of storage keys belonging to an account.
* @param params An array of 5 parameters:
* 1. The hash of the block at which to get storage from the state.
* 2. The transaction index of the requested block post which to get the storage.
* 3. The address of the account.
* 4. The starting (hashed) key from which storage will be returned. To include the entire range, pass '0x00'.
* 5. The maximum number of storage values that could be returned.
* @returns A {@link StorageRange} object that will contain at most `limit` entries in its `storage` field.
* The object will also contain `nextKey`, the next (hashed) storage key after the range included in `storage`.
*/
async storageRangeAt(
params: [PrefixedHexString, number, PrefixedHexString, PrefixedHexString, number],
) {
const [blockHash, txIndex, account, startKey, limit] = params
if (this.vm === undefined) {
throw new Error('Missing VM.')
}
let block: Block
try {
// Validator already verified that `blockHash` is properly formatted.
block = await this.chain.getBlock(hexToBytes(blockHash))
} catch (err: any) {
throw {
code: INTERNAL_ERROR,
message: 'Could not get requested block hash.',
}
}
if (txIndex >= block.transactions.length) {
throw {
code: INTERNAL_ERROR,
message: 'txIndex cannot be larger than the number of transactions in the block.',
}
}
const parentBlock = await this.chain.getBlock(block.header.parentHash)
// Copy the VM and run transactions including the relevant transaction.
const vmCopy = await this.vm.shallowCopy()
if (!('dumpStorageRange' in vmCopy.stateManager)) {
throw {
code: INTERNAL_ERROR,
message: 'stateManager has no dumpStorageRange implementation',
}
}
await vmCopy.stateManager.setStateRoot(parentBlock.header.stateRoot)
for (let i = 0; i <= txIndex; i++) {
await runTx(vmCopy, { tx: block.transactions[i], block })
}
// await here so that any error can be handled in the catch below for proper response
return vmCopy.stateManager.dumpStorageRange!(
// Validator already verified that `account` and `startKey` are properly formatted.
createAddressFromString(account),
BigInt(startKey),
limit,
)
}
/**
* Returns an RLP-encoded block
* @param blockOpt Block number or tag
*/
async getRawBlock(params: [string]) {
const [blockOpt] = params
const block = await getBlockByOption(blockOpt, this.chain)
return bytesToHex(block.serialize())
}
/**
* Returns an RLP-encoded block header
* @param blockOpt Block number or tag
* @returns
*/
async getRawHeader(params: [string]) {
const [blockOpt] = params
const block = await getBlockByOption(blockOpt, this.chain)
return bytesToHex(block.header.serialize())
}
/**
* Returns an array of EIP-2718 binary-encoded receipts
* @param blockOpt Block number or tag
*/
async getRawReceipts(params: [string]) {
const [blockOpt] = params
if (!this.service.execution.receiptsManager) throw new Error('missing receiptsManager')
const block = await getBlockByOption(blockOpt, this.chain)
const receipts = await this.service.execution.receiptsManager.getReceipts(
block.hash(),
true,
true,
)
return receipts.map((r) => bytesToHex(encodeReceipt(r, r.txType)))
}
/**
* Returns the bytes of the transaction.
* @param blockOpt Block number or tag
*/
async getRawTransaction(params: [PrefixedHexString]) {
const [txHash] = params
if (!this.service.execution.receiptsManager) throw new Error('missing receiptsManager')
const result = await this.service.execution.receiptsManager.getReceiptByTxHash(
hexToBytes(txHash),
)
if (!result) return null
const [_receipt, blockHash, txIndex] = result
const block = await this.chain.getBlock(blockHash)
const tx = block.transactions[txIndex]
return bytesToHex(tx.serialize())
}
}