Skip to content

Commit

Permalink
empty state strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Jan 17, 2025
1 parent e28f085 commit f580eef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cairo/tests/ethereum/cancun/vm/instructions/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
prev_randao,
timestamp,
)
from tests.utils.args_gen import Environment, Evm, State, TransientStorage
from tests.utils.args_gen import Environment, Evm, TransientStorage
from tests.utils.evm_builder import EvmBuilder, address_zero
from tests.utils.strategies import (
BLOCK_HASHES_LIST,
address,
bytes32,
empty_state,
uint,
uint64,
uint256,
Expand Down Expand Up @@ -53,7 +54,7 @@
gas_price=st.just(Uint(0)),
time=uint256,
prev_randao=bytes32,
state=st.just(State()),
state=empty_state,
chain_id=uint64,
excess_blob_gas=st.just(U64(0)),
blob_versioned_hashes=st.just(()),
Expand Down
1 change: 1 addition & 0 deletions cairo/tests/utils/args_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ def _gen_arg(
# In case of a Trie, we need to put the original_storage_tries (state._snapshots[0][1]) in a
# special field of the State. We want easy access / overrides to this specific snapshot in
# Cairo, as each `sstore` performs an update of this original trie.
# The state strategy should always generate a valid snapshots[0], corresponding to the initial state.
snapshots_ptr = segments.memory.get(data[2])
snapshots0_ptr = segments.memory.get(snapshots_ptr)
snapshots0_storage_tries_ptr = segments.memory.get(snapshots0_ptr + 1)
Expand Down
5 changes: 3 additions & 2 deletions cairo/tests/utils/evm_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from ethereum_types.numeric import U64, U256, Bytes32, Uint
from hypothesis import strategies as st

from ethereum.cancun.state import State, TransientStorage
from ethereum.cancun.state import TransientStorage
from ethereum.exceptions import EthereumException
from tests.utils.args_gen import Environment, Evm, Message, Stack
from tests.utils.strategies import (
Memory,
empty_state,
environment_lite,
gas_left,
memory_lite,
Expand All @@ -31,7 +32,7 @@
gas_price=st.just(Uint(0)),
time=st.just(U256(0)),
prev_randao=st.just(Bytes32(b"\x00" * 32)),
state=st.just(State()),
state=empty_state,
chain_id=st.just(U64(0)),
excess_blob_gas=st.just(U64(0)),
blob_versioned_hashes=st.just(()),
Expand Down
27 changes: 27 additions & 0 deletions cairo/tests/utils/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,33 @@ def tuple_strategy(thing):


# Fork
# A strategy for an empty state - the tries have no data.
empty_state = st.builds(
State,
_main_trie=st.builds(
Trie[Address, Optional[Account]],
secured=st.just(True),
default=st.none(),
_data=st.just({}),
),
_storage_tries=st.just({}),
_snapshots=st.just([]),
created_accounts=st.just(set()),
).map(
lambda state: State(
_main_trie=state._main_trie,
_storage_tries=state._storage_tries,
_snapshots=[
(
copy_trie(state._main_trie),
{addr: copy_trie(trie) for addr, trie in state._storage_tries.items()},
)
],
created_accounts=state.created_accounts,
)
)


state = st.lists(address, max_size=MAX_ADDRESS_SET_SIZE, unique=True).flatmap(
lambda addresses: st.builds(
State,
Expand Down

0 comments on commit f580eef

Please sign in to comment.