15

What happens to the state of a smart contract when it is selfdestruct. Are the last state variables still visible on the blockchain afterwards?

1

3 Answers 3

10

selfdestruct clears all the contract's data, it frees up space on the blockchain (exactly from the current and future state tree (a Merkle patricia tree) not from the previous blocks, the contract bytecode will remain in a old block but its account will be no longer accessible). we talk about freeing space because there will be no entry for the contract in the state tree(the mapping between the contract's account and its states), but the data is still immutable as it's will be stored in previous blocks. in other words after selfdestruct the state tree will be updated without the contract states.

enter image description here

(illustration source )

after selfdestruct enter image description here (initial illustration source)

the contract's funds are sent to the address given as parameter to selfdestruct. After calling selfdestruct the sent ethers to such contract are lost.

in the documentation you'll find :

The only possibility that code is removed from the blockchain is when a contract at that address performs the selfdestruct operation. The remaining Ether stored at that address is sent to a designated target and then the storage and code is removed from the state.

3
  • 1
    Is this really correct? It seems that all state is NEVER removed from the blockchain. Also, from the text you posted above is says that Ethere is returned and not the ethers send to such contract after selfdestruct call are lost. As you have mentioned.
    – RFV
    Dec 20, 2016 at 21:00
  • the ethers held by the contract are returned but in my answer i am talking about ethers sent by users to destructed contract (this a proof that the contract's account dosn't exists anymore)
    – Badr Bellaj
    Dec 21, 2016 at 22:08
  • @RFV The blockchain is separate from the state tree. The state tree contains the current values of all account balances and all variables in all contracts - and it's nice if it's kept as small as possible. Selfdestruct allows the contract to be deleted from the state tree.
    – user253751
    Oct 11, 2022 at 15:24
12

If I'm not mistaken, the docs are a little misleading on this point. A lot of danger around the illusion of safe destruction. Immutability wins over "delete". Nothing really gets destroyed.

Blockchain has no concept that corresponds to overwrite like DB or a disk. It can only ever add changes to what it had before. If data, or a contract was ever present, then it always will be present.

The docs say the storage is removed from the state. In a manner of speaking, yes. Possibly it should say "from the easily accessible current state". It doesn't zero out mappings. That is a potentially huge operation that doesn't occur. The blockchain doesn't get any smaller.

The data is still there, and even if the dev goes the extra mile to destroy it, a determined adversary can roll back time to a pre-destruction block, and there it is. As is the "destroyed" contract.

2
  • 3
    the current state which is cleared not the blockchain so after selfdestruct the current state will not refer the destucted smartcontract it is like if you want to free an array you use delete this means that the next state(kind of snapshot) will change but the previous value still exists in blockchain history.
    – Badr Bellaj
    Dec 21, 2016 at 22:15
  • 1
    Also worthwhile to note that the contract can still be called and the call won't revert, but won't update the state either. Sep 21, 2019 at 20:17
1

According to the http://solidity.readthedocs.io/en/v0.3.1/frequently-asked-questions.html It removes the contract bytecode and storage from the current block into the future, but since the blockchain stores every single block (i.e. all history), this will not actually free up space on full/achive nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.