Skip to content

test_child_failure_refunds_state_gas_to_reservoir_not_gas_left()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_child_failure_refunds_state_gas_to_reservoir_not_gas_left@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_child_failure_refunds_state_gas_to_reservoir_not_gas_left --fork Amsterdam

Verify state gas from a failing child is restored to the reservoir, so a sibling probe SSTORE can draw from it under a tight execution stipend. Covers SSTORE and CALL-value (new account) state-gas charge paths.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
@pytest.mark.parametrize(
    "call_opcode,charge_via",
    [
        pytest.param(Op.CALL, "sstore", id="call_sstore_charge"),
        pytest.param(
            Op.DELEGATECALL, "sstore", id="delegatecall_sstore_charge"
        ),
        pytest.param(
            Op.CALL,
            "call_value_new_account",
            id="call_call_value_new_account_charge",
        ),
        pytest.param(
            Op.DELEGATECALL,
            "call_value_new_account",
            id="delegatecall_call_value_new_account_charge",
        ),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_child_failure_refunds_state_gas_to_reservoir_not_gas_left(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    call_opcode: Op,
    charge_via: str,
) -> None:
    """
    Verify state gas from a failing child is restored to the
    reservoir, so a sibling probe SSTORE can draw from it under a
    tight execution stipend. Covers SSTORE and CALL-value (new
    account) state-gas charge paths.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    probe_storage = Storage()
    probe_code = Op.SSTORE(probe_storage.store_next(1, "probe_ran"), 1)

    if charge_via == "sstore":
        child_code: Bytecode = Op.SSTORE(0, 1) + Op.REVERT(0, 0)
        child_balance = 0
    else:
        fresh_target = pre.fund_eoa(amount=0)
        child_code = Op.POP(
            Op.CALL(
                gas=Op.GAS,
                address=fresh_target,
                value=1,
                value_transfer=True,
                account_new=True,
            )
        ) + Op.REVERT(0, 0)
        child_balance = 1
    child_state_charge = child_code.state_cost(fork)

    delegated = call_opcode == Op.DELEGATECALL
    child = pre.deploy_contract(
        code=child_code, balance=0 if delegated else child_balance
    )
    probe = pre.deploy_contract(probe_code)
    probe_stipend = probe_code.execution_cost(fork)

    parent = pre.deploy_contract(
        code=(
            Op.POP(call_opcode(gas=Op.GAS, address=child))
            + Op.POP(call_opcode(gas=probe_stipend, address=probe))
        ),
        balance=child_balance if delegated else 0,
    )

    # Reservoir must cover the child's state charge (refunded on
    # REVERT) so the probe SSTORE can draw from it afterwards.
    reservoir = max(child_state_charge, sstore_state_gas)

    tx = Transaction(
        to=parent,
        state_gas_reservoir=reservoir,
        sender=pre.fund_eoa(),
    )

    # DELEGATECALL executes the callee in the caller's storage
    # context, so the probe's SSTORE lands on `parent` instead of
    # `probe`.
    if delegated:
        post: dict = {parent: Account(storage=probe_storage)}
    else:
        post = {probe: Account(storage=probe_storage)}

    state_test(
        pre=pre,
        post=post,
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=sstore_state_gas),
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.