Skip to content

test_create_child_revert_refunds_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_child_revert_refunds_state_gas@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_child_revert_refunds_state_gas --fork Amsterdam

Verify CREATE/CREATE2 child REVERT refunds parent's account gas.

On REVERT the parent's GAS_NEW_ACCOUNT charge is refunded to the reservoir (on top of the child's state gas returned via incorporate_child_on_error). Block state gas reflects only the probe SSTORE. The spillover variant runs with tx.gas at the cap (reservoir zero), so the state gas charge spills into gas_left and the LIFO refund returns it to gas_left.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
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
@pytest.mark.parametrize(
    "gas_limit_mode",
    [
        pytest.param("reservoir", id="with_reservoir"),
        pytest.param("spillover", id="spillover"),
    ],
)
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_create_child_revert_refunds_state_gas(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
    gas_limit_mode: str,
) -> None:
    """
    Verify CREATE/CREATE2 child REVERT refunds parent's account gas.

    On REVERT the parent's `GAS_NEW_ACCOUNT` charge is refunded to
    the reservoir (on top of the child's state gas returned via
    `incorporate_child_on_error`). Block state gas reflects only the
    probe SSTORE. The spillover variant runs with tx.gas at the cap
    (reservoir zero), so the state gas charge spills into `gas_left`
    and the LIFO refund returns it to `gas_left`.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    intrinsic_cost = fork.transaction_intrinsic_cost_calculator()()

    init_code = Op.REVERT(0, 0)
    mstore_value, size = init_code_at_high_bytes(init_code)

    create_call = create_opcode(
        value=0, offset=0, size=size, init_code_size=size
    )

    storage = Storage()
    factory_code = (
        Op.MSTORE(0, mstore_value, new_memory_size=32)
        + Op.POP(create_call)
        + Op.SSTORE(storage.store_next(1, "reservoir_ok"), 1)
    )
    factory = pre.deploy_contract(code=factory_code)

    tx = Transaction(
        to=factory,
        state_gas_reservoir=(
            0 if gas_limit_mode == "spillover" else sstore_state_gas
        ),
        sender=pre.fund_eoa(),
    )

    # CREATE's GAS_NEW_ACCOUNT is refunded on child REVERT. SSTORE's
    # state portion is tracked separately. Child REVERT execution
    # (init_code execution) is propagated via
    # incorporate_child_on_error.
    tx_execution = (
        intrinsic_cost
        + factory_code.execution_cost(fork)
        + init_code.gas_cost(fork)
    )
    tx_state = sstore_state_gas
    expected = max(tx_execution, tx_state)
    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], header_verify=Header(gas_used=expected))],
        post={factory: Account(storage=storage)},
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.