Skip to content

Commit

Permalink
refactor deep copy into a util
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikix committed Jan 17, 2025
1 parent b58436f commit e4efe7f
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions cairo/tests/ethereum/cancun/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@
from tests.utils.strategies import address, bytes32, code, state, transient_storage


def _state_deep_copy(state: State) -> State:
return State(
_main_trie=copy_trie(state._main_trie),
_storage_tries={
addr: copy_trie(trie) for addr, trie in state._storage_tries.items()
},
_snapshots=[
(
copy_trie(trie_tuple[0]),
{addr: copy_trie(trie) for addr, trie in trie_tuple[1].items()},
)
for trie_tuple in state._snapshots
],
created_accounts=state.created_accounts,
)


@composite
def state_and_address_and_optional_key(
draw, state_strategy=state, address_strategy=address, key_strategy=None
Expand Down Expand Up @@ -128,20 +145,7 @@ def test_move_ether(
state, sender_address = data
# We need to create a deep copy of the state to avoid mutating the original state
# We can refactor it into a deep_copy State util if we ever need to do this again
python_state = State(
_main_trie=copy_trie(state._main_trie),
_storage_tries={
addr: copy_trie(trie) for addr, trie in state._storage_tries.items()
},
_snapshots=[
(
copy_trie(trie_tuple[0]),
{addr: copy_trie(trie) for addr, trie in trie_tuple[1].items()},
)
for trie_tuple in state._snapshots
],
created_accounts=state.created_accounts,
)
python_state = _state_deep_copy(state)

try:
move_ether(python_state, sender_address, recipient_address, amount)
Expand Down

0 comments on commit e4efe7f

Please sign in to comment.