EIPs Explained

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 SELFDESTRUCT opcode (0xff) is renamed SENDALL.
  • 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 SELFDESTRUCT are 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 SELFDESTRUCT to 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, SELFDESTRUCT still sends ETH without calling the recipient, so a contract can receive ETH without its receive function running. Do not rely on address(this).balance for accounting.
  • Solidity compilers emit a deprecation warning for selfdestruct; avoid it in new code.

#evm #selfdestruct #opcodes #statelessness #core

Details

Authors
Guillaume Ballet (@gballet), Vitalik Buterin (@vbuterin), Dankrad Feist (@dankrad)
Status
Stagnant
Type
Standards Track / Core
Created
2022-02-03

Machine-readable: Markdown · JSON