From a7b02e2f205e0a36ef476a95d332df871c5f9e8d Mon Sep 17 00:00:00 2001 From: Elias Tazartes Date: Fri, 17 Jan 2025 17:55:44 +0100 Subject: [PATCH] feat: touch account --- cairo/ethereum/cancun/state.cairo | 34 +++++++++++++++-------- cairo/tests/ethereum/cancun/test_state.py | 8 ++++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/cairo/ethereum/cancun/state.cairo b/cairo/ethereum/cancun/state.cairo index 12ef3a52..24d1a275 100644 --- a/cairo/ethereum/cancun/state.cairo +++ b/cairo/ethereum/cancun/state.cairo @@ -636,6 +636,17 @@ func set_transient_storage{poseidon_ptr: PoseidonBuiltin*, transient_storage: Tr return (); } +func account_exists{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Address) -> bool { + let account = get_account_optional(address); + + if (cast(account.value, felt) == 0) { + tempvar result = bool(0); + return result; + } + tempvar result = bool(1); + return result; +} + func account_has_code_or_nonce{poseidon_ptr: PoseidonBuiltin*, state: State}( address: Address ) -> bool { @@ -655,17 +666,6 @@ func account_has_code_or_nonce{poseidon_ptr: PoseidonBuiltin*, state: State}( return res; } -func account_exists{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Address) -> bool { - let account = get_account_optional(address); - - if (cast(account.value, felt) == 0) { - tempvar result = bool(0); - return result; - } - tempvar result = bool(1); - return result; -} - func is_account_empty{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Address) -> bool { // Get the account at the address let account = get_account(address); @@ -983,3 +983,15 @@ func set_account_balance{poseidon_ptr: PoseidonBuiltin*, state: State}( set_account(address, new_account); return (); } + +func touch_account{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Address) { + let _account_exists = account_exists(address); + if (_account_exists.value != 0) { + return (); + } + + let _empty_account = EMPTY_ACCOUNT(); + let empty_account = OptionalAccount(_empty_account.value); + set_account(address, empty_account); + return (); +} diff --git a/cairo/tests/ethereum/cancun/test_state.py b/cairo/tests/ethereum/cancun/test_state.py index 24b6c126..15916b93 100644 --- a/cairo/tests/ethereum/cancun/test_state.py +++ b/cairo/tests/ethereum/cancun/test_state.py @@ -28,6 +28,7 @@ set_code, set_storage, set_transient_storage, + touch_account, ) from tests.utils.args_gen import State, TransientStorage from tests.utils.strategies import address, bytes32, code, state, transient_storage @@ -197,6 +198,13 @@ def test_increment_nonce(self, cairo_run, data): increment_nonce(state, address) assert state_cairo == state + @given(data=state_and_address_and_optional_key()) + def test_touch_account(self, cairo_run, data): + state, address = data + state_cairo = cairo_run("touch_account", state, address) + touch_account(state, address) + assert state_cairo == state + class TestStateStorage: @given(state_and_address_and_optional_key(key_strategy=bytes32))