Skip to content

test_auth_refund_block_gas_accounting()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_set_code.py::test_auth_refund_block_gas_accounting@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_set_code.py::test_auth_refund_block_gas_accounting --fork Amsterdam

Verify block + receipt gas accounting against per-authorization state-gas refunds from set_delegation.

Four signer pre-states span every refund branch:

  • nonexistent — no account leaf; no refund;
  • existing_leaf — leaf, empty code; NEW_ACCOUNT × CPSB refilled;
  • existing_delegation overwrite — leaf + delegation; full refill (NEW_ACCOUNT + AUTH_BASE) as the 23 delegation bytes overwrite in place;
  • existing_delegation clear — auth.address = RESET_DELEGATION_ADDRESS; same full refill, since the refill keys off the pre-state code slot, not what we're writing.

When the authority's account leaf already exists, the worst-case ACCOUNT_WRITE charged at intrinsic time is additionally refunded via the regular refund counter, subject to the refund cap.

Verified via header gas_used, receipt cumulative_gas_used, and the authority post-state (catches a silently-skipped auth).

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_set_code.py
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
@pytest.mark.parametrize(
    "signer_pre_state,authorize_to_null",
    [
        pytest.param("nonexistent", False, id="nonexistent_authority"),
        pytest.param("nonexistent", True, id="nonexistent_clear"),
        pytest.param("existing_leaf", False, id="existing_leaf_empty_code"),
        pytest.param("existing_leaf", True, id="existing_leaf_clear"),
        pytest.param(
            "existing_delegation",
            False,
            id="existing_delegation_overwrite",
        ),
        pytest.param(
            "existing_delegation",
            True,
            id="existing_delegation_clear",
        ),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_auth_refund_block_gas_accounting(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    signer_pre_state: str,
    authorize_to_null: bool,
) -> None:
    """
    Verify block + receipt gas accounting against per-authorization
    state-gas refunds from `set_delegation`.

    Four signer pre-states span every refund branch:

    * `nonexistent` — no account leaf; no refund;
    * `existing_leaf` — leaf, empty code; `NEW_ACCOUNT × CPSB` refilled;
    * `existing_delegation` overwrite — leaf + delegation; full refill
      (`NEW_ACCOUNT + AUTH_BASE`) as the 23 delegation bytes overwrite
      in place;
    * `existing_delegation` clear — `auth.address` =
      `RESET_DELEGATION_ADDRESS`; same full refill, since the refill
      keys off the *pre-state* code slot, not what we're writing.

    When the authority's account leaf already exists, the worst-case
    `ACCOUNT_WRITE` charged at intrinsic time is additionally refunded
    via the regular refund counter, subject to the refund cap.

    Verified via header `gas_used`, receipt `cumulative_gas_used`, and
    the authority post-state (catches a silently-skipped auth).
    """
    intrinsic_state_gas = fork.transaction_intrinsic_state_gas(
        authorization_count=1,
    )
    total_intrinsic = fork.transaction_intrinsic_cost_calculator()(
        authorization_list_or_count=1,
    )
    intrinsic_regular = total_intrinsic - intrinsic_state_gas
    new_account_refund = fork.gas_costs().REFUND_AUTH_PER_EXISTING_ACCOUNT
    account_write = fork.gas_costs().ACCOUNT_WRITE
    # Per-auth intrinsic state gas covers NEW_ACCOUNT + AUTH_BASE; the
    # AUTH_BASE portion is what's left after stripping NEW_ACCOUNT.
    auth_base_refund = intrinsic_state_gas - new_account_refund

    contract_old = pre.deploy_contract(code=Op.STOP)
    contract_new = pre.deploy_contract(code=Op.STOP)

    # AUTH_BASE is refunded when no new delegation-indicator bytes are
    # written: either the authority already has an indicator (overwrite
    # in place / clear) or `auth.address` is zero (no indicator written).
    if signer_pre_state == "nonexistent":
        signer = pre.fund_eoa(amount=0)
        pre_nonce = 0
        auth_refund = auth_base_refund if authorize_to_null else 0
        refund_counter = 0
    elif signer_pre_state == "existing_leaf":
        signer = pre.fund_eoa()
        pre_nonce = 0
        auth_refund = new_account_refund + (
            auth_base_refund if authorize_to_null else 0
        )
        refund_counter = account_write
    elif signer_pre_state == "existing_delegation":
        # `fund_eoa(delegation=...)` sets the authority's nonce to 1.
        signer = pre.fund_eoa(delegation=contract_old)
        pre_nonce = 1
        auth_refund = new_account_refund + auth_base_refund
        refund_counter = account_write
    else:
        raise ValueError(f"unknown signer_pre_state: {signer_pre_state!r}")

    auth_target = (
        Spec7702.RESET_DELEGATION_ADDRESS
        if authorize_to_null
        else contract_new
    )
    authorization_list = [
        AuthorizationTuple(
            address=auth_target,
            nonce=pre_nonce,
            signer=signer,
        ),
    ]

    post_signer = Account(
        nonce=pre_nonce + 1,
        code=(
            b""
            if authorize_to_null
            else Spec7702.delegation_designation(auth_target)
        ),
    )
    header_gas_used = max(
        intrinsic_regular,
        intrinsic_state_gas - auth_refund,
    )
    # The state refill is not subject to the refund cap; the regular
    # `ACCOUNT_WRITE` refund is.
    gas_used_before_refund = total_intrinsic - auth_refund
    regular_refund = min(
        gas_used_before_refund // fork.max_refund_quotient(),
        refund_counter,
    )
    receipt_cumulative_gas_used = gas_used_before_refund - regular_refund

    tx = Transaction(
        to=contract_new,
        state_gas_reservoir=intrinsic_state_gas,
        authorization_list=authorization_list,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=receipt_cumulative_gas_used,
        ),
    )

    state_test(
        pre=pre,
        post={signer: post_signer},
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=header_gas_used),
    )

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.