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@622620d8.

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 top-frame NEW_ACCOUNT state gas.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
@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 top-frame NEW_ACCOUNT state gas.
    """
    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)

    creates_new_beneficiary = beneficiary_kind == "empty" and tx_value > 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, account_new=creates_new_beneficiary
        )(beneficiary)
    else:
        init_code = Op.SELFDESTRUCT.with_metadata(
            account_new=creates_new_beneficiary
        )(beneficiary)
    intrinsic_execution = intrinsic_calc(
        calldata=bytes(init_code), contract_creation=True
    )

    expected_state = fork.transaction_top_frame_state_gas(
        contract_creation=True
    ) + init_code.state_cost(fork)
    expected_execution = intrinsic_execution + init_code.execution_cost(fork)
    expected_gas_used = max(expected_execution, expected_state)
    assert expected_gas_used == expected_state, (
        "expected state gas to dominate execution gas"
    )

    tx = Transaction(
        to=None,
        data=init_code,
        gas_limit=intrinsic_execution + 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.