Skip to content

Commit

Permalink
feat: touch account (#488)
Browse files Browse the repository at this point in the history
closes #455
  • Loading branch information
Eikix authored Jan 17, 2025
1 parent 32ad5bf commit b754b72
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
34 changes: 23 additions & 11 deletions cairo/ethereum/cancun/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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 ();
}
8 changes: 8 additions & 0 deletions cairo/tests/ethereum/cancun/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit b754b72

Please sign in to comment.