|
| 1 | +""" |
| 2 | +Tests for RETURNCONTRACT instruction validation |
| 3 | +""" |
| 4 | + |
| 5 | +from ethereum_test_tools import EOFTestFiller |
| 6 | +from ethereum_test_tools.eof.v1 import Container, Section |
| 7 | +from ethereum_test_tools.exceptions import EOFException |
| 8 | +from ethereum_test_tools.vm.opcode import Opcodes as Op |
| 9 | + |
| 10 | +REFERENCE_SPEC_GIT_PATH = "EIPS/eip-7620.md" |
| 11 | +REFERENCE_SPEC_VERSION = "f20b164b00ae5553f7536a6d7a83a0f254455e09" |
| 12 | + |
| 13 | + |
| 14 | +def test_returncontract_valid_index_0( |
| 15 | + eof_test: EOFTestFiller, |
| 16 | +): |
| 17 | + """Deploy container index 0""" |
| 18 | + eof_test( |
| 19 | + data=Container( |
| 20 | + sections=[ |
| 21 | + Section.Code( |
| 22 | + code=Op.RETURNCONTRACT[0](0, 0), |
| 23 | + ), |
| 24 | + Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])), |
| 25 | + ], |
| 26 | + ) |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def test_returncontract_valid_index_1( |
| 31 | + eof_test: EOFTestFiller, |
| 32 | +): |
| 33 | + """Deploy container index 1""" |
| 34 | + eof_test( |
| 35 | + data=Container( |
| 36 | + sections=[ |
| 37 | + Section.Code( |
| 38 | + code=Op.RJUMPI[6](0) + Op.RETURNCONTRACT[0](0, 0) + Op.RETURNCONTRACT[1](0, 0), |
| 39 | + max_stack_height=2, |
| 40 | + ), |
| 41 | + Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])), |
| 42 | + Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])), |
| 43 | + ], |
| 44 | + ) |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def test_returncontract_valid_index_255( |
| 49 | + eof_test: EOFTestFiller, |
| 50 | +): |
| 51 | + """Deploy container index 255""" |
| 52 | + eof_test( |
| 53 | + data=Container( |
| 54 | + sections=[ |
| 55 | + Section.Code( |
| 56 | + sum((Op.RJUMPI[6](0) + Op.RETURNCONTRACT[i](0, 0)) for i in range(256)) |
| 57 | + + Op.REVERT(0, 0), |
| 58 | + max_stack_height=2, |
| 59 | + ) |
| 60 | + ] |
| 61 | + + [Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)]))] |
| 62 | + * 256 |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def test_returncontract_invalid_truncated_immediate( |
| 68 | + eof_test: EOFTestFiller, |
| 69 | +): |
| 70 | + """Truncated immediate""" |
| 71 | + eof_test( |
| 72 | + data=Container( |
| 73 | + sections=[ |
| 74 | + Section.Code( |
| 75 | + code=Op.PUSH0 + Op.PUSH0 + Op.RETURNCONTRACT, |
| 76 | + ), |
| 77 | + ], |
| 78 | + ), |
| 79 | + expect_exception=EOFException.TRUNCATED_INSTRUCTION, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def test_returncontract_invalid_index_0( |
| 84 | + eof_test: EOFTestFiller, |
| 85 | +): |
| 86 | + """Referring to non-existent container section index 0""" |
| 87 | + eof_test( |
| 88 | + data=Container( |
| 89 | + sections=[ |
| 90 | + Section.Code( |
| 91 | + code=Op.RETURNCONTRACT[0](0, 0), |
| 92 | + ), |
| 93 | + ], |
| 94 | + ), |
| 95 | + expect_exception=EOFException.INVALID_CONTAINER_SECTION_INDEX, |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def test_returncontract_invalid_index_1( |
| 100 | + eof_test: EOFTestFiller, |
| 101 | +): |
| 102 | + """Referring to non-existent container section index 1""" |
| 103 | + eof_test( |
| 104 | + data=Container( |
| 105 | + sections=[ |
| 106 | + Section.Code( |
| 107 | + code=Op.RETURNCONTRACT[1](0, 0), |
| 108 | + ), |
| 109 | + Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])), |
| 110 | + ], |
| 111 | + ), |
| 112 | + expect_exception=EOFException.INVALID_CONTAINER_SECTION_INDEX, |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +def test_returncontract_invalid_index_255( |
| 117 | + eof_test: EOFTestFiller, |
| 118 | +): |
| 119 | + """Referring to non-existent container section index 255""" |
| 120 | + eof_test( |
| 121 | + data=Container( |
| 122 | + sections=[ |
| 123 | + Section.Code( |
| 124 | + code=Op.RETURNCONTRACT[255](0, 0), |
| 125 | + ), |
| 126 | + Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])), |
| 127 | + ], |
| 128 | + ), |
| 129 | + expect_exception=EOFException.INVALID_CONTAINER_SECTION_INDEX, |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +def test_returncontract_terminating( |
| 134 | + eof_test: EOFTestFiller, |
| 135 | +): |
| 136 | + """Unreachable code after RETURNCONTRACT""" |
| 137 | + eof_test( |
| 138 | + data=Container( |
| 139 | + sections=[ |
| 140 | + Section.Code( |
| 141 | + code=Op.RETURNCONTRACT[0](0, 0) + Op.REVERT(0, 0), |
| 142 | + ), |
| 143 | + Section.Container(container=Container(sections=[Section.Code(code=Op.INVALID)])), |
| 144 | + ], |
| 145 | + ), |
| 146 | + expect_exception=EOFException.UNREACHABLE_INSTRUCTIONS, |
| 147 | + ) |
0 commit comments