Skip to content

test_dupn_stack_underflow()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_dupn.py::test_dupn_stack_underflow@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_dupn.py::test_dupn_stack_underflow --fork Amsterdam

Test DUPN causes transaction failure on stack underflow.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_dupn.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
@pytest.mark.parametrize(
    "immediate",
    [0, 45, 90, 128, 200, 255],
    ids=lambda x: f"dupn_underflow_imm_{x}",
)
def test_dupn_stack_underflow(
    immediate: int,
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """Test DUPN causes transaction failure on stack underflow."""
    sender = pre.fund_eoa()

    # Decode the immediate to get the stack index
    stack_index = decode_single(immediate)
    # Push one less than required to trigger underflow
    insufficient_items = stack_index - 1

    code = Op.SSTORE(0, 1)
    for i in range(insufficient_items):
        code += Op.PUSH1(i)
    # Pass immediate as bytes (raw immediate byte for testing)
    # Needs stack_index items, underflow
    code += Op.DUPN[immediate.to_bytes(1, "big")]
    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)

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

    # Transaction should fail, contract storage unchanged
    post = {contract_address: Account(storage={0: 0})}

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.