Solidity: A Small Test of the Self-Destruct Operation

Posted by Carpentry on November 27th, 2021

About the opcode that is generated at the compilation of the selfdestruct method there is a nice story about the sensibility of the Ethereum community: Originally the opcode was named SUICIDE but they decided to rename it as SELFDESTRUCT because

“Suicide is a heavy subject and we should make every effort possible to not affect those in our development community who suffer from depression or who have recently lost someone to suicide.” — 

Below you will find a small toy example that contains the smallest amount of logic to make some tests. It is easily executable using  so you can try it even without installing the development environment.

The first method to test is store, which will just store the integer value passed as a parameter in the internal state of the smart contract.send self destruct transfer  The next method to test is retrieve, which, as the smartest among you might already have figured out, retrieves the value stored beforehand in the internal state. Calling this method before actually storing anything will just return 0 because the variable is zeroed by default.
Invoking the close method will just execute selfdestruct without any further notification than what is reported in the next figure, a correctly executed transaction:

From now on, each interaction with the smart contract will be successful but still will not do anything.
This is, in a way, an inconsistency: The client that invokes the smart contract is not aware of the fact that the smart contract has been destroyed. For instance, in this particular smart contract, calling the retrieve method before and after the selfdestruct will return no error, and in the particular case where the internal variable contains 0, the two calls are indistinguishable (see the following figure)

Invoking the close method will just execute selfdestruct without any further notification than what is reported in the next figure, a correctly executed transaction:

From now on, each interaction with the smart contract will be successful but still will not do anything.

This is, in a way, an inconsistency: The client that invokes the smart contract is not aware of the fact that the smart contract has been destroyed. For instance, in this particular smart contract, calling the retrieve method before and after the selfdestruct will return no error, and in the particular case where the internal variable contains 0, the two calls are indistinguishable (see the following figure).

Like it? Share it!


Carpentry

About the Author

Carpentry
Joined: April 24th, 2021
Articles Posted: 302

More by this author