Skip to content

Commit 49159a8

Browse files
spencer-tbmarioevz
andcommitted
feat(tests): update state gas creation tests with max_code_size case and subcall pattern
Co-authored-by: Mario Vega <marioevz@gmail.com>
1 parent 1de66c6 commit 49159a8

1 file changed

Lines changed: 33 additions & 14 deletions

File tree

tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
(https://eips.ethereum.org/EIPS/eip-8037).
99
"""
1010

11-
from typing import Optional
11+
from typing import Union
1212

1313
import pytest
1414
from execution_testing import (
@@ -138,14 +138,15 @@ def test_create_with_reservoir(
138138
pytest.param(32, id="one_word"),
139139
pytest.param(256, id="small_contract"),
140140
pytest.param(1024, id="medium_contract"),
141-
pytest.param(None, id="over_max_code_size"),
141+
pytest.param("max", id="max_code_size"),
142+
pytest.param("max+1", id="over_max_code_size"),
142143
],
143144
)
144145
@pytest.mark.valid_from("Amsterdam")
145146
def test_code_deposit_state_gas_scales_with_size(
146147
state_test: StateTestFiller,
147148
pre: Alloc,
148-
code_size: Optional[int],
149+
code_size: Union[int, str],
149150
fork: Fork,
150151
) -> None:
151152
"""
@@ -156,8 +157,11 @@ def test_code_deposit_state_gas_scales_with_size(
156157
When code exceeds MAX_CODE_SIZE, the size check rejects before
157158
any gas is charged and the contract is not deployed.
158159
"""
159-
if code_size is None:
160+
if code_size == "max":
161+
code_size = fork.max_code_size()
162+
elif code_size == "max+1":
160163
code_size = fork.max_code_size() + 1
164+
assert isinstance(code_size, int)
161165

162166
gas_limit_cap = fork.transaction_gas_limit_cap()
163167
assert gas_limit_cap is not None
@@ -403,20 +407,28 @@ def test_code_deposit_oog_preserves_parent_reservoir(
403407
"""
404408
Test parent reservoir preserved after child code deposit OOG.
405409
406-
Child returns enough bytes that code deposit state gas exceeds
407-
available gas. Parent's SSTORE after the failed CREATE proves
408-
the reservoir was not inflated by a spill-then-halt refund.
410+
A caller contract invokes the factory via CALL with limited gas.
411+
The child CREATE returns enough bytes that code deposit state gas
412+
exceeds the child frame's available gas (reservoir spillover plus
413+
the limited gas_left). The factory's SSTORE after the failed
414+
CREATE proves the reservoir was not inflated by a spill-then-halt
415+
refund.
409416
"""
410417
gas_limit_cap = fork.transaction_gas_limit_cap()
411418
assert gas_limit_cap is not None
419+
gas_costs = fork.gas_costs()
420+
new_account_state_gas = gas_costs.GAS_NEW_ACCOUNT
412421
sstore_state_gas = fork.sstore_state_gas()
413422

414-
# Choose deploy_size so code deposit state gas exceeds the child
415-
# frame's available gas, guaranteeing OOG.
416-
cost_per_byte = fork.code_deposit_state_gas(code_size=1)
417-
deploy_size = gas_limit_cap // cost_per_byte + 1
423+
# Small deploy size; code deposit state gas will exceed the
424+
# limited gas available in the CREATE child frame.
425+
deploy_size = 4096
418426
init_code = Op.RETURN(0, deploy_size)
419427

428+
# Limited regular gas forwarded to the factory. After CREATE
429+
# takes 63/64, the factory retains ~15 K for its SSTOREs.
430+
child_gas = 1_000_000
431+
420432
factory_storage = Storage()
421433
factory = pre.deploy_contract(
422434
code=(
@@ -438,10 +450,17 @@ def test_code_deposit_oog_preserves_parent_reservoir(
438450
),
439451
)
440452

441-
# Reservoir = 1 SSTORE's worth of state gas
453+
# Caller invokes factory with limited gas via CALL.
454+
caller = pre.deploy_contract(
455+
code=Op.CALL(gas=child_gas, address=factory),
456+
)
457+
458+
# Reservoir = new-account state gas + one SSTORE's state gas.
459+
# Code deposit draws from the reservoir first then spills into
460+
# gas_left, which the limited CALL gas cannot cover.
442461
tx = Transaction(
443-
to=factory,
444-
gas_limit=gas_limit_cap + sstore_state_gas,
462+
to=caller,
463+
gas_limit=(gas_limit_cap + new_account_state_gas + sstore_state_gas),
445464
sender=pre.fund_eoa(),
446465
)
447466

0 commit comments

Comments
 (0)