Skip to content

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.

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. Then options (optional).
  • Returns: ReadonlyArray of entries. Without options the result of an entry is a StructLog.Trace.

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), then options (optional).
  • Returns: ReadonlyArray of entries. Here the result of an entry is a Prestate.Diff.

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, then options (optional).
  • Returns: ReadonlyArray of entries. Here the result of an entry is a CallFrame.CallFrame.
  • Bytes anvil cannot decode as a block fail with an RpcError of reason InvalidParams.
  • The same traces as one by one. The result of an entry is what Debug.traceTransaction returns 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, structLogs is empty in every entry.
  • A block the node does not have fails with an RpcError of reason ResourceNotFound on anvil 1.9, by number and by hash.
  • The error form was not seen from anvil. Its decoding was checked against a mock.