Back to Resources

How are Smart Contracts Executed?

May 10, 2021
Execute smart contracts

In one of our most recent entries, we have discussed what smart contracts are on a conceptual basis, their characteristics, and we dove a little deeper into what we should take into consideration in order to avoid possible smart contract vulnerabilities [1]. 

We now turn our attention to the technical aspects of smart contracts. Don’t worry, we will not be looking into daunting code directly. We will, however, take a deeper look into the anatomy of smart contracts and how smart contracts are executed within the blockchains they exist in.

The state

Before analyzing the anatomy of smart contracts, it is crucial to understand (at least theoretically), how the basis upon which smart contracts are created works. Smart contracts are programs executed by blockchain nodes independently, in order to record the last program state.

“A cryptocurrency behaves like a ‘normal’ currency because of the rules which govern what one can and cannot do to modify the ledger (or registry of transactions). A Bitcoin address, for example, cannot spend more Bitcoin than it has previously received.” But blockchains that allow further functionality than send/receive transactions (supporting smart contracts) need a system that can allow for more complex features [2].

Introducing state machines

The Ethereum state machine (EVM) [3] basically records a series of transactions and accounts [4] at definitive points in time. With the right inputs, a certain output is guaranteed.  Given a set of assumptions, any state at time ‘t+1’ equals the state in time ‘t’ plus the newly recorded transactions on the blockchain. That way, each independent node can separately execute the same transactions and arrive at the same new state. There is still broadcasting of transactions in newly mined blocks to the rest of the chain, but this provides an extra layer of security and decentralization, especially for cases in which transactions are cancelled.

What’s in smart contracts and how are they executed?

The vending machine

Following Nick Szabo (coiner of the concept of smart contracts back in 1996 [5]), we can think of smart contracts as a vending machine, one of the simplest transactions to be made. If the value of the money inserted is equal to the value of the selected item, the machine releases the product. Otherwise, it can display messages requesting extra funds, or return the surplus. This case would eliminate but one intermediary (the vendor), however when thinking big-picture, the functionalities of smart contracts are immense.

Getting techy

Smart contracts (SCs) are transactions sent to no one, but with bytecode as data (the code of the soon to be smart contract) that is automatically understood by the blockchains’ virtual machines as accepted code. SCs have their data assigned to a location (storage or memory), variables used to store specific data (like names, balances, etc.), and functions that utilize said variables and instructions as inputs, and turn it into outputs. smart contract execution

Source: Kaleido

Are you still unsure what’s in a SC? Let us try to simplify it.

  1. Once multiple parties identify a cooperative opportunity (business processes, asset swaps, transferral of rights and more) and the desired outcomes, smart contracts are initiated either by the parties involved or by the triggering of certain conditions (alike financial market indices). 
  2. A computer program is written in a way that the process will automatically start when the conditional parameters are met. 
  3. After a smart contract runs, all computers in the (blockchain’s) network update their ledgers to reflect the new state. And as we already know, once the record is verified and posted to the blockchain, it cannot be altered, but only appended.

Execution of smart contracts

So, when and where does the code of smart contracts run? That’s the tricky question. As we initially stated, smart contracts repeatedly run on several different blockchain nodes. The repeated validation is possible because smart contract transactions are deterministic. They may depend on factors such as the block number itself, current storage values for a contract, or the result of another smart contract’s computation, but that information is constant and can be recomputed perfectly by stepping through transactions from the start of the chain. However, the “official” execution point is the point at which the transaction occurs in the blockchain. 

Stay in touch as we continue to develop upon smart contracts and their uses, benefits and shortcomings. If you are planning on deploying a smart contract or already have, check out our Smart Contract Audit offerings.

 

 

 

 

 

References

[1] “Smart Contracts and Their Characteristics .” Scalable Solutions, Resources, 7 Apr. 2021, scalablesolutions.io/news/smart-contracts-and-their-characteristics/

[2] “Ethereum Virtual Machine (EVM).” Ethereum.org, ethereum.org/en/developers/docs/evm/#from-ledger-to-state-machine

[3] We use the Ethereum Virtual Machine as the example for every smart contract platform base machine. Plenty of resources to understand blockchain technology can also be found in the Ethereum developers documentation, and later extrapolated to many other smart contract platforms.

[4] There are two main types of “accounts” on Ethereum, Externally-owned accounts (or EOA, better known as user controlled accounts, and those deployed as smart contracts. In blockchain, we address “transactions” as cryptographically signed instructions from accounts. Transactions can be initiated only by EOA, but once initiated, it allows smart contracts to emit instructions to one another. 

[5] Szabo, Nick. Smart Contracts: Building Blocks for Digital Markets. 1996, www.fon.hum.uva.nl/rob/Courses/InformationInSpeech/CDROM/Literature/LOTwinterschool2006/szabo.best.vwh.net/smart_contracts_2.html

Sources

Zhang, Jim. “What Are Smart Contracts and How Do They Work?” Blog, Resources, 1 Oct. 2019, www.kaleido.io/blockchain-blog/what-are-smart-contracts-and-how-do-they-work

More Articles