Skip to content

Instrumentation Architecture — RPC Proxy Design

Overview

The instrumentation layer is an RPC proxy that sits transparently between any code that talks to the local Anvil fork (attack packs, Foundry scripts, LLM agents, manual cast send calls) and the Anvil process itself.

Every transaction is automatically traced — the attack pack does zero artifact collection work.


Data Flow Diagram

flowchart TB
    ARCHIVE["Archive RPC Provider<br/>eth.llamarpc.com / ankr / publicnode"]

    subgraph Researcher["Researcher"]
        AP["Attack Pack<br/>(Python Plugin)"]
        FS["Foundry Script<br/>(forge script)"]
        LLM["LLM Agent<br/>(future — dashed outline)"]
        CS["cast send / curl"]
    end

    subgraph Crucible["Crucible Testbed"]
        ER["ExperimentRunner"]
        CM["ChainManager"]
        subgraph Proxy["RpcProxy (per chain)"]
            HS["HTTP Server<br/>127.0.0.1:random"]
            EX["ThreadPoolExecutor<br/>max 10 workers"]
        end
        LE["LogEmitter"]
        DIFF["diff.py compare_runs"]
    end

    subgraph Anvil["Anvil Fork (local)"]
        ANV["anvil --fork-url &lt;ARCHIVE_RPC&gt;"]
    end

    subgraph Artifacts["Artifact Streams"]
        GAS["gas_profile.jsonl"]
        EVT["events.jsonl"]
        STO["storage_diffs.jsonl"]
        BAL["balance_diffs.jsonl"]
    end

    subgraph Downstream["Downstream"]
        REPORT["report.md"]
        INTEGRITY["log_integrity.json"]
        DIFF_OUT["storage_equal<br/>events_equal<br/>gas_equal<br/>balances_equal"]
    end

    ARCHIVE -->|eth_getBlockByNumber, eth_getStorageAt| ANV

    ER --> CM
    CM -->|start_instrumentation| Proxy
    ER -->|attack.run| AP

    AP -->|JSON-RPC via proxy_url| HS
    FS -->|JSON-RPC via proxy_url| HS
    LLM -.->|JSON-RPC via proxy_url| HS
    CS -->|JSON-RPC via proxy_url| HS

    HS -->|forward all requests| ANV
    ANV -->|response| HS
    HS -->|response| Researcher

    HS -->|eth_sendTransaction detected| EX
    EX -->|trace_async| ANV
    ANV -->|receipt + debug_traceTransaction| EX
    EX -->|gas_used| LE
    EX -->|events| LE
    EX -->|storage_diffs| LE
    EX -->|balance_diffs| LE

    LE --> GAS
    LE --> EVT
    LE --> STO
    LE --> BAL

    GAS --> DIFF
    EVT --> DIFF
    STO --> DIFF
    BAL --> DIFF

    LE --> REPORT
    LE --> INTEGRITY
    DIFF --> DIFF_OUT
Hold "Alt" / "Option" to enable pan & zoom

Legend: Solid arrow = synchronous call · Dashed arrow = planned/future · Thick arrow = data artifact


Blank Fork Mode — Zero Remote RPC Dependency

The diagrams above show the archive fork path: Anvil fetches historical state from a remote archive RPC provider. However, the instrumentation proxy is completely agnostic to how Anvil was started — it only cares that Anvil is listening on an HTTP port.

How Blank Forks Work

In blank fork mode (fork_block: null), Anvil starts without --fork-url:

# Archive fork: Anvil fetches remote state
anvil --fork-url https://eth.llamarpc.com --fork-block-number 15259100 --accounts 0 --balance 0

# Blank fork: No remote dependency — Anvil is a standalone local EVM
anvil --accounts 0 --balance 0

The proxy forwards to whatever URL it's given — it does not inspect or care about the upstream chain state. The same RpcProxy class, the same attach_instrumentation() call, the same ThreadPoolExecutor background tracing. No code changes, no config changes.

Data Flow (Blank Fork)

