Skip to content

Test Eof Example

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

Generate fixtures for these test cases for Prague with:

Prague only:

fill -v tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_eof_example.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_eof_example.py --until=Prague --evm-bin=/path/to/evm-tool-dev-version

EOF Classes example use

test_eof_example(eof_test)

Example of python EOF classes

Source code in tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_eof_example.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def test_eof_example(eof_test: EOFTestFiller):
    """
    Example of python EOF classes
    """
    # Lets construct an EOF container code
    eof_code = Container(
        name="valid_container_example",
        sections=[
            # TYPES section is constructed automatically based on CODE
            # CODE section
            Section.Code(
                code=Op.CALLF[1](Op.PUSH0) + Op.STOP,  # bytecode to be deployed in the body
                # Code: call section 1 with a single zero as input, then stop.
                max_stack_height=1,  # define code header (in body) stack size
            ),
            # There can be multiple code sections
            Section.Code(
                # Remove input and call section 2 with no inputs, then remove output and return
                code=Op.POP + Op.CALLF[2]() + Op.POP + Op.RETF,
                code_inputs=1,
                code_outputs=0,
                max_stack_height=1,
            ),
            Section.Code(
                # Call section 3 with two inputs (address twice), return
                code=Op.CALLF[3](Op.DUP1, Op.ADDRESS) + Op.POP + Op.POP + Op.RETF,
                code_outputs=1,
                max_stack_height=3,
            ),
            Section.Code(
                # Duplicate one input and return
                code=Op.DUP1 + Op.RETF,
                code_inputs=2,
                code_outputs=3,
                max_stack_height=3,
            ),
            # DATA section
            Section.Data("0xef"),
        ],
    )

    # This will construct a valid EOF container with these bytes
    assert bytes(eof_code) == bytes.fromhex(
        "ef0001010010020004000500060008000204000100008000010100000100010003020300035fe300010050"
        "e3000250e43080e300035050e480e4ef"
    )

    eof_test(
        data=eof_code,
        expect_exception=eof_code.validity_error,
    )

test_eof_example_custom_fields(eof_test)

Example of python EOF container class tuning

Source code in tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_eof_example.py
 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
119
120
121
def test_eof_example_custom_fields(eof_test: EOFTestFiller):
    """
    Example of python EOF container class tuning
    """
    # if you need to overwrite certain structure bytes, you can use customization
    # this is useful for unit testing the eof structure format, you can reorganize sections
    # and overwrite the header bytes for testing purposes
    # most of the combinations are covered by the unit tests

    # This features are subject for development and will change in the future

    eof_code = Container(
        name="valid_container_example_2",
        magic=b"\xef\x00",  # magic can be overwritten for test purposes, (default is 0xEF00)
        version=b"\x01",  # version can be overwritten for testing purposes (default is 0x01)
        header_terminator=b"\x00",  # terminator byte can be overwritten (default is 0x00)
        extra=b"",  # extra bytes to be trailed after the container body bytes (default is None)
        sections=[
            # TYPES section is constructed automatically based on CODE
            # CODE section
            Section.Code(
                code=Op.PUSH1(2)
                + Op.STOP,  # this is the actual bytecode to be deployed in the body
                max_stack_height=1,  # define code header (in body) stack size
            ),
            # DATA section
            Section.Data(
                data="0xef",
                # custom_size overrides the size bytes, so you can put only 1 byte into data
                # but still make the header size of 2 to produce invalid section
                # if custom_size != len(data), the section will be invalid
                custom_size=1,
            ),
        ],
        # auto generate types section based on provided code sections
        # AutoSection.ONLY_BODY - means the section will be generated only for the body bytes
        # AutoSection.ONLY_BODY - means the section will be generated only for the header bytes
        auto_type_section=AutoSection.AUTO,
        # auto generate default data section (0x empty), by default is True
        auto_data_section=True,
        # auto sort section by order 01 02 03 04
        # AutoSection.ONLY_BODY - means the sorting will be done only for the body bytes
        # AutoSection.ONLY_BODY - means the section will be done only for the header bytes
        auto_sort_sections=AutoSection.AUTO,
    )

    eof_test(
        data=eof_code,
        expect_exception=eof_code.validity_error,
    )

test_eof_example_parameters(eof_test, data_section_bytes, code_section_code, exception)

Example of python EOF classes

Source code in tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_eof_example.py
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
@pytest.mark.parametrize(
    "data_section_bytes",
    (b"\x01", b"\xef"),
)
@pytest.mark.parametrize(
    "code_section_code, exception",
    [(Op.PUSH1(10) + Op.STOP, None), (Op.PUSH1(14), EOFException.MISSING_STOP_OPCODE)],
)
def test_eof_example_parameters(
    eof_test: EOFTestFiller,
    data_section_bytes: bytes,
    code_section_code: Bytecode,
    exception: EOFException,
):
    """
    Example of python EOF classes
    """
    eof_code = Container(
        name="parametrized_eof_example",
        sections=[
            Section.Code(
                code=code_section_code,
                max_stack_height=1,
            ),
            Section.Data(data_section_bytes),
        ],
        validity_error=exception,
    )

    eof_test(
        data=eof_code,
        expect_exception=eof_code.validity_error,
    )