Skip to content

Test All Opcodes In Container

Documentation for tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_all_opcodes_in_container.py.

Generate fixtures for these test cases for Prague with:

Prague only:

fill -v tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_all_opcodes_in_container.py --fork=Prague --evm-bin=/path/to/evm-tool-dev-version
For all forks up to and including Prague:
fill -v tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_all_opcodes_in_container.py --until=Prague --evm-bin=/path/to/evm-tool-dev-version

EOF Container: check how every opcode behaves in the middle of the valid eof container code

test_all_opcodes_in_container(eof_test, opcode)

Test all opcodes inside valid container 257 because 0x5B is duplicated

Source code in tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_all_opcodes_in_container.py
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@pytest.mark.parametrize(
    "opcode",
    sorted((all_opcodes | undefined_opcodes) - {Op.RETF}),
)
def test_all_opcodes_in_container(
    eof_test: EOFTestFiller,
    opcode: Opcode,
):
    """
    Test all opcodes inside valid container
    257 because 0x5B is duplicated
    """
    data_portion = 1 if opcode == Op.CALLF else 0
    opcode_with_data_portion = opcode[data_portion] if opcode.has_data_portion() else opcode

    # opcode_with_data_portion has the correct minimum stack height
    bytecode = Op.PUSH0 * opcode_with_data_portion.min_stack_height + opcode_with_data_portion

    if opcode not in (halting_opcodes | section_terminating_opcodes):
        bytecode += Op.STOP

    sections = [Section.Code(code=bytecode)]

    match opcode:
        case Op.EOFCREATE | Op.RETURNCONTRACT:
            sections.append(
                Section.Container(
                    container=Container(
                        sections=[
                            Section.Code(code=Op.REVERT(0, 0)),
                        ]
                    )
                )
            )
        case Op.CALLF:
            sections.append(
                Section.Code(
                    code=Op.RETF,
                    code_outputs=0,
                )
            )
    sections.append(Section.Data("1122334455667788" * 4))

    if opcode == Op.RETURNCONTRACT:
        eof_code = Container(sections=sections, kind=ContainerKind.INITCODE)
    else:
        eof_code = Container(sections=sections)

    eof_test(
        data=eof_code,
        expect_exception=(
            None if opcode in valid_eof_opcodes else EOFException.UNDEFINED_INSTRUCTION
        ),
    )

test_all_invalid_terminating_opcodes(eof_test, opcode)

Test all opcodes that are invalid as the last opcode in a container

Source code in tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_all_opcodes_in_container.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
@pytest.mark.parametrize(
    "opcode",
    sorted(
        valid_eof_opcodes
        - halting_opcodes
        - section_terminating_opcodes
        - {Op.RJUMP, Op.RJUMPI, Op.RJUMPV}
    ),
)
def test_all_invalid_terminating_opcodes(
    eof_test: EOFTestFiller,
    opcode: Opcode,
):
    """
    Test all opcodes that are invalid as the last opcode in a container
    """
    if opcode.has_data_portion():
        # Add the appropriate data portion to the opcode by using the get_item method.
        # On the CALLF opcode we need to reference the second code section, hence the [1] index.
        opcode = opcode[0] if opcode != Op.CALLF else opcode[1]

    bytecode = (Op.PUSH0 * opcode.min_stack_height) + opcode

    sections = [Section.Code(code=bytecode)]

    if opcode == Op.CALLF[1]:
        sections += [Section.Code(code=Op.RETF, code_outputs=0)]
    elif opcode == Op.EOFCREATE[0]:
        sections += [
            Section.Container(
                container=Container(
                    sections=[
                        Section.Code(code=Op.RETURNCONTRACT[0](0, 0)),
                        Section.Container(Container.Code(code=Op.STOP)),
                    ]
                )
            )
        ]

    sections += [Section.Data(b"\0" * 32)]

    eof_test(
        data=Container(
            sections=sections,
        ),
        expect_exception=EOFException.MISSING_STOP_OPCODE,
    )

test_all_unreachable_terminating_opcodes_after_stop(eof_test, opcode)

