Skip to content

Commit

Permalink
feat(fw): Add ContainerKind to EOF V1
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Jun 26, 2024
1 parent 12e01bf commit 74ba61b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
42 changes: 37 additions & 5 deletions src/ethereum_test_tools/eof/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
"""

from dataclasses import dataclass
from enum import Enum, IntEnum
from enum import Enum, IntEnum, auto
from functools import cached_property
from typing import Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple

from pydantic import Field
from pydantic import Field, GetCoreSchemaHandler
from pydantic_core.core_schema import (
PlainValidatorFunctionSchema,
no_info_plain_validator_function,
to_string_ser_schema,
)

from ...common import Bytes
from ...common.conversions import BytesConvertible
Expand Down Expand Up @@ -47,6 +52,33 @@ def __str__(self) -> str:
return self.name


class ContainerKind(Enum):
"""
Enum class of V1 valid container kind values.
"""

RUNTIME = auto()
INITCODE = auto()

@staticmethod
def __get_pydantic_core_schema__(
source_type: Any, handler: GetCoreSchemaHandler
) -> PlainValidatorFunctionSchema:
"""
Calls the class constructor without info and appends the serialization schema.
"""
return no_info_plain_validator_function(
source_type,
serialization=to_string_ser_schema(),
)

def __str__(self) -> str:
"""
Returns the string representation of the container kind
"""
return self.name


class AutoSection(Enum):
"""
Enum class for auto section generation approach
Expand Down Expand Up @@ -327,9 +359,9 @@ class Container(CopyValidateModel):
TODO: Remove str
"""
initcode: bool = False
kind: ContainerKind = ContainerKind.RUNTIME
"""
Whether the container is an initcode.
Kind type of the container.
"""
raw_bytes: Optional[Bytes] = None
"""
Expand Down
22 changes: 11 additions & 11 deletions src/ethereum_test_tools/spec/eof/eof_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from ...common import Account, Alloc, Environment, Transaction
from ...common.base_types import Bytes
from ...eof.v1 import Container
from ...eof.v1 import Container, ContainerKind
from ...exceptions import EOFException, EvmoneExceptionMapper
from ..base.base_test import BaseFixture, BaseTest
from ..state.state_test import StateTest
Expand Down Expand Up @@ -137,7 +137,7 @@ class EOFTest(BaseTest):

data: Bytes
expect_exception: EOFException | None = None
initcode: bool = False
container_kind: ContainerKind = ContainerKind.RUNTIME

supported_fixture_formats: ClassVar[List[FixtureFormats]] = [
FixtureFormats.EOF_TEST,
Expand All @@ -152,7 +152,7 @@ def check_container_exception(cls, data: Any) -> Any:
if isinstance(data, dict):
container = data.get("data")
expect_exception = data.get("expect_exception")
initcode = data.get("initcode")
container_kind = data.get("container_kind")
if container is not None and isinstance(container, Container):
if (
"validity_error" in container.model_fields_set
Expand All @@ -165,13 +165,13 @@ def check_container_exception(cls, data: Any) -> Any:
)
if expect_exception is None:
data["expect_exception"] = container.validity_error
if "initcode" in container.model_fields_set:
if initcode is not None:
assert container.initcode == initcode, (
f"Container initcode flag {container.initcode} "
f"does not match test {initcode}."
if "container_kind" in container.model_fields_set:
if container_kind is not None:
assert str(container.kind) == container_kind, (
f"Container kind type {str(container.kind)} "
f"does not match test {container_kind}."
)
data["initcode"] = container.initcode
data["container_kind"] = str(container.kind)
return data

@classmethod
Expand All @@ -194,7 +194,7 @@ def make_eof_test_fixture(
vectors={
"0": {
"code": self.data,
"initcode": self.initcode,
"container_kind": self.container_kind,
"results": {
fork.blockchain_test_network_name(): {
"exception": self.expect_exception,
Expand All @@ -215,7 +215,7 @@ def make_eof_test_fixture(
if expected_result is None:
raise Exception(f"EOF Fixture missing vector result for fork: {fork}")
args = []
if vector.initcode:
if vector.container_kind == ContainerKind.INITCODE:
args.append("--initcode")
result = eof_parse.run(*args, input=str(vector.code))
self.verify_result(result, expected_result, vector.code)
Expand Down
3 changes: 2 additions & 1 deletion src/ethereum_test_tools/spec/eof/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from ...common.base_types import Bytes
from ...common.types import CamelModel
from ...eof.v1 import ContainerKind
from ...exceptions import EOFException
from ..base.base_test import BaseFixture

Expand Down Expand Up @@ -41,7 +42,7 @@ class Vector(CamelModel):
"""

code: Bytes
initcode: bool
container_kind: ContainerKind
results: Mapping[str, Result]


Expand Down

0 comments on commit 74ba61b

Please sign in to comment.