Skip to content

test_same_authority_increasing_nonce_net_once()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_set_code.py::test_same_authority_increasing_nonce_net_once@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_same_authority_increasing_nonce_net_once --fork Amsterdam

Verify the per authority once invariant across valid auths.

The same fresh authority is delegated by three authorizations with increasing nonces in one transaction. The account leaf and its delegation indicator are written once. NEW_ACCOUNT and AUTH_BASE are each charged once across the batch while ACCOUNT_WRITE is refunded for every auth after the leaf is created.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_set_code.py
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
@pytest.mark.valid_from("EIP8037")
def test_same_authority_increasing_nonce_net_once(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Verify the per authority once invariant across valid auths.

    The same fresh authority is delegated by three authorizations with
    increasing nonces in one transaction. The account leaf and its
    delegation indicator are written once. NEW_ACCOUNT and AUTH_BASE are
    each charged once across the batch while ACCOUNT_WRITE is refunded
    for every auth after the leaf is created.
    """
    num_auths = 3
    per_auth_state = fork.transaction_intrinsic_state_gas(
        authorization_count=1,
    )
    intrinsic_state = fork.transaction_intrinsic_state_gas(
        authorization_count=num_auths,
    )
    total_intrinsic = fork.transaction_intrinsic_cost_calculator()(
        authorization_list_or_count=num_auths,
    )
    intrinsic_regular = total_intrinsic - intrinsic_state
    new_account_refund = fork.gas_costs().REFUND_AUTH_PER_EXISTING_ACCOUNT
    account_write = fork.gas_costs().ACCOUNT_WRITE
    auth_base_refund = per_auth_state - new_account_refund

    targets = [pre.deploy_contract(code=Op.STOP) for _ in range(num_auths)]
    call_target = pre.deploy_contract(code=Op.STOP)

    signer = pre.fund_eoa(amount=0)
    authorization_list = [
        AuthorizationTuple(address=targets[i], nonce=i, signer=signer)
        for i in range(num_auths)
    ]

    # The first auth creates the leaf with no refill. Each later auth
    # refills NEW_ACCOUNT, one AUTH_BASE, and one ACCOUNT_WRITE.
    auth_refund = (num_auths - 1) * (new_account_refund + auth_base_refund)
    refund_counter = (num_auths - 1) * account_write

    header_gas_used = max(intrinsic_regular, intrinsic_state - auth_refund)
    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=call_target,
        state_gas_reservoir=intrinsic_state,
        authorization_list=authorization_list,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=receipt_cumulative_gas_used,
        ),
    )

    state_test(
        pre=pre,
        post={
            signer: Account(
                nonce=num_auths,
                code=Spec7702.delegation_designation(targets[-1]),
            ),
        },
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=header_gas_used),
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.