Test all terminating opcodes after stop.

Source code in tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_all_opcodes_in_container.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
@pytest.mark.parametrize(
    "opcode",
    sorted(halting_opcodes | section_terminating_opcodes),
)
def test_all_unreachable_terminating_opcodes_after_stop(
    eof_test: EOFTestFiller,
    opcode: Opcode,
):
    """
    Test all terminating opcodes after stop.
    """
    match opcode:
        case Op.STOP:
            sections = [Section.Code(code=Op.STOP + Op.STOP)]
        case Op.RETF:
            sections = [
                Section.Code(code=Op.CALLF[1] + Op.STOP),
                Section.Code(code=Op.STOP + Op.RETF, code_outputs=0),
            ]
        case Op.JUMPF:
            sections = [
                Section.Code(code=Op.STOP + Op.JUMPF[1]),
                Section.Code(code=Op.STOP),
            ]
        case Op.RETURNCONTRACT:
            sections = [
                Section.Code(code=Op.EOFCREATE[0](0, 0, 0, 0) + Op.STOP),
                Section.Container(
                    container=Container(
                        sections=[
                            Section.Code(code=Op.STOP + Op.RETURNCONTRACT[0](0, 0)),
                            Section.Container(Container.Code(code=Op.STOP)),
                        ]
                    )
                ),
            ]
        case Op.RETURN | Op.REVERT | Op.INVALID:
            sections = [
                Section.Code(code=Op.PUSH0 + Op.PUSH0 + Op.STOP + opcode),
            ]
        case _:
            raise NotImplementedError(f"Opcode {opcode} is not implemented")

    eof_test(
        data=Container(
            sections=sections,
        ),
        expect_exception=EOFException.UNREACHABLE_INSTRUCTIONS
        if opcode != Op.RETURNCONTRACT
        else EOFException.INCOMPATIBLE_CONTAINER_KIND,
    )

test_all_unreachable_terminating_opcodes_before_stop(eof_test, opcode)

Test all opcodes terminating opcodes before.

Source code in tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_all_opcodes_in_container.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
@pytest.mark.parametrize(
    "opcode",
    sorted((halting_opcodes | section_terminating_opcodes) - {Op.STOP}),
)
def test_all_unreachable_terminating_opcodes_before_stop(
    eof_test: EOFTestFiller,
    opcode: Opcode,
):
    """
    Test all opcodes terminating opcodes before.
    """
    match opcode:
        case Op.RETF:
            sections = [
                Section.Code(code=Op.CALLF[1] + Op.STOP),
                Section.Code(code=Op.RETF + Op.STOP, code_outputs=0),
            ]
        case Op.JUMPF:
            sections = [
                Section.Code(code=Op.JUMPF[1] + Op.STOP),
                Section.Code(code=Op.STOP),
            ]
        case Op.RETURNCONTRACT:
            sections = [
                Section.Code(code=Op.EOFCREATE[0](0, 0, 0, 0) + Op.STOP),
                Section.Container(
                    container=Container(
                        sections=[
                            Section.Code(code=Op.RETURNCONTRACT[0](0, 0) + Op.STOP),
                            Section.Container(Container.Code(code=Op.STOP)),
                        ]
                    )
                ),
            ]
        case Op.RETURN | Op.REVERT | Op.INVALID:
            sections = [
                Section.Code(code=Op.PUSH1(0) + Op.PUSH1(0) + opcode + Op.STOP),
            ]
        case _:
            raise NotImplementedError(f"Opcode {opcode} is not implemented")

    eof_test(
        data=Container(
            sections=sections,
        ),
        expect_exception=EOFException.UNREACHABLE_INSTRUCTIONS
        if opcode != Op.RETURNCONTRACT
        else EOFException.INCOMPATIBLE_CONTAINER_KIND,
    )

test_all_opcodes_stack_underflow(eof_test, opcode)

Test stack underflow on all opcodes that require at least one item on the stack

