Architecture
Guillotine Mini is structured around three boundaries: Evm orchestrates transaction state and nested calls, Frame executes bytecode for one call context, and HostInterface connects to external state when you do not want ephemeral in-memory maps.
The Evm / Frame split
The Python execution-specs store stack, memory, program counter, storage, and call state on a single object. Guillotine Mini separates per-transaction state from per-call execution state so each module fits in one file and can be tested independently.
| Component | Source | Owns | Does not own |
|---|---|---|---|
Evm(config) | src/evm.zig | Storage (persistent, original, transient), AccessListManager, balances, nonces, code, copy-on-write snapshot stacks, gas_refund, logs, frames, tracer, arena | Stack, memory, PC, opcode decoding |
Frame(config) | src/frame.zig | Operand stack (≤ config.stack_size), byte-addressed memory, pc, gas_remaining, Bytecode (JUMPDEST bitmap), output, return_data, is_static | Cross-frame storage, account balances, snapshots |
HostInterface | src/host.zig | External account/storage persistence via an 11-pointer vtable | EVM semantics or nested call dispatch |
Inside a frame, opcodes that touch account state or spawn sub-calls (SLOAD, SSTORE, BALANCE, CALL, CREATE, SELFDESTRUCT) call back into the parent EVM via frame.getEvm(). initTransactionState() reserves max_call_depth + 1 slots in Evm.frames so frame pointers stay stable through nested calls, and evm.zig indexes self.frames.items[self.frames.items.len - 1] after a nested call returns.
Memory and arena ownership
Evm embeds a std.heap.ArenaAllocator backed by the allocator passed to init(). Every transaction-scoped allocation — frame stacks, memory buffers, bytecode analysis, log slices, snapshots, and CallResult outputs — comes from that arena.
- No per-frame leak risk. An execution error (
OutOfGas,StackUnderflow,InvalidJump) returns immediately;deinit()frees the entire arena in one call. - Borrowed results by default. Slices inside a
CallResultreturned byvm.call()point intovm.arena. Callresult.toOwnedResult(allocator)beforevm.deinit()if the result must outlive the EVM. - Standalone tracer memory.
Tracertakes its own allocator so traces survive aftervm.deinit().
Exact lifecycle of vm.call()
Evm.call() is the single entry point for all six CallParams shapes (call, callcode, delegatecall, staticcall, create, create2).
Five implementation details in src/evm.zig are worth knowing before you write host code:
- Intrinsic gas is the caller's job.
vm.call()assumes the gas passed inparamsis already net of the 21,000 transaction base fee, calldata cost (including EIP-7623 floor), access list fees, and create fees. - Seeded state survives
initTransactionState(). Eachvm.call()first runsinitTransactionState(), which resets original and transient storage, warm sets, logs, the refund counter, and the snapshot stacks. It does not clearbalances,nonces,code, or storage seeded withsetStoragePrestate(), so without a host you can seed any number of accounts withvm.code.put,vm.balances.put, andvm.nonces.put, and the state persists across calls. - Top-level value transfer happens only with a host. In
vm.call(), value is moved fromcallertoaddresswhenvalue > 0 and self.host != null(and reversed if the top-level call reverts or errors). - Refunds are returned, not auto-applied.
CallResult.refund_counterreports the raw refund counter; the caller applies the fork-dependent cap (gas_used / 5from London onward,gas_used / 2earlier) after settling coinbase fees. - End-of-tx cleanup runs in spec order. Warm sets clear, transient storage (EIP-1153) clears, then accounts marked by
SELFDESTRUCT(EIP-6780) and touched empty accounts (EIP-161) are removed.
Bytecode analysis and dispatch
src/bytecode.zig scans contract bytecode once when a Frame starts:
- It builds a
JUMPDESTbitmap by skipping overPUSH1..PUSH32immediate bytes, so0x5bbytes inside push data are never accepted byJUMPorJUMPI. - Truncated
PUSHimmediates at the end of bytecode are zero-padded on the right per the Ethereum specification. Frame.execute()callsFrame.step()in a loop;Frame.executeOpcode()insrc/frame.zigdispatches each opcode through aswitchto the category handler files insrc/instructions/(handlers_arithmetic.zig,handlers_storage.zig,handlers_system.zig, etc.).src/instructions/dispatcher.zigis a standaloneDispatcherexported fromroot.zig;Framedoes not use it.- In
DebugandReleaseSafebuilds,EvmConfig.loop_quotadefaults to1_000_000steps to catch runaway loops early; inReleaseFast/ReleaseSmallit defaults tonull(unlimited).
C ABI and WebAssembly surface
Both zig build native (static library libguillotine_mini.a) and zig build wasm (WASI Preview 1 reactor guillotine_mini.wasm) compile the same entry file: src/root_c.zig.
- An opaque
*EvmHandlewraps anExecutionContext(theEvm, staged bytecode/calldata, access lists, blob hashes, and lastCallResult). - Every exported function that takes a handle accepts
nulland returns a safe zero/false sentinel. - On
wasm32-wasi,evm_allocandevm_freemanage host transfer buffers in linear memory, and the twoenvimports (js_opcode_callback,js_precompile_callback) let hosts intercept any precompile, or any opcode afterevm_set_js_opcode_callbacks_enabled(true), without recompiling Zig.
Source layout
src/
├── root.zig # Public Zig module exports
├── evm.zig # Evm(config): state, call(), inner_call(), inner_create()
├── frame.zig # Frame(config): stack, memory, PC, execute(), step()
├── bytecode.zig # JUMPDEST bitmap & PUSH immediate reader
├── storage.zig # Persistent, original, and EIP-1153 transient storage
├── access_list_manager.zig # EIP-2929 warm/cold tracking + EIP-2930 pre-warming
├── host.zig # HostInterface vtable
├── call_params.zig # 6-variant CallParams union & validation
├── call_result.zig # CallResult, logs, selfdestructs, owned cloning
├── evm_config.zig # Comptime EvmConfig, OpcodeOverride, PrecompileOverride
├── errors.zig # CallError error set
├── trace.zig # EIP-3155 Tracer, TraceEntry, TraceDiff
├── async_executor.zig # Yield/resume state-injection state machine
├── storage_injector.zig # Async cache & state-change JSON serializer
├── root_c.zig # Exported C & WASM ABI (42 evm_* symbols)
└── instructions/ # Opcode handlers grouped by category