📜 ⬆️ ⬇️

Another use of blockchains: Smart contracts

In one of our first posts, we told you that the blockchain is a decentralized system, which is supported by many computers connected to a network. The blockchain, although it has some kind of flaws (limited speed, compared to centralized databases, as well as high power consumption - in the case of blockchains based on the proof of work), still remains a safe and reliable solution. Therefore, all sorts of financial institutions, banks and even giants of the IT industry ( IBM , Cisco and Intel ) are eyeing this technology.

In 1994, cryptographer Nick Szabo proposed using computers and cryptography to automatically execute and audit contracts. Later this led to the emergence of so-called smart contracts, or simply smart contracts. Such contracts are executed in the form of a code, and then saved in the system, where their execution is monitored by a network of computers that manage the blockchain. We will talk about them in our today's material.

/ image by Jason Benjamin PD
')

What is a smart contract


A smart contract is an electronic algorithm that facilitates or even automates the contracting process. The basic idea is to modernize the labor-intensive procedure in such a way that all parties understand the agreement in the same way and without discrepancies in interpretations. To describe the conditions and outcomes of smart contracts, programming languages ​​and mathematical tools (for example, public key cryptography) are used, and such contracts are executed on computers.

Smart contracts allow you to exchange assets without resorting to the services of intermediaries. In addition, smart contracts do not simply contain information about the obligations of the parties: the program code confirms the fulfillment of the contract and automatically determines what to do with the specified asset (transfer to the transaction participant, return to the sender, or something more complicated). All this time, a copy of this document is stored in the decentralized registry, which ensures its security and accuracy and does not allow any of the parties to change the predefined conditions of the contract.

One of the main advantages of using the blockchain as a transport for smart contracts is the absence of the need to involve a third party as a guarantor of compliance. The blockchain is distributed between many nodes of the network and is guided by a formalized set of rules, which minimizes the risk of incorrect execution of the contract (and the fact that the contract will not be executed at all due to disagreements between the parties). Thus, the blockchain allows you not to rely on the authority of the intermediary.

Since all information in the blockchain is stored in the form of a decentralized ledger, the parties to smart contracts and third parties (for example, accountants, auditors or regulators) are able to easily conduct an audit of the implementation of contracts. Moreover, this audit can be performed in real time and is easy to automate.

The features of consensus in blockchains ensure that the contract is atomic - it can be either successfully completed or canceled with the activation of automatic cleaning in the same way as transactions in the DBMS. It is impossible to “get stuck” in the midst of the fulfillment of a contract or to get different execution results on different nodes of the network.


Smart Contract Execution

Ethereum is perhaps the most popular blockchain for writing complex smart contracts. Here is an example of code written for a contract in a specially developed Solidity smart contract language (the language is easy to read and is similar to JavaScript, with only static typing). This code implements the simplest token, that is, a fully digital asset that can be issued and transferred between Ethereum wallets. As part of a smart contract, each request for the transfer of tokens is automatically checked for the presence of the required number. If everything is in order, then the smart contract changes the balance of the token for the sender and receiver.

As can be seen from the code, smart contracts in Ethereum are objects (more precisely, agents ) that store the state and the methods by which this state is updated. Transactions in Ethereum, respectively, are calls to contract methods. Contracts can interact with each other by calling the methods of other contracts, but any action is ultimately initiated by a transaction from outside the blockchain (that is, it is not possible, for example, to set up Ethereum smart contract so that it runs on schedule).

There are several reasons for this. Smart contracts must be executed in the same way on all nodes of the network (otherwise the Ethereum blockchain will split into several parts) and must be verifiable. Therefore, interactions with the outside world for smart contracts are limited. For the same reason, Ethereum smart contracts cannot read or write local files or interact with the network — they run on a virtual machine specially created for Ethereum.

