Skip to content
LogoLogo

C FFI & Async State Injection

Guillotine Mini compiles src/root_c.zig to both a native static library (zig build native) and a WASI WebAssembly reactor (zig build wasm). Both expose the exact same 42-symbol evm_* C ABI.

Build the static library

zig build native -Doptimize=ReleaseFast

This writes zig-out/lib/libguillotine_mini.a with the EVM, Voltaire primitives, blst, BN254, and c-kzg-4844 linked inside.

Minimal C program

All 256-bit quantities (value, chain_id, slot, balance, base_fee) cross the ABI as 32-byte big-endian byte arrays; addresses cross as 20-byte arrays. Every handle-taking function accepts a nullable EvmHandle*. On NULL, void functions return immediately, boolean functions return false, and numeric functions return 0. Pass a valid byte pointer for empty calldata and continuation payloads with length 0.

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
 
typedef struct EvmHandle EvmHandle;
 
extern EvmHandle *evm_create(const char *hardfork, size_t hardfork_len, uint8_t log_level);
extern void       evm_destroy(EvmHandle *handle);
extern bool       evm_set_bytecode(EvmHandle *handle, const uint8_t *code, size_t len);
extern bool       evm_set_execution_context(
    EvmHandle *handle, int64_t gas,
    const uint8_t caller[20], const uint8_t address[20],
    const uint8_t value[32], const uint8_t *calldata, size_t calldata_len);
extern bool       evm_execute(EvmHandle *handle);
extern int64_t    evm_get_gas_used(EvmHandle *handle);
extern size_t     evm_get_output_len(EvmHandle *handle);
extern size_t     evm_get_output(EvmHandle *handle, uint8_t *buf, size_t buf_len);
 
int main(void) {
    const char *fork = "PRAGUE";
    EvmHandle *vm = evm_create(fork, strlen(fork), 0);
    if (!vm) return 1;
 
    uint8_t caller[20] = {0};
    uint8_t target[20] = {0};
    target[18] = 0x01; // 0x0000...0100 (outside Prague precompiles)
    uint8_t zero32[32] = {0};
    const uint8_t empty = 0;
 
    // PUSH1 42; PUSH1 0; MSTORE; PUSH1 32; PUSH1 0; RETURN
    uint8_t code[] = { 0x60, 0x2a, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3 };
 
    evm_set_bytecode(vm, code, sizeof(code));
    evm_set_execution_context(vm, 100000, caller, target, zero32, &empty, 0);
 
    bool ok = evm_execute(vm);
    uint8_t out[32] = {0};
    size_t len = evm_get_output(vm, out, sizeof(out));
 
    printf("ok=%d gas_used=%lld output_len=%zu last_byte=%u\n",
           ok, (long long)evm_get_gas_used(vm), len, len ? out[len - 1] : 0);
 
    evm_destroy(vm);
    return ok ? 0 : 1;
}

Preserved pre-state lifecycle

You can seed initial account and storage state immediately after evm_create()—before the first evm_execute() or evm_call_ffi() call:

  • evm_set_storage() calls Evm.setStoragePrestate(), populating both the live storage cache and storage_prestate. Seeded slots are preserved across transaction initialization (including when the async StorageInjector is enabled) and are not miscounted as transaction writes.
  • evm_set_balance(), evm_set_code(), and evm_set_nonce() populate Evm's arena-backed state maps allocated in evm_create(), which persist across evm_execute() calls on the same handle.
  • evm_execute() runs callWithBytecodeOverride(): if evm_set_bytecode() was called with non-empty code, that bytecode overrides the top-level target without mutating evm.code; if evm_set_bytecode() was not called, evm_execute() executes the bytecode seeded at address via evm_set_code().

Tracing, call traces & access lists over FFI

All three observability outputs use a two-step length-then-copy pattern and work identically in both native C and WASM builds:

// Enable before evm_execute() or evm_call_ffi()
evm_enable_tracing(vm, /*enable_memory=*/true, /*disable_stack=*/false,
                   /*disable_storage=*/false, /*enable_return_data=*/true);
evm_enable_call_tracer(vm, /*only_top_call=*/false, /*with_log=*/true);
evm_enable_access_list_tracker(vm);
 
bool ok = evm_execute(vm);
 
// 1. EIP-3155 JSONL opcode trace (includes trailing TraceSummary line)
size_t trace_len = evm_get_trace_json_len(vm);
// allocate buffer of trace_len bytes, then:
evm_get_trace_json(vm, trace_buf, trace_len);
 
// 2. Geth callTracer JSON tree
size_t call_len = evm_get_call_trace_json_len(vm);
evm_get_call_trace_json(vm, call_buf, call_len);
 
