Smart Contracts for Dummies

What are smart contracts and how do they work? In this article, we give a straightforward description and look at the implementation of smart contracts using the Ethereum blockchain as an example.

The smart contract concept in lay terms

The term ‘smartcontract’ has come to mean a type of code that executes on a blockchain platform in such a way that its implementation is verified by independent parties.

This is not distributed computing in the usual sense. Usually, when we talk about distributing a task, we tend to think of a distributed load. Let’s say there’s a task to calculate the number of specific words in a huge text file. We would divide the file into several parts and send each of them to different servers (e.g. using Hadoop).Each individual server would then do the computing for their assigned part of the file and return the result. By summing up the results from all the servers, we would get the total number. This makes solving the task much quicker.

Smart contracts are a totally different matter. If we stick with the example from above, we wouldn’t divide the file into several parts, but would instead send the entire file to each server. Each server would do the computing and (ideally) produce the same result. Basically, the servers check each other’s results. Getting back to the question of whether this can be classified as distributed computing — the answer is yes, it can. They are computations and they are distributed. We probably need a new term to describe this type of distributed computing.

Smart Contracts
Smart Contracts

It’s important to note that this concept is difficult to scale, as the necessary scaling mechanisms were not envisaged at the outset.

So, how can this sort of construct come in useful? The best example is for adopting a social contract between two or more parties. This is where the term ‘smart contract’ comes from. The parties commit to an agreement and the terms for a scenario of how relations between them may develop, and they use a programming language to write them, so that when specific events occur they automatically result in predefined actions.

A classic example of this type of contract, described back in1996 by Nick Szabo, was that of a vending machine that always operates to a strict set of rules: a customer inserts money and selects what they want, and the machine dispenses the appropriate item. After the client inserts their money, they can’t change their mind and ask for a refund. In the case of a smart contract, the code becomes law, as it were: it cannot be contested; it will always be executed when the required conditions occur.

It should be pointed out here that the current implementations of smart contracts are essentially confined to a blockchain environment (below, we will speak about the Ethereum network). What does this mean? You can’t write a contract that will receive data from the external world (e.g. from weather information services) and implement their logic on this data. Of course, there are oracles for that (they require a certain level of trust), or, for example, Microsoft is working on the concept of cryptlets, though there is still a lot of room for improvement there.

Therefore, I will offer a different example of a smart contract that is completely confined to blockchain – a lottery that is absolutely honest and transparent. You send a transaction to the address of a smart contract, and it makes a decision based on a predefined logic (let us assume it is some sort of randomizer) about whether you win or not. If you win, then you receive the entire accumulated bank; if you lose, then your transaction is added to the bank for future players.

All the examples so far are relatively simple and not that interesting. If we take a serious approach to them, what benefits can be gained from smart contracts? Based on the concept of verifiable code execution, we can build systems that were unavailable before, or whose creation was highly complex (e.g. online polls).

Now I would like to show how this works in the case of Polys, online voting platform.

Let’s assume there’s a community that needs to reach a decision via a vote. This is an extremely important decision, and the community is not disposed to trust its individual members. How can this be done? One option is for the entire community to meet in one place and conduct the vote. The result of the voting will be visible to everybody, so trust will be ensured. But what if the community is large and its members live in different places? Online voting may be the next option. Let’s assume the community uses a service to gather its members’opinions. This method of voting works well, up to a point. But what is that point? How can the community control the operator and ensure transparency?

This is where smart contracts come in. We could introduce an online watcher role into the online voting process — that role would be filled by a trusted and respected member of that community; or, alternatively, that right can be given to all competing candidates. The watchers will receive the participants’ votes and record them, keeping this process in sync with other watchers. After counting the votes using smart contracts, we arrive at a result that has been verified and approved by all parties. In that way, the geographically distributed community will be able to achieve a level of trust internally without resorting to a central body.

Yes, smart contracts are not readily scalable in their current implementations, as this scalability was not envisaged in their designs.However, even now heavy computations can be done outside of the smart contract environment, with only certain reference points being formalized – something that the parties can agree on beforehand – while ensuring the required operational speed for certain scenarios.

The algorithm side of the problem

Now, let’s turn to the technical side of implementing smart contracts, using the example of the Ethereum blockchain. How is a smart contract written, published and executed?

In Ethereum, smart contracts are written in the Solidity language – it is similar to JavaScript and is Turing-complete (i.e.any computable function can be implemented in it).

The actual code is executed in the so-called Ethereum Virtual Machine (EVM). It should be noted that the code is executed and verified by all participants of the system, so a mechanism is needed that will somehow limit the resources each smart contract consumes (otherwise, it may end up as an infinite loop).Therefore, the essence called ‘gas’ is introduced in Ethereum. An Ethereum contract can execute any instructions, call other contracts, read and write data etc. All these operations consume gas which is paid in a cryptocurrency called Ether. The price of gas for the Ethercryptocurrency is formed dynamically by the market. A transaction is the trigger for contract execution. The cost of gas burned in a certain transaction is deducted from the account that started the transaction. There is a limit to gas consumption – this is introduced to protect against errors in contract writing, so the entire cryptocurrency in any given account can’t be burned uncontrollably due to an error.

It’s important to note that the smart contract environment is very different from the usual code execution environment that we are used to. In the smart contract environment, an error can be very costly, while operations are also expensive and slow. The mechanism for renewing a smart contract is complicated – you basically have to write a new smart contract and have to convince the parties to use it. Yes, there is a chance that the community may adopt a new version of its contract, but often it means a new social contract is needed. When you think about it, this is a big difference from a situation where you simply publish updates toy our centralized system and users have no choice but to accept it. Before you start building any system for real-life scenarios based on smart contracts, you need to understand these limitations.