Programming smart contracts to one degree or another is possible in the vast majority of blockchains. At the same time, Ethereum's object-oriented approach is far from the only one. There are other - often inspired by academic studies - programming languages ​​that far better fulfill the key requirements for smart contracts. For example, some blockchains like Synereo use process calculus (the approach used in Erlang and Go ), presenting smart contracts as processes interacting with each other through message channels.

On the bitcoin blockchain, smart contracts are represented by the conditions under which you can spend bitcoins. As already noted, the bitcoin blockchain is built on transactions . These transactions contain one or more inputs and outputs. Moreover, each transaction input is an unspent Transaction Output (UTXO) of one of the previous transactions recorded in the blockchain.

Any transaction consumes the outputs of previous transactions and produces new outputs that will be used by future transactions, and only once. This structure has many useful mathematical properties, which also include constructive proof that the same tokens will never be spent twice if each transaction proves that the sum of its inputs is greater than the sum of its outputs. At the same time, each transaction conclusion is associated with a value, that is, a certain integer indicating the number of bitcoins consumed.

Also, each unspent output should imply a user (or several users) capable of spending the cost associated with UTXO. To fulfill this condition, the bitcoin protocol includes scripts. With each bitcoin-based UTXO, a locking script is associated that defines the conditions when this bitcoin can be spent.

The script looks like a sequence of instructions for a stack machine , a la Forth . Script instructions are executed in turn, and each instruction can influence the stack, for example, add a new element to it, push an element from the top of the stack, change the two top elements of the stack in places, and so on. In the Bitcoin scripting language, there are instructions for branching, but there are no cycles — the language is intentionally Turing-incomplete in order to minimize the risks of DoS attacks.

In order for the locking script to succeed, you must provide a unlocking script, which is also a sequence of instructions for the stack engine. It will be executed before the locking script and will form an initial stack for it. In the simplest case, the locking script provides some public key and requires a digital signature with the corresponding private key:

<Key> CHECKSIG

Where:

The unlocking script (for the locking script presented above) consists of one element — the digital signature of the transaction. The script execution algorithm is as follows:

// 1. Initialization
script: <Signature>
stack: empty
// 2. Executes the single unlock script statement.
script: empty
stack: <signature>
// 3. The locking script begins
script: <Key> CHECKSIG
stack: <signature>
// 4. The first instruction is to add a key to the stack.
script: CHECKSIG
stack: <signature> <key>
// 5. The second instruction - check the signature
scenario:
stack: <success>

In Bitcoin, more complex smart contracts are realizable, for example, a contract for depositing funds with the possibility of returning by timeout:

IF
// Requires any 2 of 3 signatures from Alice, Bob or arbitrator.
2 <Alice key> <Bob key> <Referee key> 3 CHECKMULTISIG
ELSE
// Verifies that since the receipt of funds at the deposit address
// 7 days have passed.
// DROP - instruction to push an element from the stack; here she is needed
// for backward compatibility - CHECKSEQUENCEVERIFY is recognized
// not all versions of bitcoin nodes
<7 days in seconds> CHECKSEQUENCEVERIFY DROP
// If the previous check is successful, then Alice can take the funds.
<Alice Key> CHECKSIG
ENDIF

This scenario is useful if Alice pays Bitcoins to Bob for a certain product, but the parties do not trust each other - Alice does not want to pay before receiving the goods, and Bob does not want to give the goods before receiving the bitcoins. In this case, Alice and Bob can choose an arbitrator who will resolve possible disputes. After that, Alice will send the funds to the address associated with the scenario above.

If the contract is successfully completed, Bob will be able to withdraw funds by signing a transaction with Alice. If a controversial situation arises, the arbitrator may intervene and take the side of Alice or Bob, signing a transaction with one of them. Finally, if a lot of time has passed since the transfer of funds to the address (say, 7 days, as in the example above), Alice can return the deposited funds back, even without the approval of the arbitrator or Bob.

In terms of a bitcoin stack engine, the unlocking script options might be:

// Transfer of funds with the consent of Alice and Bob.
// The first 0 is necessary because of a bug in the MULTISIG instruction -
// it takes one more item from the stack.
// The last one activates the IF branch in the lock script.
0 <Alice's signature> <Bob's signature> 1

