Skip to content

test_account_warming()

Documentation for tests/prague/eip7702_set_code_tx/test_gas.py::test_account_warming@814481c0.

Generate fixtures for these test cases for Amsterdam with:

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

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

Source code in tests/prague/eip7702_set_code_tx/test_gas.py
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
@pytest.mark.parametrize("check_delegated_account_first", [True, False])
@pytest.mark.parametrize(
    **gas_test_parameter_args(include_many=False, include_data=False)
)
@pytest.mark.eels_base_coverage
def test_account_warming(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    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)

    gas_costs = fork.gas_costs()
    cold_account_cost = gas_costs.COLD_ACCOUNT_ACCESS
    warm_account_cost = gas_costs.WARM_ACCESS

    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,
            )
            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(
        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 3 forks.