1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097 | @pytest.mark.parametrize("selfdestruct_contract_initial_balance", [0, 100_000])
@pytest.mark.valid_from("Shanghai")
def test_create_multiple_contracts_destroy_one_then_destroy_other_next_tx(
blockchain_test: BlockchainTestFiller,
eip_enabled: bool,
pre: Alloc,
sender: EOA,
fork: Fork,
selfdestruct_contract_initial_balance: int,
) -> None:
"""
Test creating multiple contracts in one transaction, destroying only one
of them, then attempting to destroy the other in a subsequent transaction.
Contract A: Self-destructs in the same transaction as creation (deleted)
Contract B: Does NOT self-destruct in creation tx, attempted destruction
in subsequent tx (persists after EIP-6780)
"""
# Pre-deploy a recipient contract
sendall_recipient = pre.deploy_contract(
code=Op.SSTORE(0, 0),
storage={0: 1},
)
# Self-destructing contract code: selfdestruct based on calldata
# If calldata[0] == 1, self-destruct; otherwise just increment storage
selfdestruct_code = Op.SSTORE(0, Op.ADD(Op.SLOAD(0), 1)) + Conditional(
condition=Op.EQ(Op.CALLDATALOAD(0), 1),
if_true=Op.SELFDESTRUCT(sendall_recipient),
if_false=Op.STOP,
)
selfdestruct_initcode = Initcode(deploy_code=selfdestruct_code)
initcode_copy_from_address = pre.deploy_contract(selfdestruct_initcode)
# Deploy entry contract
entry_code_storage = Storage()
# Entry code: create both contracts, call A with selfdestruct flag,
# call B without
entry_code = Op.EXTCODECOPY(
initcode_copy_from_address,
0,
0,
len(selfdestruct_initcode),
)
# Create contract A with salt=0
entry_code += Op.SSTORE(
entry_code_storage.store_next(0), # Replaced with actual address
Op.CREATE2(value=0, offset=0, size=len(selfdestruct_initcode), salt=0),
)
# Create contract B with salt=1
entry_code += Op.SSTORE(
entry_code_storage.store_next(0), # Replaced with actual address
Op.CREATE2(value=0, offset=0, size=len(selfdestruct_initcode), salt=1),
)
# Call contract A (slot 0) with flag=1 to self-destruct
entry_code += Op.MSTORE(0, 1)
entry_code += Op.SSTORE(
entry_code_storage.store_next(1),
Op.CALL(Op.GASLIMIT, Op.SLOAD(0), 0, 0, 32, 0, 0),
)
# Call contract B (slot 1) with flag=0 to NOT self-destruct
entry_code += Op.MSTORE(0, 0)
entry_code += Op.SSTORE(
entry_code_storage.store_next(1),
Op.CALL(Op.GASLIMIT, Op.SLOAD(1), 0, 0, 32, 0, 0),
)
entry_code += Op.STOP
entry_code_address = pre.deploy_contract(entry_code)
# Calculate contract addresses
contract_a_address = compute_create_address(
address=entry_code_address,
salt=0,
initcode=selfdestruct_initcode,
opcode=Op.CREATE2,
)
contract_b_address = compute_create_address(
address=entry_code_address,
salt=1,
initcode=selfdestruct_initcode,
opcode=Op.CREATE2,
)
# Update expected storage
entry_code_storage[0] = contract_a_address
entry_code_storage[1] = contract_b_address
if selfdestruct_contract_initial_balance > 0:
pre.fund_address(
contract_a_address, selfdestruct_contract_initial_balance
)
pre.fund_address(
contract_b_address, selfdestruct_contract_initial_balance
)
# Second transaction: try to self-destruct contract B
tx2_caller = pre.deploy_contract(
Op.MSTORE(0, 1)
+ Op.CALL(Op.GASLIMIT, contract_b_address, 0, 0, 32, 0, 0)
+ Op.STOP
)
tx1_receipt = None
tx2_receipt = None
if fork.is_eip_enabled(7708):
# Tx1: only A SELFDESTRUCTs (flag=1), transferring its pre-funded
# balance to the shared recipient. B gets called with flag=0 and
# returns without emitting any log.
tx1_logs = []
if selfdestruct_contract_initial_balance > 0:
tx1_logs.append(
transfer_log(
contract_a_address,
sendall_recipient,
selfdestruct_contract_initial_balance,
)
)
tx1_receipt = TransactionReceipt(logs=tx1_logs)
# Tx2: B (now pre-existing) SELFDESTRUCTs transferring its
# pre-funded balance to the recipient.
tx2_logs = []
if selfdestruct_contract_initial_balance > 0:
tx2_logs.append(
transfer_log(
contract_b_address,
sendall_recipient,
selfdestruct_contract_initial_balance,
)
)
tx2_receipt = TransactionReceipt(logs=tx2_logs)
txs = [
Transaction(
sender=sender,
to=entry_code_address,
gas_limit=1_000_000,
expected_receipt=tx1_receipt,
),
Transaction(
sender=sender,
to=tx2_caller,
gas_limit=500_000,
expected_receipt=tx2_receipt,
),
]
post: Dict[Address, Account] = {
entry_code_address: Account(storage=entry_code_storage),
# Contract A is always destroyed (created and destroyed same tx)
contract_a_address: Account.NONEXISTENT, # type: ignore
}
if eip_enabled:
# After EIP-6780: Contract B persists (not destroyed in same tx)
# Storage shows 2 calls (one in tx1, one in tx2)
post[contract_b_address] = Account(
storage={0: 2},
balance=0, # Balance sent but contract persists
)
post[sendall_recipient] = Account(
balance=selfdestruct_contract_initial_balance * 2,
storage={0: 1},
)
else:
# Before EIP-6780: Contract B is destroyed in tx2
post[contract_b_address] = Account.NONEXISTENT # type: ignore
post[sendall_recipient] = Account(
balance=selfdestruct_contract_initial_balance * 2,
storage={0: 1},
)
blockchain_test(pre=pre, post=post, blocks=[Block(txs=txs)])
|