2. State, Storage & Snapshots
Imagine you write a test that funds Alice with 10 ETH before calling a contract:
try vm.balances.put(alice, 10_000_000_000_000_000_000);
_ = vm.call(.{ .call = .{
.caller = alice,
.to = contract,
.value = 0,
.input = &.{},
.gas = 100_000,
} });
std.debug.print("Alice balance: {d}\n", .{vm.get_balance(alice)});With host = null, it prints the seeded value:
Alice balance: 10000000000000000000Without a host, vm.call() reads and writes vm.balances, vm.nonces, vm.code, and vm.storage directly, and those maps keep their contents across calls until vm.deinit().
What initTransactionState() resets
vm.call() runs initTransactionState() at the start of every transaction. It resets per-transaction bookkeeping only:
self.storage.original_storage.clearRetainingCapacity();
self.storage.clear_transient();
self.gas_refund = 0;
self.access_list_manager = AccessListManager.init(arena_allocator);
self.logs = std.ArrayList(call_result.Log){};It also resets the frame list, the created, self-destructed, and touched account sets, and the balance, nonce, and code snapshot stacks. It never clears balances, nonces, code, or committed storage. The test transaction startup preserves state and resets storage refund accounting in src/evm_test.zig checks this.
Three storage maps, one revert model
Once a transaction is running, src/storage.zig keeps three distinct maps. Each exists for a specific Ethereum rule:
storage(persistent): Holds the live slot values modified bySSTORE. Restored on frame revert; survives when a transaction commits (if backed by a host, writes pass through tohost.setStorage()).original_storage: Records each slot's value at the start of the transaction on first write. Never overwritten mid-transaction —SSTOREneeds(original, current, new)to compute EIP-2200 / EIP-3529 gas costs and refunds accurately.transient(EIP-1153, Cancun+): Driven byTLOAD(0x5c) andTSTORE(0x5d). Always costs 100 gas (never cold), does revert if the frame reverts, and is wiped unconditionally at the end of the transaction.
Why did the first SLOAD cost 2,100 gas and the second only 100?
Click through the three tabs below to see the exact Berlin → Osaka gas rules enforced by AccessListManager and handlers_storage.zig:
First BALANCE, EXTCODESIZE, CALL, or STATICCALL to an unwarmed address.
First SLOAD (or cold surcharge on SSTORE) for a slot not in the EIP-2930 access list.
Every subsequent read of a warm address/slot, pre-warmed precompiles, or EIP-1153 TLOAD/TSTORE.
GasConstants in Voltaire and src/instructions/handlers_storage.zig / src/frame.zig.Reading storage and refunds after a call
Let's execute a contract that writes 42 to slot 0 and reads it back, then inspect vm.storage:
const bytecode = [_]u8{
// SSTORE(slot=0, value=42) -> 20,000 + 2,100 cold = 22,100 gas
0x60, 0x2a, // PUSH1 42
0x60, 0x00, // PUSH1 0
0x55, // SSTORE
// SLOAD(slot=0) -> warm! 100 gas
0x60, 0x00, // PUSH1 0
0x54, // SLOAD
// Return the loaded value
0x60, 0x00, // PUSH1 0
0x52, // MSTORE
0x60, 0x20, // PUSH1 32
0x60, 0x00, // PUSH1 0
0xf3, // RETURN
};
try vm.code.put(target, &bytecode);
const result = vm.call(.{ .call = .{
.caller = voltaire.ZERO_ADDRESS,
.to = target,
.value = 0,
.input = &.{},
.gas = 100_000,
} });
const stored = try vm.storage.get(target, 0); // 42Notice that vm.storage stays readable after vm.call() returns (only transient storage and warm sets are cleared at end-of-tx). Without a host or storage injector, the slot also survives later vm.call()s until vm.deinit().
Snapshotting setters (for code running inside a frame)
If you write custom opcode handlers or precompiles that modify balances, nonces, or code mid-execution, never mutate vm.balances directly — use the copy-on-write helpers so nested reverts undo your write cleanly:
try vm.setBalanceWithSnapshot(addr, new_balance);
try vm.setNonceWithSnapshot(addr, new_nonce);
try vm.setCodeWithSnapshot(addr, new_code);Each helper records the pre-write value into every active snapshot on balance_snapshot_stack / nonce_snapshot_stack / code_snapshot_stack before applying the update.
Next
Ready to fund Alice's account, deploy two contracts, and watch a reverted sub-call roll back balances across a real backend? Head to Tutorial 3: Building a Custom Host.