Skip to content

test_stack_access_opcodes_mainnet()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_mainnet.py::test_stack_access_opcodes_mainnet@26332146.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_mainnet.py::test_stack_access_opcodes_mainnet --fork Amsterdam

Test that DUPN, SWAPN and EXCHANGE execute with correct results.

Each opcode moves a distinct planted marker to the top of the stack, which is then stored. The opcodes do not depend on any environment value, so the full post-state assertion holds when execute-ed on a live network. Storage keys start at nonzero canaries so a failed transaction is distinguishable from a successful one.

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_eip_mainnet.py
24
25
26
27
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def test_stack_access_opcodes_mainnet(
    state_test: StateTestFiller,
    pre: Alloc,
) -> None:
    """
    Test that DUPN, SWAPN and EXCHANGE execute with correct results.

    Each opcode moves a distinct planted marker to the top of the stack,
    which is then stored. The opcodes do not depend on any environment
    value, so the full post-state assertion holds when `execute`-ed on a
    live network. Storage keys start at nonzero canaries so a failed
    transaction is distinguishable from a successful one.
    """
    dupn_marker = 0xA1
    swapn_marker = 0xB2
    exchange_marker = 0xC3

    code = (
        # DUPN: duplicate the marker planted at depth 17.
        Op.PUSH1(dupn_marker)
        + Op.PUSH0 * 16
        + Op.DUPN[17]
        + Op.PUSH1(0)
        + Op.SSTORE
        # SWAPN: swap the top with the marker planted at depth 18.
        + Op.PUSH1(swapn_marker)
        + Op.PUSH0 * 17
        + Op.SWAPN[17]
        + Op.PUSH1(1)
        + Op.SSTORE
        # EXCHANGE: move the marker from depth 3 to depth 2, then POP.
        + Op.PUSH1(exchange_marker)
        + Op.PUSH0 * 2
        + Op.EXCHANGE[1, 2]
        + Op.POP
        + Op.PUSH1(2)
        + Op.SSTORE
        + Op.STOP
    )
    contract = pre.deploy_contract(
        code=code,
        storage={0: 0xBA5E, 1: 0xBA5E, 2: 0xBA5E},
    )
    tx = Transaction(
        ty=0x02,
        to=contract,
        sender=pre.fund_eoa(),
        gas_limit=200_000,
    )
    post = {
        contract: Account(
            storage={
                0: dupn_marker,
                1: swapn_marker,
                2: exchange_marker,
            },
        ),
    }

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.