flowchart TB
    subgraph Researcher["Researcher"]
        AP["Attack Pack<br/>(Python Plugin)"]
        FS["Foundry Script<br/>(forge script)"]
        CS["cast send / curl"]
    end

    subgraph Crucible["Crucible Testbed"]
        ER["ExperimentRunner"]
        CM["ChainManager"]
        subgraph Proxy["RpcProxy (per chain)"]
            HS["HTTP Server<br/>127.0.0.1:random"]
            EX["ThreadPoolExecutor<br/>max 10 workers"]
        end
        LE["LogEmitter"]
    end

    subgraph Anvil["Anvil (local, no fork)"]
        ANV["anvil --accounts 0 --balance 0<br/>(standalone EVM — no --fork-url)"]
    end

    subgraph Artifacts["Artifact Streams"]
        GAS["gas_profile.jsonl"]
        EVT["events.jsonl"]
        STO["storage_diffs.jsonl"]
        BAL["balance_diffs.jsonl"]
    end

    ER --> CM
    CM -->|start_instrumentation| Proxy
    ER -->|attack.run| AP

    AP -->|JSON-RPC via proxy_url| HS
    FS -->|JSON-RPC via proxy_url| HS
    CS -->|JSON-RPC via proxy_url| HS

    HS -->|forward all requests| ANV
    ANV -->|response| HS
    HS -->|response| Researcher

    HS -->|eth_sendTransaction detected| EX
    EX -->|trace_async| ANV
    ANV -->|receipt + debug_traceTransaction| EX
    EX -->|gas_used| LE
    EX -->|events| LE
    EX -->|storage_diffs| LE
    EX -->|balance_diffs| LE

    LE --> GAS
    LE --> EVT
    LE --> STO
    LE --> BAL
Hold "Alt" / "Option" to enable pan & zoom

Notice what's missing: The "Archive RPC Provider" box and the eth_getBlockByNumber/ eth_getStorageAt arrows. They simply don't exist in blank fork mode. Everything else — the proxy, the artifact streams, the trace pipeline — is identical to archive fork mode.

Why This Matters

Property Archive Fork Blank Fork
Remote RPC needed? Yes — archive provider No — fully offline
Startup time 30s–5 min (cold cache) <1s — instant
Determinism Depends on archive state Fully deterministic
CI/hermetic testable Needs fixture replay Yes — directly
Artifact collection Same proxy + streams Same proxy + streams
Fidelity levels L2 (verified fork state) L3/L4 (validation, no history)

The artifact collection pipeline is identical in both modes. The instrumented data — gas, events, storage diffs, balance diffs — has the same format, the same integrity hashing, the same diffability. The only difference is whether Anvil fetches historical state at startup.

Practical Use Cases

  • L4 validation experiments (l4_full_validation, nomad_bridge_l4): Two blank forks (ethereum + polygon), bridge plugin deployed on both, bidirectional cross-chain messaging. Zero RPC calls during the entire run — the proxy only talks to local Anvil instances.
  • Offline demo/testing: Run crucible experiment run with blank forks on a plane, in CI, or on an air-gapped machine. Artifact collection works identically.
  • Development workflow: Iterate on attack packs without waiting for archive forks. Switch to archive forks only when ready for L2 fidelity verification.

Important caveat: Blank forks can fail on first startup due to an Anvil nightly-build quirk with --accounts 0 --balance 0. See Troubleshooting §7C and Testbed Scope Guide — Blank Fork Caveats for recovery steps.


Sequence: Experiment Run with Instrumentation

sequenceDiagram
    participant ER as ExperimentRunner
    participant CM as ChainManager
    participant SC as ScenarioEngine
    participant PX as RpcProxy
    participant AN as Anvil Fork
    participant ARC as Archive RPC
    participant AK as Attack Pack
    participant LE as LogEmitter

    ER->>SC: run(scenario)
    SC->>CM: start_topology()
    CM->>AN: spawn anvil --fork-block 15259100
    AN->>ARC: fetch historical state (lazy, on-demand)
    ARC-->>AN: accounts, storage, code
    AN-->>CM: ready at http://127.0.0.1:8545
    CM-->>SC: runtime
    SC-->>ER: done

    ER->>CM: start_instrumentation("ethereum", log)
    CM->>CM: read meta file for upstream URL
    CM->>PX: attach_instrumentation(upstream, log)
    PX->>PX: start HTTP server on 127.0.0.1:random
    PX-->>CM: proxy_url = http://127.0.0.1:port
    Note over AK,AN: Now runtime_for("ethereum").rpc_url == proxy_url

    ER->>AK: run(context)

    AK->>PX: eth_sendTransaction({...})
    PX->>AN: forward eth_sendTransaction
    AN-->>PX: tx_hash
    PX-->>AK: tx_hash

    par Background Trace (via ThreadPoolExecutor)
        PX->>AN: poll eth_getTransactionReceipt(tx_hash)
        AN-->>PX: receipt {gasUsed, logs, status}
        PX->>AN: debug_traceTransaction(tx_hash, prestateTracer + diffMode)
        AN-->>PX: {pre: {accounts}, post: {accounts}}
        PX->>LE: gas(step="auto_tx", gas_used=21000)
        PX->>LE: event(chain, event_name="Transfer", ...)
        PX->>LE: storage_diff(chain, contract, slot, before, after)
        PX->>LE: balance_diff(chain, contract, account, before, after)
    end

    AK-->>ER: AttackResult(success=true)

    ER->>CM: stop_instrumentation()
    CM->>PX: drain() — wait for pending traces
    PX->>PX: executor.shutdown(wait=True, timeout=10s)
    CM->>PX: stop() — shutdown HTTP server

    ER->>LE: write_report(manifest_path)
    ER->>LE: write_integrity() — hashes + closes handles
