Skip to content

Test Selfdestruct Revert

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py.

Generate fixtures for these test cases with:

fill -v tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py

tests for selfdestruct interaction with revert

test_selfdestruct_created_in_same_tx_with_revert(state_test, env, pre, entry_code_address, selfdestruct_on_outer_call, selfdestruct_with_transfer_contract_code, selfdestruct_with_transfer_contract_initcode, selfdestruct_with_transfer_contract_address, selfdestruct_recipient_address, selfdestruct_with_transfer_initcode_copy_from_address, recursive_revert_contract_address, recursive_revert_contract_code)

Given

Contract A which has methods to receive balance and selfdestruct, and was created in current tx

Test the following call sequence: Transfer value to A and call A.selfdestruct. Recurse into a new call from transfers value to A, calls A.selfdestruct, and reverts.

Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
@pytest.mark.parametrize(
    "selfdestruct_on_outer_call",
    [0, 1, 2],
    ids=[
        "no_outer_selfdestruct",
        "outer_selfdestruct_before_inner_call",
        "outer_selfdestruct_after_inner_call",
    ],
)
@pytest.mark.valid_from("Cancun")
def test_selfdestruct_created_in_same_tx_with_revert(  # noqa SC200
    state_test: StateTestFiller,
    env: Environment,
    pre: Dict[Address, Account],
    entry_code_address: Address,
    selfdestruct_on_outer_call: int,
    selfdestruct_with_transfer_contract_code: Bytecode,
    selfdestruct_with_transfer_contract_initcode: Bytecode,
    selfdestruct_with_transfer_contract_address: Address,
    selfdestruct_recipient_address: Address,
    selfdestruct_with_transfer_initcode_copy_from_address: Address,
    recursive_revert_contract_address: Address,
    recursive_revert_contract_code: Bytecode,
):
    """
    Given:
        Contract A which has methods to receive balance and selfdestruct, and was created in current tx
    Test the following call sequence:
         Transfer value to A and call A.selfdestruct.
         Recurse into a new call from transfers value to A, calls A.selfdestruct, and reverts.
    """  # noqa: E501
    entry_code = Op.EXTCODECOPY(
        selfdestruct_with_transfer_initcode_copy_from_address,
        0,
        0,
        len(bytes(selfdestruct_with_transfer_contract_initcode)),
    )

    entry_code += Op.SSTORE(
        0,
        Op.CREATE(
            0, 0, len(bytes(selfdestruct_with_transfer_contract_initcode))  # Value  # Offset
        ),
    )

    entry_code += Op.CALL(
        Op.GASLIMIT(),
        recursive_revert_contract_address,
        0,  # value
        0,  # arg offset
        0,  # arg length
        0,  # ret offset
        0,  # ret length
    )

    post: Dict[Address, Account] = {
        entry_code_address: Account(
            code="0x",
            storage=Storage(
                {
                    0: selfdestruct_with_transfer_contract_address,  # type: ignore
                }
            ),
        ),
        selfdestruct_with_transfer_initcode_copy_from_address: Account(
            code=selfdestruct_with_transfer_contract_initcode,
        ),
        recursive_revert_contract_address: Account(
            code=recursive_revert_contract_code,
            storage=Storage({1: 1}),  # type: ignore
        ),
    }

    if selfdestruct_on_outer_call > 0:
        post[selfdestruct_with_transfer_contract_address] = Account.NONEXISTENT  # type: ignore
        post[selfdestruct_recipient_address] = Account(
            balance=1 if selfdestruct_on_outer_call == 1 else 2,
        )
    else:
        post[selfdestruct_with_transfer_contract_address] = Account(
            balance=1,
            code=selfdestruct_with_transfer_contract_code,
            storage=Storage(
                {
                    # 2 value transfers (1 in outer call, 1 in reverted inner call)
                    0: 1,  # type: ignore
                    # 1 selfdestruct in reverted inner call
                    1: 0,  # type: ignore
                }
            ),
        )
        post[selfdestruct_recipient_address] = Account.NONEXISTENT  # type: ignore

    nonce = count()
    tx = Transaction(
        ty=0x0,
        value=0,
        data=entry_code,
        chain_id=0x0,
        nonce=next(nonce),
        to=None,
        gas_limit=100_000_000,
        gas_price=10,
        protected=False,
    )

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

