From f5279250b2a4751600eb75875dc7966255706370 Mon Sep 17 00:00:00 2001 From: Elias Tazartes Date: Thu, 16 Jan 2025 13:27:37 +0100 Subject: [PATCH] Revert "feat: set_account (#437)" This reverts commit 96056069c91659c9be10f8c3951b1f830a30c4e0. --- cairo/ethereum/cancun/fork_types.cairo | 6 +-- cairo/ethereum/cancun/state.cairo | 52 +++++++---------------- cairo/ethereum/cancun/trie.cairo | 38 ++++++++--------- cairo/tests/ethereum/cancun/test_state.py | 12 +----- cairo/tests/ethereum/cancun/test_trie.py | 14 +++--- cairo/tests/test_serde.py | 1 - cairo/tests/utils/args_gen.py | 7 ++- cairo/tests/utils/serde.py | 11 ----- 8 files changed, 44 insertions(+), 97 deletions(-) diff --git a/cairo/ethereum/cancun/fork_types.cairo b/cairo/ethereum/cancun/fork_types.cairo index bf3f9f39..f1aefe05 100644 --- a/cairo/ethereum/cancun/fork_types.cairo +++ b/cairo/ethereum/cancun/fork_types.cairo @@ -85,10 +85,6 @@ struct Account { value: AccountStruct*, } -struct OptionalAccount { - value: AccountStruct*, -} - struct AddressAccountDictAccess { key: Address, prev_value: Account, @@ -134,7 +130,7 @@ func EMPTY_ACCOUNT() -> Account { return account; } -func Account__eq__(a: OptionalAccount, b: OptionalAccount) -> bool { +func Account__eq__(a: Account, b: Account) -> bool { if (cast(a.value, felt) == 0) { let b_is_none = is_zero(cast(b.value, felt)); let res = bool(b_is_none); diff --git a/cairo/ethereum/cancun/state.cairo b/cairo/ethereum/cancun/state.cairo index cfe049f2..983e87e7 100644 --- a/cairo/ethereum/cancun/state.cairo +++ b/cairo/ethereum/cancun/state.cairo @@ -7,7 +7,6 @@ from starkware.cairo.common.math import assert_not_zero from ethereum.cancun.fork_types import ( Address, Account, - OptionalAccount, MappingAddressAccount, SetAddress, EMPTY_ACCOUNT, @@ -17,14 +16,13 @@ from ethereum.cancun.fork_types import ( ) from ethereum.cancun.trie import ( TrieBytes32U256, - TrieAddressOptionalAccount, - trie_get_TrieAddressOptionalAccount, - trie_set_TrieAddressOptionalAccount, + TrieAddressAccount, + trie_get_TrieAddressAccount, trie_get_TrieBytes32U256, trie_set_TrieBytes32U256, AccountStruct, TrieBytes32U256Struct, - TrieAddressOptionalAccountStruct, + TrieAddressAccountStruct, ) from ethereum_types.bytes import Bytes, Bytes32 from ethereum_types.numeric import U256, U256Struct, Bool, bool @@ -49,22 +47,22 @@ struct MappingAddressTrieBytes32U256 { value: MappingAddressTrieBytes32U256Struct*, } -struct TupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256Struct { - trie_address_account: TrieAddressOptionalAccount, +struct TupleTrieAddressAccountMappingAddressTrieBytes32U256Struct { + trie_address_account: TrieAddressAccount, mapping_address_trie: MappingAddressTrieBytes32U256, } -struct TupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256 { - value: TupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256Struct*, +struct TupleTrieAddressAccountMappingAddressTrieBytes32U256 { + value: TupleTrieAddressAccountMappingAddressTrieBytes32U256Struct*, } -struct ListTupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256Struct { - data: TupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256*, +struct ListTupleTrieAddressAccountMappingAddressTrieBytes32U256Struct { + data: TupleTrieAddressAccountMappingAddressTrieBytes32U256*, len: felt, } -struct ListTupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256 { - value: ListTupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256Struct*, +struct ListTupleTrieAddressAccountMappingAddressTrieBytes32U256 { + value: ListTupleTrieAddressAccountMappingAddressTrieBytes32U256Struct*, } struct TransientStorageSnapshotsStruct { @@ -86,9 +84,9 @@ struct TransientStorage { } struct StateStruct { - _main_trie: TrieAddressOptionalAccount, + _main_trie: TrieAddressAccount, _storage_tries: MappingAddressTrieBytes32U256, - _snapshots: ListTupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256, + _snapshots: ListTupleTrieAddressAccountMappingAddressTrieBytes32U256, created_accounts: SetAddress, } @@ -96,12 +94,13 @@ struct State { value: StateStruct*, } +using OptionalAccount = Account; func get_account_optional{poseidon_ptr: PoseidonBuiltin*, state: State}( address: Address ) -> OptionalAccount { let trie = state.value._main_trie; with trie { - let account = trie_get_TrieAddressOptionalAccount(address); + let account = trie_get_TrieAddressAccount(address); } return account; @@ -115,26 +114,7 @@ func get_account{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Address) return empty_account; } - tempvar res = Account(account.value); - return res; -} - -func set_account{poseidon_ptr: PoseidonBuiltin*, state: State}( - address: Address, account: OptionalAccount -) { - let trie = state.value._main_trie; - with trie { - trie_set_TrieAddressOptionalAccount(address, account); - } - tempvar state = State( - new StateStruct( - _main_trie=trie, - _storage_tries=state.value._storage_tries, - _snapshots=state.value._snapshots, - created_accounts=state.value.created_accounts, - ), - ); - return (); + return account; } func get_storage{poseidon_ptr: PoseidonBuiltin*, state: State}( diff --git a/cairo/ethereum/cancun/trie.cairo b/cairo/ethereum/cancun/trie.cairo index 395f952c..c1657819 100644 --- a/cairo/ethereum/cancun/trie.cairo +++ b/cairo/ethereum/cancun/trie.cairo @@ -34,7 +34,6 @@ from ethereum.cancun.fork_types import ( Account__eq__, AccountStruct, Address, - OptionalAccount, Bytes32U256DictAccess, MappingAddressAccount, MappingAddressAccountStruct, @@ -168,14 +167,14 @@ struct Node { value: NodeEnum*, } -struct TrieAddressOptionalAccountStruct { +struct TrieAddressAccountStruct { secured: bool, - default: OptionalAccount, + default: Account, _data: MappingAddressAccount, } -struct TrieAddressOptionalAccount { - value: TrieAddressOptionalAccountStruct*, +struct TrieAddressAccount { + value: TrieAddressAccountStruct*, } struct TrieBytes32U256Struct { @@ -335,8 +334,7 @@ func encode_node{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: Kecc // @notice Copies the trie to a new segment. // @dev This function simply creates a new segment for the new dict and associates it with the // dict_tracker of the source dict. -func copy_TrieAddressOptionalAccount{range_check_ptr, trie: TrieAddressOptionalAccount}( - ) -> TrieAddressOptionalAccount { +func copy_trieAddressAccount{range_check_ptr, trie: TrieAddressAccount}() -> TrieAddressAccount { alloc_locals; // TODO: soundness // We need to ensure it is sound when finalizing that copy. @@ -357,8 +355,8 @@ func copy_TrieAddressOptionalAccount{range_check_ptr, trie: TrieAddressOptionalA ids.new_dict_ptr = __dict_manager.new_dict(segments, copied_data) %} - tempvar res = TrieAddressOptionalAccount( - new TrieAddressOptionalAccountStruct( + tempvar res = TrieAddressAccount( + new TrieAddressAccountStruct( trie.value.secured, trie.value.default, MappingAddressAccount( @@ -395,9 +393,9 @@ func copy_trieBytes32U256{range_check_ptr, trie: TrieBytes32U256}() -> TrieBytes return res; } -func trie_get_TrieAddressOptionalAccount{ - poseidon_ptr: PoseidonBuiltin*, trie: TrieAddressOptionalAccount -}(key: Address) -> OptionalAccount { +func trie_get_TrieAddressAccount{poseidon_ptr: PoseidonBuiltin*, trie: TrieAddressAccount}( + key: Address +) -> Account { alloc_locals; let dict_ptr = cast(trie.value._data.value.dict_ptr, DictAccess*); @@ -414,10 +412,10 @@ func trie_get_TrieAddressOptionalAccount{ trie.value._data.value.dict_ptr_start, new_dict_ptr, original_mapping ), ); - tempvar trie = TrieAddressOptionalAccount( - new TrieAddressOptionalAccountStruct(trie.value.secured, trie.value.default, mapping) + tempvar trie = TrieAddressAccount( + new TrieAddressAccountStruct(trie.value.secured, trie.value.default, mapping) ); - tempvar res = OptionalAccount(cast(pointer, AccountStruct*)); + tempvar res = Account(cast(pointer, AccountStruct*)); return res; } @@ -443,9 +441,9 @@ func trie_get_TrieBytes32U256{poseidon_ptr: PoseidonBuiltin*, trie: TrieBytes32U return res; } -func trie_set_TrieAddressOptionalAccount{ - poseidon_ptr: PoseidonBuiltin*, trie: TrieAddressOptionalAccount -}(key: Address, value: OptionalAccount) { +func trie_set_TrieAddressAccount{poseidon_ptr: PoseidonBuiltin*, trie: TrieAddressAccount}( + key: Address, value: Account +) { let dict_ptr_start = cast(trie.value._data.value.dict_ptr_start, DictAccess*); let dict_ptr = cast(trie.value._data.value.dict_ptr, DictAccess*); @@ -475,8 +473,8 @@ func trie_set_TrieAddressOptionalAccount{ trie.value._data.value.original_mapping, ), ); - tempvar trie = TrieAddressOptionalAccount( - new TrieAddressOptionalAccountStruct(trie.value.secured, trie.value.default, mapping) + tempvar trie = TrieAddressAccount( + new TrieAddressAccountStruct(trie.value.secured, trie.value.default, mapping) ); return (); } diff --git a/cairo/tests/ethereum/cancun/test_state.py b/cairo/tests/ethereum/cancun/test_state.py index 9e7104b3..f2c1acff 100644 --- a/cairo/tests/ethereum/cancun/test_state.py +++ b/cairo/tests/ethereum/cancun/test_state.py @@ -1,5 +1,3 @@ -from typing import Optional - import pytest from ethereum_types.bytes import Bytes32 from ethereum_types.numeric import U256 @@ -7,7 +5,7 @@ from hypothesis import strategies as st from hypothesis.strategies import composite -from ethereum.cancun.fork_types import Account, Address +from ethereum.cancun.fork_types import Address from ethereum.cancun.state import ( account_exists, account_has_code_or_nonce, @@ -16,7 +14,6 @@ get_storage, get_transient_storage, is_account_empty, - set_account, set_storage, set_transient_storage, ) @@ -70,13 +67,6 @@ def test_get_account_optional(self, cairo_run, data): assert result_cairo == get_account_optional(state, address) assert state_cairo == state - @given(data=state_and_address_and_optional_key(), account=...) - def test_set_account(self, cairo_run, data, account: Optional[Account]): - state, address = data - state_cairo = cairo_run("set_account", state, address, account) - set_account(state, address, account) - assert state_cairo == state - @given(data=state_and_address_and_optional_key()) def test_account_has_code_or_nonce(self, cairo_run, data): state, address = data diff --git a/cairo/tests/ethereum/cancun/test_trie.py b/cairo/tests/ethereum/cancun/test_trie.py index caca4aa8..82b5e674 100644 --- a/cairo/tests/ethereum/cancun/test_trie.py +++ b/cairo/tests/ethereum/cancun/test_trie.py @@ -155,12 +155,10 @@ def test_patricialize(self, cairo_run, obj: Mapping[Bytes, Bytes]): class TestTrieOperations: @given(trie=..., key=...) - def test_trie_get_TrieAddressOptionalAccount( + def test_trie_get_TrieAddressAccount( self, cairo_run, trie: Trie[Address, Optional[Account]], key: Address ): - [trie_cairo, result_cairo] = cairo_run( - "trie_get_TrieAddressOptionalAccount", trie, key - ) + [trie_cairo, result_cairo] = cairo_run("trie_get_TrieAddressAccount", trie, key) result_py = trie_get(trie, key) assert result_cairo == result_py assert trie_cairo == trie @@ -175,14 +173,14 @@ def test_trie_get_TrieBytes32U256( assert trie_cairo == trie @given(trie=..., key=..., value=...) - def test_trie_set_TrieAddressOptionalAccount( + def test_trie_set_TrieAddressAccount( self, cairo_run, trie: Trie[Address, Optional[Account]], key: Address, value: Account, ): - cairo_trie = cairo_run("trie_set_TrieAddressOptionalAccount", trie, key, value) + cairo_trie = cairo_run("trie_set_TrieAddressAccount", trie, key, value) trie_set(trie, key, value) assert cairo_trie == trie @@ -198,9 +196,7 @@ def test_trie_set_TrieBytes32U256( def test_copy_trie_AddressAccount( self, cairo_run, trie: Trie[Address, Optional[Account]] ): - [original_trie, copied_trie] = cairo_run( - "copy_TrieAddressOptionalAccount", trie - ) + [original_trie, copied_trie] = cairo_run("copy_trieAddressAccount", trie) trie_copy_py = copy_trie(trie) assert original_trie == trie assert copied_trie == trie_copy_py diff --git a/cairo/tests/test_serde.py b/cairo/tests/test_serde.py index 4bc37557..3cff1a91 100644 --- a/cairo/tests/test_serde.py +++ b/cairo/tests/test_serde.py @@ -203,7 +203,6 @@ def test_type( Address, Root, Account, - Optional[Account], Bloom, VersionedHash, Tuple[VersionedHash, ...], diff --git a/cairo/tests/utils/args_gen.py b/cairo/tests/utils/args_gen.py index 9ad28e82..f668a9e9 100644 --- a/cairo/tests/utils/args_gen.py +++ b/cairo/tests/utils/args_gen.py @@ -266,7 +266,6 @@ def __eq__(self, other): ("ethereum", "cancun", "fork_types", "SetAddress"): Set[Address], ("ethereum", "cancun", "fork_types", "Root"): Root, ("ethereum", "cancun", "fork_types", "Account"): Account, - ("ethereum", "cancun", "fork_types", "OptionalAccount"): Optional[Account], ("ethereum", "cancun", "fork_types", "Bloom"): Bloom, ("ethereum", "cancun", "fork_types", "VersionedHash"): VersionedHash, ("ethereum", "cancun", "fork_types", "TupleVersionedHash"): Tuple[ @@ -312,7 +311,7 @@ def __eq__(self, other): ("ethereum", "cancun", "trie", "BranchNode"): BranchNode, ("ethereum", "cancun", "trie", "InternalNode"): InternalNode, ("ethereum", "cancun", "trie", "Node"): Node, - ("ethereum", "cancun", "trie", "TrieAddressOptionalAccount"): Trie[ + ("ethereum", "cancun", "trie", "TrieAddressAccount"): Trie[ Address, Optional[Account] ], ("ethereum", "cancun", "trie", "TrieBytes32U256"): Trie[Bytes32, U256], @@ -336,13 +335,13 @@ def __eq__(self, other): "ethereum", "cancun", "state", - "TupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256", + "TupleTrieAddressAccountMappingAddressTrieBytes32U256", ): Tuple[Trie[Address, Optional[Account]], Mapping[Address, Trie[Bytes32, U256]]], ( "ethereum", "cancun", "state", - "ListTupleTrieAddressOptionalAccountMappingAddressTrieBytes32U256", + "ListTupleTrieAddressAccountMappingAddressTrieBytes32U256", ): List[ Tuple[Trie[Address, Optional[Account]], Mapping[Address, Trie[Bytes32, U256]]] ], diff --git a/cairo/tests/utils/serde.py b/cairo/tests/utils/serde.py index 7c941d84..b118eff4 100644 --- a/cairo/tests/utils/serde.py +++ b/cairo/tests/utils/serde.py @@ -160,16 +160,6 @@ def serialize_type(self, path: Tuple[str, ...], ptr) -> Any: python_cls, *annotations = get_args(python_cls) origin_cls = get_origin(python_cls) - # arg_type = Optional[T] <=> arg_type_origin = Union[T, None] - if origin_cls is Union and get_args(python_cls)[1] is type(None): - # Get the value pointer: if it's zero, return None. - # Otherwise, consider this the non-optional type: - value_ptr = self.serialize_pointers(path, ptr)["value"] - if value_ptr is None: - return None - python_cls = get_args(python_cls)[0] - origin_cls = get_origin(python_cls) - if origin_cls is Union: value_ptr = self.serialize_pointers(path, ptr)["value"] if value_ptr is None: @@ -186,7 +176,6 @@ def serialize_type(self, path: Tuple[str, ...], ptr) -> Any: if value != 0 and value is not None } if len(variant_keys) != 1: - breakpoint() raise ValueError( f"Expected 1 item only to be relocatable in enum, got {len(variant_keys)}" )