Smart Contracts and Ethereum

In this article, we introduce ourselves to smart contracts, how to write smart contracts and how they are executed on the Ethereum blockchain.

Table of contents.

  1. Introduction.
  2. Smart Contracts.
  3. Multisig Smart Contracts.
  4. Writing a Smart Contract.
  5. Code Execution.
  6. Limitations of Smart Contracts.
  7. Summary.
  8. References.

Introduction.

As we have learned in previous articles, the sole purpose of Bitcoin is to act as a form of peer-to-peer transfer of value among two blockchain participants and as a store of value. Thus far it does its job well. It is used by users around the world for simple day-to-day transactions such as the buying and selling of goods and services. The system is decentralized and does not suffer from a single point of failure, also the distributed ledger is tamper-proof and all records are public. We learned the different types of blockchain implementations from public ones such as bitcoin to private and permssioned blockchains and looked at their pros and cons.

As innovation never sleeps, Etherium came about with a framework for code execution back in 2013, whose main component was the smart contract

We consider the following comparison between the two blockchain technologies;

bit116

Above, on the left we have the bitcoin blockchain; As we can see wallets and exchange applications are responsible for initiating transactions among participants on the bitcoin network.

Other the other side - right, we have the Ethereum network, it took what was there and added a computational framework that broadens the scope of what can be achieved by blockchain technology in the decentralized realm.

As mentioned earlier, Ethereum's core component is smart contracts which are executed on the Etherium Virtual Machine. Smart contracts are important since they are solely responsible for extending the functionalities of a blockchain beyond just the transfer or store of value.

Smart Contracts.

In simple terms, it is code deployed on a blockchain node, in this case, an Ethereum node. For a smart contract to execute, it first has to receive a message which is embedded in a transaction.

They work as conditional statements, that is, 'if, ...then...'. When the conditions have been met and validated nodes on the blockchain are responsible for executing the code.

As stated, bitcoin only supports the exchange of value between parties, the logic is simple addition and subtraction, that is, subtracting from the sender, adding to the recipient, each operation executed as a single transaction that is publicly accessible and traceable to its source.

Smart contracts on Ethereum on the other hand allow for more complex operations such as conditional statements and much much more. For example, for a transaction to execute it might be dependent on a lot of factors being just right. For example, in a decentralized crowdfunding application, the business logic may state that for a person to be eligible to donate, he/she must fulfill a specific requirement, this could be the amount or other factors. Such conditions need evaluation, an evaluation that is supported by a smart contract.

With smart contracts, we can go ahead and implement logic as needed and also define a framework for resolving disputes when they occur as they are inevitable.

Some of the pros smart contracts have brought with them include;
Trust and transparency between involved parties, this is because there is no central authority and transactions are public.
Security - Ethereum uses the Elliptic Curve Digital Signature Algorithm(ECDSA) where the bit size of the private key needed is twice the security level. That is, if we are at a security level of 80 bits, a hacker needs a maximum of 2 ^ 80 computations so as to find the private key unless it is phished.
Smart contracts save a lot of time, no bank lines, 24-hr operations, no intermediary, just code executing.
Also once a specified condition is met, an action is performed as stipulated, with no paperwork or any kind of unnecessary process.

Multisig Smart Contracts.

In Bitcoin, we have multi-signature wallets that can be owned and controlled by multiple parties. For example, a 3-of-4 multisignature wallet requires a minimum of three signatures out of four for a transaction to execute successfully. Ethereum allows smart contracts to be multisignatured whereby multiple signatories are required to review and agree on an action before it is executed on the blockchain. The same concept in the example(3-of-4) can be used here, in our case, three out of four signatories need to sign a smart contract before it can perform any operation on the blockchain.

