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@9c2813ee.

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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
@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, gas_limit=1_000_000)

    # 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.