From 948f255f43deace89b102118860dfe9c911faf33 Mon Sep 17 00:00:00 2001 From: Elias Tazartes <66871571+Eikix@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:02:46 +0100 Subject: [PATCH] feat: is_account_alive (#443) closes #426 --- cairo/ethereum/cancun/state.cairo | 31 +++++++++++++++++++++++ cairo/tests/ethereum/cancun/test_state.py | 8 ++++++ 2 files changed, 39 insertions(+) diff --git a/cairo/ethereum/cancun/state.cairo b/cairo/ethereum/cancun/state.cairo index be450ae4..a6aea5c3 100644 --- a/cairo/ethereum/cancun/state.cairo +++ b/cairo/ethereum/cancun/state.cairo @@ -529,3 +529,34 @@ func account_exists_and_is_empty{poseidon_ptr: PoseidonBuiltin*, state: State}( tempvar res = bool(1); return res; } + +func is_account_alive{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Address) -> bool { + let account = get_account_optional(address); + if (cast(account.value, felt) == 0) { + tempvar res = bool(0); + return res; + } + + if (account.value.nonce.value != 0) { + tempvar res = bool(1); + return res; + } + + if (account.value.code.value.len != 0) { + tempvar res = bool(1); + return res; + } + + if (account.value.balance.value.low != 0) { + tempvar res = bool(1); + return res; + } + + if (account.value.balance.value.high != 0) { + tempvar res = bool(1); + return res; + } + + tempvar res = bool(0); + return res; +} diff --git a/cairo/tests/ethereum/cancun/test_state.py b/cairo/tests/ethereum/cancun/test_state.py index c85990fc..6422abaf 100644 --- a/cairo/tests/ethereum/cancun/test_state.py +++ b/cairo/tests/ethereum/cancun/test_state.py @@ -16,6 +16,7 @@ get_account_optional, get_storage, get_transient_storage, + is_account_alive, is_account_empty, set_account, set_storage, @@ -110,6 +111,13 @@ def test_account_exists_and_is_empty(self, cairo_run, data): assert result_cairo == account_exists_and_is_empty(state, address) assert state_cairo == state + @given(data=state_and_address_and_optional_key()) + def test_is_account_alive(self, cairo_run, data): + state, address = data + state_cairo, result_cairo = cairo_run("is_account_alive", state, address) + assert result_cairo == is_account_alive(state, address) + assert state_cairo == state + class TestStateStorage: @given(data=state_and_address_and_optional_key(key_strategy=bytes32))