Skip to content

test_dupn_basic()

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

Generate fixtures for these test cases for Amsterdam with:

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

Test DUPN with various stack indices (17-235).

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_dupn.py
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
@pytest.mark.parametrize(
    "stack_index",
    [17, 18, 32, 64, 107, 108, 200, 235],
    ids=lambda x: f"dupn_stack_{x}",
)
def test_dupn_basic(
    stack_index: int,
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """Test DUPN with various stack indices (17-235)."""
    sender = pre.fund_eoa()

    # Build stack with enough items, then use DUPN to duplicate the nth item
    # DUPN with immediate x duplicates the decode_single(x)th stack item
    stack_height = stack_index
    expected_value = 0xBEEF + stack_index

    # Push values onto stack: the value at the target position will
    # be expected_value
    code = Bytecode()
    for i in range(stack_height):
        if i == 0:
            # The first push will end up at position stack_index from top
            code += Op.PUSH2(expected_value)
        else:
            code += Op.PUSH2(0x1000 + i)

    # Pass stack index directly - encoder will handle encoding
    code += Op.DUPN[stack_index]
    # Store the duplicated value
    code += Op.PUSH1(0) + Op.SSTORE
    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)

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

    post = {contract_address: Account(storage={0: expected_value})}

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

Parametrized Test Cases

This test generates 8 parametrized test cases across 1 fork.