Skip to content

test_dupn_gas_cost_boundary()

Documentation for tests/amsterdam/eip8024_dupn_swapn_exchange/test_dupn.py::test_dupn_gas_cost_boundary@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

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

Test DUPN at the gas cost boundary.

DUPN is invoked in a callee that receives exactly its execution cost plus gas_cost_delta. The caller records the CALL result: a negative delta starves DUPN of its base gas (3) and the sub-call runs out of gas (result 0); a zero or positive delta succeeds (result 1).

Source code in tests/amsterdam/eip8024_dupn_swapn_exchange/test_dupn.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
@EIPChecklist.Opcode.Test.GasUsage.Normal()
@EIPChecklist.Opcode.Test.GasUsage.OutOfGasExecution()
@EIPChecklist.Opcode.Test.GasUsage.ExtraGas()
@pytest.mark.parametrize("gas_cost_delta", [-2, -1, 0, 1, 2])
def test_dupn_gas_cost_boundary(
    gas_cost_delta: int,
    pre: Alloc,
    fork: Fork,
    state_test: StateTestFiller,
) -> None:
    """
    Test DUPN at the gas cost boundary.

    DUPN is invoked in a callee that receives exactly its execution cost
    plus `gas_cost_delta`. The caller records the CALL result: a negative
    delta starves DUPN of its base gas (3) and the sub-call runs out of
    gas (result 0); a zero or positive delta succeeds (result 1).
    """
    stack_index = Spec.MIN_STACK_INDEX  # 17

    code = Bytecode()
    for i in range(stack_index):
        code += Op.PUSH1(i)
    code += Op.DUPN[stack_index]

    contract_address = pre.deploy_contract(code=code)

    call_code = Op.SSTORE(
        0,
        Op.CALL(
            gas=code.gas_cost(fork) + gas_cost_delta,
            address=contract_address,
        ),
    )
    call_address = pre.deploy_contract(
        code=call_code,
        storage={0: 0xDEADBEEF},
    )

    tx = Transaction(to=call_address, sender=pre.fund_eoa())

    post = {call_address: Account(storage={0: 0 if gas_cost_delta < 0 else 1})}

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

Parametrized Test Cases

This test generates 5 parametrized test cases across 1 fork.