diff --git a/tests/frontier/create/test_create_suicide_during_init.py b/tests/frontier/create/test_create_suicide_during_init.py new file mode 100644 index 00000000000..654564da2b7 --- /dev/null +++ b/tests/frontier/create/test_create_suicide_during_init.py @@ -0,0 +1,102 @@ +"""Deploy contract that calls selfdestruct in it's initcode.""" + +from enum import Enum + +import pytest + +from ethereum_test_forks import Byzantium, Fork +from ethereum_test_tools import ( + Account, + Alloc, + Environment, + Initcode, + StateTestFiller, + Transaction, + compute_create_address, +) +from ethereum_test_tools import Opcodes as Op + + +class Operation(Enum): + """Enum for created contract actions.""" + + SUICIDE = 1 + SUICIDE_TO_ITSELF = 2 + + def __int__(self): + """Convert to int.""" + return int(self.value) + + +@pytest.mark.ported_from( + [ + "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stCreateTest/CREATE_ContractSuicideDuringInit_ThenStoreThenReturnFiller.json", + "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueFiller.json", + "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueToItselfFiller.json", + "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stCreateTest/CREATE_ContractSuicideDuringInitFiller.json", + ], + pr=["https://github.com/ethereum/execution-spec-tests/pull/1871"], + # coverage_missed_reason="Converting solidity code result in following opcode not being used:", +) +@pytest.mark.valid_from("Frontier") +@pytest.mark.with_all_create_opcodes +@pytest.mark.parametrize("transaction_create", [False, True]) +@pytest.mark.parametrize( + "operation", + [Operation.SUICIDE, Operation.SUICIDE_TO_ITSELF], +) +def test_create_suicide_during_transaction_create( + state_test: StateTestFiller, + fork: Fork, + pre: Alloc, + create_opcode: Op, + operation: Operation, + transaction_create: bool, +): + """Contract init code calls suicide then measures different metrics.""" + if create_opcode == Op.CREATE2 and transaction_create: + pytest.skip("Excluded: CREATE2 with transaction_create=True") + + sender = pre.fund_eoa() + contract_deploy = pre.deploy_contract( + code=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE) + + create_opcode(size=Op.CALLDATASIZE(), value=Op.CALLVALUE()) + ) + contract_success = pre.deploy_contract(code=Op.SSTORE(1, 1)) + contract_after_suicide = pre.deploy_contract(code=Op.SSTORE(1, 1)) + + contract_initcode = Initcode( + initcode_prefix=Op.CALL(address=contract_success, gas=Op.SUB(Op.GAS, 100_000)) + + Op.SELFDESTRUCT( + Op.ADDRESS if operation == Operation.SUICIDE_TO_ITSELF else contract_success + ) + + Op.CALL(address=contract_after_suicide, gas=Op.SUB(Op.GAS, 100_000)), + deploy_code=Op.SSTORE(0, 1), + ) + + expected_create_address = compute_create_address( + address=sender if transaction_create else contract_deploy, + nonce=1 if transaction_create else 0, + initcode=contract_initcode, + opcode=create_opcode, + ) + + tx = Transaction( + gas_limit=1_000_000, + to=None if transaction_create else contract_deploy, + data=contract_initcode, + nonce=0, + value=100, + sender=sender, + protected=fork >= Byzantium, + ) + + post = { + contract_success: Account( + balance=0 if operation == Operation.SUICIDE_TO_ITSELF else 100, storage={1: 1} + ), + contract_deploy: Account(storage={0: 0}), + contract_after_suicide: Account(storage={1: 0}), # suicide eats all gas + expected_create_address: Account.NONEXISTENT, + } + state_test(env=Environment(), pre=pre, post=post, tx=tx) diff --git a/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInitFiller.json b/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInitFiller.json deleted file mode 100644 index fdca1044d16..00000000000 --- a/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInitFiller.json +++ /dev/null @@ -1,66 +0,0 @@ -{ - "CREATE_ContractSuicideDuringInit" : { - "env" : { - "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", - "currentDifficulty" : "0x020000", - "currentGasLimit" : "10000000", - "currentNumber" : "1", - "currentTimestamp" : "1000" - }, - "expect" : [ - { - "indexes" : { - "data" : -1, - "gas" : -1, - "value" : -1 - }, - "network" : [">=Cancun"], - "result" : { - "6295ee1b4f6dd65047762f924ecd367c17eabf8f" : { - "shouldnotexist" : "1" - }, - "c94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "0", - "storage" : { - "0x01" : "0x0c" - } - } - } - } - ], - "pre" : - { - "a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "1000000000000", - "code" : "", - "nonce" : "0", - "storage" : { - } - }, - "c94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "0", - "// code" : "This indicates that a call from transaction was initiated", - "code" : "{[[1]]12}", - "nonce" : "0", - "storage" : { - } - } - }, - "transaction" : { - "// data" : "{(CALL 60000 0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b 0 0 0 0 0) (MSTORE 0 0x64600c6000556000526005601bf3) (SELFDESTRUCT 0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b)}", - "data" : [ - ":raw 0x6000600060006000600073c94f5374fce5edbc8e2a8697c15331677e6ebf0b61ea60f1506d64600c6000556000526005601bf360005273c94f5374fce5edbc8e2a8697c15331677e6ebf0bff" - ], - "gasLimit" : [ - "150000" - ], - "gasPrice" : "10", - "nonce" : "0", - "secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8", - "to" : "", - "value" : [ - "0" - ] - } - } -} diff --git a/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInit_ThenStoreThenReturnFiller.json b/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInit_ThenStoreThenReturnFiller.json deleted file mode 100644 index 8b13ce103af..00000000000 --- a/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInit_ThenStoreThenReturnFiller.json +++ /dev/null @@ -1,80 +0,0 @@ -{ - "CREATE_ContractSuicideDuringInit_ThenStoreThenReturn" : { - "env" : { - "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", - "currentDifficulty" : "0x020000", - "currentGasLimit" : "10000000", - "currentNumber" : "1", - "currentTimestamp" : "1000" - }, - "expect" : [ - { - "indexes" : { - "data" : -1, - "gas" : -1, - "value" : -1 - }, - "network" : [">=Cancun"], - "result" : { - "6295ee1b4f6dd65047762f924ecd367c17eabf8f" : { - "shouldnotexist" : "1" - }, - "c94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "0", - "storage" : { - "0x01" : "0x0c" - } - }, - "d94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "10", - "storage" : { - "0x01" : "0x00" - } - } - } - } - ], - "pre" : - { - "a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "1000000000000", - "code" : "", - "nonce" : "0", - "storage" : { - } - }, - "c94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "0", - "// code" : "This indicates that a call from transaction was initiated", - "code" : "{[[1]]12}", - "nonce" : "0", - "storage" : { - } - }, - "d94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "0", - "// code" : "This indicates that a call from transaction was initiated", - "code" : "{[[1]]12}", - "nonce" : "0", - "storage" : { - } - } - }, - "transaction" : { - "// data" : "{ (CALL 60000 0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b 0 0 0 0 0) (MSTORE 0 0x64600c6000556000526005601bf3) (SELFDESTRUCT 0xd94f5374fce5edbc8e2a8697c15331677e6ebf0b) (CALL 60000 0xd94f5374fce5edbc8e2a8697c15331677e6ebf0b 0 0 0 0 0) [[0]]11 (RETURN 18 14)}", - "data" : [ - ":raw 0x6000600060006000600073c94f5374fce5edbc8e2a8697c15331677e6ebf0b61ea60f1506d64600c6000556000526005601bf360005273d94f5374fce5edbc8e2a8697c15331677e6ebf0bff6000600060006000600073d94f5374fce5edbc8e2a8697c15331677e6ebf0b61ea60f150600b600055600e6012f3" - ], - "gasLimit" : [ - "600000" - ], - "gasPrice" : "10", - "nonce" : "0", - "secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8", - "to" : "", - "value" : [ - "10" - ] - } - } -} diff --git a/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueFiller.json b/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueFiller.json deleted file mode 100644 index e2ce0d3a8ac..00000000000 --- a/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueFiller.json +++ /dev/null @@ -1,66 +0,0 @@ -{ - "CREATE_ContractSuicideDuringInit_WithValue" : { - "env" : { - "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", - "currentDifficulty" : "0x020000", - "currentGasLimit" : "10000000", - "currentNumber" : "1", - "currentTimestamp" : "1000" - }, - "expect" : [ - { - "indexes" : { - "data" : -1, - "gas" : -1, - "value" : -1 - }, - "network" : [">=Cancun"], - "result" : { - "6295ee1b4f6dd65047762f924ecd367c17eabf8f" : { - "shouldnotexist" : "1" - }, - "c94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "10", - "storage" : { - "0x01" : "0x0c" - } - } - } - } - ], - "pre" : - { - "a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "1000000000000", - "code" : "", - "nonce" : "0", - "storage" : { - } - }, - "c94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "0", - "// code" : "This indicates that a call from transaction was initiated", - "code" : "{[[1]]12}", - "nonce" : "0", - "storage" : { - } - } - }, - "transaction" : { - "// data" : "{(CALL 60000 0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b 0 0 0 0 0) (MSTORE 0 0x64600c6000556000526005601bf3) (SELFDESTRUCT 0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b)}", - "data" : [ - ":raw 0x6000600060006000600073c94f5374fce5edbc8e2a8697c15331677e6ebf0b61ea60f1506d64600c6000556000526005601bf360005273c94f5374fce5edbc8e2a8697c15331677e6ebf0bff" - ], - "gasLimit" : [ - "150000" - ], - "gasPrice" : "10", - "nonce" : "0", - "secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8", - "to" : "", - "value" : [ - "10" - ] - } - } -} diff --git a/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueToItselfFiller.json b/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueToItselfFiller.json deleted file mode 100644 index fe8bf0a99d9..00000000000 --- a/tests/static/state_tests/stCreateTest/CREATE_ContractSuicideDuringInit_WithValueToItselfFiller.json +++ /dev/null @@ -1,70 +0,0 @@ -{ - "CREATE_ContractSuicideDuringInit_WithValueToItself" : { - "env" : { - "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", - "currentDifficulty" : "0x020000", - "currentGasLimit" : "10000000", - "currentNumber" : "1", - "currentTimestamp" : "1000" - }, - "expect" : [ - { - "network" : [">=Cancun"], - "result" : { - "6295ee1b4f6dd65047762f924ecd367c17eabf8f" : { - "shouldnotexist" : "1" - }, - "1a3e089e89a1d995552b4005c40cd5c8832b929b" : { - "shouldnotexist" : "1" - }, - "c94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "0", - "storage" : { - "0x01" : "0x0c" - } - } - } - } - ], - "pre" : - { - "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" : { - "balance" : "0", - "code" : "", - "nonce" : "1", - "storage" : { - } - }, - "a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "100000000000000", - "code" : "", - "nonce" : "0", - "storage" : { - } - }, - "c94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { - "balance" : "0", - "// code" : "This indicates that a call from transaction was initiated", - "code" : "{[[1]]12}", - "nonce" : "0", - "storage" : { - } - } - }, - "transaction" : { - "data" : [ - "{(CALL 60000 0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b 0 0 0 0 0) (MSTORE 0 0x64600c6000556000526005601bf3) (SELFDESTRUCT (ADDRESS))}" - ], - "gasLimit" : [ - "150000" - ], - "gasPrice" : "10", - "nonce" : "0", - "secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8", - "to" : "", - "value" : [ - "10" - ] - } - } -}