Skip to content

test_gas_cost()

Documentation for tests/prague/eip7702_set_code_tx/test_gas.py::test_gas_cost@b314d18e.

Generate fixtures for these test cases for Osaka with:

fill -v tests/prague/eip7702_set_code_tx/test_gas.py::test_gas_cost --fork Osaka

Test gas at the execution start of a set-code transaction in multiple scenarios.

Source code in tests/prague/eip7702_set_code_tx/test_gas.py
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
@pytest.mark.parametrize(
    **gas_test_parameter_args(
        include_pre_authorized=False, execution_gas_allowance=True
    )
)
@pytest.mark.slow()
# TODO[EIP-8037]: discount accounting here uses Prague refund_counter
# mechanics (with the EIP-3529 1/5 cap). On Amsterdam the existing-authority
# refund flows through state_gas_reservoir / state_refund and is not capped
# the same way. Needs a fork-aware rewrite before this can run on Amsterdam.
@pytest.mark.valid_before("EIP8037")
def test_gas_cost(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    authorization_list_with_properties: List[AuthorizationWithProperties],
    authorization_list: List[AuthorizationTuple],
    data: bytes,
    access_list: List[AccessList],
    sender: EOA,
) -> None:
    """
    Test gas at the execution start of a set-code transaction in multiple
    scenarios.
    """
    # Calculate the intrinsic gas cost of the authorizations, by default the
    # full empty account cost is charged for each authorization.
    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        calldata=data,
        access_list=access_list,
        authorization_list_or_count=authorization_list,
    )

    discounted_authorizations = 0
    seen_authority = set()
    for authorization_with_properties in authorization_list_with_properties:
        if authorization_with_properties.invalidity_type is None:
            authority = authorization_with_properties.tuple.signer
            if not authorization_with_properties.empty:
                seen_authority.add(authority)
            if authority in seen_authority:
                discounted_authorizations += 1
            else:
                seen_authority.add(authority)

    discount_gas = (
        Spec.AUTH_PER_EMPTY_ACCOUNT - Spec.REFUND_AUTH_PER_EXISTING_ACCOUNT
    ) * discounted_authorizations

    # We calculate the exact gas required to execute the test code. We add
    # SSTORE opcodes in order to make sure that the refund is less than one
    # fifth (EIP-3529) of the total gas used, so we can see the full discount
    # being reflected in most of the tests.
    gas_opcode_cost = Op.GAS.gas_cost(fork)
    sstore_opcode_count = 10
    push_opcode_count = (2 * (sstore_opcode_count)) - 1
    execution_gas = (
        Op.GAS
        + Op.PUSH1(0) * push_opcode_count
        + Op.SSTORE(key_warm=False) * sstore_opcode_count
    ).gas_cost(fork)

    # The first opcode that executes in the code is the GAS opcode, which costs
    # 2 gas, so we subtract that from the expected gas measure.
    expected_gas_measure = execution_gas - gas_opcode_cost

    test_code_storage = Storage()
    test_code = (
        Op.SSTORE(test_code_storage.store_next(expected_gas_measure), Op.GAS)
        + sum(
            Op.SSTORE(test_code_storage.store_next(1), 1)
            for _ in range(sstore_opcode_count - 1)
        )
        + Op.STOP
    )
    test_code_address = pre.deploy_contract(test_code)

    tx_gas_limit = intrinsic_gas + execution_gas

    # EIP-3529
    max_discount = tx_gas_limit // 5

    if discount_gas > max_discount:
        # Only one test hits this condition, but it's ok to also test this
        # case.
        discount_gas = max_discount

    gas_used = tx_gas_limit - discount_gas

    sender_account = pre[sender]
    assert sender_account is not None

    tx = Transaction(
        gas_limit=tx_gas_limit,
        to=test_code_address,
        value=0,
        data=data,
        authorization_list=authorization_list,
        access_list=access_list,
        sender=sender,
        expected_receipt=TransactionReceipt(cumulative_gas_used=gas_used),
    )

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

Parametrized Test Cases

This test generates 33 parametrized test cases across 2 forks.