Skip to content

test_create_child_halt_refunds_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_child_halt_refunds_state_gas@c74f1a67.

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_halt_refunds_state_gas --fork Amsterdam

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

Exceptional halts (invalid opcode, EIP-3541 invalid prefix) consume all forwarded gas as regular_gas_used, so block accounting cannot strictly discriminate via header gas. Tight gas tuning via a caller wrapper leaves the factory with just enough gas_left to pay the probe SSTORE's regular portion but not enough to spill the state portion, so the probe SSTORE can only succeed via the refunded reservoir.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
@pytest.mark.parametrize(
    "failure_mode",
    [
        pytest.param("initcode_halt", id="initcode_halt"),
        pytest.param("invalid_prefix", id="invalid_prefix"),
    ],
)
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_create_child_halt_refunds_state_gas(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
    failure_mode: str,
) -> None:
    """
    Verify CREATE/CREATE2 child halt refunds parent's account gas.

    Exceptional halts (invalid opcode, EIP-3541 invalid prefix)
    consume all forwarded gas as `regular_gas_used`, so block
    accounting cannot strictly discriminate via header gas. Tight
    gas tuning via a caller wrapper leaves the factory with just
    enough `gas_left` to pay the probe SSTORE's regular portion
    but not enough to spill the state portion, so the probe SSTORE
    can only succeed via the refunded reservoir.
    """
    gas_costs = fork.gas_costs()
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    new_account_state_gas = gas_costs.NEW_ACCOUNT

    init_code: Op | Bytecode
    if failure_mode == "initcode_halt":
        init_code = Op.INVALID
    elif failure_mode == "invalid_prefix":
        # Return code starting with 0xEF (EIP-3541 invalid prefix).
        init_code = Op.MSTORE8(0, 0xEF) + Op.RETURN(0, 1)

    mstore_value, size = init_code_at_high_bytes(init_code)

    create_call = (
        create_opcode(value=0, offset=0, size=size, salt=0)
        if create_opcode == Op.CREATE2
        else create_opcode(value=0, offset=0, size=size)
    )

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

    # Tight gas tuning: child halt consumes all forwarded gas as
    # regular_gas_used. Factory retains
    # ~(forwarded - pre_sstore_regular) / 64 after CREATE. Target
    # the discrimination window `(probe_regular,
    # probe_regular + sstore_state_gas)` so the probe SSTORE
    # regular fits but state gas spillover from `gas_left` under
    # the old behavior OOGs.
    pre_sstore_code = Op.MSTORE(0, mstore_value) + Op.POP(create_call)
    pre_sstore_regular = pre_sstore_code.gas_cost(fork) - new_account_state_gas
    probe_code = Op.SSTORE(0, 1)
    probe_regular = probe_code.gas_cost(fork) - sstore_state_gas
    target_gas_left = probe_regular + sstore_state_gas // 2
    forwarded_gas = target_gas_left * 64 + pre_sstore_regular
    # Reservoir sized for CREATE charge only — SSTORE must pull
    # from the refunded reservoir, not from spill.
    caller = pre.deploy_contract(
        code=Op.CALL(gas=forwarded_gas, address=factory)
    )
    tx = Transaction(
        to=caller,
        state_gas_reservoir=new_account_state_gas,
        sender=pre.fund_eoa(),
    )

    state_test(pre=pre, post={factory: Account(storage=storage)}, tx=tx)

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.