Skip to content

Commit f736706

Browse files
committed
chore: adds extra modifier validation; adds unit tests
1 parent c3462e0 commit f736706

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

packages/testing/src/execution_testing/test_types/block_access_list/modifiers.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ def _modify_field_value(
7171
transaction.
7272
"""
7373
found_address = False
74+
found_index = False
7475

7576
def transform(bal: BlockAccessList) -> BlockAccessList:
76-
nonlocal found_address
77+
nonlocal found_address, found_index
7778
new_root = []
7879
for account_change in bal.root:
7980
if account_change.address == address:
@@ -84,15 +85,18 @@ def transform(bal: BlockAccessList) -> BlockAccessList:
8485
if changes:
8586
if nested and slot is not None:
8687
# nested structure (storage)
88+
found_slot = False
8789
for storage_slot in changes:
8890
if storage_slot.slot == slot:
91+
found_slot = True
8992
for j, change in enumerate(
9093
storage_slot.slot_changes
9194
):
9295
if (
9396
change.block_access_index
9497
== block_access_index
9598
):
99+
found_index = True
96100
kwargs = {
97101
"block_access_index": (
98102
block_access_index
@@ -104,10 +108,16 @@ def transform(bal: BlockAccessList) -> BlockAccessList:
104108
)
105109
break
106110
break
111+
if not found_slot:
112+
raise ValueError(
113+
f"Storage slot {slot} not found in "
114+
f"storage_changes of account {address}"
115+
)
107116
else:
108117
# flat structure (nonce, balance, code)
109118
for i, change in enumerate(changes):
110119
if change.block_access_index == block_access_index:
120+
found_index = True
111121
kwargs = {
112122
"block_access_index": block_access_index,
113123
value_field: new_value,
@@ -120,10 +130,14 @@ def transform(bal: BlockAccessList) -> BlockAccessList:
120130
new_root.append(account_change)
121131

122132
if not found_address:
123-
# sanity check that we actually found the address
124133
raise ValueError(
125134
f"Address {address} not found in BAL to modify {field_name}"
126135
)
136+
if not found_index:
137+
raise ValueError(
138+
f"Block access index {block_access_index} not found in "
139+
f"{field_name} of account {address}"
140+
)
127141

128142
return BlockAccessList(root=new_root)
129143

@@ -721,8 +735,14 @@ def reorder_accounts(
721735
"""Reorder accounts according to the provided index list."""
722736

723737
def transform(bal: BlockAccessList) -> BlockAccessList:
724-
if len(indices) != len(bal.root):
738+
n = len(bal.root)
739+
if len(indices) != n:
725740
raise ValueError("Index list length must match number of accounts")
741+
if sorted(indices) != list(range(n)):
742+
raise ValueError(
743+
f"Indices must be a valid permutation of 0..{n - 1}, "
744+
f"got {indices}"
745+
)
726746
new_root = [bal.root[i] for i in indices]
727747
return BlockAccessList(root=new_root)
728748

packages/testing/src/execution_testing/test_types/tests/test_block_access_list_modifiers.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
duplicate_storage_read,
2222
duplicate_storage_slot,
2323
insert_storage_read,
24+
modify_balance,
25+
modify_code,
26+
modify_nonce,
27+
modify_storage,
28+
reorder_accounts,
2429
)
2530

2631
ALICE = Address(0xA)
@@ -217,3 +222,57 @@ def test_insert_storage_read_missing_address_raises() -> None:
217222
bal = BlockAccessList([BalAccountChange(address=ALICE, nonce_changes=[])])
218223
with pytest.raises(ValueError, match="not found"):
219224
insert_storage_read(CONTRACT, 1)(bal)
225+
226+
227+
def test_modify_nonce_missing_index_raises(
228+
sample_bal: BlockAccessList,
229+
) -> None:
230+
"""Raise when the block_access_index is absent from nonce_changes."""
231+
with pytest.raises(ValueError, match="not found"):
232+
modify_nonce(ALICE, 99, 42)(sample_bal)
233+
234+
235+
def test_modify_balance_missing_index_raises(
236+
sample_bal: BlockAccessList,
237+
) -> None:
238+
"""Raise when the block_access_index is absent from balance_changes."""
239+
with pytest.raises(ValueError, match="not found"):
240+
modify_balance(ALICE, 99, 9999)(sample_bal)
241+
242+
243+
def test_modify_code_missing_index_raises(sample_bal: BlockAccessList) -> None:
244+
"""Raise when the block_access_index is absent from code_changes."""
245+
with pytest.raises(ValueError, match="not found"):
246+
modify_code(ALICE, 99, b"\x00")(sample_bal)
247+
248+
249+
def test_modify_storage_missing_index_raises(
250+
sample_bal: BlockAccessList,
251+
) -> None:
252+
"""Raise when block_access_index is absent within the storage slot."""
253+
with pytest.raises(ValueError, match="not found"):
254+
modify_storage(CONTRACT, 99, 1, 0xFF)(sample_bal)
255+
256+
257+
def test_modify_storage_missing_slot_raises(
258+
sample_bal: BlockAccessList,
259+
) -> None:
260+
"""Raise when the storage slot itself is absent."""
261+
with pytest.raises(ValueError, match="not found"):
262+
modify_storage(CONTRACT, 1, 99, 0xFF)(sample_bal)
263+
264+
265+
def test_reorder_accounts_duplicate_index_raises(
266+
sample_bal: BlockAccessList,
267+
) -> None:
268+
"""Raise when indices contain duplicates (not a valid permutation)."""
269+
with pytest.raises(ValueError, match="valid permutation"):
270+
reorder_accounts([0, 0])(sample_bal)
271+
272+
273+
def test_reorder_accounts_out_of_range_raises(
274+
sample_bal: BlockAccessList,
275+
) -> None:
276+
"""Raise when indices are not a valid permutation (skipped index)."""
277+
with pytest.raises(ValueError, match="valid permutation"):
278+
reorder_accounts([0, 2])(sample_bal)

0 commit comments

Comments
 (0)