Blockchain – What is Removed During a Contract Suicide and Its Impact

blockchaincontract-designselfdestruct

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?

Best Answer

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

Related Topic