Skip to content

test_selfdestruct_via_delegatecall_chain_no_refund()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py::test_selfdestruct_via_delegatecall_chain_no_refund@2119b382.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py::test_selfdestruct_via_delegatecall_chain_no_refund --fork Amsterdam

Verify SELFDESTRUCT in a nested DELEGATECALL/CALLCODE frame below a same-tx-created contract does not refund state gas.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
@pytest.mark.parametrize(
    "num_hops",
    [
        pytest.param(1, id="single_hop"),
        pytest.param(2, id="two_hops"),
    ],
)
@pytest.mark.with_all_call_opcodes(
    selector=lambda call_opcode: call_opcode in (Op.DELEGATECALL, Op.CALLCODE)
)
@pytest.mark.valid_from("EIP8037")
def test_selfdestruct_via_delegatecall_chain_no_refund(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    num_hops: int,
    call_opcode: Op,
) -> None:
    """
    Verify SELFDESTRUCT in a nested DELEGATECALL/CALLCODE frame below
    a same-tx-created contract does not refund state gas.
    """
    new_account_state_gas = fork.gas_costs().NEW_ACCOUNT
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()()

    # Bottom of the chain does the SELFDESTRUCT; intermediate helpers
    # just delegate further down. Track each frame's bytecode so we
    # can sum its regular gas into `expected_gas_used` below.
    sd_code = Op.SELFDESTRUCT.with_metadata(address_warm=True)(Op.ADDRESS)
    chain_regular_gas = sd_code.gas_cost(fork)
    delegate_target = pre.deploy_contract(code=sd_code)
    for _ in range(num_hops - 1):
        hop_code = (
            Op.POP(
                call_opcode.with_metadata(address_warm=False)(
                    gas=Op.GAS, address=delegate_target
                )
            )
            + Op.STOP
        )
        chain_regular_gas += hop_code.gas_cost(fork)
        delegate_target = pre.deploy_contract(code=hop_code)

    # A's deployed runtime: one delegation into the top of the chain.
    deployed_code = (
        Op.POP(
            call_opcode.with_metadata(address_warm=False)(
                gas=Op.GAS, address=delegate_target
            )
        )
        + Op.STOP
    )
    deployed = bytes(deployed_code)
    code_deposit_state_gas = fork.code_deposit_state_gas(
        code_size=len(deployed)
    )
    initcode = Initcode(deploy_code=deployed)
    initcode_len = len(initcode)

    # Slots 0 and 1 guard against a vacuously-NONEXISTENT A: slot 0
    # fails if CREATE silently returned 0, slot 1 fails if the factory
    # OOGed before completing the nested CALL.  TSTORE caches the
    # CREATE return so both can reuse it.
    factory_storage = Storage()
    factory_code = (
        Op.CALLDATACOPY(
            0,
            0,
            Op.CALLDATASIZE,
            data_size=initcode_len,
            new_memory_size=initcode_len,
        )
        + Op.TSTORE(
            0,
            Op.CREATE.with_metadata(init_code_size=initcode_len)(
                value=0,
                offset=0,
                size=Op.CALLDATASIZE,
            ),
        )
        + Op.SSTORE.with_metadata(
            key_warm=False,
            original_value=0,
            current_value=0,
            new_value=1,
        )(
            factory_storage.store_next(1, "create_returned_nonzero"),
            Op.ISZERO(Op.ISZERO(Op.TLOAD(0))),
        )
        + Op.SSTORE.with_metadata(
            key_warm=False,
            original_value=0,
            current_value=0,
            new_value=1,
        )(
            factory_storage.store_next(1, "call_returned_success"),
            Op.CALL.with_metadata(address_warm=True)(
                gas=Op.GAS, address=Op.TLOAD(0)
            ),
        )
    )
    factory = pre.deploy_contract(code=factory_code)
    created_address = compute_create_address(address=factory, nonce=1)

    total_state_gas = (
        new_account_state_gas + code_deposit_state_gas + 2 * sstore_state_gas
    )
    regular_used = (
        intrinsic_gas
        + factory_code.gas_cost(fork)
        + initcode.gas_cost(fork)
        + deployed_code.gas_cost(fork)
        + chain_regular_gas
        - new_account_state_gas
        - code_deposit_state_gas
        - 2 * sstore_state_gas
    )
    expected_gas_used = max(regular_used, total_state_gas)

    tx = Transaction(
        to=factory,
        data=bytes(initcode),
        state_gas_reservoir=total_state_gas,
        sender=pre.fund_eoa(),
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx],
                header_verify=Header(gas_used=expected_gas_used),
            )
        ],
        post={
            created_address: Account.NONEXISTENT,
            factory: Account(storage=factory_storage),
        },
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.