|
| 1 | +""" |
| 2 | +EOF validation tests for EIP-3540 container size |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ethereum_test_tools import EOFTestFiller |
| 8 | +from ethereum_test_tools import Opcodes as Op |
| 9 | +from ethereum_test_tools.eof.v1 import Container, EOFException, Section |
| 10 | +from ethereum_test_tools.eof.v1.constants import MAX_INITCODE_SIZE |
| 11 | + |
| 12 | +from .. import EOF_FORK_NAME |
| 13 | + |
| 14 | +REFERENCE_SPEC_GIT_PATH = "EIPS/eip-3540.md" |
| 15 | +REFERENCE_SPEC_VERSION = "" # todo |
| 16 | + |
| 17 | +pytestmark = pytest.mark.valid_from(EOF_FORK_NAME) |
| 18 | + |
| 19 | +VALID_CONTAINER = Container(sections=[Section.Code(code=Op.STOP)]) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize( |
| 23 | + "over_limit", |
| 24 | + [0, 1, 2, 2**16 - MAX_INITCODE_SIZE], |
| 25 | +) |
| 26 | +def test_max_size( |
| 27 | + eof_test: EOFTestFiller, |
| 28 | + over_limit: int, |
| 29 | +): |
| 30 | + """ |
| 31 | + Verify EOF container valid at maximum size, invalid above |
| 32 | + """ |
| 33 | + minimal_code = bytearray(bytes(VALID_CONTAINER)) |
| 34 | + # Expand the minimal EOF code by more noop code, reaching the desired target container size. |
| 35 | + code = Container( |
| 36 | + sections=[ |
| 37 | + Section.Code( |
| 38 | + code=Op.JUMPDEST * (MAX_INITCODE_SIZE - len(minimal_code) + over_limit) + Op.STOP |
| 39 | + ) |
| 40 | + ] |
| 41 | + ) |
| 42 | + assert len(code) == MAX_INITCODE_SIZE + over_limit |
| 43 | + eof_test( |
| 44 | + data=bytes(code), |
| 45 | + expect_exception=None if over_limit == 0 else EOFException.CONTAINER_SIZE_ABOVE_LIMIT, |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + "size", |
| 51 | + [MAX_INITCODE_SIZE + 1, MAX_INITCODE_SIZE * 2], |
| 52 | +) |
| 53 | +def test_above_max_size_raw( |
| 54 | + eof_test: EOFTestFiller, |
| 55 | + size: int, |
| 56 | +): |
| 57 | + """ |
| 58 | + Verify EOF container invalid above maximum size, regardless of header contents |
| 59 | + """ |
| 60 | + code = Op.INVALID * size |
| 61 | + eof_test( |
| 62 | + data=bytes(code), |
| 63 | + expect_exception=EOFException.CONTAINER_SIZE_ABOVE_LIMIT, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "code", |
| 69 | + [ |
| 70 | + pytest.param( |
| 71 | + Container(sections=[Section.Code(code=Op.STOP, custom_size=MAX_INITCODE_SIZE)]), |
| 72 | + id="1st_code_section", |
| 73 | + ), |
| 74 | + pytest.param( |
| 75 | + Container( |
| 76 | + sections=[ |
| 77 | + Section.Code(code=Op.STOP), |
| 78 | + Section.Code(code=Op.STOP, custom_size=MAX_INITCODE_SIZE), |
| 79 | + ] |
| 80 | + ), |
| 81 | + id="2nd_code_section", |
| 82 | + ), |
| 83 | + pytest.param( |
| 84 | + Container( |
| 85 | + sections=[ |
| 86 | + Section.Code(code=Op.STOP), |
| 87 | + Section.Container(container=Op.STOP, custom_size=MAX_INITCODE_SIZE), |
| 88 | + ] |
| 89 | + ), |
| 90 | + id="1st_container_section", |
| 91 | + ), |
| 92 | + pytest.param( |
| 93 | + Container( |
| 94 | + sections=[ |
| 95 | + Section.Code(code=Op.STOP), |
| 96 | + Section.Container(container=Op.STOP), |
| 97 | + Section.Container(container=Op.STOP, custom_size=MAX_INITCODE_SIZE), |
| 98 | + ] |
| 99 | + ), |
| 100 | + id="2nd_container_section", |
| 101 | + ), |
| 102 | + ], |
| 103 | +) |
| 104 | +def test_section_after_end_of_container( |
| 105 | + eof_test: EOFTestFiller, |
| 106 | + code: Container, |
| 107 | +): |
| 108 | + """ |
| 109 | + Verify EOF container is invalid if any of sections declares above container size |
| 110 | + """ |
| 111 | + eof_test( |
| 112 | + data=bytes(code), |
| 113 | + expect_exception=EOFException.INVALID_SECTION_BODIES_SIZE, |
| 114 | + ) |
0 commit comments