Skip to content

test_value_contract_creation_tx()

Documentation for tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_transactions.py::test_value_contract_creation_tx@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_transactions.py::test_value_contract_creation_tx --fork Amsterdam

Test value moving contract creation transactions.

When the init code succeeds, the contract is deployed with the transferred value and the receipt's gas_used equals the intrinsic plus the execution gas.

When the init code reverts, the deploy is rolled back: no code is set, the value transfer is reversed, and the top-frame NEW_ACCOUNT state-gas charge for the created account is refilled. The sender therefore pays only the execution intrinsic plus the few EVM gas units spent before the revert -- the NEW_ACCOUNT charge does not appear on the receipt.

Source code in tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_transactions.py
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
388
389
390
391
392
@EIPChecklist.GasCostChanges.Test.GasUpdatesMeasurement()
@pytest.mark.parametrize(
    "value",
    [
        pytest.param(0, id="zero_value"),
        pytest.param(1, id="non-zero_value"),
    ],
)
@pytest.mark.parametrize(
    "tx_reverts",
    [
        pytest.param(False, id="success"),
        pytest.param(True, id="init_reverts"),
    ],
)
def test_value_contract_creation_tx(
    fork: Fork,
    pre: Alloc,
    state_test: StateTestFiller,
    tx_reverts: bool,
    value: int,
) -> None:
    """
    Test value moving contract creation transactions.

    When the init code succeeds, the contract is deployed with the
    transferred value and the receipt's ``gas_used`` equals the
    intrinsic plus the execution gas.

    When the init code reverts, the deploy is rolled back: no code is
    set, the value transfer is reversed, and the top-frame
    ``NEW_ACCOUNT`` state-gas charge for the created account is
    refilled. The sender therefore pays only the execution intrinsic
    plus the few EVM gas units spent before the revert -- the
    ``NEW_ACCOUNT`` charge does not appear on the receipt.
    """
    sender_initial_balance = 10**18
    sender = pre.fund_eoa(sender_initial_balance)

    code_to_deploy = Op.STOP
    if tx_reverts:
        # ``PUSH1 0 PUSH1 0 REVERT`` -- aborts immediately, so no
        # code is deployed.
        call_data = Op.REVERT(0, 0)
    else:
        call_data = Initcode(deploy_code=code_to_deploy)
    execution_gas = call_data.gas_cost(fork)

    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        calldata=call_data,
        contract_creation=True,
        sends_value=bool(value),
        return_cost_deducted_prior_execution=True,
    )

    # EIP-2780: the created account's ``NEW_ACCOUNT`` state gas is
    # charged at the top frame (not the intrinsic). It must be covered
    # by the gas limit; it is consumed on a successful deploy and
    # refilled if the init code reverts.
    new_account_state_gas = fork.transaction_top_frame_state_gas(
        contract_creation=True,
    )
    if tx_reverts:
        # The deploy is rolled back, so the top-frame ``NEW_ACCOUNT``
        # charge is refilled and does not appear on the receipt.
        gas_used = intrinsic_gas + execution_gas
        # A tiny init code can leave the decomposed calldata floor above
        # the execution gas actually consumed; gas_used then pins to the
        # floor, which EIP-2780 anchors on the create intrinsic base.
        gas_used = max(
            gas_used,
            fork.transaction_data_floor_cost_calculator()(
                data=call_data,
                contract_creation=True,
                sends_value=bool(value),
            ),
        )
        # Value transfer rolled back.
        sender_value_delta = 0
        expected_target = None
    else:
        gas_used = intrinsic_gas + new_account_state_gas + execution_gas
        sender_value_delta = value
        expected_target = Account(code=code_to_deploy, balance=value)

    expected_target_address = compute_create_address(address=sender, nonce=0)

    if value > 0 and not tx_reverts:
        expected_logs = [transfer_log(sender, expected_target_address, value)]
    else:
        expected_logs = []

    gas_price = 1_000_000_000
    gas_limit = intrinsic_gas + new_account_state_gas + execution_gas + 1000

    tx = Transaction(
        sender=sender,
        to=None,
        value=value,
        data=call_data,
        gas_limit=gas_limit,
        gas_price=gas_price,
        expected_receipt=TransactionReceipt(logs=expected_logs),
    )

    sender_final_balance = (
        sender_initial_balance - sender_value_delta - gas_used * gas_price
    )

    post = {
        sender: Account(nonce=1, balance=sender_final_balance),
        expected_target_address: expected_target,
    }

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

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.