Skip to content

test_call_pre_charged_costs_excluded_from_forwarding()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_call_pre_charged_costs_excluded_from_forwarding@a9abd46e.

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

Verify pre-charged CALL costs do not reduce the 63/64 forwarding budget.

CALL charges access gas and memory expansion up front, before computing the 63/64 sub-call gas. Those costs must not be subtracted again during the forwarding calculation.

A wrapper contract receives a precise gas budget and calls a child with maximum gas and a large ret_size (triggering memory expansion). The child does a cold zero-to-nonzero SSTORE as proof of execution. The gas budget is tight enough that any double-counting of the pre-charged costs (access gas, memory expansion, or both) causes the child to OOG and the SSTORE to revert.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
@pytest.mark.valid_from("EIP8037")
def test_call_pre_charged_costs_excluded_from_forwarding(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Verify pre-charged CALL costs do not reduce the 63/64 forwarding budget.

    CALL charges access gas and memory expansion up front, before
    computing the 63/64 sub-call gas.  Those costs must not be
    subtracted again during the forwarding calculation.

    A wrapper contract receives a precise gas budget and calls a child
    with maximum gas and a large ret_size (triggering memory expansion).
    The child does a cold zero-to-nonzero SSTORE as proof of execution.
    The gas budget is tight enough that any double-counting of the
    pre-charged costs (access gas, memory expansion, or both) causes
    the child to OOG and the SSTORE to revert.
    """
    gas_costs = fork.gas_costs()
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    # Child: SSTORE(0, 1) as proof of execution
    child_storage = Storage()
    child_code = Op.SSTORE(child_storage.store_next(1, "child_ran"), 1)
    child = pre.deploy_contract(child_code)

    child_regular_gas = child_code.regular_cost(fork)

    # Memory expansion triggered by ret_size on the wrapper's CALL
    ret_size = 512 * 32  # 512 words
    memory_cost = fork.memory_expansion_gas_calculator()(new_bytes=ret_size)

    extra_gas = gas_costs.COLD_ACCOUNT_ACCESS  # cold call, value=0

    # Wrapper: CALL child requesting max gas with memory expansion
    wrapper_code = Op.CALL(
        gas=0xFFFFFFFF,
        address=child,
        value=0,
        args_offset=0,
        args_size=0,
        ret_offset=0,
        ret_size=ret_size,
    )
    wrapper = pre.deploy_contract(wrapper_code)

    wrapper_pushes = 7 * gas_costs.VERY_LOW  # 7 CALL args

    # After the pre-charge of extra_gas + memory_cost, the wrapper has
    # gas_remaining left.  The 63/64 rule should forward
    # gas_remaining * 63/64 to the child — just enough for its SSTORE.
    gas_remaining = child_regular_gas * 64 // 63 + memory_cost // 2

    wrapper_gas = wrapper_pushes + extra_gas + memory_cost + gas_remaining

    caller = pre.deploy_contract(
        Op.POP(Op.CALL(gas=wrapper_gas, address=wrapper))
    )

    sender = pre.fund_eoa()
    tx = Transaction(
        sender=sender,
        to=caller,
        state_gas_reservoir=sstore_state_gas,
    )

    post = {
        child: Account(storage=child_storage),
    }

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.