Skip to content

Commit

Permalink
new(tests): EOF: add tests for DATALOADN validation and execution (#1162
Browse files Browse the repository at this point in the history
)
  • Loading branch information
chfast authored Feb 5, 2025
1 parent 016bc4d commit 83942b0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 13 deletions.
1 change: 1 addition & 0 deletions converted-ethereum-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ EOFTests/EIP3670/validInvalid.json
EOFTests/EIP4200/validInvalid.json
EOFTests/EIP4750/validInvalid.json
EOFTests/efValidation/callf_into_nonreturning_.json
EOFTests/efValidation/dataloadn_.json
EOFTests/efValidation/EOF1_embedded_container_.json
EOFTests/efValidation/EOF1_eofcreate_valid_.json
EOFTests/efValidation/EOF1_callf_truncated_.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@
]

INVALID: List[Container] = [
Container(
name="DATALOADN_0_empty_data",
sections=[
Section.Code(
code=Op.DATALOADN[0] + Op.POP + Op.STOP,
),
],
validity_error=EOFException.INVALID_DATALOADN_INDEX,
),
Container(
name="DATALOADN_max_empty_data",
sections=[
Expand All @@ -98,6 +107,56 @@
],
validity_error=EOFException.INVALID_DATALOADN_INDEX,
),
Container(
name="DATALOADN_1_over_data",
sections=[
Section.Code(
code=Op.DATALOADN[1] + Op.POP + Op.STOP,
),
Section.Data(b"\x00"),
],
validity_error=EOFException.INVALID_DATALOADN_INDEX,
),
Container(
name="DATALOADN_32_over_data",
sections=[
Section.Code(
code=Op.DATALOADN[32] + Op.POP + Op.STOP,
),
Section.Data(b"\xda" * 32),
],
validity_error=EOFException.INVALID_DATALOADN_INDEX,
),
Container(
name="DATALOADN_0_data_31",
sections=[
Section.Code(
code=Op.DATALOADN[0] + Op.POP + Op.STOP,
),
Section.Data(b"\xda" * 31),
],
validity_error=EOFException.INVALID_DATALOADN_INDEX,
),
Container(
name="DATALOADN_32_data_63",
sections=[
Section.Code(
code=Op.DATALOADN[32] + Op.POP + Op.STOP,
),
Section.Data(b"\xda" * 63),
],
validity_error=EOFException.INVALID_DATALOADN_INDEX,
),
Container(
name="DATALOADN_max_imm",
sections=[
Section.Code(
code=Op.DATALOADN[0xFFFF] + Op.POP + Op.STOP,
),
Section.Data(b"\xda" * 32),
],
validity_error=EOFException.INVALID_DATALOADN_INDEX,
),
Container(
name="DATALOADN_max_small_data",
sections=[
Expand Down Expand Up @@ -143,14 +202,11 @@ def container_name(c: Container):
VALID,
ids=container_name,
)
def test_legacy_initcode_valid_eof_v1_contract(
def test_valid_containers_with_data_section(
eof_test: EOFTestFiller,
container: Container,
):
"""
Test creating various types of valid EOF V1 contracts using legacy
initcode and a contract creating transaction.
"""
"""Test EOF validation of valid containers with data sections."""
assert container.validity_error is None, (
f"Valid container with validity error: {container.validity_error}"
)
Expand All @@ -164,14 +220,11 @@ def test_legacy_initcode_valid_eof_v1_contract(
INVALID,
ids=container_name,
)
def test_legacy_initcode_invalid_eof_v1_contract(
def test_invalid_containers_with_data_section(
eof_test: EOFTestFiller,
container: Container,
):
"""
Test creating various types of valid EOF V1 contracts using legacy
initcode and a contract creating transaction.
"""
"""Test EOF validation of invalid containers with data sections."""
assert container.validity_error is not None, "Invalid container without validity error"
eof_test(
container=container,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from ethereum_test_specs import EOFStateTestFiller
from ethereum_test_tools import Account, Alloc, Environment, StateTestFiller, Transaction
from ethereum_test_tools.eof.v1 import Container, Section
from ethereum_test_tools.vm.opcode import Opcodes as Op
Expand All @@ -14,6 +15,26 @@
pytestmark = pytest.mark.valid_from(EOF_FORK_NAME)


@pytest.mark.parametrize("index", [0, 1, 31, 32, 33, 63, 64])
@pytest.mark.parametrize("suffix_len", [0, 1, 31, 32, 24000])
def test_dataloadn(eof_state_test: EOFStateTestFiller, index: int, suffix_len: int):
"""Basic tests for DATALOADN execution."""
sentinel = 0x8000000000000000000000000000000000000000000000000000000000000001
eof_state_test(
container=Container(
sections=[
Section.Code(
Op.SSTORE(0, Op.DATALOADN[index]) + Op.STOP,
),
Section.Data(
index * b"\xbe" + sentinel.to_bytes(32, byteorder="big") + suffix_len * b"\xaf"
),
],
),
container_post=Account(storage={0: sentinel}),
)


def create_data_test(offset: int, datasize: int):
"""Generate data load operators test cases based on load offset and data section size."""
data = b"".join(i.to_bytes(length=2, byteorder="big") for i in range(1, datasize // 2 + 1))
Expand Down
6 changes: 3 additions & 3 deletions tests/osaka/eip7692_eof_v1/eof_tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,10 @@

### Validation

- [ ] Valid DATALOADN with various offsets (ethereum/tests: src/EOFTestsFiller/efValidation/dataloadn_Copier.json)
- [x] Valid DATALOADN with various offsets ([`tests/osaka/eip7692_eof_v1/eip7480_data_section/test_data_opcodes.py::test_dataloadn`](./eip7480_data_section/test_data_opcodes/test_dataloadn.md)
- [x] Truncated DATALOADN immediate ([`tests/osaka/eip7692_eof_v1/eip7480_data_section/test_code_validation.py::test_dataloadn_truncated_immediate`](./eip7480_data_section/test_code_validation/test_dataloadn_truncated_immediate.md)
- [ ] DATALOADN offset out of bounds (ethereum/tests: src/EOFTestsFiller/efValidation/dataloadn_Copier.json)
- [ ] DATALOADN accessing not full word (ethereum/tests: src/EOFTestsFiller/efValidation/dataloadn_Copier.json)
- [x] DATALOADN offset out of bounds ([`tests/osaka/eip7692_eof_v1/eip7480_data_section/test_code_validation.py::test_invalid_containers_with_data_section`](./eip7480_data_section/test_code_validation/test_invalid_containers_with_data_section.md)
- [x] DATALOADN accessing not full word ([`tests/osaka/eip7692_eof_v1/eip7480_data_section/test_code_validation.py::test_invalid_containers_with_data_section`](./eip7480_data_section/test_code_validation/test_invalid_containers_with_data_section.md)

## EIP-663: SWAPN, DUPN and EXCHANGE instructions

Expand Down

0 comments on commit 83942b0

Please sign in to comment.