Skip to content

Commit

Permalink
feat: tload
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Jan 16, 2025
1 parent bbc2428 commit 2967a72
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
39 changes: 38 additions & 1 deletion cairo/ethereum/cancun/vm/instructions/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from ethereum.cancun.vm.stack import pop, push
from ethereum.cancun.vm import Evm, EvmImpl, Environment, EnvImpl
from ethereum.cancun.vm.exceptions import ExceptionalHalt
from ethereum.cancun.vm.gas import charge_gas, GasConstants
from ethereum.cancun.state import get_storage
from ethereum.cancun.state import get_storage, get_transient_storage
from ethereum.cancun.fork_types import (
SetTupleAddressBytes32,
SetTupleAddressBytes32DictAccess,
Expand Down Expand Up @@ -95,3 +95,40 @@ func sload{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, poseidon_ptr: Poseidon
let ok = cast(0, ExceptionalHalt*);
return ok;
}

// @notice Loads to the stack the value corresponding to a certain key from the
// transient storage of the current account.
func tload{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, poseidon_ptr: PoseidonBuiltin*, evm: Evm}(
) -> ExceptionalHalt* {
alloc_locals;
// STACK
let stack = evm.value.stack;
with stack {
let (key, err) = pop();
if (cast(err, felt) != 0) {
return err;
}
}

// GAS
let err = charge_gas(Uint(GasConstants.GAS_WARM_ACCESS));
if (cast(err, felt) != 0) {
return err;
}

// OPERATION
let transient_storage = evm.value.env.value.transient_storage;
let key_bytes32 = U256_to_be_bytes(key);
let value = get_transient_storage{transient_storage=transient_storage}(
evm.value.message.value.current_target, Bytes32(key.value)
);
let err = push{stack=stack}(value);
if (cast(err, felt) != 0) {
return err;
}

// PROGRAM COUNTER
EvmImpl.set_pc_stack(Uint(evm.value.pc.value + 1), stack);
let ok = cast(0, ExceptionalHalt*);
return ok;
}
14 changes: 13 additions & 1 deletion cairo/tests/ethereum/cancun/vm/instructions/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from hypothesis import strategies as st
from hypothesis.strategies import composite

from ethereum.cancun.vm.instructions.storage import sload
from ethereum.cancun.vm.instructions.storage import sload, tload
from tests.utils.args_gen import Evm
from tests.utils.evm_builder import EvmBuilder
from tests.utils.strategies import MAX_STORAGE_KEY_SET_SIZE
Expand Down Expand Up @@ -46,3 +46,15 @@ def test_sload(self, cairo_run, evm: Evm):

sload(evm)
assert evm == cairo_evm

@given(evm=evm_with_accessed_storage_keys())
def test_tload(self, cairo_run, evm: Evm):
try:
cairo_evm = cairo_run("tload", evm)
except Exception as cairo_error:
with pytest.raises(type(cairo_error)):
tload(evm)
return

tload(evm)
assert evm == cairo_evm

0 comments on commit 2967a72

Please sign in to comment.