Skip to content

test_eip_vector_end_of_code()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py::test_eip_vector_end_of_code@8acae1b0.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py::test_eip_vector_end_of_code --fork Amsterdam

Test EIP-8024 opcodes at end of code (no immediate byte).

When an opcode appears at end of code, code[pc+1] = 0 beyond end of code. - DUPN: decode_single(0) = 145, needs 145 items on stack - SWAPN: decode_single(0) = 145, needs 146 items on stack - EXCHANGE: decode_pair(0) = (9, 16), needs 17 items on stack

Store a marker before the opcode to verify the transaction succeeded, since adding any opcode after would make that opcode byte the immediate.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_vectors.py
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
@pytest.mark.parametrize(
    "eip8024_opcode,stack_items",
    [
        pytest.param(Op.DUPN, 145, id="dupn"),
        pytest.param(Op.SWAPN, 146, id="swapn"),
        pytest.param(Op.EXCHANGE, 17, id="exchange"),
    ],
)
def test_eip_vector_end_of_code(
    pre: Alloc,
    state_test: StateTestFiller,
    eip8024_opcode: Op,
    stack_items: int,
) -> None:
    """
    Test EIP-8024 opcodes at end of code (no immediate byte).

    When an opcode appears at end of code, code[pc+1] = 0 beyond end of code.
    - DUPN: decode_single(0) = 145, needs 145 items on stack
    - SWAPN: decode_single(0) = 145, needs 146 items on stack
    - EXCHANGE: decode_pair(0) = (9, 16), needs 17 items on stack

    Store a marker before the opcode to verify the transaction succeeded,
    since adding any opcode after would make that opcode byte the immediate.
    """
    sender = pre.fund_eoa()
    marker_value = 0x42

    code = (
        # store marker for verification
        Op.SSTORE(0, marker_value)
        # push minimum required stack items for the opcode
        + Op.PUSH0 * stack_items
        # end-of-code EIP-8024 opcode
        + eip8024_opcode
    )
    contract_address = pre.deploy_contract(code=code)

    tx = Transaction(to=contract_address, sender=sender)

    # verify marker was stored (tx succeeded)
    post = {contract_address: Account(storage={0: marker_value})}
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.