Skip to content

test_staticcall_passes_reservoir()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_staticcall_passes_reservoir@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_staticcall_passes_reservoir --fork Amsterdam

Preserve the reservoir across successful and rejected static calls.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
 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
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
@pytest.mark.parametrize(
    "child_action,reservoir_shortfall",
    [
        pytest.param("write_rejected", 0, id="write_rejected"),
        pytest.param("read_only", 0, id="read_only"),
        pytest.param("create", 0, id="create_exact"),
        pytest.param("create", 1, id="create_one_short"),
        pytest.param("create2", 0, id="create2_exact"),
        pytest.param("create2", 1, id="create2_one_short"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_staticcall_passes_reservoir(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    child_action: str,
    reservoir_shortfall: int,
) -> None:
    """Preserve the reservoir across successful and rejected static calls."""
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    if child_action == "read_only":
        child_code: Bytecode = Op.POP(Op.SLOAD(0, key_warm=False))
        static_result = 1
    elif child_action == "write_rejected":
        child_code = Op.SSTORE(0, 1)
        static_result = 0
    else:
        create_opcode = Op.CREATE if child_action == "create" else Op.CREATE2
        child_code = create_opcode(value=0, offset=0, size=0, init_code_size=0)
        static_result = 0
    child = pre.deploy_contract(code=child_code)
    child_gas = child_code.execution_cost(fork)
    creating = child_action in ("create", "create2")
    if creating:
        # Fund a misplaced account charge far enough to expose spill handling.
        child_gas += child_code.state_cost(fork)
        assert child_code.state_cost(fork) > sstore_state_gas
    probe_succeeds = reservoir_shortfall == 0

    probe_code = Op.SSTORE(0, 1)
    probe = pre.deploy_contract(code=probe_code)
    probe_gas = probe_code.execution_cost(fork)

    parent_storage = Storage()
    static_slot = parent_storage.store_next(
        static_result + 1, "staticcall_result"
    )
    # The probe receives execution gas only: exact reservoir succeeds;
    # one short must fail, exposing either lost gas or an inflated reservoir.
    probe_slot = parent_storage.store_next(1 + probe_succeeds, "probe_result")
    parent_code = Op.SSTORE(
        static_slot,
        Op.ADD(Op.STATICCALL(gas=child_gas, address=child), 1),
        # gas accounting
        original_value=1,
        current_value=1,
        new_value=static_result + 1,
        key_warm=False,
    ) + Op.SSTORE(
        probe_slot,
        Op.ADD(Op.CALL(gas=probe_gas, address=probe), 1),
        # gas accounting
        original_value=1,
        current_value=1,
        new_value=1 + probe_succeeds,
        key_warm=False,
    )
    parent = pre.deploy_contract(
        code=parent_code, storage={static_slot: 1, probe_slot: 1}
    )

    execution_gas = (
        fork.transaction_intrinsic_cost_calculator()()
        + parent_code.execution_cost(fork)
        + child_gas
        + probe_gas
    )
    state_gas = sstore_state_gas if probe_succeeds else 0
    expected_gas_used = max(execution_gas, state_gas)
    if not creating:
        assert expected_gas_used == state_gas, (
            "expected state gas to dominate execution gas"
        )

    tx = Transaction(
        to=parent,
        state_gas_reservoir=sstore_state_gas - reservoir_shortfall,
        sender=pre.fund_eoa(),
    )

    post: dict[Address, Account | None] = {
        parent: Account(storage=parent_storage),
        child: Account(nonce=1, storage={0: 0}),
        probe: Account(storage={0: int(probe_succeeds)}),
    }
    if creating:
        created = compute_create_address(
            address=child, nonce=1, salt=0, initcode=b"", opcode=create_opcode
        )
        post[created] = Account.NONEXISTENT
    state_test(
        pre=pre,
        post=post,
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=expected_gas_used),
    )

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.