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@5c024cbb.

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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
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.