Skip to content

test_sstore_oog_no_reservoir_inflation()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_sstore_oog_no_reservoir_inflation@a9abd46e.

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

Verify SSTORE state gas is not charged when regular gas OOGs.

With zero reservoir, all state gas spills into gas_left. A child frame does CREATE (charging state gas from gas_left) followed by SSTORE. When the factory is 1 gas short, SSTORE OOGs. If state gas is incorrectly charged before regular gas, the extra state gas inflates the parent's reservoir on frame failure, changing the transaction's effective gas consumption.

Regression test for SSTORE gas ordering: regular gas must be checked before state gas.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
@pytest.mark.parametrize(
    "gas_shortfall",
    [
        pytest.param(0, id="exact_gas"),
        pytest.param(1, id="short_one_gas"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_sstore_oog_no_reservoir_inflation(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    gas_shortfall: int,
) -> None:
    """
    Verify SSTORE state gas is not charged when regular gas OOGs.

    With zero reservoir, all state gas spills into gas_left. A child
    frame does CREATE (charging state gas from gas_left) followed by
    SSTORE. When the factory is 1 gas short, SSTORE OOGs. If state
    gas is incorrectly charged before regular gas, the extra state gas
    inflates the parent's reservoir on frame failure, changing the
    transaction's effective gas consumption.

    Regression test for SSTORE gas ordering: regular gas must be
    checked before state gas.
    """
    initcode = Initcode(deploy_code=Op.STOP)
    initcode_len = len(initcode)

    factory_code = Op.CALLDATACOPY(
        0,
        0,
        Op.CALLDATASIZE,
        data_size=initcode_len,
        new_memory_size=initcode_len,
    ) + Op.SSTORE(
        0,
        Op.CREATE(
            value=0,
            offset=0,
            size=Op.CALLDATASIZE,
            init_code_size=initcode_len,
        ),
    )
    factory = pre.deploy_contract(factory_code)
    create_address = compute_create_address(address=factory, nonce=1)

    # Total gas includes both regular and state components since
    # reservoir is zero — all state gas comes from gas_left.
    factory_gas = (
        factory_code.gas_cost(fork)
        + initcode.execution_gas(fork)
        + initcode.deployment_gas(fork)
    )

    # Caller forwards total gas (regular + state) through CALL.
    # With zero reservoir, the CALL gas parameter is the only source.
    caller = pre.deploy_contract(
        Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
        + Op.CALL(
            gas=factory_gas - gas_shortfall,
            address=factory,
            value=0,
            args_offset=0,
            args_size=Op.CALLDATASIZE,
            ret_offset=0,
            ret_size=0,
        )
    )

    sender = pre.fund_eoa()
    tx = Transaction(
        sender=sender,
        to=caller,
        data=bytes(initcode),
        state_gas_reservoir=0,
    )

    created = not gas_shortfall
    post = {
        create_address: Account(code=Op.STOP)
        if created
        else Account.NONEXISTENT,
        factory: Account(storage={0: create_address if created else 0}),
    }

    state_test(pre=pre, tx=tx, post=post)

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.