Skip to content

test_account_write_authority_is_recipient()

Documentation for tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_authorization_charges.py::test_account_write_authority_is_recipient@26332146.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_authorization_charges.py::test_account_write_authority_is_recipient --fork Amsterdam

An authority that is also tx.to pays ACCOUNT_WRITE only when the transaction moves no value to it.

The charge depends on whether the transaction transfers value:

  • zero_value: no value is transferred, so at authorization-processing time tx.to has not been written yet (only the sender is written at inclusion). The delegation write is the transaction's first write to it, so ACCOUNT_WRITE is charged.
  • non-zero_value: the transaction already pays to write tx.to when it transfers value to it, so the delegation write is not the first write and no ACCOUNT_WRITE accrues.

This resolves the EIP text's "(i.e. the authority differs from tx.to)" parenthetical: the rule is first-write tracking, and tx.to counts as pre-written only when the transaction moves value to it.

Either way the authorization writes a net-new delegation indicator (AUTH_BASE), and after it applies the recipient is delegated, so the top frame additionally resolves the delegation target at the cold rate before dispatching its code (a STOP).

Source code in tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_authorization_charges.py
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
@pytest.mark.parametrize(
    "value",
    [
        pytest.param(0, id="zero_value"),
        pytest.param(1, id="non-zero_value"),
    ],
)
def test_account_write_authority_is_recipient(
    fork: Fork,
    pre: Alloc,
    state_test: StateTestFiller,
    value: int,
) -> None:
    """
    An authority that is also ``tx.to`` pays ``ACCOUNT_WRITE`` only when
    the transaction moves no value to it.

    The charge depends on whether the transaction transfers value:

    - ``zero_value``: no value is transferred, so at
      authorization-processing time ``tx.to`` has not been written yet
      (only the sender is written at inclusion). The delegation write
      is the transaction's first write to it, so ``ACCOUNT_WRITE`` is
      charged.
    - ``non-zero_value``: the transaction already pays to write
      ``tx.to`` when it transfers value to it, so the delegation write
      is not the first write and no ``ACCOUNT_WRITE`` accrues.

    This resolves the EIP text's "(i.e. the authority differs from
    ``tx.to``)" parenthetical: the rule is first-write tracking, and
    ``tx.to`` counts as pre-written only when the transaction moves
    value to it.

    Either way the authorization writes a net-new delegation indicator
    (``AUTH_BASE``), and after it applies the recipient is delegated, so
    the top frame additionally resolves the delegation target at the
    cold rate before dispatching its code (a ``STOP``).
    """
    authority_initial_balance = 100
    sender = pre.fund_eoa()
    delegation_target = pre.deploy_contract(code=Op.STOP)
    recipient = pre.fund_eoa(amount=authority_initial_balance)

    authorization = AuthorizationTuple(
        address=delegation_target,
        nonce=0,
        signer=recipient,
        first_write=not bool(value),
    )

    # The recipient is delegated by the time the top frame resolves it,
    # so model it as a 7702 delegation: the framework then charges the
    # cold delegation-target access on top of the intrinsic recipient
    # access, matching the spec's resolution of the freshly-set
    # delegation.
    recipient_type = RecipientType.DELEGATION_7702
    authorizations = [authorization]
    top_frame_execution = fork.transaction_top_frame_gas_calculator()(
        recipient_type=recipient_type,
        authorizations=authorizations,
    )
    top_frame_state = fork.transaction_top_frame_state_gas(
        recipient_type=recipient_type,
        authorizations=authorizations,
    )
    total_gas_cost = (
        _intrinsic_gas(
            fork,
            1,
            recipient_type=recipient_type,
            sends_value=bool(value),
        )
        + top_frame_execution
        + top_frame_state
    )

    tx = Transaction(
        sender=sender,
        to=recipient,
        value=value,
        authorization_list=[authorization],
        gas_limit=total_gas_cost,
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=total_gas_cost,
        ),
    )

    post = {
        recipient: Account(
            nonce=1,
            balance=authority_initial_balance + value,
            code=Spec7702.delegation_designation(delegation_target),
        ),
    }

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

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.