Skip to content

Commit

Permalink
new(tests): eip-7620: RETURNCONTRACT validation tests
Browse files Browse the repository at this point in the history
Co-authored-by: Paweł Bylica <[email protected]>
  • Loading branch information
gumb0 and chfast committed Jul 1, 2024
1 parent 971fb0c commit 16b7e5d
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 4 deletions.
2 changes: 2 additions & 0 deletions converted-ethereum-tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
EOFTests/efValidation/EOF1_returncontract_invalid_.json
EOFTests/efValidation/EOF1_returncontract_valid_.json
3 changes: 3 additions & 0 deletions src/ethereum_test_tools/exceptions/evmone_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class EvmoneExceptionMapper:
ExceptionMessage(
EOFException.CONTAINER_SIZE_ABOVE_LIMIT, "err: container_size_above_limit"
),
ExceptionMessage(
EOFException.INVALID_CONTAINER_SECTION_INDEX, "err: invalid_container_section_index"
),
)

def __init__(self) -> None:
Expand Down
4 changes: 4 additions & 0 deletions src/ethereum_test_tools/exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ class EOFException(ExceptionBase):
"""
EOF container is above size limit
"""
INVALID_CONTAINER_SECTION_INDEX = auto()
"""
Instruction references container section that does not exist.
"""


"""
Expand Down
155 changes: 155 additions & 0 deletions tests/prague/eip7692_eof_v1/eip7620_eof_create/test_returncontract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
"""
Tests for RETURNCONTRACT instruction validation
"""

from ethereum_test_tools import EOFTestFiller
from ethereum_test_tools.eof.v1 import Container, Section, ContainerKind
from ethereum_test_tools.exceptions import EOFException
from ethereum_test_tools.vm.opcode import Opcodes as Op

REFERENCE_SPEC_GIT_PATH = "EIPS/eip-7620.md"
REFERENCE_SPEC_VERSION = "f20b164b00ae5553f7536a6d7a83a0f254455e09"


def test_returncontract_valid_index_0(
eof_test: EOFTestFiller,
):
"""Deploy container index 0"""
eof_test(
container_kind=ContainerKind.INITCODE,
data=Container(
sections=[
Section.Code(
code=Op.RETURNCONTRACT[0](0, 0),
),
Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])),
],
)
)


def test_returncontract_valid_index_1(
eof_test: EOFTestFiller,
):
"""Deploy container index 1"""
eof_test(
container_kind=ContainerKind.INITCODE,
data=Container(
sections=[
Section.Code(
code=Op.RJUMPI[6](0) + Op.RETURNCONTRACT[0](0, 0) + Op.RETURNCONTRACT[1](0, 0),
max_stack_height=2,
),
Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])),
Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])),
],
)
)


def test_returncontract_valid_index_255(
eof_test: EOFTestFiller,
):
"""Deploy container index 255"""
eof_test(
container_kind=ContainerKind.INITCODE,
data=Container(
sections=[
Section.Code(
sum((Op.RJUMPI[6](0) + Op.RETURNCONTRACT[i](0, 0)) for i in range(256))
+ Op.REVERT(0, 0),
max_stack_height=2,
)
]
+ [Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)]))]
* 256
)
)


def test_returncontract_invalid_truncated_immediate(
eof_test: EOFTestFiller,
):
"""Truncated immediate"""
eof_test(
container_kind=ContainerKind.INITCODE,
data=Container(
sections=[
Section.Code(
code=Op.PUSH0 + Op.PUSH0 + Op.RETURNCONTRACT,
),
],
),
expect_exception=EOFException.TRUNCATED_INSTRUCTION,
)


def test_returncontract_invalid_index_0(
eof_test: EOFTestFiller,
):
"""Referring to non-existent container section index 0"""
eof_test(
container_kind=ContainerKind.INITCODE,
data=Container(
sections=[
Section.Code(
code=Op.RETURNCONTRACT[0](0, 0),
),
],
),
expect_exception=EOFException.INVALID_CONTAINER_SECTION_INDEX,
)


def test_returncontract_invalid_index_1(
eof_test: EOFTestFiller,
):
"""Referring to non-existent container section index 1"""
eof_test(
container_kind=ContainerKind.INITCODE,
data=Container(
sections=[
Section.Code(
code=Op.RETURNCONTRACT[1](0, 0),
),
Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])),
],
),
expect_exception=EOFException.INVALID_CONTAINER_SECTION_INDEX,
)


def test_returncontract_invalid_index_255(
eof_test: EOFTestFiller,
):
"""Referring to non-existent container section index 255"""
eof_test(
container_kind=ContainerKind.INITCODE,
data=Container(
sections=[
Section.Code(
code=Op.RETURNCONTRACT[255](0, 0),
),
Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])),
],
),
expect_exception=EOFException.INVALID_CONTAINER_SECTION_INDEX,
)


def test_returncontract_terminating(
eof_test: EOFTestFiller,
):
"""Unreachable code after RETURNCONTRACT"""
eof_test(
container_kind=ContainerKind.INITCODE,
data=Container(
sections=[
Section.Code(
code=Op.RETURNCONTRACT[0](0, 0) + Op.REVERT(0, 0),
),
Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])),
],
),
expect_exception=EOFException.UNREACHABLE_INSTRUCTIONS,
)
8 changes: 4 additions & 4 deletions tests/prague/eip7692_eof_v1/tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@
- [ ] EOFCREATE is not a valid terminating instruction (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_eofcreate_invalid_Copier.json)
- [ ] EOFCREATE immediate referring to non-existing container (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_eofcreate_invalid_Copier.json)
- [ ] EOFCREATE immediate referring to container with truncated data (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_eofcreate_invalid_Copier.json)
- [ ] Valid RETURNCONTRACTs referring to various container numbers (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_returncontract_valid_Copier.json)
- [ ] Truncated before RETURNCONTRACT immediate (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_returncontract_invalid_Copier.json)
- [ ] RETURNCONTRACT immediate referring to non-existing container (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_returncontract_invalid_Copier.json)
- [ ] Unreachable code after RETURNCONTRACT, check that RETURNCONTRACT is terminating (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_returncontract_invalid_Copier.json)
- [x] Valid RETURNCONTRACTs referring to various container numbers (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_returncontract_valid_Copier.json)
- [x] Truncated before RETURNCONTRACT immediate (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_returncontract_invalid_Copier.json)
- [x] RETURNCONTRACT immediate referring to non-existing container (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_returncontract_invalid_Copier.json)
- [x] Unreachable code after RETURNCONTRACT, check that RETURNCONTRACT is terminating (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_returncontract_invalid_Copier.json)

## EIP-7698: EOF - Creation transaction

0 comments on commit 16b7e5d

Please sign in to comment.