# Trace

The tracers a `Debug.trace*` call can run, as data: the options that select each one, the
schema of what it returns, and the shape of a traced block.

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

const busiest = (hash: Hash.Hash) =>
  Debug.traceTransaction(hash, { tracer: "4byteTracer" }).pipe(
    // counts: Trace.FourByte
    Effect.map((counts) => Object.entries(counts).sort(([, a], [, b]) => b - a)[0]),
    // ["0x00000000-28", 1]
  );
```

The page that explains the tracers and how the options select one is
[Tracers](https://ethect.dev/rpc/debug/tracers/). This module is what the `Debug.trace*` methods are built
from.

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `Trace.fourByteSchema` | `Trace.FourByte` | the result of the `4byteTracer`: a count per selector | a `Debug.trace*` call with `tracer: "4byteTracer"` |
| `Trace.blockSchema(result)` | an array of entries | what tracing a block returns, for a tracer whose result has the schema `result` | [the methods that trace a block](https://ethect.dev/rpc/debug/trace-block/) |
| `Trace.tracers` | a tuple of `Trace.Tracer` | every tracer the library defines, in the order a call is matched against them | the five `Debug.trace*` methods |

`Trace.Tracer` is an interface, not a schema: the description of one tracer.

## FourByte

`Trace.FourByte` is `Record<string, number>`: how many calls each function selector got.

| Part | Type | Notes |
| --- | --- | --- |
| key | `string` | the selector, a dash, then the size in bytes of the calldata after it: `"0x00000000-28"` |
| value | `number` | how many calls had that selector and that size |

## blockSchema

`Trace.blockSchema(result)` builds the schema of a traced block from the schema of one trace: an
array with one entry per transaction.

| Entry | Type | When |
| --- | --- | --- |
| `{ txHash, result }` | `txHash` is a `Hash`, `result` is the value the given schema decodes | the transaction was traced |
| `{ txHash, error }` | `txHash` is a `Hash`, `error` is a `string` | the node could not trace it |

Use it to give a type to a tracer of your own when it runs on a block.

```ts
import { Schema } from "effect";
import { BlockId, Method, Trace } from "@ethect/core/rpc";

// a JavaScript tracer that collects the name of every opcode executed
const tracer = [
  "{ ops: [],",
  "step(log) { this.ops.push(log.op.toString()) },",
  "fault() {},",
  "result() { return this.ops } }",
].join(" ");

// per transaction: { txHash, result: ReadonlyArray<string> } or { txHash, error }
const opcodesOfBlock = Method.make<[block: unknown, options: unknown]>()(
  "debug_traceBlockByNumber",
  [BlockId.numberOrTagSchema, Schema.Struct({ tracer: Schema.Literal(tracer) })],
  Trace.blockSchema(Schema.Array(Schema.String)),
);

const latest = opcodesOfBlock("latest", { tracer });
```

See [Custom methods](https://ethect.dev/rpc/guides/custom-methods/).

## Tracer

`Trace.Tracer` describes one tracer with three schemas.

| Field | Type | Holds |
| --- | --- | --- |
| `options` | a record of field schemas | the fields of the options that select and configure the tracer |
| `execution` | a schema | what the tracer returns for one execution |
| `block` | a schema | what it returns for a block: `Trace.blockSchema` of the one above |

## tracers

`Trace.tracers` is the list of every tracer, in the order a call is matched against them.

| Position | Tracer | `execution` decodes to |
| --- | --- | --- |
| `0` | the struct logger: options without a `tracer` key | [`StructLog.Trace`](https://ethect.dev/rpc/types/struct-log/) |
| `1` | `callTracer` | [`CallFrame.CallFrame`](https://ethect.dev/rpc/types/call-frame/) |
| `2` | `prestateTracer` in diff mode | [`Prestate.Diff`](https://ethect.dev/rpc/types/prestate/) |
| `3` | `prestateTracer` | [`Prestate.Prestate`](https://ethect.dev/rpc/types/prestate/) |
| `4` | `4byteTracer` | `Trace.FourByte` |
| `5` | `noopTracer` | an empty object |
| `6` | any other `tracer` string | `unknown` |

```ts
import { Debug, Trace } from "@ethect/core/rpc";

Trace.tracers.length; // 7
Trace.tracers[1].execution; // the schema of a CallFrame
Debug.traceTransaction.variants.length; // 7: one definition per tracer
```

## Behavior

- **The order is the matching order.** A call goes to the first tracer whose options accept the
  ones given. Diff mode is before plain `prestateTracer` because its config is the one that
  has to ask for it, and the custom tracer is last because it accepts any name.
- **Every tracer takes `timeout` and `reexec`.** The two fields are part of the `options` of
  each entry.
- **The struct logger refuses a `tracer` key.** Its options declare `tracer` as a field that
  can hold no value, so options that name a tracer never match it.
- **The list is fixed.** Adding a tracer of your own is done with a
  [custom method](https://ethect.dev/rpc/guides/custom-methods/), as above, not by changing `Trace.tracers`.
