# 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`](#traceblockbynumber) | `debug_traceBlockByNumber` | a trace per transaction | Traces a block named by number or tag. |
| [`traceBlockByHash`](#traceblockbyhash) | `debug_traceBlockByHash` | a trace per transaction | Traces a block named by hash. |
| [`traceBlock`](#traceblock) | `debug_traceBlock` | a trace per transaction | Traces an RLP-encoded block. |

Each takes the block, then the same optional `options` as
[`Debug.traceTransaction`](https://ethect.dev/rpc/debug/trace-transaction/#options-optional), which select
the [tracer](https://ethect.dev/rpc/debug/tracers/). Each returns an
[`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method) 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 |

```ts
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

Traces every transaction of a block named by number or tag.

```ts
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`](https://ethect.dev/rpc/types/block-id/)): 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`](https://ethect.dev/rpc/types/struct-log/).

## traceBlockByHash

Traces every transaction of a block named by hash.

```ts
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`](https://ethect.dev/reference/primitives/hash/)), then `options`
  (optional).
- **Returns:** `ReadonlyArray` of entries. Here the `result` of an entry is a
  [`Prestate.Diff`](https://ethect.dev/rpc/types/prestate/).

## traceBlock

Traces every transaction of a block given in its RLP-encoded form, such as the one
[`Debug.getRawBlock`](https://ethect.dev/rpc/debug/raw/#getrawblock) returns.

```ts
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`](https://ethect.dev/reference/primitives/hex/)), the encoded block, then
  `options` (optional).
- **Returns:** `ReadonlyArray` of entries. Here the `result` of an entry is a
  [`CallFrame.CallFrame`](https://ethect.dev/rpc/types/call-frame/).
- Bytes anvil cannot decode as a block fail with an `RpcError` of reason `InvalidParams`.

## Behavior

- **The same traces as one by one.** The `result` of an entry is what
  [`Debug.traceTransaction`](https://ethect.dev/rpc/debug/trace-transaction/) 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](https://ethect.dev/reference/testing/mock-provider/).
