Skip to content
LogoLogo

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 == null selects config.hardfork (which defaults to Hardfork.DEFAULT, .OSAKA). The Voltaire enum also includes .AMSTERDAM.
  • block_context == null installs chain_id = 1, block_gas_limit = config.block_gas_limit, block_coinbase = ZERO_ADDRESS, and zero for all other fields.
  • init() allocates storage, storage_prestate, balances, nonces, and code inside self.arena, so callers can seed pre-state before the first call().
  • deinit() destroys self.arena, releasing all transaction-scoped memory at once.
pub fn initTransactionState(self: *Self, blob_versioned_hashes: ?[]const [32]u8) !void

Resets 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 u8
  • call delegates to callWithBytecodeOverride(params, null).
  • callWithBytecodeOverride executes any of the six CallParams shapes, optionally overriding the top-level target bytecode without mutating self.code. Returns borrowed slices allocated in self.arena.
  • resultErrorName returns null on success or a Geth-compatible error string (via inspector.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!CreateResult
  • callOrContinue drives the async yield/resume state machine (src/async_executor.zig).
  • inner_call and inner_create handle nested calls and contract creation with copy-on-write snapshot/rollback, enforcing config.max_call_depth, config.max_initcode_size, and config.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) void
  • setStoragePrestate seeds a storage slot in both storage.storage and storage_prestate without counting it as a transaction write.
  • setInspector attaches a read-only Inspector vtable (see Tracing & Inspectors); clearInspector detaches 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) void

Single-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) !void
  • getBytecode() 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).

MethodSemantics
get(address, slot) !u256With 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) !voidCaptures 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) u256Pre-transaction slot value for EIP-2200 / EIP-3529 gas math.
get_transient(address, slot) u256Reads EIP-1153 transient slot (0 if unset).
set_transient(address, slot, value) !voidWrites EIP-1153 transient slot (reverted on frame rollback; cleared at tx end).
clear_transient() voidEmpties 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

FieldTypeMeaning
stackstd.ArrayList(u256)LIFO operand stack (bounded by config.stack_size, default 1024)
memorystd.ArrayListUnmanaged(u8)Contiguous byte-addressed memory
memory_sizeu32Current word-aligned memory length in bytes (bounded by config.memory_limit)
pcu32Program counter
gas_remainingi64Remaining execution gas
bytecodeBytecodeRaw bytes + JUMPDEST bitmap
output[]u8Bytes produced by RETURN or REVERT
return_data[]const u8Output from the most recent nested call
stopped, revertedboolHalt flags
is_staticboolRead-only mode (STATICCALL)
call_depthu32Nesting 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