Skip to content

Commit

Permalink
feat: account_exists_and_is_empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikix committed Jan 16, 2025
1 parent 5673565 commit 2806033
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cairo/ethereum/cancun/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,38 @@ func is_account_empty{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Add
tempvar res = bool(1);
return res;
}

func account_exists_and_is_empty{poseidon_ptr: PoseidonBuiltin*, state: State}(
address: Address
) -> bool {
// Get the account at the address
let account = get_account_optional(address);
if (cast(account.value, felt) == 0) {
tempvar res = bool(0);
return res;
}

// 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;
}
10 changes: 10 additions & 0 deletions cairo/tests/ethereum/cancun/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ethereum.cancun.fork_types import Account, Address
from ethereum.cancun.state import (
account_exists,
account_exists_and_is_empty,
account_has_code_or_nonce,
get_account,
get_account_optional,
Expand Down Expand Up @@ -100,6 +101,15 @@ def test_is_account_empty(self, cairo_run, data):
assert result_cairo == is_account_empty(state, address)
assert state_cairo == state

@given(data=state_and_address_and_optional_key())
def test_account_exists_and_is_empty(self, cairo_run, data):
state, address = data
state_cairo, result_cairo = cairo_run(
"account_exists_and_is_empty", state, address
)
assert result_cairo == account_exists_and_is_empty(state, address)
assert state_cairo == state


class TestStateStorage:
@given(data=state_and_address_and_optional_key(key_strategy=bytes32))
Expand Down

0 comments on commit 2806033

Please sign in to comment.