Skip to content

test_swapn_with_dup1_and_push()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_with_dup1_and_push@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py::test_swapn_with_dup1_and_push --fork Amsterdam

Test SWAPN swapping top and bottom after building stack with DUP1.

Stack layout: PUSH1(1) PUSH1(0) DUP1*15 PUSH1(2) SWAPN[0x80] Before SWAPN: [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] After SWAPN[0x80] (decode_single(0x80)=17, swaps pos 1 and 18): [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2] Result: 18 stack items, top=1, bottom=2, rest=0

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_swapn.py
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
def test_swapn_with_dup1_and_push(
    pre: Alloc,
    state_test: StateTestFiller,
) -> None:
    """
    Test SWAPN swapping top and bottom after building stack with DUP1.

    Stack layout: PUSH1(1) PUSH1(0) DUP1*15 PUSH1(2) SWAPN[0x80]
    Before SWAPN: [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
    After SWAPN[0x80] (decode_single(0x80)=17, swaps pos 1 and 18):
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]
    Result: 18 stack items, top=1, bottom=2, rest=0
    """
    sender = pre.fund_eoa()

    # Build the stack: PUSH1(1), PUSH1(0), 15x DUP1, PUSH1(2)
    code = Bytecode()
    code += Op.PUSH1(1)  # Position 18 after DUP1s and final PUSH1
    code += Op.PUSH1(0)
    for _ in range(15):
        code += Op.DUP1
    code += Op.PUSH1(2)  # Top of stack (position 1)

    # Stack: [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
    # SWAPN with immediate 0x80 (decode_single(0x80)=17) swaps pos 1 and 18
    # Pass as bytes (raw immediate byte for testing)
    code += Op.SWAPN[b"\x80"]

    # Store all stack values to verify
    for i in range(18):
        code += Op.PUSH1(i) + Op.SSTORE

    code += Op.STOP

    contract_address = pre.deploy_contract(code=code)

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

    # Expected: top (position 0) = 1, bottom (position 17) = 2, rest = 0
    expected_storage = {}
    for i in range(18):
        if i == 0:
            expected_storage[i] = 1  # Was at bottom, now at top
        elif i == 17:
            expected_storage[i] = 2  # Was at top, now at bottom
        else:
            expected_storage[i] = 0  # All middle values

    post = {contract_address: Account(storage=expected_storage)}

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.