The following are steps involved in the execution of a multi-signature transaction using smart contracts;

  • First a transaction is created.
  • The transaction is wrapped in a multisig transaction - this is another new transaction
  • The transaction is signed and broadcasted to the blockchain
  • n out of m signers are notified to review the transaction
  • n out of m signers approve the transaction
  • The n signers sign the transaction and submit the transaction to the blockchain
  • When the last required signer submits his/her approval, the transaction is executed.

Writing a Smart Contract.

Programmatically, a smart contract is a class definition in an Object-Oriented Design, this means it contains data, functions that operate on the data, modifiers e.g public and private, getters and setters.

Solidity is a programming language that was designed for this specific task, coding smart contracts.

We have the following example of a smart contract that allows anyone on the Ethereum network to store a single number that can be assessed by anyone in the world. Anyone can set another value, but the previous will not be destroyed, it will be stored in the history of the blockchain.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;

contract SimpleStorage{
    uint storedData;

    function set(uint x) public{
        storedData = x;
    }

    function get() public view returns (uint){
        return storedData;
    }
}

Above the first line states that the source code is licensed under GPL version 3.0.
We have written this code for Solidity version 0.4.16 up to, but not including version 0.9.0. This is to ensure ease of compilation.

A contract will hold all source code, in our case our contract is referred to as SimpleStorage. This code consists of the functions that act on its data/state residing at a specific address on the blockchain.

On the fourth line, we declare a state variable storedData of type uint - Unsigned Integer(256 bits).

We have also defined a getter and a setter. The getter method returns the value of storedData while a setter method sets or updates it. The this keyword is used to access a member in the contract.

Code Execution.

For any arbitrary piece of code, an infrastructure for execution is needed, for example, Java code is executed on the Java Virtual Machine, in this case, we are dealing with solidity code. Therefore we need a platform that allows a machine to execute solidity code irrespective of the underlying computer architecture. Solidity is a high-level language as any high-level language undergoes compilation so that it can be executed as machine code on the target machine.

Solidity code, code used to write smart contracts, is executed on the Ethereum Virtual Machine(EVM).
The EVM provides an abstraction layer for smart contracts to be able to be executable on any computer. That is solidity code is compiler into byte code that is deployed in the EVM for execution.
Every node on the Ethereum blockchain will host similar code on the EVM.

The following image shows the architecture of the EVM;
bit117

Ethereum acts as a distributed state machine that not only holds accounts and their balances but also a state of the machine that changes block to block according to a set of specified rules and executes machine code.
These rules of the changing state are specified by the EVM

More on the Ethereum Virtual Machine(EVM) will be covered in later articles.

Limitations of Smart Contracts.

Smart contracts facilitate the execution of an agreement stored in the blockchain. They execute when a predetermined condition is met without fail.

Smart contracts eliminate the need for middlemen during transactions. They define rules and penalties around an agreement and enforce the obligations.
They are a faster, cheaper, and safe way to execute and manage agreements.

Although having all this upside, smart contracts are limited in some ways. These are;

  • Once we write and deploy a smart contract, we cannot change it. Although this is good because they cannot be manipulated, in some cases when an honest mistake was made, it is expensive to make the changes.
  • Smart contract is basically code and code should be clearly written with well-defined steps, however, some terms and conditions cannot be translated into code compared to a lawyer writing a contract.
  • Using smart contracts makes it difficult to make sure that terms and conditions are met because they present loopholes.
  • There is no effective way to guarantee the security of a smart contract on the blockchain.
  • The programming languages used to create smart contracts are yet to advance.
  • Resources to learn about smart contracts effectively are still few.

Summary.

Smart contracts were introduced to add a layer of computational logic to the trusted blockchain that is bitcoin. Apart from the transfer of value, we are now able to add complex business logic to a trusted technology.

Smart contracts are written in a high-level language known as Solidity which has an Object-Oriented Design encompassing functions, data, getters, modifiers, getters, and setters.

High-level solidity code is compiled into bytecode that is executed on an EVM - Ethereum Virtual Machine.

References

Docs