Skip to content

Test DUPn

Documentation for tests/prague/eip7692_eof_v1/eip663_dupn_swapn_exchange/test_dupn.py.

Generate fixtures for these test cases for Prague with:

Prague only:

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

Tests EIP-663: SWAPN, DUPN and EXCHANGE instructions

Tests for the DUPN instruction.

test_dupn_all_valid_immediates(eof_state_test)

Test case for all valid DUPN immediates.

Source code in tests/prague/eip7692_eof_v1/eip663_dupn_swapn_exchange/test_dupn.py
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
@pytest.mark.valid_from(EOF_FORK_NAME)
def test_dupn_all_valid_immediates(eof_state_test: EOFStateTestFiller):
    """
    Test case for all valid DUPN immediates.
    """
    n = 2**8
    values = range(0xD00, 0xD00 + n)

    eof_code = Container(
        sections=[
            Section.Code(
                code=sum(Op.PUSH2[v] for v in values)
                + sum(Op.SSTORE(x, Op.DUPN[x]) for x in range(0, n))
                + Op.STOP,
            )
        ],
    )

    post = Account(storage=dict(zip(range(0, n), reversed(values))))

    eof_state_test(
        tx_sender_funding_amount=1_000_000_000,
        data=eof_code,
        container_post=post,
    )

test_dupn_stack_underflow(stack_height, max_stack_height, eof_test)

Test case out of bounds DUPN immediate.

Source code in tests/prague/eip7692_eof_v1/eip663_dupn_swapn_exchange/test_dupn.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@pytest.mark.parametrize(
    "stack_height,max_stack_height",
    [
        [0, 0],
        [0, 1],
        [1, 1],
        [1, 2],
        [2**8 - 1, 2**8 - 1],
        [2**8 - 1, 2**8],
    ],
)
@pytest.mark.valid_from(EOF_FORK_NAME)
def test_dupn_stack_underflow(
    stack_height: int,
    max_stack_height: int,
    eof_test: EOFTestFiller,
):
    """
    Test case out of bounds DUPN immediate.
    """
    eof_code = Container(
        sections=[
            Section.Code(
                code=sum(Op.PUSH2[v] for v in range(0, stack_height))
                + Op.DUPN[stack_height]
                + Op.STOP,
                max_stack_height=max_stack_height,
            )
        ],
    )
    eof_test(
        data=eof_code,
        expect_exception=EOFException.STACK_UNDERFLOW,
    )

test_dupn_stack_overflow(dupn_operand, max_stack_height, expect_exception, eof_test)

Test case where DUPN produces an stack overflow.

Source code in tests/prague/eip7692_eof_v1/eip663_dupn_swapn_exchange/test_dupn.py
 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
@pytest.mark.parametrize(
    "dupn_operand,max_stack_height,expect_exception",
    [
        [0, MAX_OPERAND_STACK_HEIGHT, EOFException.INVALID_MAX_STACK_HEIGHT],
        [0, MAX_OPERAND_STACK_HEIGHT + 1, EOFException.MAX_STACK_HEIGHT_ABOVE_LIMIT],
        [2**8 - 1, MAX_OPERAND_STACK_HEIGHT, EOFException.INVALID_MAX_STACK_HEIGHT],
        [2**8 - 1, MAX_OPERAND_STACK_HEIGHT + 1, EOFException.MAX_STACK_HEIGHT_ABOVE_LIMIT],
    ],
)
@pytest.mark.valid_from(EOF_FORK_NAME)
def test_dupn_stack_overflow(
    dupn_operand: int,
    max_stack_height: int,
    expect_exception: EOFException,
    eof_test: EOFTestFiller,
):
    """
    Test case where DUPN produces an stack overflow.
    """
    eof_code = Container(
        sections=[
            Section.Code(
                code=sum(Op.PUSH2[v] for v in range(0, MAX_OPERAND_STACK_HEIGHT))
                + Op.DUPN[dupn_operand]
                + Op.STOP,
                max_stack_height=max_stack_height,
            )
        ],
    )
    eof_test(
        data=eof_code,
        expect_exception=expect_exception,
    )