Evm & Frame
Evm(comptime config: EvmConfig) type
Defined in src/evm.zig. Returns a concrete EVM type specialized to config.
Associated types
pub const CallParams = call_params.CallParams(config);
pub const CallResult = call_result.CallResult(config);
pub const AsyncExecutorType = async_executor_mod.AsyncExecutor(Self, CallParams, CallResult);
pub const CallOrContinueInput = AsyncExecutorType.CallOrContinueInput;
pub const CallOrContinueOutput = AsyncExecutorType.CallOrContinueOutput;Lifecycle
pub fn init(
self: *Self,
allocator: std.mem.Allocator,
h: ?HostInterface,
hardfork: ?Hardfork,
block_context: ?BlockContext,
origin: Address,
gas_price: u256,
log_level: ?LogLevel,
) !void
pub fn deinit(self: *Self) void- Initialize in place:
var vm: Evm(.{}) = undefined; try vm.init(...); defer vm.deinit();. hardfork == nullselectsconfig.hardfork(which defaults toHardfork.DEFAULT,.OSAKA). The Voltaire enum also includes.AMSTERDAM.block_context == nullinstallschain_id = 1,block_gas_limit = config.block_gas_limit,block_coinbase = ZERO_ADDRESS, and zero for all other fields.init()allocatesstorage,storage_prestate,balances,nonces, andcodeinsideself.arena, so callers can seed pre-state before the firstcall().deinit()destroysself.arena, releasing all transaction-scoped memory at once.
pub fn initTransactionState(self: *Self, blob_versioned_hashes: ?[]const [32]u8) !voidResets per-transaction execution state: clears original_storage, transient storage, trace_slots, gas_refund, pending_state_changes, access_list_manager, frames (pre-allocated to config.max_call_depth + 1), logs, account tracking sets, and snapshot stacks, and copies blob_versioned_hashes into self.arena. Pre-seeded balances, nonces, code, and non-injector storage are preserved; when a StorageInjector is active, storage.storage is reset back to storage_prestate. Invoked automatically by call().
Execution
pub fn call(self: *Self, params: CallParams) CallResult
pub fn callWithBytecodeOverride(self: *Self, params: CallParams, bytecode: ?[]const u8) CallResult
pub fn resultErrorName(result: CallResult) ?[]const u8calldelegates tocallWithBytecodeOverride(params, null).callWithBytecodeOverrideexecutes any of the sixCallParamsshapes, optionally overriding the top-level target bytecode without mutatingself.code. Returns borrowed slices allocated inself.arena.resultErrorNamereturnsnullon success or a Geth-compatible error string (viainspector.formatErrorName) on failure.
pub fn callOrContinue(self: *Self, input: CallOrContinueInput) !CallOrContinueOutput
pub fn inner_call(self: *Self, params: CallParams) CallResult
pub fn inner_create(self: *Self, value: u256, init_code: []const u8, gas: u64, salt: ?u256) errors.CallError!CreateResultcallOrContinuedrives the async yield/resume state machine (src/async_executor.zig).inner_callandinner_createhandle nested calls and contract creation with copy-on-write snapshot/rollback, enforcingconfig.max_call_depth,config.max_initcode_size, andconfig.max_bytecode_size.
Pre-call setters, tracing & inspectors
pub fn setBytecode(self: *Self, bytecode: []const u8) void
pub fn setAccessList(self: *Self, access_list: ?AccessList) void
pub fn setBlobVersionedHashes(self: *Self, hashes: []const [32]u8) void
pub fn setStoragePrestate(self: *Self, key: StorageKey, value: u256) !void
pub fn setTracer(self: *Self, tracer: *Tracer) void
pub fn setInspector(self: *Self, inspector: Inspector) void
pub fn clearInspector(self: *Self) voidsetStoragePrestateseeds a storage slot in bothstorage.storageandstorage_prestatewithout counting it as a transaction write.setInspectorattaches a read-onlyInspectorvtable (see Tracing & Inspectors);clearInspectordetaches it and resets active frame observation state (async_observation,inspector_frame_id).
State, access lists & address helpers
pub fn getActiveFork(self: *const Self) Hardfork
pub fn get_balance(self: *Self, address: Address) u256
pub fn get_code(self: *Self, address: Address) []const u8
pub fn getNonce(self: *Self, address: Address) u64
pub fn accountExists(self: *Self, addr: Address) bool
pub fn accountIsEmpty(self: *Self, addr: Address) bool
pub fn setBalanceWithSnapshot(self: *Self, addr: Address, new_balance: u256) !void
pub fn setNonceWithSnapshot(self: *Self, addr: Address, new_nonce: u64) !void
pub fn setCodeWithSnapshot(self: *Self, addr: Address, new_code: []const u8) !void
pub fn preWarmTransaction(self: *Self, target: Address) CallError!void
pub fn accessAddress(self: *Self, address: Address) !u64
pub fn accessStorageSlot(self: *Self, contract_address: Address, slot: u256) !u64
pub fn computeCreateAddress(self: *Self, sender: Address, nonce: u64) !Address
pub fn computeCreate2Address(self: *Self, sender: Address, salt: u256, init_code: []const u8) !Address
pub fn add_refund(self: *Self, amount: u64) void
pub fn sub_refund(self: *Self, amount: u64) voidSingle-stepping & active frame inspection
pub fn getCurrentFrame(self: *Self) ?*FrameType
pub fn getPC(self: *const Self) u32
pub fn getBytecode(self: *const Self) []const u8
pub fn step(self: *Self) !voidgetBytecode()returns the active top frame's bytecode slice (frame.bytecode.code), or&.{}when no frame is currently executing.
BlockContext
pub const BlockContext = struct {
chain_id: u256,
block_number: u64,
block_timestamp: u64,
block_difficulty: u256,
block_prevrandao: u256,
block_coinbase: Address,
block_gas_limit: u64,
block_base_fee: u256,
blob_base_fee: u256,
block_hashes: []const [32]u8 = &[_][32]u8{},
};block_hashes stores recent block hashes (up to 256) indexed from the tail matching Python execution-specs: block_hashes[block_hashes.len - (block_number - target_block)].
Storage
Defined in src/storage.zig (vm.storage).
| Method | Semantics |
|---|---|
get(address, slot) !u256 | With the injector enabled, reads pending writes, then the injector cache, and returns error.NeedAsyncData on a miss. With the injector disabled, reads directly from the host if present or from the local map (default 0). |
set(address, slot, value) !void | Captures original_storage on first write in the transaction. With the injector disabled, writes directly to the host if present or to the local map. With the injector enabled, marks the slot dirty, updates the local overlay, and also writes to the host if present. |
get_original(address, slot) u256 | Pre-transaction slot value for EIP-2200 / EIP-3529 gas math. |
get_transient(address, slot) u256 | Reads EIP-1153 transient slot (0 if unset). |
set_transient(address, slot, value) !void | Writes EIP-1153 transient slot (reverted on frame rollback; cleared at tx end). |
clear_transient() void | Empties transient storage at transaction boundary. |
Frame(comptime config: EvmConfig) type
Defined in src/frame.zig. Holds execution state for a single call context.
Selected fields
| Field | Type | Meaning |
|---|---|---|
stack | std.ArrayList(u256) | LIFO operand stack (bounded by config.stack_size, default 1024) |
memory | std.ArrayListUnmanaged(u8) | Contiguous byte-addressed memory |
memory_size | u32 | Current word-aligned memory length in bytes (bounded by config.memory_limit) |
pc | u32 | Program counter |
gas_remaining | i64 | Remaining execution gas |
bytecode | Bytecode | Raw bytes + JUMPDEST bitmap |
output | []u8 | Bytes produced by RETURN or REVERT |
return_data | []const u8 | Output from the most recent nested call |
stopped, reverted | bool | Halt flags |
is_static | bool | Read-only mode (STATICCALL) |
call_depth | u32 | Nesting depth (0 for top-level, bounded by config.max_call_depth) |
Key methods
pub fn getEvm(self: *Self) *Evm(config)
pub fn pushStack(self: *Self, value: u256) EvmError!void
pub fn popStack(self: *Self) EvmError!u256
pub fn peekStack(self: *const Self, index: usize) EvmError!u256
pub fn readMemory(self: *const Self, offset: u32) u8
pub fn writeMemory(self: *Self, offset: u32, value: u8) EvmError!void
pub fn consumeGas(self: *Self, amount: u64) EvmError!void
pub fn memoryExpansionCost(self: *Self, end_bytes: u64) u64
pub fn executeOpcode(self: *Self, opcode: u8) EvmError!void
pub fn step(self: *Self) EvmError!void
pub fn execute(self: *Self) EvmError!void