Tracers
The tracers a Debug.trace* call can run. The options of the call select the tracer, and with
it the type of the result.
import { Debug } from "@ethect/core/rpc";import type { Hash } from "@ethect/core";
const traces = (hash: Hash.Hash) => ({ // no `tracer` key, the struct logger: Effect<StructLog.Trace, …> opcodes: Debug.traceTransaction(hash), // a `tracer` key changes the tracer and the type: Effect<CallFrame.CallFrame, …> calls: Debug.traceTransaction(hash, { tracer: "callTracer" }),});The same options go to Debug.traceTransaction,
Debug.traceCall and the three methods that
trace a block.
How the options select a tracer
Section titled “How the options select a tracer”| Options | Tracer | Result type |
|---|---|---|
no tracer key |
struct logger | StructLog.Trace |
tracer: "callTracer" |
callTracer |
CallFrame.CallFrame |
tracer: "prestateTracer" |
prestateTracer |
Prestate.Prestate |
tracer: "prestateTracer", tracerConfig: { diffMode: true } |
prestateTracer, diff mode |
Prestate.Diff |
tracer: "4byteTracer" |
4byteTracer |
Trace.FourByte |
tracer: "noopTracer" |
noopTracer |
an empty object |
tracer: any other string |
custom tracer | unknown |
- A name the library knows gives a typed result. The answer of the node is decoded with the schema of that tracer.
- Any other name, or a JavaScript tracer, gives
unknown. The library cannot know what the tracer returns, so the answer is handed over as the node sent it.
Every tracer also takes two fields at the top level of the options:
| Field | Type | Description |
|---|---|---|
timeout |
string (optional) |
how long the trace may take, as a Go duration such as "10s" |
reexec |
number (optional) |
how many blocks the node may run again to rebuild a state it no longer has |
The answers shown below are what anvil 1.9 sent for one transaction: a call to a contract that stores the 32-byte word it is given, emits one log with it and returns it.
Struct logger
Section titled “Struct logger”The tracer that runs when the options name none. It logs every opcode executed.
- Selected by: options without a
tracerkey, or no options at all. - Config: the fields of
StructLog.Config, at the top level of the options:enableMemory,disableStack,disableStorage,enableReturnData,debug,limit. - Result:
StructLog.Trace.
import { Debug } from "@ethect/core/rpc";import type { Hash } from "@ethect/core";
// Effect<StructLog.Trace, …>const firstSteps = (hash: Hash.Hash) => Debug.traceTransaction(hash, { limit: 2 });{ "failed": false, "gas": 44293, "returnValue": "0x0000000000000000000000000000000000000000000000000000000000000007", "structLogs": [ { "pc": 0, "op": "CALLDATASIZE", "gas": 23153, "gasCost": 2, "depth": 1, "stack": [], "refund": 0 }, { "pc": 1, "op": "PUSH1", "gas": 23151, "gasCost": 3, "depth": 1, "stack": [ "0x20" ], "refund": 0 } ]}The stack values are decoded to bigint: "0x20" is read as 32n. For a mined transaction,
anvil only has the logs when it is started with anvil --steps-tracing.
callTracer
Section titled “callTracer”Returns the execution as the tree of the calls it made.
- Selected by:
tracer: "callTracer". - Config:
tracerConfig(optional), aCallFrame.Config:onlyTopCallleaves out the calls the top one made,withLogadds the logs each call emitted. - Result:
CallFrame.CallFrame.
import { Debug } from "@ethect/core/rpc";import type { Hash } from "@ethect/core";
// Effect<CallFrame.CallFrame, …>const calls = (hash: Hash.Hash) => Debug.traceTransaction(hash, { tracer: "callTracer", tracerConfig: { withLog: true }, });{ "from": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", "gas": "0x5a71", "gasUsed": "0xad05", "to": "0x5fbdb2315678afecb367f032d93f642f64180aa3", "input": "0x0000000000000000000000000000000000000000000000000000000000000007", "output": "0x0000000000000000000000000000000000000000000000000000000000000007", "logs": [ { "address": "0x5fbdb2315678afecb367f032d93f642f64180aa3", "topics": [ "0xabababababababababababababababababababababababababababababababab" ], "data": "0x0000000000000000000000000000000000000000000000000000000000000007", "position": "0x0", "index": "0x0" } ], "value": "0x0", "type": "CALL"}The quantities are decoded: gasUsed is 44293n, value is 0n, and the position and
index of a log are 0. This contract calls no other, so the frame has no calls.
prestateTracer
Section titled “prestateTracer”Returns the accounts the execution touched, as they were before it.
- Selected by:
tracer: "prestateTracer". - Config:
tracerConfig(optional), aPrestate.Config:disableCodeanddisableStorageleave the code and the storage out of every account. - Result:
Prestate.Prestate, a record of addresses to account states.
import { Debug } from "@ethect/core/rpc";import type { Hash } from "@ethect/core";
// Effect<Prestate.Prestate, …>const before = (hash: Hash.Hash) => Debug.traceTransaction(hash, { tracer: "prestateTracer" });{ "0x0000000000000000000000000000000000000000": { "balance": "0x3d23cea3f000" }, "0x5fbdb2315678afecb367f032d93f642f64180aa3": { "balance": "0x0", "code": "0x3660105763deadbeef5f526004601cfd5b5f35805f555f527fabababababababababababababababababababababababababababababababab60205fa160205ff3", "nonce": 1, "storage": { "0x0000000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000000" } }, "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266": { "balance": "0x21e19e04f7314f82000", "nonce": 1 }}The three accounts are the fee recipient, the contract and the sender. The contract’s storage holds the one slot the transaction touched, with the value it had before.
Diff mode
Section titled “Diff mode”With diffMode: true the tracer returns what the execution changed.
- Selected by:
tracer: "prestateTracer"andtracerConfig: { diffMode: true }. - Config: a
Prestate.DiffConfig:diffMode: true, withdisableCodeanddisableStorageas above. - Result:
Prestate.Diff: the accounts as they were inpre, and what became of them inpost.
import { Debug } from "@ethect/core/rpc";import type { Hash } from "@ethect/core";
// Effect<Prestate.Diff, …>const changes = (hash: Hash.Hash) => Debug.traceTransaction(hash, { tracer: "prestateTracer", tracerConfig: { diffMode: true }, });{ "post": { "0x0000000000000000000000000000000000000000": { "balance": "0x656c932be200" }, "0x5fbdb2315678afecb367f032d93f642f64180aa3": { "storage": { "0x0000000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000007" } }, "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266": { "balance": "0x21e19e003e4dd81d4d8", "nonce": 2 } }, "pre": { "0x0000000000000000000000000000000000000000": { "balance": "0x3d23cea3f000" }, "0x5fbdb2315678afecb367f032d93f642f64180aa3": { "balance": "0x0", "code": "0x3660105763deadbeef5f526004601cfd5b5f35805f555f527fabababababababababababababababababababababababababababababababab60205fa160205ff3", "nonce": 1 }, "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266": { "balance": "0x21e19e04f7314f82000", "nonce": 1 } }}post holds only the fields that changed: the slot the contract wrote, the nonce and the
balance of the sender.
4byteTracer
Section titled “4byteTracer”Counts the calls each function selector got.
- Selected by:
tracer: "4byteTracer". - Config: none.
- Result:
Trace.FourByte, a record of counts. A key is the selector, a dash, then the size of the calldata that followed it, in bytes.
import { Debug } from "@ethect/core/rpc";import type { Hash } from "@ethect/core";
// Effect<Trace.FourByte, …>const selectors = (hash: Hash.Hash) => Debug.traceTransaction(hash, { tracer: "4byteTracer" });{ "0x00000000-28": 1}The transaction called the contract once, with 32 bytes of calldata: 4 read as the selector and
28 after it. anvil 1.9 runs this tracer for a mined transaction and for a block, and refuses it
for Debug.traceCall.
noopTracer
Section titled “noopTracer”Runs the execution and returns nothing about it.
- Selected by:
tracer: "noopTracer". - Config: none.
- Result: an empty object, typed as a record without values.
import { Debug } from "@ethect/core/rpc";import type { Hash } from "@ethect/core";
// Effect<{ readonly [name: string]: never }, …>const nothing = (hash: Hash.Hash) => Debug.traceTransaction(hash, { tracer: "noopTracer" });{}Custom tracers
Section titled “Custom tracers”Any other tracer string is sent to the node as it is: the name of a tracer the library has no
definition for, or the source of a JavaScript tracer.
- Selected by: a
tracerthat is none of the four names above. - Config:
tracerConfig(optional), any value. - Result:
unknown. Decode it with a schema of what your tracer returns.
import { Effect, Schema } from "effect";import { Debug } from "@ethect/core/rpc";import type { Hash } from "@ethect/core";
// a JavaScript tracer that collects the name of every opcode executedconst tracer = [ "{ ops: [],", "step(log) { this.ops.push(log.op.toString()) },", "fault() {},", "result() { return this.ops } }",].join(" ");
const opcodes = (hash: Hash.Hash) => // Effect<unknown, …> Debug.traceTransaction(hash, { tracer }).pipe( // Effect<ReadonlyArray<string>, …> Effect.flatMap(Schema.decodeUnknownEffect(Schema.Array(Schema.String))), );[ "CALLDATASIZE", "PUSH1", "JUMPI", "JUMPDEST", "PUSH0", "CALLDATALOAD", "DUP1", "PUSH0", "SSTORE", "PUSH0", "MSTORE", "PUSH32", "PUSH1", "PUSH0", "LOG1", "PUSH1", "PUSH0", "RETURN"]anvil 1.9 runs JavaScript tracers. It also reads a name it does not know as JavaScript, so a
misspelled name fails with an RpcError of reason InternalError.
geth’s other tracers, such as muxTracer and flatCallTracer, have no definition in the
library: they are custom tracers here.
Behavior
Section titled “Behavior”- One definition per tracer. Each tracing method is a
Method.overloadover the tracers ofTrace.tracers, in the order of the table above with diff mode before plainprestateTracer. TypeScript and the run-time match both take the first one that accepts the options.Debug.Tracesis the type of that list of definitions, which is whatDebug.traceCall.variantsholds. - A typo in
tracerConfiggivesunknown, not a compile error. WithtracerConfig: { onlyTopCal: true }the options no longer fitcallTracerfor TypeScript, which falls back to the custom tracer and types the resultunknown. At run time the misspelled key is dropped from the request and the tracer runs with its defaults. A result that isunknownwhere you expected a trace is the sign to check the config. - The same goes for a config the tracer does not take.
4byteTracerandnoopTracerhave no config: giving them atracerConfigtypes the resultunknown. - A typo at the top level does not compile. A misspelled struct logger setting, or a
misspelled
tracerConfigkey name, is refused by TypeScript. - Options built beforehand need
as const. Inconst options = { tracer: "callTracer" }the tracer is astring, which selects the custom tracer. Write{ tracer: "callTracer" } as constto keep the name. diffModehas to be a literal. AdiffModeof typebooleanfits neither mode and types the resultunknown.- Struct logger settings do not mix with a tracer.
enableMemoryand the others are only accepted without atracerkey.