Source code in tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_all_opcodes_in_container.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
@pytest.mark.parametrize(
    "opcode",
    sorted(op for op in valid_eof_opcodes if op.min_stack_height > 0)
    + [
        # Opcodes that have variable min_stack_height
        Op.SWAPN[0x00],
        Op.SWAPN[0xFF],
        Op.DUPN[0x00],
        Op.DUPN[0xFF],
        Op.EXCHANGE[0x00],
        Op.EXCHANGE[0xFF],
    ],
)
def test_all_opcodes_stack_underflow(
    eof_test: EOFTestFiller,
    opcode: Opcode,
):
    """
    Test stack underflow on all opcodes that require at least one item on the stack
    """
    sections: List[Section]
    if opcode == Op.EOFCREATE:
        sections = [
            Section.Code(code=Op.PUSH0 * (opcode.min_stack_height - 1) + opcode[0] + Op.STOP),
            Section.Container(
                container=Container(
                    sections=[
                        Section.Code(code=Op.RETURNCONTRACT[0](0, 0)),
                        Section.Container(Container.Code(code=Op.STOP)),
                    ]
                )
            ),
        ]
    elif opcode == Op.RETURNCONTRACT:
        sections = [
            Section.Code(code=Op.EOFCREATE[0](0, 0, 0, 0) + Op.STOP),
            Section.Container(
                container=Container(
                    sections=[
                        Section.Code(code=Op.PUSH0 * (opcode.min_stack_height - 1) + opcode[0]),
                        Section.Container(Container.Code(code=Op.STOP)),
                    ]
                )
            ),
        ]
    else:
        bytecode = Op.PUSH0 * (opcode.min_stack_height - 1)
        if opcode.has_data_portion():
            bytecode += opcode[0]
        else:
            bytecode += opcode
        bytecode += Op.STOP
        sections = [Section.Code(code=bytecode)]
    eof_code = Container(sections=sections)

    eof_test(
        data=eof_code,
        expect_exception=EOFException.STACK_UNDERFLOW,
    )

test_all_opcodes_stack_overflow(eof_test, opcode, exception)

Test stack overflow on all opcodes that push more items than they pop

Source code in tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_all_opcodes_in_container.py
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
@pytest.mark.parametrize(
    "opcode",
    sorted(op for op in valid_eof_opcodes if op.pushed_stack_items > op.popped_stack_items)
    + [
        Op.DUPN[0xFF],
    ],
)
@pytest.mark.parametrize(
    "exception",
    # We test two types of exceptions here:
    # 1. Invalid max stack height, where we modify the `max_stack_height` field of the code section
    #    to the maximum stack height allowed by the EIP-3540, so the code still has to be checked
    #    for stack overflow.
    # 2. Max stack height above limit, where we don't modify the `max_stack_height` field of the
    #    code section, so the actual code doesn't have to be verified for the stack overflow.
    [EOFException.INVALID_MAX_STACK_HEIGHT, EOFException.MAX_STACK_HEIGHT_ABOVE_LIMIT],
)
def test_all_opcodes_stack_overflow(
    eof_test: EOFTestFiller,
    opcode: Opcode,
    exception: EOFException,
):
    """
    Test stack overflow on all opcodes that push more items than they pop
    """
    opcode = opcode[0] if opcode.has_data_portion() else opcode

    assert opcode.pushed_stack_items - opcode.popped_stack_items == 1
    opcode_count = MAX_OPERAND_STACK_HEIGHT + 1 - opcode.min_stack_height

    bytecode = Op.PUSH0 * opcode.min_stack_height
    bytecode += opcode * opcode_count
    bytecode += Op.STOP

    kwargs: Dict[str, Any] = {"code": bytecode}

    if exception == EOFException.INVALID_MAX_STACK_HEIGHT:
        # Lie about the max stack height to make the code be checked for stack overflow.
        kwargs["max_stack_height"] = MAX_OPERAND_STACK_HEIGHT

    sections = [Section.Code(**kwargs)]

    if opcode == Op.DATALOADN[0]:
        sections.append(Section.Data(b"\0" * 32))
    eof_code = Container(sections=sections)

    eof_test(
        data=eof_code,
        expect_exception=exception,
    )