Skip to content
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

feat/adapt eip6780 selfdestruct revert & reentrancy_selfdestruct to execute mode #1167

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions tests/cancun/eip6780_selfdestruct/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Pytest (plugin) definitions local to EIP-6780 tests."""

import random

import pytest

from ethereum_test_tools import EOA, Address, Alloc, Environment


@pytest.fixture
def sender(pre: Alloc) -> EOA:
"""EOA that will be used to send transactions."""
return pre.fund_eoa()


@pytest.fixture
def env() -> Environment:
"""Environment for all tests."""
return Environment()


@pytest.fixture
def selfdestruct_recipient_address() -> Address:
"""Address that can receive a SELFDESTRUCT operation."""
return Address("0x" + "".join(["{:02x}".format(random.randint(0, 255)) for _ in range(20)]))
huyngopt1994 marked this conversation as resolved.
Show resolved Hide resolved
139 changes: 55 additions & 84 deletions tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
"""tests for selfdestruct interaction with revert."""

from itertools import count
from typing import Dict

import pytest

from ethereum_test_forks import Cancun
from ethereum_test_tools import (
EOA,
Account,
Address,
Alloc,
Bytecode,
Environment,
Initcode,
StateTestFiller,
Storage,
TestAddress,
Transaction,
YulCompiler,
compute_create_address,
Expand All @@ -28,31 +28,30 @@


@pytest.fixture
def entry_code_address() -> Address:
def entry_code_address(sender: EOA) -> Address:
"""Address where the entry code will run."""
return compute_create_address(address=TestAddress, nonce=0)
return compute_create_address(address=sender, nonce=0)


@pytest.fixture
def recursive_revert_contract_address() -> Address:
"""Address where the recursive revert contract address exists."""
return Address(0xDEADBEEF)
def recursive_revert_contract_address_init_balance() -> int:
"""Return initial balance for recursive_revert_contract_address."""
return 3


@pytest.fixture
def env() -> Environment:
"""Environment for all tests."""
return Environment(
fee_recipient="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
def recursive_revert_contract_address(
pre: Alloc,
recursive_revert_contract_code: Bytecode,
recursive_revert_contract_address_init_balance: int,
) -> Address:
"""Address where the recursive revert contract address exists."""
return pre.deploy_contract(
code=recursive_revert_contract_code,
balance=recursive_revert_contract_address_init_balance,
)


@pytest.fixture
def selfdestruct_recipient_address() -> Address:
"""List of possible addresses that can receive a SELFDESTRUCT operation."""
return Address(0x1234)


@pytest.fixture
def selfdestruct_on_outer_call() -> int:
"""Whether to selfdestruct the target contract in the outer call scope."""
Expand Down Expand Up @@ -128,9 +127,17 @@ def recursive_revert_contract_code(


@pytest.fixture
def selfdestruct_with_transfer_contract_address(entry_code_address: Address) -> Address:
def selfdestruct_with_transfer_contract_address(
pre: Alloc,
entry_code_address: Address,
selfdestruct_with_transfer_contract_code: Bytecode,
same_tx: bool,
) -> Address:
"""Contract address for contract that can selfdestruct and receive value."""
return compute_create_address(address=entry_code_address, nonce=1)
if same_tx:
return compute_create_address(address=entry_code_address, nonce=1)
# We need to deploy the contract before.
return pre.deploy_contract(selfdestruct_with_transfer_contract_code)


@pytest.fixture
Expand Down Expand Up @@ -173,30 +180,20 @@ def selfdestruct_with_transfer_contract_initcode(


@pytest.fixture
def selfdestruct_with_transfer_initcode_copy_from_address() -> Address:
"""Address of a pre-existing contract we use to simply copy initcode from."""
return Address(0xABCD)


@pytest.fixture
def pre(
recursive_revert_contract_address: Address,
recursive_revert_contract_code: Bytecode,
selfdestruct_with_transfer_initcode_copy_from_address: Address,
def selfdestruct_with_transfer_initcode_copy_from_address(
pre: Alloc,
selfdestruct_with_transfer_contract_initcode: Bytecode,
selfdestruct_with_transfer_contract_address: Address,
yul: YulCompiler,
) -> Dict[Address, Account]:
"""Prestate for test_selfdestruct_not_created_in_same_tx_with_revert."""
return {
TestAddress: Account(balance=100_000_000_000_000_000_000),
selfdestruct_with_transfer_initcode_copy_from_address: Account(
code=selfdestruct_with_transfer_contract_initcode
),
recursive_revert_contract_address: Account(code=recursive_revert_contract_code, balance=3),
}
) -> Address:
"""Address of a pre-existing contract we use to simply copy initcode from."""
addr = pre.deploy_contract(selfdestruct_with_transfer_contract_initcode)
return addr


@pytest.mark.parametrize(
"same_tx",
[True],
ids=["same_tx"],
)
@pytest.mark.parametrize(
"selfdestruct_on_outer_call",
[0, 1, 2],
Expand All @@ -209,8 +206,9 @@ def pre(
@pytest.mark.valid_from("Cancun")
def test_selfdestruct_created_in_same_tx_with_revert( # noqa SC200
state_test: StateTestFiller,
sender: EOA,
env: Environment,
pre: Dict[Address, Account],
pre: Alloc,
entry_code_address: Address,
selfdestruct_on_outer_call: int,
selfdestruct_with_transfer_contract_code: Bytecode,
Expand Down Expand Up @@ -292,41 +290,27 @@ def test_selfdestruct_created_in_same_tx_with_revert( # noqa SC200
)
post[selfdestruct_recipient_address] = Account.NONEXISTENT # type: ignore

nonce = count()
tx = Transaction(
ty=0x0,
value=0,
data=entry_code,
chain_id=0x0,
nonce=next(nonce),
sender=sender,
to=None,
gas_limit=100_000_000,
gas_price=10,
protected=False,
gas_limit=20_000_000,
)

state_test(env=env, pre=pre, post=post, tx=tx)


@pytest.fixture
def pre_with_selfdestructable( # noqa: SC200
recursive_revert_contract_address: Address,
recursive_revert_contract_code: Bytecode,
selfdestruct_with_transfer_initcode_copy_from_address: Address,
selfdestruct_with_transfer_contract_initcode: Bytecode,
selfdestruct_with_transfer_contract_address: Address,
yul: YulCompiler,
) -> Dict[Address, Account]:
"""Preset for selfdestruct_not_created_in_same_tx_with_revert."""
return {
TestAddress: Account(balance=100_000_000_000_000_000_000),
selfdestruct_with_transfer_initcode_copy_from_address: Account(
code=selfdestruct_with_transfer_contract_initcode
),
recursive_revert_contract_address: Account(code=recursive_revert_contract_code, balance=2),
}


@pytest.mark.parametrize(
"recursive_revert_contract_address_init_balance",
[2],
ids=["init_balance_2"],
)
@pytest.mark.parametrize(
"same_tx",
[False],
huyngopt1994 marked this conversation as resolved.
Show resolved Hide resolved
ids=["not_same_tx"],
)
@pytest.mark.parametrize(
"selfdestruct_on_outer_call",
[0, 1, 2],
Expand All @@ -339,8 +323,10 @@ def pre_with_selfdestructable( # noqa: SC200
@pytest.mark.valid_from("Cancun")
def test_selfdestruct_not_created_in_same_tx_with_revert(
state_test: StateTestFiller,
sender: EOA,
env: Environment,
entry_code_address: Address,
pre: Alloc,
selfdestruct_on_outer_call: int,
selfdestruct_with_transfer_contract_code: Bytecode,
selfdestruct_with_transfer_contract_address: Address,
Expand All @@ -362,16 +348,6 @@ def test_selfdestruct_not_created_in_same_tx_with_revert(
0, # ret length
)

pre: Dict[Address, Account] = {
TestAddress: Account(balance=100_000_000_000_000_000_000),
selfdestruct_with_transfer_contract_address: Account(
code=selfdestruct_with_transfer_contract_code
),
recursive_revert_contract_address: Account(
code=bytes(recursive_revert_contract_code), balance=2
),
}

post: Dict[Address, Account] = {
entry_code_address: Account(code="0x"),
}
Expand Down Expand Up @@ -407,17 +383,12 @@ def test_selfdestruct_not_created_in_same_tx_with_revert(
)
post[selfdestruct_recipient_address] = Account.NONEXISTENT # type: ignore

nonce = count()
tx = Transaction(
ty=0x0,
value=0,
data=entry_code,
chain_id=0x0,
nonce=next(nonce),
sender=sender,
to=None,
gas_limit=100_000_000,
gas_price=10,
protected=False,
gas_limit=20_000_000,
)

state_test(env=env, pre=pre, post=post, tx=tx)
Loading