Skip to content

test_tx_installs_delegation_on_empty_recipient()

Documentation for tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_with_tx_delegation.py::test_tx_installs_delegation_on_empty_recipient@87aba1a3.

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

Scenario 2: tx.to is a non-existent (empty) account. The type-4 transaction's authorization installs delegation on tx.to.

The authority's account leaf does not exist, so the authorization pays NEW_ACCOUNT (account creation) and AUTH_BASE (net-new delegation indicator). Whether it also pays ACCOUNT_WRITE depends on the value transfer: with zero_value the delegation write is the transaction's first write to tx.to and ACCOUNT_WRITE is charged; with non-zero_value the transaction already pays to write tx.to when it transfers value to it, so no ACCOUNT_WRITE accrues. set_delegation runs before the recipient top-frame check and makes the recipient alive, so the recipient NEW_ACCOUNT charge a value transfer would otherwise incur is suppressed (the per-authorization NEW_ACCOUNT accounts for the leaf). The COLD_ACCOUNT_ACCESS charge for the now-delegated recipient still fires.

Source code in tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_with_tx_delegation.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
@pytest.mark.parametrize(
    "value",
    [
        pytest.param(0, id="zero_value"),
        pytest.param(1, id="non-zero_value"),
    ],
)
def test_tx_installs_delegation_on_empty_recipient(
    fork: Fork,
    pre: Alloc,
    state_test: StateTestFiller,
    value: int,
) -> None:
    """
    Scenario 2: ``tx.to`` is a non-existent (empty) account. The type-4
    transaction's authorization installs delegation on ``tx.to``.

    The authority's account leaf does not exist, so the authorization
    pays ``NEW_ACCOUNT`` (account creation) and ``AUTH_BASE`` (net-new
    delegation indicator). Whether it also pays ``ACCOUNT_WRITE``
    depends on the value transfer: with ``zero_value`` the delegation
    write is the transaction's first write to ``tx.to`` and
    ``ACCOUNT_WRITE`` is charged; with ``non-zero_value`` the
    transaction already pays to write ``tx.to`` when it transfers value
    to it, so no ``ACCOUNT_WRITE`` accrues. ``set_delegation`` runs
    before the recipient top-frame check and makes the recipient alive,
    so the recipient ``NEW_ACCOUNT`` charge a value transfer would
    otherwise incur is suppressed (the per-authorization ``NEW_ACCOUNT``
    accounts for the leaf). The ``COLD_ACCOUNT_ACCESS`` charge for the
    now-delegated recipient still fires.
    """
    sender_initial_balance = 10**18
    sender = pre.fund_eoa(sender_initial_balance)

    target = pre.fund_eoa(amount=0)
    delegated_to = pre.deploy_contract(code=Op.STOP)

    auth = AuthorizationTuple(
        address=delegated_to,
        nonce=0,
        signer=target,
        # Empty authority leaf must be created.
        creates_account=True,
        first_write=not bool(value),
    )
    authorization_list = [auth]

    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        sends_value=bool(value),
        recipient_type=RecipientType.EMPTY_ACCOUNT,
        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=RecipientType.DELEGATION_7702,
        delegation_warm=False,
        authorizations=authorization_list,
    )
    top_frame_state = fork.transaction_top_frame_state_gas(
        sends_value=bool(value),
        recipient_type=RecipientType.DELEGATION_7702,
        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,
    )

    sender_final_balance = (
        sender_initial_balance - value - (total_gas_cost * gas_price)
    )

    post = {
        sender: Account(nonce=1, balance=sender_final_balance),
        target: Account(
            nonce=1,
            balance=value,
            code=Spec7702.delegation_designation(delegated_to),
        ),
    }

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

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.