From 5a7cfb5063bfa2f34bd2097103d20c7ef81d1be5 Mon Sep 17 00:00:00 2001 From: Oba Date: Wed, 15 Jan 2025 16:34:38 +0100 Subject: [PATCH] feat: is_account_empty --- cairo/ethereum/cancun/state.cairo | 29 +++++++++++++++++++++++ cairo/tests/ethereum/cancun/test_state.py | 8 +++++++ 2 files changed, 37 insertions(+) diff --git a/cairo/ethereum/cancun/state.cairo b/cairo/ethereum/cancun/state.cairo index 5c17ee79..983e87e7 100644 --- a/cairo/ethereum/cancun/state.cairo +++ b/cairo/ethereum/cancun/state.cairo @@ -436,3 +436,32 @@ func account_exists{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Addre 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); + + // Check if nonce is 0, code is empty, and balance is 0 + if (account.value.nonce.value != 0) { + tempvar res = bool(0); + return res; + } + + if (account.value.code.value.len != 0) { + tempvar res = bool(0); + return res; + } + + if (account.value.balance.value.low != 0) { + tempvar res = bool(0); + return res; + } + + if (account.value.balance.value.high != 0) { + tempvar res = bool(0); + return res; + } + + tempvar res = bool(1); + return res; +} diff --git a/cairo/tests/ethereum/cancun/test_state.py b/cairo/tests/ethereum/cancun/test_state.py index 3e41e722..f2c1acff 100644 --- a/cairo/tests/ethereum/cancun/test_state.py +++ b/cairo/tests/ethereum/cancun/test_state.py @@ -13,6 +13,7 @@ get_account_optional, get_storage, get_transient_storage, + is_account_empty, set_storage, set_transient_storage, ) @@ -82,6 +83,13 @@ def test_account_exists(self, cairo_run, data): assert result_cairo == account_exists(state, address) assert state_cairo == state + @given(data=state_and_address_and_optional_key()) + def test_is_account_empty(self, cairo_run, data): + state, address = data + state_cairo, result_cairo = cairo_run("is_account_empty", state, address) + assert result_cairo == is_account_empty(state, address) + assert state_cairo == state + class TestStateStorage: @given(data=state_and_address_and_optional_key(key_strategy=bytes32))