# The debug_ methods (Debug)

The `debug_` namespace: blocks, transactions and receipts in their encoded form, and executions
traced with geth's tracers.

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Debug, Eth } from "@ethect/core/rpc";

const program = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  const receipt = yield* Eth.sendTransactionSync({ from, to, value: 1n });
  const frame = yield* Debug.traceTransaction(receipt.transactionHash, {
    tracer: "callTracer",
  });
  return { type: frame.type, value: frame.value, gasUsed: frame.gasUsed };
});

const traced = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// { type: "CALL", value: 1n, gasUsed: 21000n }
```

A method takes its JSON-RPC params in order and returns an
[`Effect`](https://ethect.dev/rpc/method/#calling-a-method) of the decoded result. The name is the wire
name without its prefix: `debug_traceCall` is `Debug.traceCall`.

The namespace comes from geth. anvil 1.9 serves every method on these pages except
`getBadBlocks` and `getRawBlockAccessList`, so a local test node is where to try them: see
[Testing](https://ethect.dev/guides/testing/).

## Raw data

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getRawHeader`](https://ethect.dev/rpc/debug/raw/#getrawheader) | `debug_getRawHeader` | `Hex` | A block header, RLP-encoded. |
| [`getRawBlock`](https://ethect.dev/rpc/debug/raw/#getrawblock) | `debug_getRawBlock` | `Hex` | A block, RLP-encoded. |
| [`getRawTransaction`](https://ethect.dev/rpc/debug/raw/#getrawtransaction) | `debug_getRawTransaction` | `Hex \| null` | A transaction in its signed, encoded form, by hash. |
| [`getRawTransactions`](https://ethect.dev/rpc/debug/raw/#getrawtransactions) | `debug_getRawTransactions` | `Hex[]` | The transactions of a block, each in its signed, encoded form. |
| [`getRawReceipts`](https://ethect.dev/rpc/debug/raw/#getrawreceipts) | `debug_getRawReceipts` | `Hex[]` | The receipts of a block, each in its encoded form. |
| [`getRawBlockAccessList`](https://ethect.dev/rpc/debug/raw/#getrawblockaccesslist) | `debug_getRawBlockAccessList` | `Hex` | What a block read and wrote, RLP-encoded (EIP-7928). |
| [`getBadBlocks`](https://ethect.dev/rpc/debug/raw/#getbadblocks) | `debug_getBadBlocks` | bad blocks | The invalid blocks the node saw on the network lately. |

## Tracing

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`traceTransaction`](https://ethect.dev/rpc/debug/trace-transaction/) | `debug_traceTransaction` | a trace | Runs a mined transaction again and traces it. |
| [`traceCall`](https://ethect.dev/rpc/debug/trace-call/) | `debug_traceCall` | a trace | Runs a call without sending a transaction and traces it. |
| [`traceBlockByNumber`](https://ethect.dev/rpc/debug/trace-block/#traceblockbynumber) | `debug_traceBlockByNumber` | a trace per transaction | Runs a block again and traces each of its transactions, by block number or tag. |
| [`traceBlockByHash`](https://ethect.dev/rpc/debug/trace-block/#traceblockbyhash) | `debug_traceBlockByHash` | a trace per transaction | The same, by block hash. |
| [`traceBlock`](https://ethect.dev/rpc/debug/trace-block/#traceblock) | `debug_traceBlock` | a trace per transaction | The same, for an RLP-encoded block. |

## Tracers

The last argument of a tracing method is its options. They select the
[tracer](https://ethect.dev/rpc/debug/tracers/), and the tracer decides the type of the trace.

| Options | Tracer | Trace |
| --- | --- | --- |
| no `tracer` | [the struct logger](https://ethect.dev/rpc/debug/tracers/#struct-logger) | [`StructLog.Trace`](https://ethect.dev/rpc/types/struct-log/): one log per opcode |
| `tracer: "callTracer"` | [`callTracer`](https://ethect.dev/rpc/debug/tracers/#calltracer) | [`CallFrame.CallFrame`](https://ethect.dev/rpc/types/call-frame/): the tree of calls |
| `tracer: "prestateTracer"` | [`prestateTracer`](https://ethect.dev/rpc/debug/tracers/#prestatetracer) | [`Prestate.Prestate`](https://ethect.dev/rpc/types/prestate/): the accounts touched, as they were before |
| the same with `tracerConfig: { diffMode: true }` | [`prestateTracer`, diff mode](https://ethect.dev/rpc/debug/tracers/#prestatetracer) | [`Prestate.Diff`](https://ethect.dev/rpc/types/prestate/): the accounts changed, before and after |
| `tracer: "4byteTracer"` | [`4byteTracer`](https://ethect.dev/rpc/debug/tracers/#4bytetracer) | [`Trace.FourByte`](https://ethect.dev/rpc/types/trace/): calls per function selector |
| `tracer: "noopTracer"` | [`noopTracer`](https://ethect.dev/rpc/debug/tracers/#nooptracer) | an empty object |
| any other `tracer` | [a custom tracer](https://ethect.dev/rpc/debug/tracers/#custom-tracers) | `unknown` |

```ts
import { Debug } from "@ethect/core/rpc";
import type { Hash } from "@ethect/core";

const traces = (hash: Hash.Hash) => ({
  // Effect<StructLog.Trace, …>
  opcodes: Debug.traceTransaction(hash),
  // Effect<CallFrame.CallFrame, …>
  calls: Debug.traceTransaction(hash, { tracer: "callTracer" }),
  // Effect<Prestate.Prestate, …>
  state: Debug.traceTransaction(hash, { tracer: "prestateTracer" }),
});
```

The five tracing methods take the same options and are typed the same way. The methods that
trace a block return one entry per transaction, each holding the trace of the tracer.
