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

increase state coverage, delete unused functions; #715

Merged
merged 1 commit into from
Feb 7, 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
85 changes: 0 additions & 85 deletions cairo/ethereum/cancun/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -823,91 +823,6 @@ func close_transaction{
return ();
}

func copy_storage_tries_recursive{
range_check_ptr,
poseidon_ptr: PoseidonBuiltin*,
new_storage_tries: MappingTupleAddressBytes32U256,
}(dict_start: TupleAddressBytes32U256DictAccess*, dict_end: TupleAddressBytes32U256DictAccess*) {
alloc_locals;
// Base case: if start == end, return
if (dict_start == dict_end) {
return ();
}

// Get the current entry
let key = [dict_start].key;
let trie_ptr = [dict_start].new_value;

// Copy the trie
tempvar trie_to_copy = TrieAddressOptionalAccount(
cast(trie_ptr.value, TrieAddressOptionalAccountStruct*)
);
let copied_trie = copy_TrieAddressOptionalAccount{trie=trie_to_copy}();

// Write to new storage tries mapping
let new_storage_trie_ptr = cast(new_storage_tries.value.dict_ptr, DictAccess*);
hashdict_write{poseidon_ptr=poseidon_ptr, dict_ptr=new_storage_trie_ptr}(
1, &key.value, cast(copied_trie.value, felt)
);

// Update new_storage_tries with new dict_ptr
tempvar new_storage_tries = MappingTupleAddressBytes32U256(
new MappingTupleAddressBytes32U256Struct(
dict_ptr_start=new_storage_tries.value.dict_ptr_start,
dict_ptr=cast(new_storage_trie_ptr, TupleAddressBytes32U256DictAccess*),
parent_dict=new_storage_tries.value.parent_dict,
),
);

// Recursive call with next entry
return copy_storage_tries_recursive(
dict_start + TupleAddressBytes32U256DictAccess.SIZE, dict_end
);
}

func copy_transient_storage_tries_recursive{
range_check_ptr,
poseidon_ptr: PoseidonBuiltin*,
new_transient_tries: MappingTupleAddressBytes32U256,
}(dict_start: TupleAddressBytes32U256DictAccess*, dict_end: TupleAddressBytes32U256DictAccess*) {
alloc_locals;

// Base case: if start == end, return
if (dict_start == dict_end) {
return ();
}

// Get the current entry
let key = [dict_start].key;
let trie_ptr = [dict_start].new_value;

// Copy the trie
let trie_to_copy = TrieTupleAddressBytes32U256(
cast(trie_ptr.value, TrieTupleAddressBytes32U256Struct*)
);
let copied_trie = copy_TrieTupleAddressBytes32U256{trie=trie_to_copy}();

// Write to new transient storage tries mapping
let new_transient_trie_ptr = cast(new_transient_tries.value.dict_ptr, DictAccess*);
hashdict_write{poseidon_ptr=poseidon_ptr, dict_ptr=new_transient_trie_ptr}(
1, &key.value, cast(copied_trie.value, felt)
);

// Update new_transient_tries with new dict_ptr
tempvar new_transient_tries = MappingTupleAddressBytes32U256(
new MappingTupleAddressBytes32U256Struct(
dict_ptr_start=new_transient_tries.value.dict_ptr_start,
dict_ptr=cast(new_transient_trie_ptr, TupleAddressBytes32U256DictAccess*),
parent_dict=new_transient_tries.value.parent_dict,
),
);

// Recursive call with next entry
return copy_transient_storage_tries_recursive(
dict_start + TupleAddressBytes32U256DictAccess.SIZE, dict_end
);
}

func set_code{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Address, code: Bytes) {
// Get the current account
let account = get_account(address);
Expand Down
15 changes: 14 additions & 1 deletion cairo/tests/ethereum/cancun/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional

import pytest
from ethereum.cancun.fork_types import Account, Address
from ethereum.cancun.fork_types import EMPTY_ACCOUNT, Account, Address
from ethereum.cancun.state import (
account_exists,
account_exists_and_is_empty,
Expand Down Expand Up @@ -383,6 +383,19 @@ def test_destroy_touched_empty_accounts(self, cairo_run, data):
destroy_touched_empty_accounts(state, touched_accounts)
assert state_cairo == state

@given(data=touched_accounts_strategy(), address=...)
def test_destroy_touched_empty_accounts_with_empty_account(
self, cairo_run, data, address: Address
):
state, touched_accounts = data
touched_accounts.add(address)
set_account(state, address, EMPTY_ACCOUNT)
state_cairo = cairo_run(
"destroy_touched_empty_accounts", state, touched_accounts
)
destroy_touched_empty_accounts(state, touched_accounts)
assert state_cairo == state


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