// 3. EIP-2930 access list JSON array
size_t al_len = evm_get_access_list_json_len(vm);
evm_get_access_list_json(vm, al_buf, al_len);

When both evm_enable_call_tracer and evm_enable_access_list_tracker are active, src/root_c.zig automatically fans out events via MuxInspector. Call evm_disable_tracing(vm) to turn off opcode tracing for subsequent executions.

In WASM builds, JS opcode callbacks (env.js_opcode_callback) are disabled by default so normal execution does not cross the WASM/JS boundary on every opcode. Call evm_set_js_opcode_callbacks_enabled(true) to opt in when installing custom JS opcode handlers.

Async state injection (evm_call_ffi / evm_continue_ffi)

When state lives in an async database, remote JSON-RPC node, or rollup storage layer, pre-loading every slot is wasteful or impossible. Instead, enable the storage injector and drive the yield/resume loop:

Yield / Resume Async State-Injection Protocolevm_call_ffi + evm_continue_ffi
Host ApplicationGuillotine Mini (FFI)External DB / RPC1. evm_enable_storage_injector() → evm_call_ffi(h, &req)2. req.output_type = 1..4 (NEED_STORAGE / BALANCE / CODE / NONCE)3. await rpc.getStorageAt(addr, slot)4. evm_continue_ffi(h, type=1..4, packed_bytes, len, &req)5. output_type = 5 (READY_TO_COMMIT) · changes JSON6. evm_continue_ffi(h, type=5, ...) → output_type = 0 (RESULT)
Because Zig and WASM execution are synchronous, the EVM yields an AsyncRequest whenever it hits a cold external state miss. The host fetches the data (from RPC, Postgres, or RocksDB) and resumes execution.

The AsyncRequest C struct

Transcribed directly from src/root_c.zig:829:

typedef struct {
    uint8_t  output_type;      // 0=RESULT, 1=NEED_STORAGE, 2=NEED_BALANCE,
                               // 3=NEED_CODE, 4=NEED_NONCE, 5=READY_TO_COMMIT, 255=ERROR
    uint8_t  address[20];
    uint8_t  slot[32];         // big-endian u256 (used when output_type == 1)
    uint32_t json_len;         // bytes copied into inline json_data (capped at 16384)
    uint8_t  json_data[16384]; // inline state-changes JSON preview
} AsyncRequest;

When output_type == 5 (READY_TO_COMMIT), the full state-changes JSON is stored in an arena-allocated buffer with no 16 KiB truncation limit. Use evm_get_state_changes_len(vm) to query the exact full byte length and evm_get_state_changes(vm, buf, len) to copy the complete JSON when it may exceed sizeof(req.json_data).

Packed response formats for evm_continue_ffi

output_type yieldedcontinue_type to senddata_ptr layoutLength
1 (NEED_STORAGE)1address[20] || slot_be[32] || value_be[32]84 bytes
2 (NEED_BALANCE)2address[20] || balance_be[32]52 bytes
3 (NEED_CODE)3address[20] || raw_bytecode[...]20 + code_len bytes
4 (NEED_NONCE)4address[20] || nonce_be[8]28 bytes
5 (READY_TO_COMMIT)5valid dummy byte pointer (after persisting state changes)0 bytes

Driving loop skeleton

evm_enable_storage_injector(vm);
 
const uint8_t empty = 0;
AsyncRequest req = {0};
bool ok = evm_call_ffi(vm, &req);
 
while (ok && req.output_type != 0) {
    if (req.output_type == 1) {
        uint8_t resp[84];
        memcpy(resp,      req.address, 20);
        memcpy(resp + 20, req.slot,    32);
        fetch_storage_from_db(req.address, req.slot, resp + 52);
        ok = evm_continue_ffi(vm, 1, resp, sizeof(resp), &req);
    } else if (req.output_type == 5) {
        size_t full_len = evm_get_state_changes_len(vm);
        if (full_len <= sizeof(req.json_data)) {
            persist_changes_json(req.json_data, req.json_len);
        } else {
            uint8_t *full = malloc(full_len);
            evm_get_state_changes(vm, full, full_len);
            persist_changes_json(full, full_len);
            free(full);
        }
        ok = evm_continue_ffi(vm, 5, &empty, 0, &req);
    } else {
        // Handle types 2 (balance), 3 (code), 4 (nonce) similarly
        break;
    }
}
// When req.output_type == 0, read results with evm_is_success / evm_get_output / evm_get_gas_used

For the complete list of all 42 exported functions, see the C & WASM ABI Reference.