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 | @pytest.mark.parametrize("create_opcode", [Op.CREATE, Op.CREATE2])
@pytest.mark.parametrize("call_times", [0, 1])
@pytest.mark.parametrize(
"selfdestruct_contract_initial_balance",
[0, 100_000],
)
@pytest.mark.valid_from("Shanghai")
def test_self_destructing_initcode(
state_test: StateTestFiller,
pre: Alloc,
sender: EOA,
selfdestruct_code: Bytecode,
sendall_recipient_addresses: List[Address],
create_opcode: Op,
call_times: int, # Number of times to call the self-destructing contract
# in the same tx
selfdestruct_contract_initial_balance: int,
) -> None:
"""
Test that a contract can self-destruct in its initcode.
Behavior is the same before and after EIP-6780.
Test using:
- Different initial balances for the self-destructing contract
- Different opcodes: CREATE, CREATE2
- Different number of calls to the self-destructing contract in
the same tx
"""
initcode_copy_from_address = pre.deploy_contract(selfdestruct_code)
# Our entry point is an initcode that in turn creates a self-destructing
# contract
entry_code_storage = Storage()
sendall_amount = 0
# Bytecode used to create the contract, can be CREATE or CREATE2
create_bytecode = create_opcode(size=len(selfdestruct_code))
selfdestruct_contract_address = compute_create_address(
address=compute_create_address(address=sender, nonce=0),
nonce=1,
initcode=selfdestruct_code,
opcode=create_opcode,
)
# Entry code that will be executed, creates the contract and then calls it
# in the same tx
entry_code = (
# Initcode is already deployed at `initcode_copy_from_address`, so just
# copy it
Op.EXTCODECOPY(
initcode_copy_from_address,
0,
0,
len(selfdestruct_code),
)
# And we store the created address for verification purposes
+ Op.SSTORE(
entry_code_storage.store_next(selfdestruct_contract_address),
create_bytecode,
)
)
# Store the EXTCODE* properties of the created address
entry_code += Op.SSTORE(
entry_code_storage.store_next(0),
Op.EXTCODESIZE(selfdestruct_contract_address),
)
entry_code += Op.SSTORE(
entry_code_storage.store_next(Bytecode().keccak256()),
Op.EXTCODEHASH(selfdestruct_contract_address),
)
# Call the self-destructing contract multiple times as required, increasing
# the wei sent each time
entry_code_balance = 0
for i in range(call_times):
entry_code += Op.SSTORE(
entry_code_storage.store_next(1),
Op.CALL(
Op.GASLIMIT, # Gas
selfdestruct_contract_address, # Address
i, # Value
0,
0,
0,
0,
),
)
entry_code_balance += i
entry_code += Op.SSTORE(
entry_code_storage.store_next(0),
Op.BALANCE(selfdestruct_contract_address),
)
# Lastly return zero so the entry point contract is created and we can
# retain the stored values for verification.
entry_code += Op.RETURN(max(len(selfdestruct_code), 32), 1)
if selfdestruct_contract_initial_balance > 0:
# Address where the contract is created already had some balance,
# which must be included in the send-all operation
sendall_amount += selfdestruct_contract_initial_balance
pre.fund_address(
selfdestruct_contract_address,
selfdestruct_contract_initial_balance,
)
tx = Transaction(
value=entry_code_balance,
data=entry_code,
sender=sender,
to=None,
gas_limit=500_000,
)
entry_code_address = tx.created_contract
post: Dict[Address, Account] = {
entry_code_address: Account(
storage=entry_code_storage,
),
selfdestruct_contract_address: Account.NONEXISTENT, # type: ignore
sendall_recipient_addresses[0]: Account(
balance=sendall_amount, storage={0: 1}
),
}
state_test(pre=pre, post=post, tx=tx)
|