Skip to content

Commit

Permalink
new(tests): EOF - EIP-3540: out of order container section (#741)
Browse files Browse the repository at this point in the history
Test containers section being out of order in the header and/or body.
This extends and follows the convention of the test_section_order()
for the optional container section.

This also converts EOFTests/efValidation/EOF1_section_order_.json.
  • Loading branch information
chfast authored Aug 26, 2024
1 parent e35feea commit 40d8dd2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions converted-ethereum-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GeneralStateTests/stCreate2/call_then_create2_successful_then_returndatasize.jso
EOFTests/EIP3540/validInvalid.json

EOFTests/efValidation/EOF1_eofcreate_valid_.json
EOFTests/efValidation/EOF1_section_order_.json
EOFTests/efValidation/EOF1_truncated_section_.json

([#647](https://github.com/ethereum/execution-spec-tests/pull/647))
Expand Down
70 changes: 65 additions & 5 deletions tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_section_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ def make_section_order(kind) -> List[Section]:
skip_body_listing=calculate_skip_flag(SectionKind.DATA, CasePosition.BODY),
)

expected_code, expected_exception = get_expected_code_exception(
section_kind, section_test, test_position
)

eof_code = Container(
sections=make_section_order(section_kind),
auto_type_section=AutoSection.NONE,
Expand All @@ -195,16 +199,72 @@ def make_section_order(kind) -> List[Section]:
)
)
),
expected_bytecode=expected_code,
)

expected_code, expected_exception = get_expected_code_exception(
section_kind, section_test, test_position
eof_test(
data=eof_code,
expect_exception=expected_exception,
)


@pytest.mark.parametrize("container_position", range(4))
@pytest.mark.parametrize(
"test_position", [CasePosition.BODY, CasePosition.HEADER, CasePosition.BODY_AND_HEADER]
)
def test_container_section_order(
eof_test: EOFTestFiller,
container_position: int,
test_position: CasePosition,
):
"""
Test containers section being out of order in the header and/or body.
This extends and follows the convention of the test_section_order()
for the optional container section.
"""
section_code = Section.Code(
code=Op.EOFCREATE[0](0, 0, 0, 0)
# TODO: Migrated tests had the following infinite loop, so it is kept here
# to equalize code coverage.
+ Op.RJUMP[0]
+ Op.STOP()
)
section_type = Section(kind=SectionKind.TYPE, data=bytes.fromhex("00800004"))
section_data = Section.Data("ef")
section_container = Section.Container(Container.Code(Op.INVALID))

# TODO remove this after Container class implementation is reliable
assert bytes(eof_code).hex() == bytes.fromhex(expected_code).hex()
sections = [section_type, section_code, section_data]
sections.insert(container_position, section_container)
eof_code = Container(
sections=sections,
auto_type_section=AutoSection.NONE,
auto_sort_sections=(
AutoSection.ONLY_BODY
if test_position == CasePosition.HEADER
else (
AutoSection.ONLY_HEADER if test_position == CasePosition.BODY else AutoSection.NONE
)
),
)

def get_expected_exception():
match container_position, test_position:
case 2, _:
return None # Valid containers section position
case 0, CasePosition.BODY: # Messes up with the type section
return EOFException.INVALID_FIRST_SECTION_TYPE
case 1, CasePosition.BODY: # Messes up with the code section
return EOFException.UNDEFINED_INSTRUCTION
case 3, CasePosition.BODY: # Data section messes up with the container section
return EOFException.INVALID_MAGIC
case 0, CasePosition.HEADER | CasePosition.BODY_AND_HEADER:
return EOFException.MISSING_TYPE_HEADER
case 1, CasePosition.HEADER | CasePosition.BODY_AND_HEADER:
return EOFException.MISSING_CODE_HEADER
case 3, CasePosition.HEADER | CasePosition.BODY_AND_HEADER:
return EOFException.MISSING_TERMINATOR

eof_test(
data=eof_code,
expect_exception=expected_exception,
expect_exception=get_expected_exception(),
)

0 comments on commit 40d8dd2

Please sign in to comment.