What are Smart Contracts and how do they get executed?
What are Smart Contracts and how do they get executed?
Since Bitcoin was presented on 2008, boom-ing the Blockchain technology, much has been spoken about the power of this newly launched area. News portals show when big companies adopt blockchain in their workflow, giving holders one reason more to keep their positions. Terms like “private blockchain” start seeking executives’ attention and being subject of meetings in several air-conditioned offices.
Later, in 2014, Vitalik Buterin presented Ethereum’s white paper, bringing the possibility to write and run decentralized applications, or “smart contracts”.
A smart contract is basically a software, written in a human-readable programing language and then compiled to byte code, which is a set of machine-readable instructions that runs on a processor or a virtual machine (a virtual processor). But, where and when do they get executed? Also, what’s the difference between a money transfer transaction and a smart contract transaction?
What’s a Dapp?
When you visit your favorite social network, you are relying on their servers to provide you the right information. You have to trust the social networking website that a friend’s post was really made by them. You got that information from a centralized source, i.e. the social networking website’s servers. In this case, the social networking website is a “trusted party”.
When you accept a payment for a job through a banking transfer, you have to trust your bank that the funds it says you’ve just received were really sent by your employer, and that your position really reflects the funds you have available in your account. You got that information from a centralized source: your bank. In this case, your bank is a “trusted party”.
Although most of the trusted parties we use everyday have reason not to try to fool us with fake information (after all, they have a name to care about), sometimes they become available due to maintenance or hacker attack, and our only source of information is unaccessible. Also, you have to trust, during all the time using their services, that no information had been changed by a software bug or hacker attack.
In Dapps (Decentralized Applications), the central “trusted party” simply does not exist. Instead of a central company who owns the servers and databases with the information you want to access, Dapps rely on the blockchain: a distributed database which tracks logs of every change and have security measures (such as the consensus algorithm itself) to prevent frauds.
Dapps may or may not have a graphical interface, depending on what they are used for. Often, when graphical interfaces are needed, they are nothing more than static interfaces that connect to a node (in a remote machine or in your local machine) and runs over a smart contract on the blockchain.
Yes, Bitcoin also has smart contracts
Although smart contracts are commonly related to Ethereum and newer platforms, it’s true that Bitcoin transactions are also in the form of smart contracts. Limited, however. When you transfer bitcoins to another wallet, actually you are emitting a contract that tells that the owner of the private key for the receiving wallet has the right to spent the amount you’ve just sent.
Alan Turing, a British mathematician considered to be the father of computers, built, in 1936, a machine named as the author that was fed up with a set of instructions and a tape split in many “cells”, to be used as storage (the “state”). It was equipped with a reader, to recognize what was registered on the current cell of the tape; a writer, to insert new data on the current cell; and a mechanism to move to the next or the previous cell. Through a limited set of operations, the Turing machine was able to execute any known algorithm, and started the concept of what we know nowadays as “Turing completude”.
A Turing-complete programming language must be able to execute any task meant to be executed on the Turing machine. Bitcoin’s contract language is not Turing-complete, whereas Ethereum’s smart contract languages (there are a few, but let’s say Solidity, the most popular one) are Turing-complete.
Transactions that create and execute contracts
When you write a smart contract, you have access to a mutable object to be used as storage engine. Your smart contract must contain the initial state, which is the untouched state of the contract.
Once your contract is sent to the network, every full node (nodes that contain the whole Blockchain data downloaded on their hard disks) will download and save your contracts’ byte codes.
A contract may be queried or executed. If you need to read a value in the contract, you will not need to execute a method from it just to get this information: rather, you could just refer to the latest state (after the latest transaction against your contract) and read the needed data from the state.
However, if you want to execute parts of your contract that may produce changes, it’s needed to emit a contract execution transaction, with the input data. In this case, every full node will execute your contract, based on the input data you’ve specified on your execution transaction, and save the new contract state.
It’s important to note that, since Blockchain is immutable, any full-node desiring to download the entire blockchain in the future will execute, once again, your smart contracts and every execution transaction it has received.
Ethereum is often named as “supercomputer with planetarium scale”. The idea behind Ethereum is pretty simple: you use ether (Ethereum’s coin) to pay for CPU processing from other parties, and the network will grant the execution is correct.
However, since Ethereum is a crypto-currency with its price defined by a large amount of players in many exchanges around the world, if you paid for executing your contract with actual ether, you could expect that running a simple contract could get quite expensive as soon as Ethereum’s price rises. Luckily, the team knew it and, to avoid such situation, created an alternative currency which would be used for paying for CPU processing, and named it “gas”.
After writing the contract code in Solidity or other language that can compile to Ethereum’s byte codes, your client (wallet) converts your code to byte codes and estimates the “gas limit”, through a simple calculation: one gas is spent on each contract instruction. Gas limit is the maximum “gas” one can spend for sending a transaction, whether creating or executing a contract.
Then you are able to define the “gas price”, or how much ether you are going to pay per each gas. This price is commonly set by the network and represents a stabilization trial over the price fluctuation. In other words, this value tends to decrease as Ethereum price rises, and increase as Ethereum price drops, in order to keep the same average price (if compare to US Dollars, for example). Like bitcoin transaction fees, you may reduce it lower than the recommended and pay cheaper if you have no urgency and might wait a little longer to have your transaction executed, as well as increase it higher than the recommended if you want it executed faster.
Gas also solves a problem that is acquired once Turing-completude is adopted: infinite loops. A poorly-written or intentionally malicious software may enter a looping state with no condition to leave the loop and, in this case, never stop running. In Ethereum ecosystem, once a node starts executing your contract, it reduces -1 gas from the gas you’ve sent for each instruction it has executed. Therefore, if your smart contract code is buggy and enters an infinite loop, you’ll run out of gas. When it happens, the node stops executing your contract, your transaction is cancelled and the funds you’ve spent on it, returned to your wallet.
Don’t trust. Verify.
This sentence, based on a slightly different Russian proverb, tells enough about how Blockchain nodes work (note that it is not related to Ethereum, only): any information should be verified for validity, despite its origin. There aren’t trusted parties in Blockchain. Nodes should verify every block received from miners and every transaction and funds on it.
With smart contracts, this is not different. When you set the gas price and limit, miners stop competing to be the first one to execute it and include it in a block (additional tasks are needed). Just like Bitcoin transaction fees, only the first miner who achieves it will receive the ether associated with the execution of your transaction. Other miners and nodes will, however, be required also to run your contract, in order to ensure that the execution result is correct.
Ethereum’s model for executing contracts is often target of criticism due to its requirement for every node to execute contracts, although only the first miner receives the payments for doing so. Also, storing so many contracts and their entire execution history demands a substantial amount of free storage for the blockchain database.
A few other projects have presented a different way for running these contracts. A mentionable example is Zilliqa, which plays with the meaning of ‘consensus’. While, in Ethereum, consensus is only reached after most of the nodes in the network have checked the execution result from themselves and achieved the same return, Zilliqa allows you to define ‘how much consensus’ you need, by setting how many machines you want running your contract.
How can my company get benefited with smart contracts?
While the possibilities with smart contracts are very large, computation-related blockchains, such as Ethereum, have two direct impacts on the business that adopt them: data immutability and financial resources saving.
Data saved by smart contracts are stored forever, even if it suffer new changes. This can be used to prevent frauds, since no one is able to edit what had once been sent to the contract.
Also, using smart contracts may show itself as cheaper than using centralized servers in many cases, since a centralized server usually costs money for the company, even when it’s not being accessed, whereas smart contracts only charge some fees every time it receives some transaction.
Smart contracts represent the future of computation, and are powerful enough to solve several problems (such as fraud-related) in business and politics. We, at Forteras, work developing custom smart contracts solutions for individuals and companies. If you want to go decentralized, get in touch with us and tell us your idea. We are looking forward to read about it.