A Byte of Blockchain - Week 49 :
Smart Contract Life Cycle (Part II)

A Byte of Blockchain - Week 49 : Smart Contract Life Cycle (Part II)

·

4 min read

Recap

Last week we explained the smart contract life cycle which consists of the below stages :

  1. Stage 1 - Create a Smart Contract

  2. Stage 2 - Execute a Smart Contract

  3. Stage 3 - Delete a Smart Contract

We explained Stage 1 on how to create a smart contract and the difference between high-level & low-level computer languages.

To create a smart contract,

  1. We have to write a set of instructions in a programming language (like Solidity)

  2. Deploy the instructions on the Ethereum Virtual Machine (EVM).

  3. To deploy the smart contract, these instructions need to be compiled to lower-level BYTE CODE that runs on the Ethereum Virtual Machine (EVM) (We explained compiling last week)

  4. Once compiled, a contract is created through a special transaction on the Ethereum platform.

  5. An address is then allocated to the contract. Every contract deployed in the Ethereum Blockchain is identified through this address.

Let us now explore the next stage of the Smart contract Life Cycle

Stage 2 - Execute a Smart Contract

A computer program or website normally does not respond or initiate anything on its own. A user has to do something like type a command or click on a button for a website or program to respond.

A program or website responds to user actions depending on

a. What the user typed

b. Which button on the website the user clicked etc.,

A smart contract behaves very much the same way. Some event activates a smart contract & that event is a transaction. A smart contract can only be activated by a transaction under below two circumstances :

a. Direct Interactions via transactions initiated by an Externally Owned Account or

b. Indirect interactions through another smart contract (by calling its function(s))

It is worth mentioning that only EoAs can initiate transactions in the Ethereum blockchain.

So, what happens after a transaction activates a smart contract?

The smart contract checks for conditions to be satisfied based on which the transaction gets executed. There are some inputs as part of the transaction which get checked by the conditions & then result in some output. Execution of a transaction results in a change of state in the Ethereum Blockchain (Refer to Week 39 for an explanation of State Transition).

For example, a change of state in a vending machine would mean fewer items & more cash at the end of the day compared to the beginning of the day due to customers buying items from the vending machine.

Here, a change of state will be a change in Externally Owned Account balances (Both sender & beneficiary).

After the execution of a smart contract, all the computers on the Ethereum blockchain will update their ledgers to reflect the new state. This means the smart contract was executed successfully.

The below visualization makes it clear

What happens if a smart contract execution fails due to an error? In that case, the transaction is "rolled back" with all the effects of the transaction reversed - as if the transaction never occurred.

We mentioned earlier that an address is allocated to the contract on its creation in the Ethereum Blockchain. All the aspects related to that smart contract like

a. Storage (or memory)

b. Variables used to store specific data &

c. Functions that call some inputs & compute some output using these inputs

are stored in that location.

Stage 3 - Delete a Smart Contract

One important point to be considered when deploying a smart contract is that a contract's code once deployed cannot be changed.

However, a contract can be deleted by removing the code which in turn removes its internal storage from that address. However, as the Blockchain is immutable, the transactions related to the deleted contract are not removed from the Blockchain.

For deleting a smart contract, it should have the

selfdestruct

option programmed into the contract functionality. If the contract's code does not have this functionality at the time of creation, the smart contract cannot be deleted.