Skip to content

test_create_tx_selfdestruct_initcode_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py::test_create_tx_selfdestruct_initcode_state_gas@c74f1a67.

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_create_tx_selfdestruct_initcode_state_gas --fork Amsterdam

Verify a creation tx whose initcode SELFDESTRUCTs the new contract still pays the intrinsic NEW_ACCOUNT state gas.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
@pytest.mark.parametrize(
    "tx_value,beneficiary_kind",
    [
        pytest.param(0, "self", id="value0_to_self"),
        pytest.param(0, "existing", id="value0_to_existing"),
        pytest.param(0, "empty", id="value0_to_empty"),
        pytest.param(1, "self", id="value1_to_self"),
        pytest.param(1, "existing", id="value1_to_existing"),
        pytest.param(1, "empty", id="value1_to_empty"),
    ],
)
@pytest.mark.pre_alloc_mutable()
@pytest.mark.valid_from("EIP8037")
def test_create_tx_selfdestruct_initcode_state_gas(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    tx_value: int,
    beneficiary_kind: str,
) -> None:
    """
    Verify a creation tx whose initcode SELFDESTRUCTs the new contract
    still pays the intrinsic NEW_ACCOUNT state gas.
    """
    new_account_state_gas = fork.gas_costs().NEW_ACCOUNT
    intrinsic_calc = fork.transaction_intrinsic_cost_calculator()

    sender = pre.fund_eoa(amount=10**18)
    contract_addr = compute_create_address(address=sender, nonce=0)

    if beneficiary_kind == "self":
        beneficiary = contract_addr
    elif beneficiary_kind == "existing":
        beneficiary = pre.deploy_contract(code=Op.STOP)
    else:
        beneficiary = pre.fund_eoa(amount=0)

    # `current_target` is added to `accessed_addresses` at message
    # entry, so SELFDESTRUCT to self skips the cold-access surcharge.
    if beneficiary_kind == "self":
        init_code = Op.SELFDESTRUCT.with_metadata(address_warm=True)(
            beneficiary
        )
    else:
        init_code = Op.SELFDESTRUCT(beneficiary)
    intrinsic_total = intrinsic_calc(
        calldata=bytes(init_code), contract_creation=True
    )
    intrinsic_regular = intrinsic_total - new_account_state_gas

    creates_new_beneficiary = beneficiary_kind == "empty" and tx_value > 0
    expected_state = new_account_state_gas + (
        new_account_state_gas if creates_new_beneficiary else 0
    )
    expected_regular = intrinsic_regular + init_code.regular_cost(fork)
    expected_gas_used = max(expected_regular, expected_state)

    tx = Transaction(
        to=None,
        data=init_code,
        gas_limit=intrinsic_total + 100_000 + expected_state,
        sender=sender,
        value=tx_value,
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx],
                header_verify=Header(gas_used=expected_gas_used),
            ),
        ],
        post={},
    )

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.