Skip to content

test_account_warming()

Documentation for tests/prague/eip7702_set_code_tx/test_gas.py::test_account_warming@7b8124a7.

Generate fixtures for these test cases for Osaka with:

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

Test warming of the authority and authorized accounts for set-code transactions.

Source code in tests/prague/eip7702_set_code_tx/test_gas.py
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 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
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
@pytest.mark.parametrize("check_delegated_account_first", [True, False])
@pytest.mark.parametrize(
    **gas_test_parameter_args(include_many=False, include_data=False)
)
def test_account_warming(
    state_test: StateTestFiller,
    pre: Alloc,
    authorization_list_with_properties: List[AuthorizationWithProperties],
    authorization_list: List[AuthorizationTuple],
    access_list: List[AccessList],
    data: bytes,
    sender: EOA,
    check_delegated_account_first: bool,
) -> None:
    """
    Test warming of the authority and authorized accounts for set-code
    transactions.
    """
    # Overhead cost is the single push operation required for the address to
    # check.
    overhead_cost = 3 * len(Op.CALL.kwargs)

    cold_account_cost = 2600
    warm_account_cost = 100

    access_list_addresses = {
        access_list.address for access_list in access_list
    }

    # Dictionary to keep track of the addresses to check for warming, and the
    # expected cost of accessing such account.
    addresses_to_check: Dict[Address, int] = {}

    for authorization_with_properties in authorization_list_with_properties:
        authority = authorization_with_properties.tuple.signer
        assert authority is not None, "authority address is not set"
        delegated_account = authorization_with_properties.tuple.address

        authority_contains_delegation_after_authorization = (
            authorization_with_properties.invalidity_type is None
            # If the authority already contained a delegation prior to the
            # transaction, even if the authorization is invalid, there will be
            # a delegation when we check the address.
            or authorization_with_properties.authority_type
            == AddressType.EOA_WITH_SET_CODE
        )

        if check_delegated_account_first:
            if delegated_account not in addresses_to_check:
                addresses_to_check[delegated_account] = (
                    warm_account_cost
                    if delegated_account in access_list_addresses
                    else cold_account_cost
                )

            if authority not in addresses_to_check:
                if not authorization_with_properties.skip:
                    if (
                        authorization_with_properties.invalidity_type is None
                        or (
                            authorization_with_properties.invalidity_type
                            != AuthorizationInvalidityType.INVALID_CHAIN_ID
                        )
                        or authority in access_list_addresses
                    ):
                        access_cost = warm_account_cost
                    else:
                        access_cost = cold_account_cost
                else:
                    access_cost = (
                        cold_account_cost
                        if Address(sender)
                        != authorization_with_properties.tuple.signer
                        else warm_account_cost
                    )

                if authority_contains_delegation_after_authorization:
                    # The double charge for accessing the delegated account,
                    # only if the account ends up with a delegation in its
                    # code.
                    access_cost += warm_account_cost

                addresses_to_check[authority] = access_cost

        else:
            if authority not in addresses_to_check:
                access_cost = (
                    cold_account_cost
                    if Address(sender)
                    != authorization_with_properties.tuple.signer
                    else warm_account_cost
                )
                if not authorization_with_properties.skip and (
                    authorization_with_properties.invalidity_type is None
                    or (
                        authorization_with_properties.invalidity_type
                        != AuthorizationInvalidityType.INVALID_CHAIN_ID
                    )
                    or authority in access_list_addresses
                ):
                    access_cost = warm_account_cost

                if (
                    # We can only charge the delegated account access cost if
                    # the authorization went through
                    authority_contains_delegation_after_authorization
                ):
                    if (
                        delegated_account in addresses_to_check
                        or delegated_account in access_list_addresses
                    ):
                        access_cost += warm_account_cost
                    else:
                        access_cost += cold_account_cost

                addresses_to_check[authority] = access_cost

            if delegated_account not in addresses_to_check:
                if (
                    authority_contains_delegation_after_authorization
                    or delegated_account in access_list_addresses
                ):
                    access_cost = warm_account_cost
                else:
                    access_cost = cold_account_cost
                addresses_to_check[delegated_account] = access_cost

    callee_code: Bytecode = sum(  # type: ignore
        (
            CodeGasMeasure(
                code=Op.CALL(gas=0, address=check_address),
                overhead_cost=overhead_cost,
                extra_stack_items=1,
                sstore_key=check_address,
                stop=False,
            )
            for check_address in addresses_to_check
        )
    )
    callee_code += Op.STOP
    callee_address = pre.deploy_contract(
        callee_code,
        storage=dict.fromkeys(addresses_to_check, 0xDEADBEEF),
    )

    tx = Transaction(
        gas_limit=1_000_000,
        to=callee_address,
        authorization_list=authorization_list if authorization_list else None,
        access_list=access_list,
        sender=sender,
        data=data,
    )
    post = {
        callee_address: Account(
            storage=addresses_to_check,
        ),
    }

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

Parametrized Test Cases

This test generates 60 parametrized test cases across 2 forks.