|
| 1 | +"""Deploy contract that calls selfdestruct in it's initcode.""" |
| 2 | + |
| 3 | +from enum import Enum |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ethereum_test_forks import Byzantium, Fork |
| 8 | +from ethereum_test_tools import ( |
| 9 | + Account, |
| 10 | + Alloc, |
| 11 | + Environment, |
| 12 | + Initcode, |
| 13 | + StateTestFiller, |
| 14 | + Transaction, |
| 15 | + compute_create_address, |
| 16 | +) |
| 17 | +from ethereum_test_tools import Opcodes as Op |
| 18 | + |
| 19 | + |
| 20 | +class Operation(Enum): |
| 21 | + """Enum for created contract actions.""" |
| 22 | + |
| 23 | + SUICIDE = 1 |
| 24 | + SUICIDE_TO_ITSELF = 2 |
| 25 | + |
| 26 | + def __int__(self): |
| 27 | + """Convert to int.""" |
| 28 | + return int(self.value) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.ported_from( |
| 32 | + [ |
| 33 | + "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stCreateTest/CREATE_ContractSuicideDuringInit_ThenStoreThenReturnFiller.json", |
| 34 | + "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueFiller.json", |
| 35 | + "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueToItselfFiller.json", |
| 36 | + "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stCreateTest/CREATE_ContractSuicideDuringInitFiller.json", |
| 37 | + ], |
| 38 | + pr=["https://github.com/ethereum/execution-spec-tests/pull/1871"], |
| 39 | + # coverage_missed_reason="Converting solidity code result in following opcode not being used:", |
| 40 | +) |
| 41 | +@pytest.mark.valid_from("Frontier") |
| 42 | +@pytest.mark.with_all_create_opcodes |
| 43 | +@pytest.mark.parametrize("transaction_create", [False, True]) |
| 44 | +@pytest.mark.parametrize( |
| 45 | + "operation", |
| 46 | + [Operation.SUICIDE, Operation.SUICIDE_TO_ITSELF], |
| 47 | +) |
| 48 | +def test_create_suicide_during_transaction_create( |
| 49 | + state_test: StateTestFiller, |
| 50 | + fork: Fork, |
| 51 | + pre: Alloc, |
| 52 | + create_opcode: Op, |
| 53 | + operation: Operation, |
| 54 | + transaction_create: bool, |
| 55 | +): |
| 56 | + """Contract init code calls suicide then measures different metrics.""" |
| 57 | + if create_opcode == Op.CREATE2 and transaction_create: |
| 58 | + pytest.skip("Excluded: CREATE2 with transaction_create=True") |
| 59 | + |
| 60 | + sender = pre.fund_eoa() |
| 61 | + contract_deploy = pre.deploy_contract( |
| 62 | + code=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE) |
| 63 | + + create_opcode(size=Op.CALLDATASIZE(), value=Op.CALLVALUE()) |
| 64 | + ) |
| 65 | + contract_success = pre.deploy_contract(code=Op.SSTORE(1, 1)) |
| 66 | + contract_after_suicide = pre.deploy_contract(code=Op.SSTORE(1, 1)) |
| 67 | + |
| 68 | + contract_initcode = Initcode( |
| 69 | + initcode_prefix=Op.CALL(address=contract_success, gas=Op.SUB(Op.GAS, 100_000)) |
| 70 | + + Op.SELFDESTRUCT( |
| 71 | + Op.ADDRESS if operation == Operation.SUICIDE_TO_ITSELF else contract_success |
| 72 | + ) |
| 73 | + + Op.CALL(address=contract_after_suicide, gas=Op.SUB(Op.GAS, 100_000)), |
| 74 | + deploy_code=Op.SSTORE(0, 1), |
| 75 | + ) |
| 76 | + |
| 77 | + expected_create_address = compute_create_address( |
| 78 | + address=sender if transaction_create else contract_deploy, |
| 79 | + nonce=1 if transaction_create else 0, |
| 80 | + initcode=contract_initcode, |
| 81 | + opcode=create_opcode, |
| 82 | + ) |
| 83 | + |
| 84 | + tx = Transaction( |
| 85 | + gas_limit=1_000_000, |
| 86 | + to=None if transaction_create else contract_deploy, |
| 87 | + data=contract_initcode, |
| 88 | + nonce=0, |
| 89 | + value=100, |
| 90 | + sender=sender, |
| 91 | + protected=fork >= Byzantium, |
| 92 | + ) |
| 93 | + |
| 94 | + post = { |
| 95 | + contract_success: Account( |
| 96 | + balance=0 if operation == Operation.SUICIDE_TO_ITSELF else 100, storage={1: 1} |
| 97 | + ), |
| 98 | + contract_deploy: Account(storage={0: 0}), |
| 99 | + contract_after_suicide: Account(storage={1: 0}), # suicide eats all gas |
| 100 | + expected_create_address: Account.NONEXISTENT, |
| 101 | + } |
| 102 | + state_test(env=Environment(), pre=pre, post=post, tx=tx) |
0 commit comments