test_selfdestruct_not_created_in_same_tx_with_revert(state_test, env, entry_code_address, selfdestruct_on_outer_call, selfdestruct_with_transfer_contract_code, selfdestruct_with_transfer_contract_address, selfdestruct_recipient_address, recursive_revert_contract_address, recursive_revert_contract_code)

Same test as selfdestruct_created_in_same_tx_with_revert except selfdestructable contract is pre-existing

Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
@pytest.mark.parametrize(
    "selfdestruct_on_outer_call",
    [0, 1, 2],
    ids=[
        "no_outer_selfdestruct",
        "outer_selfdestruct_before_inner_call",
        "outer_selfdestruct_after_inner_call",
    ],
)
@pytest.mark.valid_from("Cancun")
def test_selfdestruct_not_created_in_same_tx_with_revert(
    state_test: StateTestFiller,
    env: Environment,
    entry_code_address: Address,
    selfdestruct_on_outer_call: int,
    selfdestruct_with_transfer_contract_code: Bytecode,
    selfdestruct_with_transfer_contract_address: Address,
    selfdestruct_recipient_address: Address,
    recursive_revert_contract_address: Address,
    recursive_revert_contract_code: Bytecode,
):
    """
    Same test as selfdestruct_created_in_same_tx_with_revert except selfdestructable contract
    is pre-existing
    """
    entry_code = Op.CALL(
        Op.GASLIMIT(),
        recursive_revert_contract_address,
        0,  # value
        0,  # arg offset
        0,  # arg length
        0,  # ret offset
        0,  # ret length
    )

    pre: Dict[Address, Account] = {
        TestAddress: Account(balance=100_000_000_000_000_000_000),
        selfdestruct_with_transfer_contract_address: Account(
            code=selfdestruct_with_transfer_contract_code
        ),
        recursive_revert_contract_address: Account(
            code=bytes(recursive_revert_contract_code), balance=2
        ),
    }

    post: Dict[Address, Account] = {
        entry_code_address: Account(code="0x"),
    }

    if selfdestruct_on_outer_call > 0:
        post[selfdestruct_with_transfer_contract_address] = Account(
            balance=1 if selfdestruct_on_outer_call == 1 else 0,
            code=selfdestruct_with_transfer_contract_code,
            storage=Storage(
                {
                    # 2 value transfers: 1 in outer call, 1 in reverted inner call
                    0: 1,  # type: ignore
                    # 1 selfdestruct in reverted inner call
                    1: 1,  # type: ignore
                }
            ),
        )
        post[selfdestruct_recipient_address] = Account(
            balance=1 if selfdestruct_on_outer_call == 1 else 2
        )
    else:
        post[selfdestruct_with_transfer_contract_address] = Account(
            balance=1,
            code=selfdestruct_with_transfer_contract_code,
            storage=Storage(
                {
                    # 2 value transfers: 1 in outer call, 1 in reverted inner call
                    0: 1,  # type: ignore
                    # 2 selfdestructs: 1 in outer call, 1 in reverted inner call # noqa SC100
                    1: 0,  # type: ignore
                }
            ),
        )
        post[selfdestruct_recipient_address] = Account.NONEXISTENT  # type: ignore

    nonce = count()
    tx = Transaction(
        ty=0x0,
        value=0,
        data=entry_code,
        chain_id=0x0,
        nonce=next(nonce),
        to=None,
        gas_limit=100_000_000,
        gas_price=10,
        protected=False,
    )

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