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

new(tests): Exported Evmone EOF unit tests #786

Open
wants to merge 3 commits 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
8 changes: 7 additions & 1 deletion src/ethereum_test_exceptions/evmone_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class EvmoneExceptionMapper:
ExceptionMessage(
EOFException.INVALID_NON_RETURNING_FLAG, "err: invalid_non_returning_flag"
),
ExceptionMessage(EOFException.CALLF_TO_NON_RETURNING_FUNCTION, "err: callf_to_non_returning_function"),
ExceptionMessage(EOFException.INVALID_MAGIC, "err: invalid_prefix"),
ExceptionMessage(
EOFException.INVALID_FIRST_SECTION_TYPE, "err: invalid_first_section_type"
Expand All @@ -57,6 +58,7 @@ class EvmoneExceptionMapper:
ExceptionMessage(EOFException.INVALID_RJUMP_DESTINATION, "err: invalid_rjump_destination"),
ExceptionMessage(EOFException.UNREACHABLE_CODE_SECTIONS, "err: unreachable_code_sections"),
ExceptionMessage(EOFException.STACK_UNDERFLOW, "err: stack_underflow"),
ExceptionMessage(EOFException.STACK_OVERFLOW, "err: stack_overflow"),
ExceptionMessage(
EOFException.MAX_STACK_HEIGHT_ABOVE_LIMIT, "err: max_stack_height_above_limit"
),
Expand Down Expand Up @@ -84,10 +86,14 @@ class EvmoneExceptionMapper:
EOFException.INCOMPATIBLE_CONTAINER_KIND, "err: incompatible_container_kind"
),
ExceptionMessage(EOFException.STACK_HEIGHT_MISMATCH, "err: stack_height_mismatch"),
ExceptionMessage(EOFException.TOO_MANY_CONTAINERS, "err: too_many_container_sections"),
ExceptionMessage(EOFException.TOO_MANY_CONTAINER_SECTIONS, "err: too_many_container_sections"),
ExceptionMessage(EOFException.EOF_CREATE_WITH_TRUNCATED_CONTAINER, "err: eofcreate_with_truncated_container"),
ExceptionMessage(
EOFException.INVALID_CODE_SECTION_INDEX, "err: invalid_code_section_index"
),
ExceptionMessage(
EOFException.AMBIGUOUS_CONTAINER_KIND, "err: ambiguous_container_kind"
),
)

def __init__(self) -> None:
Expand Down
18 changes: 17 additions & 1 deletion src/ethereum_test_exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,10 @@ class EOFException(ExceptionBase):
"""
EOF container's section has non-returning flag set incorrectly.
"""
CALLF_TO_NON_RETURNING_FUNCTION = auto()
"""
EOF container's section has CALLF instruction to non-returning function.
"""
INVALID_RJUMP_DESTINATION = auto()
"""
Code has RJUMP instruction with invalid parameters.
Expand Down Expand Up @@ -665,6 +669,10 @@ class EOFException(ExceptionBase):
"""
EOF container header has too many code sections.
"""
EOF_CREATE_WITH_TRUNCATED_CONTAINER = auto()
"""
EOF Create has truncated container.
"""
MISSING_STOP_OPCODE = auto()
"""
EOF container's code missing STOP bytecode at it's end.
Expand All @@ -685,6 +693,10 @@ class EOFException(ExceptionBase):
"""
EOF container's code produces an stack underflow.
"""
STACK_OVERFLOW = auto()
"""
EOF container's code produces an stack overflow.
"""
STACK_HEIGHT_MISMATCH = auto()
"""
EOF container section stack height mismatch.
Expand Down Expand Up @@ -734,7 +746,7 @@ class EOFException(ExceptionBase):
"""
Incompatible instruction found in a container of a specific kind.
"""
TOO_MANY_CONTAINERS = auto()
TOO_MANY_CONTAINER_SECTIONS = auto()
"""
EOF container header has too many sub-containers.
"""
Expand All @@ -746,6 +758,10 @@ class EOFException(ExceptionBase):
"""
Header parsing encounterd a section kind it wasn't expecting
"""
AMBIGUOUS_CONTAINER_KIND = auto()
"""
Container referenced by eofcreate and returncontract.
"""


"""
Expand Down
14 changes: 8 additions & 6 deletions src/ethereum_test_specs/eof.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path
from shutil import which
from subprocess import CompletedProcess
from typing import Any, Callable, ClassVar, Dict, Generator, List, Optional, Type
from typing import Any, Callable, ClassVar, Dict, Generator, List, Optional, Tuple, Type

