From 74ba61bb55fb708438dd6a785b9aaaf1dacf4b94 Mon Sep 17 00:00:00 2001 From: Mario Vega Date: Wed, 26 Jun 2024 18:06:57 +0000 Subject: [PATCH] feat(fw): Add `ContainerKind` to EOF V1 --- src/ethereum_test_tools/eof/v1/__init__.py | 42 +++++++++++++++++--- src/ethereum_test_tools/spec/eof/eof_test.py | 22 +++++----- src/ethereum_test_tools/spec/eof/types.py | 3 +- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/ethereum_test_tools/eof/v1/__init__.py b/src/ethereum_test_tools/eof/v1/__init__.py index c9bb0fc74a..204d77da74 100644 --- a/src/ethereum_test_tools/eof/v1/__init__.py +++ b/src/ethereum_test_tools/eof/v1/__init__.py @@ -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 @@ -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 @@ -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 """ diff --git a/src/ethereum_test_tools/spec/eof/eof_test.py b/src/ethereum_test_tools/spec/eof/eof_test.py index 419a33b2e5..7450a2a633 100644 --- a/src/ethereum_test_tools/spec/eof/eof_test.py +++ b/src/ethereum_test_tools/spec/eof/eof_test.py @@ -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 @@ -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, @@ -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 @@ -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 @@ -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, @@ -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) diff --git a/src/ethereum_test_tools/spec/eof/types.py b/src/ethereum_test_tools/spec/eof/types.py index 36aae976a6..daa8e9507f 100644 --- a/src/ethereum_test_tools/spec/eof/types.py +++ b/src/ethereum_test_tools/spec/eof/types.py @@ -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 @@ -41,7 +42,7 @@ class Vector(CamelModel): """ code: Bytes - initcode: bool + container_kind: ContainerKind results: Mapping[str, Result]