Skip to content

test_tx_installs_delegation_on_sender()

Documentation for tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_with_tx_delegation.py::test_tx_installs_delegation_on_sender@5c024cbb.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_with_tx_delegation.py::test_tx_installs_delegation_on_sender --fork Amsterdam

Self-sponsored type-4 transaction: the sender signs an authorization installing delegation on itself, and the authorization's nonce equals the sender's nonce after the transaction-side increment (1). After set_delegation the sender holds delegation code and its nonce reaches 2.

The sender authority already exists, so no NEW_ACCOUNT accrues, and its leaf was already written at inclusion (priced into TX_BASE), so the delegation write is not the transaction's first write to it and no ACCOUNT_WRITE accrues either. The authorization pays only AUTH_BASE (the net-new delegation indicator).

Parametrized over the call target:

  • calls_self: tx.to == sender. The intrinsic self-transfer carve-out suppresses the recipient access and value-transfer charges; the top-frame fires the delegation access charge because set_delegation has installed delegation code on the sender by then. The transaction then dispatches into the sender's delegated code.
  • calls_other: tx.to is a separate funded EOA. The intrinsic charges include COLD_ACCOUNT_ACCESS for the recipient (and the value-transfer charges when value > 0). The top-frame fires no recipient charge because the recipient is a plain EOA. The sender's delegation is installed and persists past the transaction without ever being invoked.
Source code in tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_with_tx_delegation.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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
@pytest.mark.parametrize(
    "value",
    [
        pytest.param(0, id="zero_value"),
        pytest.param(1, id="non-zero_value"),
    ],
)
@pytest.mark.parametrize(
    "call_target",
    [
        pytest.param("self", id="calls_self"),
        pytest.param("other_eoa", id="calls_other"),
    ],
)
def test_tx_installs_delegation_on_sender(
    fork: Fork,
    pre: Alloc,
    state_test: StateTestFiller,
    call_target: str,
    value: int,
) -> None:
    """
    Self-sponsored type-4 transaction: the sender signs an
    authorization installing delegation on itself, and the
    authorization's nonce equals the sender's nonce *after* the
    transaction-side increment (``1``). After ``set_delegation`` the
    sender holds delegation code and its nonce reaches ``2``.

    The sender authority already exists, so no ``NEW_ACCOUNT`` accrues,
    and its leaf was already written at inclusion (priced into
    ``TX_BASE``), so the delegation write is not the transaction's
    first write to it and no ``ACCOUNT_WRITE`` accrues either. The
    authorization pays only ``AUTH_BASE`` (the net-new delegation
    indicator).

    Parametrized over the call target:

    - ``calls_self``: ``tx.to == sender``. The intrinsic self-transfer
      carve-out suppresses the recipient access and value-transfer
      charges; the top-frame fires the delegation access charge because
      ``set_delegation`` has installed delegation code on the sender by
      then. The transaction then dispatches into the sender's delegated
      code.
    - ``calls_other``: ``tx.to`` is a separate funded EOA. The intrinsic
      charges include ``COLD_ACCOUNT_ACCESS`` for the recipient (and the
      value-transfer charges when ``value > 0``). The top-frame fires no
      recipient charge because the recipient is a plain EOA. The
      sender's delegation is installed and persists past the transaction
      without ever being invoked.
    """
    sender_initial_balance = 10**18
    sender = pre.fund_eoa(sender_initial_balance)

    delegated_to = pre.deploy_contract(code=Op.STOP)

    auth = AuthorizationTuple(
        address=delegated_to,
        nonce=1,
        signer=sender,
        # Sender authority already exists, so no NEW_ACCOUNT; its leaf
        # was already written at inclusion, so no ACCOUNT_WRITE.
        creates_account=False,
        first_write=False,
    )
    authorization_list = [auth]

    target_initial_balance = 0
    if call_target == "self":
        target = sender
        # Intrinsic carve-out fires (SELF); top-frame fires the
        # delegation access charge because the sender is delegated by
        # the time the check runs.
        intrinsic_recipient_type = RecipientType.SELF
        top_frame_recipient_type = RecipientType.DELEGATION_7702
    else:
        target_initial_balance = 100
        target = pre.fund_eoa(amount=target_initial_balance)
        # Recipient is a plain EOA, so no carve-out and no top-frame
        # recipient charge.
        intrinsic_recipient_type = RecipientType.EOA
        top_frame_recipient_type = RecipientType.EOA

    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        sends_value=bool(value),
        recipient_type=intrinsic_recipient_type,
        authorization_list_or_count=authorization_list,
        return_cost_deducted_prior_execution=True,
    )
    top_frame_regular = fork.transaction_top_frame_gas_calculator()(
        sends_value=bool(value),
        recipient_type=top_frame_recipient_type,
        delegation_warm=False,
        authorizations=authorization_list,
    )
    top_frame_state = fork.transaction_top_frame_state_gas(
        sends_value=bool(value),
        recipient_type=top_frame_recipient_type,
        authorizations=authorization_list,
    )

    total_gas_cost = intrinsic_gas + top_frame_regular + top_frame_state
    tx_gas_limit = total_gas_cost + 1000
    gas_price = 1_000_000_000

    tx = Transaction(
        sender=sender,
        to=target,
        value=value,
        authorization_list=authorization_list,
        gas_limit=tx_gas_limit,
        max_fee_per_gas=gas_price,
        max_priority_fee_per_gas=gas_price,
    )

    if call_target == "self":
        # Value moves sender -> sender, net zero on balance.
        sender_final_balance = (
            sender_initial_balance - total_gas_cost * gas_price
        )
        post = {
            sender: Account(
                nonce=2,
                balance=sender_final_balance,
                code=Spec7702.delegation_designation(delegated_to),
            ),
        }
    else:
        sender_final_balance = (
            sender_initial_balance - value - total_gas_cost * gas_price
        )
        post = {
            sender: Account(
                nonce=2,
                balance=sender_final_balance,
                code=Spec7702.delegation_designation(delegated_to),
            ),
            target: Account(balance=target_initial_balance + value),
        }

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

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.