diff --git a/cairo/ethereum/cancun/vm/instructions/storage.cairo b/cairo/ethereum/cancun/vm/instructions/storage.cairo index 42aa6f37..e7163562 100644 --- a/cairo/ethereum/cancun/vm/instructions/storage.cairo +++ b/cairo/ethereum/cancun/vm/instructions/storage.cairo @@ -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, @@ -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; +} diff --git a/cairo/tests/ethereum/cancun/vm/instructions/test_storage.py b/cairo/tests/ethereum/cancun/vm/instructions/test_storage.py index dc212422..e9f3f83d 100644 --- a/cairo/tests/ethereum/cancun/vm/instructions/test_storage.py +++ b/cairo/tests/ethereum/cancun/vm/instructions/test_storage.py @@ -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 @@ -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