// The arbitrator agreed with Alice.
0 <Alice’s signature> <Arbitrator’s signature> 1

// The arbitrator agreed with Bob
0 <Bob Signature> <Arbitrator Signature> 1

// Refund on time out.
// 0 activates the ELSE branch in the locking script.
// This script will not be valid if the timeout has not yet passed.
<Alice's Signature> 0

Rootstock - “seedling” in blockchain-environment


Bitcoin-blockchain, although it has the highest stability and security among all blockchains, due to its size, it still lacks a certain flexibility when it comes to the introduction of new technologies. For this reason, Ethereum platform is mainly used for working with smart contracts, since it was the first technology that implemented smart contracts in an easy-to-use form. However, the technology was faced with certain failures, for example, last year because of an error in the code written on Ethereum technology, hackers stole about $ 64 million.

Therefore, the community thought about creating a network that would take the best from two worlds: reliability and security from Bitcoin and user experience from Ethereum. This led to the Rootstock blockchain solution from RSK Labs, which on May 22 received an investment of $ 3.5 million. Anthony Di Iorio, CEO of the Jaxx cryptocurrency wallet, as well as several mining companies, including Bitfury and Bitmain, have invested in the development of the project.

In an interview with Diego Gutierrez Zaldivar, RKS Labs CEO, Diego Gutierrez Zaldivar noted that the project’s goal is to create a blockchain that would be supported by both Bitcoin miners and smart application developers working with Ethereum.

At its core, Rootstock is a decentralized Turing-complete platform for smart contracts. Rootstock uses the bitcoin ecosystem, but with some improvements instead of shaping the entire system from scratch. Today, the platform is able to process 400 transactions per second, while Bitcoin can process only seven. In the future, RSK plans to achieve a value of 2,000 TPS using the LTCP (Lumino Transaction Compression Protocol) protocol.

Rootstock’s biggest advantage over other platforms using its own blockchains is merged mining with bitcoin, which raises its security to the level of the older blockchain network. The technology is still being tested in a test network, but it will soon be launched into real life. Given that RSK uses bitcoin blockchain, which is currently the safest blockchain, RSK smart contracts will be able to outperform Ethereum in some protection issues. For example, they will provide greater security against the rollback of transactions in the blockchain and the “51% attack”.

Future and application of smart contracts


Due to its properties - the security of the system is guaranteed by mathematical laws (and not by the authority of the intermediary), which are the basis of the blockchain technology and Bitcoin in particular - smart contracts are used in various fields of activity. One of the potential directions of development of smart contracts is the elective system. Smart contracts will completely eliminate the likelihood of interference in elections by third parties. In this case, the votes will be placed in the distributed registry (of course, in an encrypted and anonymized form).

Another use of digital contracts is in logistics. In the field of supply involves a large number of different documents that need to be signed and approved at different levels. This creates loopholes for scammers who get a chance to make money. Blockchain allows you to build a system in such a way that each of the participants in the supply chain will have access to an electronic system that controls all the work execution processes. This is effective when working with documents in general. For example, Barclays uses smart contracts to register the transfer of ownership and automatically register payments to credit organizations.

Another area in which smart contracts will find application is the auto industry. With the help of smart contracts, insurance companies will be able to set the size of contributions depending on how and under what conditions drivers drive cars. Such systems will even be able to determine who was to blame for the accident: the sensors or the driver, as well as to resolve more complex traffic incidents.

It is worth noting other industries, such as lending and accounting. Here, smart contracts will be used for risk assessment and real-time auditing. The Blockchain Technologies website says that smart contracts will be a paper-electronic hybrid when they are confirmed by the blockchain, and then printed as a paper copy.

At the same time, many market participants are convinced that in the future companies will be able to completely move from drawing up traditional contracts to their digital implementation and even the implementation of contracts with the support of artificial intelligence.

Source: https://habr.com/ru/post/330316/


All Articles