Skip to content

test_auth_state_gas_in_header_after_failure()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_set_code.py::test_auth_state_gas_in_header_after_failure@87aba1a3.

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_state_gas_in_header_after_failure --fork Amsterdam

Verify the header gas_used when the top-level call fails after the authorization is applied.

The delegation is applied in the top-frame preparation (before the execution snapshot), so it persists through every failure mode -- and so does the state gas that paid for it (NEW_ACCOUNT + AUTH_BASE for a fresh authority, AUTH_BASE for an existing one), which is folded out of the frame's refillable pools. The header is max(block_regular, block_state):

  • REVERT -- the unused execution budget returns, so the regular component is intrinsic_regular + top_frame_regular + execution_regular and the state component is the persisting authorization state gas.
  • HALT / OOG -- the frame consumes its whole gas limit; the authorization state gas within it is accounted on the state component, and the remainder on the regular component.
Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_set_code.py
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
@pytest.mark.parametrize(
    "failure_mode",
    [
        pytest.param("revert", id="revert"),
        pytest.param("halt", id="halt"),
        pytest.param("oog", id="oog"),
    ],
)
@pytest.mark.parametrize(
    "authority_exists",
    [
        pytest.param(False, id="new_account"),
        pytest.param(True, id="existing_account"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_auth_state_gas_in_header_after_failure(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    failure_mode: str,
    authority_exists: bool,
) -> None:
    """
    Verify the header ``gas_used`` when the top-level call fails after the
    authorization is applied.

    The delegation is applied in the top-frame preparation (before the
    execution snapshot), so it persists through every failure mode --
    and so does the state gas that paid for it (``NEW_ACCOUNT`` +
    ``AUTH_BASE`` for a fresh authority, ``AUTH_BASE`` for an existing
    one), which is folded out of the frame's refillable pools. The
    header is ``max(block_regular, block_state)``:

    * REVERT -- the unused execution budget returns, so the regular
      component is ``intrinsic_regular + top_frame_regular +
      execution_regular`` and the state component is the persisting
      authorization state gas.
    * HALT / OOG -- the frame consumes its whole gas limit; the
      authorization state gas within it is accounted on the state
      component, and the remainder on the regular component.
    """
    gas_limit = 500_000

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

    if failure_mode == "revert":
        revert_code = Op.REVERT(0, 0)
        target = pre.deploy_contract(code=revert_code)
    elif failure_mode == "halt":
        target = pre.deploy_contract(code=Op.INVALID)
    else:
        # Consume all remaining gas at once (a spin loop would execute
        # millions of ops in the EVM and slow down filling).
        target = pre.deploy_contract(code=Om.OOG)

    if authority_exists:
        signer = pre.fund_eoa()
        creates_account = False
    else:
        signer = pre.fund_eoa(amount=0)
        creates_account = True

    authorization_list = [
        AuthorizationTuple(
            address=delegate,
            nonce=0,
            signer=signer,
            creates_account=creates_account,
            writes_delegation=True,
        ),
    ]

    intrinsic_regular, top_frame_regular, top_frame_state = _auth_gas(
        fork, authorization_list
    )

    if failure_mode == "revert":
        # The authorization's state gas persists with its delegation.
        _, expected_gas_used = _receipt_and_header(
            intrinsic_regular,
            top_frame_regular,
            top_frame_state,
            execution_regular=revert_code.regular_cost(fork),
        )
    else:
        # HALT / OOG consume the whole gas limit, of which the
        # persisting authorization state gas is accounted on the state
        # component and the remainder on the regular component.
        expected_gas_used = max(gas_limit - top_frame_state, top_frame_state)

    tx = Transaction(
        ty=4,
        to=target,
        gas_limit=gas_limit,
        sender=pre.fund_eoa(),
        authorization_list=authorization_list,
    )

    post = {
        signer: Account(code=Spec7702.delegation_designation(delegate)),
    }
    state_test(
        pre=pre,
        post=post,
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=expected_gas_used),
    )

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.