Hold "Alt" / "Option" to enable pan & zoom

Integration Points

Component Role File
RpcProxy HTTP proxy + background tracer (gas, events, storage diffs, balance) crucible/instrumentation.py
ChainManager Starts/stops proxy per chain; runtime_for() returns proxy URL transparently crucible/chain_manager.py
ExperimentRunner Starts proxy before attack, drains/stops in finally block crucible/experiment.py
LogEmitter Receives traced artifacts into typed JSONL streams crucible/logging.py
diff.py Compares artifact streams across runs for determinism check crucible/diff.py

Proxy URL Transparency

# Before instrumentation:
runtime_for("ethereum").rpc_url  # → "http://127.0.0.1:8545"

# After start_instrumentation("ethereum", log):
runtime_for("ethereum").rpc_url  # → "http://127.0.0.1:54321" (proxy)

All existing code continues to work unchanged — the proxy is invisible to attack packs, scenario steps, and bridge plugins.

Standalone Mode (crucible instrument)

Beyond experiment-mode auto-instrumentation, crucible instrument starts the proxy standalone — no fork, no experiment runner, no scenario. This is useful for ad-hoc research where you:

  1. Start an Anvil fork manually (or use an existing one)
  2. Point the proxy at it with --upstream
  3. Send transactions through the proxy URL (via cast send, Foundry scripts, Python scripts)
  4. Get complete artifact streams without writing any instrumentation code
# Terminal 1: Start Anvil manually
anvil --fork-url https://eth.llamarpc.com --fork-block-number 15259100

# Terminal 2: Start the instrumentation proxy
crucible instrument --upstream http://127.0.0.1:8545 --chain ethereum --output-dir ./logs/my_run

# Terminal 3: Send transactions through the proxy
cast send --rpc-url http://127.0.0.1:<proxy-port> 0xTarget --value 1ether

Standalone Options

Flag Default Description
--upstream, -u http://127.0.0.1:8545 Upstream RPC URL to proxy (typically the Anvil fork)
--chain, -c ethereum Chain name for artifact tagging and subdirectory naming
--output-dir, -o ./instrument_output Directory to write artifact streams into
--port, -p 0 (random) Proxy listen port. Use a specific port for scripting/chaining
--timeout, -t 0 (run until Ctrl+C) Auto-exit after N seconds. Useful for scripted runs

Artifacts are written to --output-dir with the same JSONL stream structure as experiment-mode captures.


Thread Safety

Concern Solution
Multi-chain topologies _make_handler() factory creates a fresh handler class per proxy instance — each chain's proxy forwards to its own Anvil independently
Rapid-fire tx loops ThreadPoolExecutor(max_workers=10) bounds concurrent traces; excess txs queue
Race with LogEmitter.close() drain() → stop() ordering: executor shutdown waits for pending traces, then HTTP server stops, then write_integrity() closes handles

Fallback Behavior

When debug_traceTransaction is unavailable or returns an error:

Artifact Captured? Source
Gas used Yes eth_getTransactionReceipt
Events / logs Yes eth_getTransactionReceipt
Storage diffs Warning logged N/A — no trace available
Balance changes Warning logged N/A — no trace available

A prominent failure step is logged showing the tx hash, what was lost (storage diffs + balance changes), what was saved (gas + events), and a reference to TROUBLESHOOTING.md for Anvil version advice.