Skip to content

test_create_code_deposit_oog_refunds_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_code_deposit_oog_refunds_state_gas@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_create_code_deposit_oog_refunds_state_gas --fork Amsterdam

Verify CREATE/CREATE2 code-deposit OOG refunds account state gas.

The initcode executes successfully and returns code longer than MAX_CODE_SIZE, triggering an exceptional halt during code deposit. Tight gas tuning limits the factory's post-halt gas_left so the probe SSTORE can only succeed via the refunded reservoir, not by spilling state gas from gas_left.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_create_code_deposit_oog_refunds_state_gas(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
) -> None:
    """
    Verify CREATE/CREATE2 code-deposit OOG refunds account state gas.

    The initcode executes successfully and returns code longer than
    `MAX_CODE_SIZE`, triggering an exceptional halt during code
    deposit. Tight gas tuning limits the factory's post-halt
    `gas_left` so the probe SSTORE can only succeed via the
    refunded reservoir, not by spilling state gas from `gas_left`.
    """
    gas_costs = fork.gas_costs()
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    new_account_state_gas = gas_costs.NEW_ACCOUNT
    max_code_size = fork.max_code_size()

    # Init code returns (max_code_size + 1) bytes, triggering the
    # OOG path in process_create_message code deposit.
    init_code = Op.RETURN(0, max_code_size + 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)
        ),
    )

    # Child halt consumes all forwarded gas; factory retains only
    # ~(forwarded - pre_sstore_regular) / 64. Target the
    # discrimination window so SSTORE regular fits but state gas
    # spillover fails.
    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
    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 2 parametrized test cases across 1 fork.