Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: touch account #488

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading