Skip to content

test_dynamic_create2_selfdestruct_collision_two_different_transactions()

Documentation for tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py::test_dynamic_create2_selfdestruct_collision_two_different_transactions@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py::test_dynamic_create2_selfdestruct_collision_two_different_transactions --fork Amsterdam

Dynamic Create2->Suicide->Create2 collision scenario.

Perform a CREATE2, make sure that the initcode sets at least a couple of storage keys, then on a different call, in the same tx, perform a self-destruct.

Then

a) on the same tx, attempt to recreate the contract 1) and create2 contract already in the state 2) and create2 contract is not in the state b) on a different tx, attempt to recreate the contract -> Covered in this test

Perform a CREATE2, make sure that the initcode sets at least a couple of storage keys, then in a different tx, perform a self-destruct.

Then

a) on the same tx, attempt to recreate the contract b) on a different tx, attempt to recreate the contract

Check the test case described in https://lf-hyperledger.atlassian.net/wiki/spaces/BESU/pages/22156575/2024-01-06 +Mainnet+Halting+Event

Source code in tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
@pytest.mark.valid_from("Paris")
@pytest.mark.parametrize(
    "create2_dest_already_in_state",
    (
        pytest.param(True, marks=pytest.mark.pre_alloc_mutable),
        False,
    ),
)
@pytest.mark.parametrize(
    "call_create2_contract_at_the_end",
    (True, False),
)
def test_dynamic_create2_selfdestruct_collision_two_different_transactions(
    fork: Fork,
    create2_dest_already_in_state: bool,
    call_create2_contract_at_the_end: bool,
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
) -> None:
    """
    Dynamic Create2->Suicide->Create2 collision scenario.

    Perform a CREATE2, make sure that the initcode sets at least a couple of
    storage keys, then on a different call, in the same tx, perform a
    self-destruct.

    Then:
      a) on the same tx, attempt to recreate the contract
        1) and create2 contract already in the state
        2) and create2 contract is not in the state
      b) on a different tx, attempt to recreate the contract
         -> Covered in this test

    Perform a CREATE2, make sure that the initcode sets at
    least a couple of storage keys, then in a different tx, perform a
    self-destruct.

    Then:
      a) on the same tx, attempt to recreate the contract
      b) on a different tx, attempt to recreate the contract

    Check the test case described in
    https://lf-hyperledger.atlassian.net/wiki/spaces/BESU/pages/22156575/2024-01-06
    +Mainnet+Halting+Event
    """
    # assert call_create2_contract_at_the_end, "invalid test"

    # Storage locations
    create2_constructor_worked = 1
    first_create2_result = 2
    second_create2_result = 3
    code_worked = 4

    # Constants
    address_zero = Address(0x00)
    create2_salt = 1

    # Create EOA for sendall destination (receives selfdestruct funds)
    sendall_destination = pre.fund_eoa(0)  # Will be funded by selfdestruct
    # calls

    # Create storage contract that will be called during initialization
    address_create2_storage = pre.deploy_contract(
        code=Op.SSTORE(1, 1),
        balance=7000000000000000000,
    )

    # CREATE2 Initcode
    deploy_code = Op.SELFDESTRUCT(sendall_destination)
    initcode = Initcode(
        deploy_code=deploy_code,
        initcode_prefix=Op.SSTORE(create2_constructor_worked, 1)
        + Op.CALL(Op.GAS(), address_create2_storage, 0, 0, 0, 0, 0),
    )

    # Create the contract that performs CREATE2 operations
    address_code = pre.deploy_contract(
        code=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
        + Op.MSTORE(
            0,
            Op.CREATE2(Op.SELFBALANCE(), 0, Op.CALLDATASIZE(), create2_salt),
        )
        + Op.RETURN(0, 32),
    )

    # Created addresses
    create2_address = compute_create2_address(
        address_code, create2_salt, initcode
    )
    call_address_in_the_end = (
        create2_address if call_create2_contract_at_the_end else address_zero
    )

    # Values
    pre_existing_create2_balance = 1
    first_create2_value = 10
    first_call_value = 100
    second_create2_value = 1000
    second_call_value = 10000

    # Create the first contract that performs the first transaction
    address_to = pre.deploy_contract(
        code=Op.JUMPDEST()
        # Make a subcall that do CREATE2 and returns its the result
        + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
        + Op.CALL(
            100000,
            address_code,
            first_create2_value,
            0,
            Op.CALLDATASIZE(),
            0,
            32,
        )
        + Op.SSTORE(
            first_create2_result,
            Op.MLOAD(0),
        )
        # In case the create2 didn't work, flush account balance
        + Op.CALL(100000, address_code, 0, 0, 0, 0, 0)
        # Call to the created account to trigger selfdestruct
        + Op.CALL(100000, create2_address, first_call_value, 0, 0, 0, 0)
        + Op.SSTORE(code_worked, 1),
        balance=100000000,
        storage={first_create2_result: 0xFF},
    )

    # Create the second contract that performs the second transaction
    address_to_second = pre.deploy_contract(
        code=Op.JUMPDEST()
        # Make a subcall that do CREATE2 collision and returns its address as
        # the result
        + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
        + Op.CALL(
            100000,
            address_code,
            second_create2_value,
            0,
            Op.CALLDATASIZE(),
            0,
            32,
        )
        + Op.SSTORE(
            second_create2_result,
            Op.MLOAD(0),
        )
        # Call to the created account to trigger selfdestruct
        + Op.CALL(
            200000, call_address_in_the_end, second_call_value, 0, 0, 0, 0
        )
        + Op.SSTORE(code_worked, 1),
        balance=100000000,
        storage={second_create2_result: 0xFF},
    )

    # Create the sender EOA
    sender = pre.fund_eoa(7000000000000000000)

    if create2_dest_already_in_state:
        # Create2 address already in the state, e.g. deployed in a previous
        # block
        pre[create2_address] = Account(
            balance=pre_existing_create2_balance,
            nonce=1,
            code=deploy_code,
            storage={},
        )

    post: Dict[Address, Union[Account, object]] = {}

    # Create2 address only exists if it was pre-existing and after cancun
    post[create2_address] = (
        Account(
            balance=0,
            nonce=1,
            code=deploy_code,
            storage={create2_constructor_worked: 0x00},
        )
        if create2_dest_already_in_state and fork >= Cancun
        else (
            Account.NONEXISTENT
            if call_create2_contract_at_the_end
            else Account(balance=1000, nonce=1, code=deploy_code)
        )
    )

    # after Cancun Create2 initcode is only executed if the contract did not
    # already exist and before it will always be executed as the first tx
    # deletes the account
    post[address_create2_storage] = Account(
        storage={
            create2_constructor_worked: int(
                fork < Cancun or not create2_dest_already_in_state
            )
        }
    )

    # Entry code that makes the calls to the create2 contract creator
    post[address_to] = Account(
        storage={
            code_worked: 0x01,
            # First create2 only works if the contract was not preexisting
            first_create2_result: 0x00
            if create2_dest_already_in_state
            else create2_address,
        }
    )
    post[address_to_second] = Account(
        storage={
            code_worked: 0x01,
            # Second create2 will not collide before Cancun as the first tx
            # calls selfdestruct
            #
            # After cancun it will collide only if
            # create2_dest_already_in_state otherwise the first tx creates and
            # deletes it
            second_create2_result: (
                (0x00 if create2_dest_already_in_state else create2_address)
                if fork >= Cancun
                else create2_address
            ),
        }
    )

    # Calculate the destination account expected balance for the
    # selfdestruct/sendall calls
    sendall_destination_balance = 0

    if create2_dest_already_in_state:
        sendall_destination_balance += pre_existing_create2_balance
        if fork >= Cancun:
            # first create2 fails, but first calls ok. the account is not
            # removed on cancun therefore with the second create2 it is not
            # successful
            sendall_destination_balance += first_call_value
        else:
            # first create2 fails, first calls totally removes the account
            # in the second transaction second create2 is successful
            sendall_destination_balance += first_call_value
            if call_create2_contract_at_the_end:
                sendall_destination_balance += second_create2_value
    else:
        # if no account in the state, first create2 successful, first call
        # successful and removes because it is removed in the next transaction
        # second create2 successful
        sendall_destination_balance = first_create2_value + first_call_value
        if call_create2_contract_at_the_end:
            sendall_destination_balance += second_create2_value

    if call_create2_contract_at_the_end:
        sendall_destination_balance += second_call_value

    post[sendall_destination] = Account(balance=sendall_destination_balance)

    blockchain_test(
        pre=pre,
        post=post,
        blocks=[
            Block(
                txs=[
                    Transaction(
                        to=address_to,
                        data=initcode,
                        gas_limit=5_000_000,
                        sender=sender,
                    ),
                    Transaction(
                        to=address_to_second,
                        data=initcode,
                        gas_limit=5_000_000,
                        sender=sender,
                    ),
                ]
            )
        ],
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 6 forks.