import pytest
from pydantic import Field, model_validator
Expand All @@ -32,7 +32,7 @@
from .base import BaseTest
from .state import StateTest

existing_tests: Dict[Bytes, str] = {}
existing_tests: Dict[Tuple[Bytes, ContainerKind], str] = {}


class EOFBaseException(Exception):
Expand Down Expand Up @@ -203,11 +203,12 @@ def make_eof_test_fixture(
"""
Generate the EOF test fixture.
"""
if self.data in existing_tests:
container_kind = self.container_kind or ContainerKind.RUNTIME
if (self.data, container_kind) in existing_tests:
pytest.fail(
f"Duplicate EOF test: {self.data}, existing test: {existing_tests[self.data]}"
f"Duplicate EOF test: {self.data} ({container_kind}), existing test: {existing_tests[self.data, container_kind]}"
)
existing_tests[self.data] = request.node.nodeid
existing_tests[(self.data, container_kind)] = request.node.nodeid
vectors = [
Vector(
code=self.data,
Expand Down Expand Up @@ -386,7 +387,8 @@ def generate(
Generate the BlockchainTest fixture.
"""
if fixture_format == EOFFixture:
if self.data in existing_tests:
container_kind = self.container_kind or ContainerKind.RUNTIME
if (self.data, container_kind) in existing_tests:
# Gracefully skip duplicate tests because one EOFStateTest can generate multiple
# state fixtures with the same data.
pytest.skip(f"Duplicate EOF container on EOFStateTest: {request.node.nodeid}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
""""
EOF v1 validation code - Exported from evmone unit tests
"""

import pytest
from ethereum_test_tools import EOFTestFiller, EOFException
from ethereum_test_tools.eof.v1 import Container, Section

REFERENCE_SPEC_GIT_PATH = "EIPS/eip-3540.md"
REFERENCE_SPEC_VERSION = "12ca2f0bd2f7380100e154aaaa0995c46cbb2710"

@pytest.mark.parametrize(
"eof_code,expected_hex_bytecode,exception",
[
pytest.param(
Container(
name="EOFV1_0001",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file can be removed since it's very similar to tests/osaka/eip7692_eof_v1/eip3540_eof_v1/test_container_validation.py::test_invalid_containers[fork_Osaka-eof_test-single_code_section_no_data_section] with only difference being that this one uses Op.INVALID instead of Op.STOP.

raw_bytes=(
[
0xef, 0x00, 0x01, # Version: 1
0x01, 0x00, 0x04, # Types Length: 4
0x02, 0x00, 0x01, # Code Sections (Length: 1)
0x00, 0x01, # Code Section 0 (Length: 1)
#--- Error: Invalid Data Header ---#
0x00,
0x00,
0x80,
0x00,
0x00,
0xfe,
]),
)
,
"ef000101000402000100010000800000fe",
EOFException.MISSING_DATA_SECTION,
id="data_section_missing_0"
),

]
)

def test_example_valid_invalid(
eof_test: EOFTestFiller,
eof_code: Container,
expected_hex_bytecode: str,
exception: EOFException | None,
):
"""
Verify eof container construction and exception
"""
assert bytes(eof_code) == bytes.fromhex(expected_hex_bytecode)

eof_test(
data=eof_code,
expect_exception=exception,
)
Loading
Loading