---
eip: 6780
label: EIP-6780
title: SELFDESTRUCT only in same transaction
status: Final
type: Standards Track
category: Core
created: 2023-03-25
requires: 2681, 2929, 3529
official: https://eips.ethereum.org/EIPS/eip-6780
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-6780.md
canonical: https://eips.huatuo.xyz/zh/eip-6780/
lang: zh
tldr: 削弱 SELFDESTRUCT：它现在只会把合约的 ETH 发送给目标地址，不再删除代码或存储，除非该合约是在同一笔交易中创建的。
tags: [evm, opcode, selfdestruct, cancun, state]
---

# EIP-6780: SELFDESTRUCT only in same transaction

> SELFDESTRUCT will recover all funds to the target but not delete the account, except when called in the same transaction as creation

**TL;DR:** 削弱 SELFDESTRUCT：它现在只会把合约的 ETH 发送给目标地址，不再删除代码或存储，除非该合约是在同一笔交易中创建的。

## 它解决什么问题？

`SELFDESTRUCT` 过去会彻底清除一个账户：它的代码、每个存储槽以及账户本身，并把余额发送到指定地址。这是一个庞大且没有上限的状态变更，在 Verkle 树这类未来的状态设计中很难支持，因为一个账户的数据会分散在许多互不相关的键上。

该操作码已经被 [EIP-6049](/eip-6049/) 弃用。EIP-6780 去掉了其破坏性的部分，同时保留了两种常见且无害的用法：从合约中取出全部 ETH，以及在单笔交易内创建并销毁合约。

## 它如何运作

现在的行为取决于合约是否**在当前交易中创建**。

**合约创建于更早的交易（常规情况）：**

- 当前执行帧停止。
- 全部余额转给目标地址。
- 代码、存储、nonce 和账户本身**不会**被删除。
- 如果目标是合约自身，则没有任何变化；ETH 不再被销毁。

**合约在同一笔交易中更早创建：**

- 沿用旧行为：余额发送给目标地址，账户及其所有数据被删除。
- 如果目标是合约自身，这部分 ETH 会像以前一样被销毁。

"创建"指从合约创建交易开始时，或从 `CREATE` / `CREATE2` 开始执行时算起，即使该地址原本已有余额。[EIP-2929](/eip-2929/) 的 gas 规则不变，仍然没有退款（已被 [EIP-3529](/eip-3529/) 移除）。

## 示例代码

新操作码逻辑的伪代码：

```python
def selfdestruct(account, target):
    balance = state.get_balance(account)
    state.add_balance(target, balance)
    state.sub_balance(account, balance)        # net zero if target == account

    if account in tx.created_accounts:         # created in this same transaction
        state.set_balance(account, 0)          # burns ETH if target == account
        tx.accounts_to_delete.add(account)     # old behaviour: wipe code + storage
    halt_frame()
```

一个**不再有效**的模式 – 通过 `CREATE2` 实现的变形（metamorphic）升级：

```solidity
// Before Cancun: selfdestruct, then CREATE2 new code at the same address.
// After EIP-6780: selfdestruct leaves the code in place, so the second
// CREATE2 to that address fails because the account already has code.
function kill() external {
    require(msg.sender == owner);
    selfdestruct(payable(owner)); // now only moves the ETH out
}
```

## 需要注意

- **状态：Final。** 已在 Cancun/Deneb（"Dencun"）升级中于主网激活，与 [EIP-5656](/eip-5656/) 的 `MCOPY` 一同上线。
- 基于 `SELFDESTRUCT` + `CREATE2` 重新部署的升级方案已经失效。请使用代理模式，例如 [ERC-2535](/eip-2535/) 或标准的可升级代理。
- 依赖针对自身的 `SELFDESTRUCT` 来销毁 ETH 的合约，除非是在同一笔交易中创建的，否则不再销毁 ETH。
- `SELFDESTRUCT` 仍处于弃用状态。Solidity 会对其发出警告，未来的升级可能进一步削减其功能，因此不要在新代码中使用它。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-6780
