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

test eof container custom_size field #620

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
6 changes: 6 additions & 0 deletions src/ethereum_test_tools/eof/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class SectionKind(IntEnum):
CONTAINER = 3
DATA = 4

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


class AutoSection(Enum):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,55 +35,6 @@
None,
id="simple_eof_1_deploy",
),
pytest.param(
# Check that EOF1 undersize data is ok (4 declared, 2 provided)
# https://github.com/ipsilon/eof/blob/main/spec/eof.md#data-section-lifecycle
Container(
name="EOF1V0016",
sections=[
Section.Code(
code=Op.ADDRESS + Op.POP + Op.STOP,
max_stack_height=1,
),
Section.Data("0x0bad", custom_size=4),
],
),
"ef0001010004020001000304000400008000013050000bad",
EOFException.TOPLEVEL_CONTAINER_TRUNCATED,
winsvega marked this conversation as resolved.
Show resolved Hide resolved
id="undersize_data_not_ok_on_toplevel_container",
),
pytest.param(
# Check that EOF1 with too many or too few bytes fails
Container(
name="EOF1I0006",
sections=[
Section.Code(
code=Op.ADDRESS + Op.POP + Op.STOP,
max_stack_height=1,
),
Section.Data("0x0bad60A70BAD", custom_size=4),
],
),
"ef0001010004020001000304000400008000013050000bad60A70BAD",
EOFException.INVALID_SECTION_BODIES_SIZE,
id="oversize_data_fail",
),
pytest.param(
# Check that data section size is valid
Container(
name="EOF1V0001",
sections=[
Section.Code(
code=Op.ADDRESS + Op.POP + Op.STOP,
max_stack_height=1,
),
Section.Data("0x0bad60A7"),
],
),
"ef0001010004020001000304000400008000013050000bad60A7",
None,
id="data_ok",
),
pytest.param(
# Check that EOF1 with an illegal opcode fails
Container(
Expand Down
95 changes: 95 additions & 0 deletions tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_section_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
EOF Container, test custom_size field for sections
"""

from enum import IntEnum

import pytest

from ethereum_test_tools import EOFTestFiller
from ethereum_test_tools import Opcodes as Op
from ethereum_test_tools.eof.v1 import Container, EOFException, Section, SectionKind

from .. import EOF_FORK_NAME

REFERENCE_SPEC_GIT_PATH = "EIPS/eip-3540.md"
REFERENCE_SPEC_VERSION = "8dcb0a8c1c0102c87224308028632cc986a61183"

pytestmark = pytest.mark.valid_from(EOF_FORK_NAME)


class SectionSize(IntEnum):
"""
Enum for the section size
"""

NORMAL = 0
UNDERSIZE = 2
OVERSIZE = 100

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

winsvega marked this conversation as resolved.
Show resolved Hide resolved

@pytest.mark.parametrize(
"section_kind, section_size, exception",
[
(SectionKind.DATA, SectionSize.NORMAL, None),
(SectionKind.DATA, SectionSize.UNDERSIZE, EOFException.INVALID_SECTION_BODIES_SIZE),
(SectionKind.DATA, SectionSize.OVERSIZE, EOFException.TOPLEVEL_CONTAINER_TRUNCATED),
(SectionKind.CODE, SectionSize.UNDERSIZE, EOFException.INVALID_SECTION_BODIES_SIZE),
(SectionKind.CODE, SectionSize.OVERSIZE, EOFException.INVALID_SECTION_BODIES_SIZE),
(SectionKind.CODE, SectionSize.NORMAL, None),
(SectionKind.TYPE, SectionSize.UNDERSIZE, EOFException.INVALID_TYPE_SECTION_SIZE),
(SectionKind.TYPE, SectionSize.OVERSIZE, EOFException.INVALID_SECTION_BODIES_SIZE),
(SectionKind.TYPE, SectionSize.NORMAL, None),
],
)
def test_section_size(
eof_test: EOFTestFiller,
section_size: SectionSize,
section_kind: SectionKind,
exception: EOFException,
):
"""
Test custom_size is auto, more or less then the actual size of the section
"""
eof_code = Container()

if section_size != SectionSize.NORMAL and section_kind == SectionKind.TYPE:
eof_code.sections.append(
Section(
kind=SectionKind.TYPE,
data="0x00800001",
custom_size=section_size,
),
)

if section_size != SectionSize.NORMAL and section_kind == SectionKind.CODE:
eof_code.sections.append(
Section.Code(
code=Op.ADDRESS + Op.POP + Op.STOP,
max_stack_height=1,
custom_size=section_size,
)
)
else:
eof_code.sections.append(
Section.Code(
code=Op.ADDRESS + Op.POP + Op.STOP,
max_stack_height=1,
)
)

if section_size != SectionSize.NORMAL and section_kind == SectionKind.DATA:
eof_code.sections.append(Section.Data("0x00daaa", custom_size=section_size))
else:
eof_code.sections.append(Section.Data("0x00aaaa"))

eof_test(
data=eof_code,
expect_exception=exception,
)