Skip to content

test_spilled_state_gas_consumed_across_halt_chain()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_reservoir.py::test_spilled_state_gas_consumed_across_halt_chain@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_reservoir.py::test_spilled_state_gas_consumed_across_halt_chain --fork Amsterdam

Verify spilled state gas stays consumed along a chain of halting frames.

A self-DELEGATECALLing contract writes NOT(storage[slot]), so set and clear alternate down the shared-storage call stack and the reservoir is recycled instead of drained once. Each frame reuses the value it wrote as a CALL's args_size, so the frames that set the slot halt on the memory-size overflow while the frames that cleared it re-enter the contract, interleaving halting and surviving frames. Every halt must burn its spill rather than credit it back to the caller's reservoir, so the top-level halt charges the whole gas limit.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_reservoir.py
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
@pytest.mark.parametrize(
    "descent_frames",
    [
        pytest.param(2, id="descent_frames_2"),
        pytest.param(4, id="descent_frames_4"),
        pytest.param(8, id="descent_frames_8"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_spilled_state_gas_consumed_across_halt_chain(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    descent_frames: int,
) -> None:
    """
    Verify spilled state gas stays consumed along a chain of halting frames.

    A self-`DELEGATECALL`ing contract writes `NOT(storage[slot])`, so set and
    clear alternate down the shared-storage call stack and the reservoir is
    recycled instead of drained once. Each frame reuses the value it wrote as
    a `CALL`'s `args_size`, so the frames that set the slot halt on the
    memory-size overflow while the frames that cleared it re-enter the
    contract, interleaving halting and surviving frames. Every halt must burn
    its spill rather than credit it back to the caller's reservoir, so the
    top-level halt charges the whole gas limit.
    """
    slot = 0
    value_offset = 0
    code = (
        # Memory is per-frame, so each frame keeps the value it wrote
        # and reuses it below as the CALL's args_size.
        Op.MSTORE(value_offset, Op.NOT(Op.SLOAD(slot)))
        + Op.SSTORE(slot, Op.MLOAD(value_offset))
        + Op.POP(Op.DELEGATECALL(address=Op.ADDRESS))
        # An all-ones args_size overflows the memory-size calculation
        # and halts the frame. A zero one, in a frame that cleared the
        # slot, re-enters the contract and spawns further frames.
        + Op.POP(Op.CALL(address=Op.ADDRESS, args_size=Op.MLOAD(value_offset)))
    )
    contract = pre.deploy_contract(code=code)

    # One fresh spilled set plus a descent budget in static frame
    # costs. Clear credits recycle the reservoir for deeper sets, and
    # warm frames cost less than the static sum, so the descent runs
    # past the budget. Fork-derived so the depth regimes survive
    # repricings.
    gas_limit = (
        fork.transaction_intrinsic_cost_calculator()()
        + Op.SSTORE(new_value=1).state_cost(fork)
        + descent_frames * code.execution_cost(fork)
    )
    gas_limit_cap = fork.transaction_gas_limit_cap()
    assert gas_limit_cap is not None
    # Below the cap, so the reservoir starts empty and every set spills.
    assert gas_limit < gas_limit_cap

    tx = Transaction(
        to=contract,
        gas_limit=gas_limit,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(cumulative_gas_used=gas_limit),
    )

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

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.