EIP-4758 · Stagnant · Core · 2022-02-03
EIP-4758: Deactivate SELFDESTRUCT
Proposed turning SELFDESTRUCT into SENDALL, which only sends the contract's ETH to the target and never deletes code or storage, to prepare Ethereum for Verkle trees and statelessness.
What problem does it solve?
SELFDESTRUCT wipes an account: its code, all of its storage, and its nonce. That is a large, unbounded state change. With Verkle trees, the data of one account is spread over many keys that are not obviously linked to the account root, so deleting "everything belonging to this account" becomes impractical.
The spec argues there are no strong reasons left to use SELFDESTRUCT, and that disabling it is a requirement for statelessness.
How it works
At a hard fork:
- The
SELFDESTRUCTopcode (0xff) is renamedSENDALL. - It now only moves all of the account's ETH to the target address, immediately.
- It no longer deletes code or storage and does not change the nonce.
- All gas refunds related to
SELFDESTRUCTare removed.
Contracts that only used SELFDESTRUCT to sweep their ETH keep working. What breaks:
- Metamorphic / redeploy patterns: destroying a contract and re-creating different code at the same address with
CREATE2. After this change the old code never goes away, so redeployment fails. The spec says upgradeability should use delegate-call proxies instead. - "Destroy to lock" patterns, where a contract self-destructs at the end of an operation so nobody else can use it.
- Using
SELFDESTRUCTto burn non-ETH tokens held by the contract (the authors knew of no such use).
Sample code
Code that only uses selfdestruct to sweep funds behaves the same under SENDALL, except the contract stays alive afterwards:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Piggy {
address public immutable owner = msg.sender;
receive() external payable {}
function smash() external {
require(msg.sender == owner, "not owner");
// Before: ETH sent to owner AND code/storage deleted.
// Under EIP-4758 (SENDALL): ETH sent to owner, contract keeps its code.
selfdestruct(payable(owner));
}
}
A safer pattern that works under any rule set:
function withdrawAll() external {
require(msg.sender == owner, "not owner");
(bool ok, ) = owner.call{value: address(this).balance}("");
require(ok, "send failed");
}
Things to know
- Status: Stagnant. Ethereum took a softer route: EIP-6049 formally deprecated
SELFDESTRUCT, and EIP-6780 (Cancun) restricts it so code and storage are only deleted when the contract was created in the same transaction. Otherwise it just sends the ETH, which is close to SENDALL. - Like SENDALL,
SELFDESTRUCTstill sends ETH without calling the recipient, so a contract can receive ETH without itsreceivefunction running. Do not rely onaddress(this).balancefor accounting. - Solidity compilers emit a deprecation warning for
selfdestruct; avoid it in new code.