Skip to content

type(tests): convert create suicide during init #1871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions tests/frontier/create/test_create_suicide_during_init.py
Original file line number Diff line number Diff line change
@@ -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)

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading