Skip to content

Commit

Permalink
refactor: rewrite code using Opcodes library
Browse files Browse the repository at this point in the history
  • Loading branch information
danceratopz committed Jul 6, 2023
1 parent 3895796 commit 7dc5a63
Showing 1 changed file with 48 additions and 15 deletions.
63 changes: 48 additions & 15 deletions tests/cancun/eip6780_selfdestruct/test_selfdestruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pytest

from ethereum_test_tools import Account, Environment, StateTestFiller, TestAddress, Transaction
from ethereum_test_tools.vm.opcode import Opcodes as Op

REFERENCE_SPEC_GIT_PATH = "EIPS/eip-6780.md"
REFERENCE_SPEC_VERSION = "2f8299df31bb8173618901a03a8366a3183479b0"
Expand All @@ -18,7 +19,8 @@
@pytest.mark.valid_from("Shanghai")
def test_create_selfdestruct_same_tx(state_test: StateTestFiller):
"""
TODO Test selfdestruct in CREATE.
TODO test that if a contract is created and then selfdestructs in the same
transaction the contract should not be created.
"""
env = Environment(
coinbase="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
Expand All @@ -29,6 +31,12 @@ def test_create_selfdestruct_same_tx(state_test: StateTestFiller):
)
post: Dict[Any, Any] = {}

pre = {
TestAddress: Account(balance=1000000000000000000000),
}

"""
original code
test_ops = [
"6032",
"601c",
Expand Down Expand Up @@ -62,15 +70,36 @@ def test_create_selfdestruct_same_tx(state_test: StateTestFiller):
# inner payload:
"60016000556000ff",
]
"""
code = Op.CODECOPY(0, 0x1C, 0x32)
# create account
code += Op.CREATE(0, 0, 0x32)
# TODO call created account
code += Op.PUSH0
code += Op.PUSH0
code += Op.PUSH0
code += Op.PUSH0
code += Op.PUSH0
code += Op.DUP6 # dup the returned address
code += Op.GASLIMIT
code += Op.CALL()
code += Op.STOP
# payload:
# copy inner payload to memory
code += Op.CODECOPY(0, 0x0C, 0x08)
# return payload
# inner payload:
code += Op.RETURN(0x00, 0x08)
code += Op.SSTORE(0, 1)
code += Op.PUSH0
code += Op.SELFDESTRUCT

post["0x5fef11c6545be552c986e9eaac3144ecf2258fd3"] = Account.NONEXISTENT

pre = {
TestAddress: Account(balance=1000000000000000000000),
}
tx = Transaction(
ty=0x0,
data="".join(test_ops),
# data="".join(test_ops),
data=code,
chain_id=0x0,
nonce=0,
to=None,
Expand All @@ -82,11 +111,14 @@ def test_create_selfdestruct_same_tx(state_test: StateTestFiller):
state_test(env=env, pre=pre, post=post, txs=[tx], tag="6780-create-inside-tx")


@pytest.mark.xfail(run=True, reason="Unknown, tbd")
@pytest.mark.xfail(
run=True, reason="The account containing self-destruct is not present in the post-alloc."
)
@pytest.mark.valid_from("Shanghai")
def test_selfdestruct_prev_created(state_test: StateTestFiller):
"""
TODO
Test that if a previously created account that contains a selfdestruct is
called, its balance is sent to the zero address.
"""
env = Environment(
coinbase="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
Expand All @@ -95,19 +127,20 @@ def test_selfdestruct_prev_created(state_test: StateTestFiller):
number=1,
timestamp=1000,
)
post: Dict[Any, Any] = {}

post["0x1111111111111111111111111111111111111111"] = Account(
balance=0, code="0x60016000556000ff"
)
post["0x0000000000000000000000000000000000000000"] = Account(balance=1)
# original code: 0x60016000556000ff # noqa: SC100
code = Op.SSTORE(0, 1) + Op.PUSH0 + Op.SELFDESTRUCT

pre = {
TestAddress: Account(balance=1000000000000000000000),
"0x1111111111111111111111111111111111111111": Account(
balance=1, code="0x60016000556000ff"
),
"0x1111111111111111111111111111111111111111": Account(balance=1, code=code),
}

post = {
"0x1111111111111111111111111111111111111111": Account(balance=0, code=code),
"0x0000000000000000000000000000000000000000": Account(balance=1),
}

tx = Transaction(
ty=0x0,
data="",
Expand Down

0 comments on commit 7dc5a63

Please sign in to comment.