7

I'm trying to get my head around how contract suicides work, and what actually happens during them. I understand why we use them.

This post states that when the SUICIDE opcode is executed, the data of the contract being killed is removed:

In fact, the SUICIDE opcode uses negative gas because the operation frees up space on the blockchain by clearing all of the contract's data.

If the contract's data is removed to free up space, then surely the block in which it resides changes, and therefore that block's hash also changes. Wouldn't this therefore mean changing all subsequent blocks that rely on this hash?

6

EDIT: I tried but realized I don't have the answer to "If the contract's data is removed to free up space, then surely the block in which it resides changes, and therefore that block's hash also changes. Wouldn't this therefore mean changing all subsequent blocks that rely on this hash?"

Leaving this here in case it helps others explain.

Each block has a "state root" which contains accounts. With a SELFDESTRUCT (original name SUICIDE) opcode, the account is zeroed out.

An account has a nonce, balance, storageRoot, codeHash. The storageRoot is a pointer to a tree structure of the contract's storage: when it is set to zero, it's setting the pointer to null (in languages like C) and this removes all the children of the tree. You're correct that this leads to a new stateRoot, but it doesn't mean an entire copy of the state since everything is pointers.

Picture is worth 1000 words, for example: Ethereum block architecture (all credit to https://ethereum.stackexchange.com/users/124/zanzu for efforts on the diagram)

enter image description here

  • Having recently read up on how the Bitcoin blockchain works, at least in Bitcoin's case the block header's hash is based on everything inside the block header, except for the Merkle Root (Tx_Root). I'm assuming the same probably applies in the Ethereum case: the hash is based on everything in the header, except the State Root. (So changing the contents of the State Root make no difference.) – Richard Horrocks Apr 20 '16 at 21:26
4

From the Ethereum Yellow Paper Section 4, page 3:

4.1. World State. The world state (state), is a mapping between addresses (160-bit identifiers) and account states (a data structure serialised as RLP, see Appendix ??). Though not stored on the blockchain, it is assumed that the implementation will maintain this mapping in a mod- ified Merkle Patricia tree (trie, see Appendix ??). The trie requires a simple database backend that maintains a mapping of bytearrays to bytearrays; we name this under- lying database the state database. This has a number of benefits; firstly the root node of this structure is crypto- graphically dependent on all internal data and as such its hash can be used as a secure identity for the entire system state. Secondly, being an immutable data structure, it allows any previous state (whose root hash is known) to be recalled by simply altering the root hash accordingly. Since we store all such root hashes in the blockchain, we are able to trivially revert to old states.

Note the last sentence. Ethereum can "trivially revert to old states". This includes states before the block that includes the transaction executing the SELFDESTRUCT opcode. As per my understanding, it follows that the contract's data being "removed" frees up space does not mean that any earlier blocks are affected. Instead, the world state from this block onward will not contain the contract account that was destroyed, i.e. there will be no node corresponding to this account. That reduces the storage because there will be no further changes to this account and its storage that need to be recorded. However, it is still possible to revert to an old state before this contract account executed SELFDESTRUCT.

EDIT 1: Confirmation of my interpretation: https://github.com/ethereum/EIPs/pull/42#issuecomment-160237928

EDIT 2: The SUICIDE opcode has been renamed to SELFDESTRUCT to acknowledge the seriousness and sensitivity of mental health issues. (I applaud this change. SUICIDE really seemed unpalatable to me)

1

Also all event logs disappears. The only way to get them after suicide is to use etherum explorer, e.g. etherscan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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