Skip to content

Commit

Permalink
new(tests): EOF - EIP-7698: context variables
Browse files Browse the repository at this point in the history
  • Loading branch information
pdobacz committed Sep 30, 2024
1 parent 069bb07 commit df9a851
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
EOF creation transaction (empty `to`) tests
"""
16 changes: 16 additions & 0 deletions tests/prague/eip7692_eof_v1/eip7698_eof_creation_tx/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
A collection of contracts used in 7698 EOF tests
"""
import itertools

from ethereum_test_tools import Opcodes as Op
from ethereum_test_tools.eof.v1 import Container

"""Storage addresses for common testing fields"""
_slot = itertools.count()
next(_slot) # don't use slot 0
slot_call_result = next(_slot)

slot_last_slot = next(_slot)

smallest_runtime_subcontainer = Container.Code(code=Op.STOP, name="Runtime Subcontainer")
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""
Test execution of EOF creation txs
"""

import pytest

from ethereum_test_base_types.base_types import Address
from ethereum_test_tools import Account, Alloc, Environment, StateTestFiller, Transaction
from ethereum_test_tools.eof.v1 import Container, Section
from ethereum_test_tools.vm.opcode import Opcodes as Op
from ethereum_test_types.helpers import compute_create_address
from ethereum_test_vm.bytecode import Bytecode

from .. import EOF_FORK_NAME
from .helpers import slot_call_result, smallest_runtime_subcontainer

REFERENCE_SPEC_GIT_PATH = "EIPS/eip-7698.md"
REFERENCE_SPEC_VERSION = "ff544c14889aeb84be214546a09f410a67b919be"

pytestmark = pytest.mark.valid_from(EOF_FORK_NAME)


@pytest.mark.parametrize(
["destination_code", "expected_result"],
[
pytest.param(Op.ADDRESS, "destination"),
pytest.param(Op.CALLER, "sender"),
pytest.param(Op.CALLVALUE, "value"),
pytest.param(Op.ORIGIN, "sender"),
],
)
def test_eof_creation_tx_context(
state_test: StateTestFiller,
pre: Alloc,
destination_code: Bytecode,
expected_result: str,
):
"""Test EOF creation txs' initcode context instructions"""
env = Environment()
sender = pre.fund_eoa()
value = 0x1123

initcode = Container(
sections=[
Section.Code(
Op.SSTORE(slot_call_result, destination_code) + Op.RETURNCONTRACT[0](0, 0)
),
Section.Container(smallest_runtime_subcontainer),
]
)

destination_contract_address = compute_create_address(address=sender, nonce=sender.nonce)

tx = Transaction(sender=sender, to=None, gas_limit=100000, value=value, input=initcode)

expected_bytes: Address | int
if expected_result == "destination":
expected_bytes = destination_contract_address
elif expected_result == "sender":
expected_bytes = sender
elif expected_result == "value":
expected_bytes = value
else:
raise TypeError("Unexpected expected_result", expected_result)

destination_contract_storage = {
slot_call_result: expected_bytes,
}

post = {
destination_contract_address: Account(storage=destination_contract_storage),
}

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

0 comments on commit df9a851

Please sign in to comment.