Skip to content

test_top_level_halt_burns_spilled_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_top_level_halt_burns_spilled_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_call.py::test_top_level_halt_burns_spilled_state_gas --fork Amsterdam

Verify a top-level halt burns the spilled state gas, so only the start reservoir survives. The parent calls a child that reverts or halts, then INVALIDs at the top level.

Under LIFO refills a frame's spilled state gas refills to gas_left, which the halt then zeros. Only the reservoir-funded portion survives, equal to the reservoir at frame start.

That start value equals the sized reservoir R, so for every child failure mode and reservoir_delta:

`state_gas_left_end = R`,
`tx_gas_used = tx.gas - R = gas_limit_cap`.

With the reservoir one short (reservoir_delta == -1) the child's SSTORE spills one unit from gas_left, which is refilled then burned by the halt. So tx_gas_used stays gas_limit_cap. The old behavior refunded the spill, giving gas_limit_cap - 1.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
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
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
@pytest.mark.parametrize(
    "reservoir_delta",
    [
        pytest.param(-1, id="reservoir_one_short"),
        pytest.param(0, id="reservoir_exact"),
        pytest.param(1, id="reservoir_one_over"),
    ],
)
@pytest.mark.parametrize(
    "child_termination",
    [
        pytest.param("revert", id="child_revert"),
        pytest.param("halt", id="child_halt"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_top_level_halt_burns_spilled_state_gas(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    child_termination: str,
    reservoir_delta: int,
) -> None:
    """
    Verify a top-level halt burns the spilled state gas, so only the
    start reservoir survives. The parent calls a child that reverts or
    halts, then INVALIDs at the top level.

    Under LIFO refills a frame's spilled state gas refills to
    `gas_left`, which the halt then zeros. Only the reservoir-funded
    portion survives, equal to the reservoir at frame start.

    That start value equals the sized reservoir R, so for every child
    failure mode and `reservoir_delta`:

        `state_gas_left_end = R`,
        `tx_gas_used = tx.gas - R = gas_limit_cap`.

    With the reservoir one short (`reservoir_delta == -1`) the child's
    SSTORE spills one unit from `gas_left`, which is refilled then
    burned by the halt. So `tx_gas_used` stays `gas_limit_cap`. The
    old behavior refunded the spill, giving `gas_limit_cap - 1`.
    """
    gas_limit_cap = fork.transaction_gas_limit_cap()
    assert gas_limit_cap is not None
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    if child_termination == "revert":
        child_code: Bytecode = Op.SSTORE(0, 1) + Op.REVERT(0, 0)
    else:
        child_code = Op.SSTORE(0, 1) + Op.INVALID

    child = pre.deploy_contract(code=child_code)

    parent = pre.deploy_contract(
        code=(
            Op.POP(
                Op.CALL(
                    gas=child_code.execution_cost(fork) - reservoir_delta,
                    address=child,
                )
            )
            + Op.INVALID
        ),
    )

    reservoir = sstore_state_gas + reservoir_delta
    tx_gas = gas_limit_cap + reservoir

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

    # LIFO refills: the spill refills to `gas_left` and is burned by
    # the halt. Only the sized reservoir survives, so
    # `tx_gas_used = gas_limit_cap`.
    state_gas_left_end = reservoir
    expected_gas_used = tx_gas - state_gas_left_end

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx],
                header_verify=Header(gas_used=expected_gas_used),
            ),
        ],
        post={child: Account(storage={0: 0})},
    )

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.