Skip to content
LogoLogo

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: 10000000000000000000

Without 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:

Three Storage Maps & Copy-on-Write Rollbacksrc/storage.zig + src/evm.zig
THREE MAPS INSIDE Storage1. storage (persistent)Current value during tx.Reverts on frame rollback; persists on commit.2. original_storageCaptured once on first SSTORE in tx.Drives EIP-2200/3529 gas & refund math.3. transient (EIP-1153)TLOAD / TSTORE (always 100 gas).Reverts on frame rollback; wiped at tx end.NESTED CALL SNAPSHOT STACKDepth 0 · Contract ASSTORE(slot 0, 10) → snapshot #1 pushed → CALL BDepth 1 · Contract B → REVERTSSTORE(slot 0, 99) · TSTORE(slot 1, 7) · LOG1(...)All writes rolled back to snapshot #1; B's logs dropped.Depth 0 · Contract A resumes → RETURNSLOAD(slot 0) == 10 · TLOAD(slot 1) == 0CALL pushed 0 onto A's stack; A keeps slot 0 = 10 and finishes cleanly.
On a nested call, the EVM snapshots storage, balances, nonces, code, warm sets, and logs. If Call B reverts, only Call B's changes unwind — Call A continues intact.
  1. storage (persistent): Holds the live slot values modified by SSTORE. Restored on frame revert; survives when a transaction commits (if backed by a host, writes pass through to host.setStorage()).
  2. original_storage: Records each slot's value at the start of the transaction on first write. Never overwritten mid-transaction — SSTORE needs (original, current, new) to compute EIP-2200 / EIP-3529 gas costs and refunds accurately.
  3. transient (EIP-1153, Cancun+): Driven by TLOAD (0x5c) and TSTORE (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:

EVM Gas Mechanics (Berlin → Osaka)interactive
Cold Account Access
2,600 gas

First BALANCE, EXTCODESIZE, CALL, or STATICCALL to an unwarmed address.

Cold Storage Read
2,100 gas

First SLOAD (or cold surcharge on SSTORE) for a slot not in the EIP-2930 access list.

Warm Access / TLOAD
100 gas

Every subsequent read of a warm address/slot, pre-warmed precompiles, or EIP-1153 TLOAD/TSTORE.

All figures match 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); // 42

Notice 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.