Tracing blocks
The three methods that run a block again and trace each of its transactions. The block is named by number or tag, by hash, or given RLP-encoded.
| Method | JSON-RPC | Returns | Description |
|---|---|---|---|
traceBlockByNumber |
debug_traceBlockByNumber |
a trace per transaction | Traces a block named by number or tag. |
traceBlockByHash |
debug_traceBlockByHash |
a trace per transaction | Traces a block named by hash. |
traceBlock |
debug_traceBlock |
a trace per transaction | Traces an RLP-encoded block. |
Each takes the block, then the same optional options as
Debug.traceTransaction, which select
the tracer. Each returns an
Effect<…, Method.Error, Transport> of one entry per
transaction of the block, in one of two forms:
| Entry | Type | When |
|---|---|---|
{ txHash, result } |
result is the trace of the tracer selected |
the transaction was traced |
{ txHash, error } |
error is a string |
the node could not trace the transaction |
import { Effect } from "effect";import { Debug } from "@ethect/core/rpc";
const gasUsed = Debug.traceBlockByNumber("latest", { tracer: "callTracer" }).pipe( Effect.map((entries) => entries.map((entry) => "result" in entry ? entry.result.gasUsed : `not traced: ${entry.error}`, ), ),);Test for result before reading it: TypeScript then knows which form the entry has.
traceBlockByNumber
Section titled “traceBlockByNumber”Traces every transaction of a block named by number or tag.
import { Effect } from "effect";import { Debug } from "@ethect/core/rpc";
const selectors = Effect.gen(function* () { const entries = yield* Debug.traceBlockByNumber("latest", { tracer: "4byteTracer" }); return entries.map((entry) => ("result" in entry ? Object.keys(entry.result) : [])); // [["0x00000000-28"]]});- JSON-RPC:
debug_traceBlockByNumber - Parameters:
block(BlockId.NumberOrTag): a block number or a tag, not an object that names the block by hash. Thenoptions(optional). - Returns:
ReadonlyArrayof entries. Without options theresultof an entry is aStructLog.Trace.
traceBlockByHash
Section titled “traceBlockByHash”Traces every transaction of a block named by hash.
import { Effect } from "effect";import { Debug, Eth } from "@ethect/core/rpc";
const diffs = Effect.gen(function* () { const block = yield* Eth.getBlockByNumber("latest", false); if (block === null || block.hash === null) return []; return yield* Debug.traceBlockByHash(block.hash, { tracer: "prestateTracer", tracerConfig: { diffMode: true }, });});- JSON-RPC:
debug_traceBlockByHash - Parameters:
blockHash(Hash), thenoptions(optional). - Returns:
ReadonlyArrayof entries. Here theresultof an entry is aPrestate.Diff.
traceBlock
Section titled “traceBlock”Traces every transaction of a block given in its RLP-encoded form, such as the one
Debug.getRawBlock returns.
import { Effect } from "effect";import { Debug } from "@ethect/core/rpc";
const frames = Effect.gen(function* () { const rlp = yield* Debug.getRawBlock("latest"); return yield* Debug.traceBlock(rlp, { tracer: "callTracer" });});- JSON-RPC:
debug_traceBlock - Parameters:
block(Hex.Hex), the encoded block, thenoptions(optional). - Returns:
ReadonlyArrayof entries. Here theresultof an entry is aCallFrame.CallFrame. - Bytes anvil cannot decode as a block fail with an
RpcErrorof reasonInvalidParams.
Behavior
Section titled “Behavior”- The same traces as one by one. The
resultof an entry is whatDebug.traceTransactionreturns for that transaction with the same options. - An empty block gives an empty array.
- Struct logs need a flag on anvil. Without options the tracer is the struct logger, and
anvil has the struct logs of a mined transaction only when started with
anvil --steps-tracing. Without the flag,structLogsis empty in every entry. - A block the node does not have fails with an
RpcErrorof reasonResourceNotFoundon anvil 1.9, by number and by hash. - The
errorform was not seen from anvil. Its decoding was checked against a mock.