Blockchain explanation for web developers

Translator's Preface


I offer readers of Habrahabr a translation of the article "The Blockchain Explained to Web Developers, Part 1: The Theory" by Francois Zaninotto . I found this article on the Marmelab blog. This article is an excellent introduction to blockchain technology from scratch, but it can be interesting for those who are already in the know. It concerns not only how the blockchain works, but also the prospects for its development, as well as where to start if you want to create your own project using the blockchain.

Blockchain Explanation for Web Developers, Part One: Theory


Blockchain is a new popular technology. Even if you have not heard about it, you most likely know about Bitcoin. Blockchain is one of the fundamental technologies on which Bitcoin is based. Experts say that blockchain will cause a revolution similar to the one that the Internet once caused. But what is it really and how can it be used to create applications? This article is the first in a series of three that talks about the Blockchain phenomenon. We will discuss the theory, show the actual code and share our experience based on a real project.

First, let's try to understand what Blockchain is.

What is Blockchain, Part One


Although the blockchain was created to support Bitcoin , the idea of ​​a blockchain can be determined independently of the Bitcoin ecosystem. Literature usually defines blockchain as follows:
Blokcheyn - a magazine with facts , is replicated on multiple computers in a network peer to peer (P2P). Facts can be anything from money transactions to signing content. Network members are anonymous individuals called nodes. All communications within the network use cryptography to reliably identify the sender and receiver. When a node wants to add a fact to the journal, a consensus is formed on the network to determine where this fact should appear in the journal; this consensus is called a block .

I don’t know about you, but after reading these definitions, I still had problems understanding the blockchain. Let's look a little deeper.

Ordering facts


Decentralized peer-to-peer networks are not new. Napster and BitTorrent are P2P networks. Just instead of sharing movies, blockchain network members exchange facts. So what is the real feature of the blockchain?

P2P networks, like other distributed systems, have to solve a very complex computer science problem: conflict resolution, or reconciliation . Relational databases offer referential integrity , but there is no such feature in a distributed system. If two incompatible facts arrive at the same time, the system must have rules to determine which fact is considered correct.

Take, for example, the problem of double spending: Alice has $ 10 and she sends them twice to Bob and Charlie. Who will have $ 10 in total? In order to answer this question, the best way is to streamline the facts. If two incompatible facts appear on the network, then the one that will be first recorded will win.

image

In P2P networks, two facts sent at about the same time can arrive in different order at the remote sites. Then how to coordinate the whole network, what fact came first? To ensure integrity in a P2P network, you need a way to reconcile the facts. You need a consensus system.

Consensus algorithms for distributed systems are a very active field for research.. You may have heard of the Paxos or Raft algorithms. Blockchain implements another algorithm, consensus based on proof of work, using blocks.

Blocks


Blocks are a tricky trick to organize facts on a network with untrusted nodes. The idea is simple: facts are grouped into blocks , and there is only one chain of blocks that is replicated throughout the network. Each block refers to the previous one. That is, if fact F is in block 21, and fact E is in block 22, then fact E is considered by the whole network as following fact F. Before adding to the block, the facts are under consideration , i.e. not confirmed.

image

Mining


Some nodes in the chain create new local blocks with unconfirmed facts. They compete to find out if their local block will become the next block in the chain for the entire network by throwing dice. If a node throws out two sixes, then it gets the opportunity to publish its local block, and all the facts in this block become confirmed. This block is sent to all nodes in the network. All nodes verify that the block is correct, add it to their copy of the circuit, and try to build a new block with new unconfirmed facts.

image

But in fact, the nodes do not just throw a pair of dice. The task that miners solve on the blockchain involves throwing a huge number of dice. By design, finding a random key to verify a block is unlikely. This prevents fraud and makes the network secure (as long as the attacker does not have control over more than half of the nodes in the network). As a result, new blocks will be published to the circuit at a fixed time interval. In Bitcoin, blocks are published, on average, every 10 minutes.

In Bitcoin, the task is a double SHA-256 hash of a string of unverified facts, the identifier of the previous block and a random string. A node wins if its hash contains at least n leading zeros. The number n is adjusted from time to time in order to preserve the duration of the block calculation, despite the change in the number of nodes. This number is called complexity . Other blockchain implementations use special hashing methods that prevent the use of GPUs for calculations (for example, require the transfer of large amounts of memory).

// проигрышный хэш для Bitcoin
787308540121f4afd2ff5179898934291105772495275df35f00cc5e44db42dd
// выигрышный хэш для Bitcoin, если n=10
00000000009f766c17c736169f79cb0c65dd6e07244e9468bc60cde9538b551e




The process of finding blocks is called mining . The thing is that, like gold mining (approx. Transl. Mining - mining), block mining brings economic rewards - some form of money. That is why people who contain nodes in the blockchain are called miners .

Note: By default, a node does not mine - it simply receives blocks that mine other nodes. This is a voluntary process - turning a node into a miner node.

Money and cryptocurrencies


Every second, every miner tests thousands of random strings to form a new block. That is, the work of the miner in the blockchain requires a huge amount of computer resources (memory and CPU). That's why you have to pay to write facts on the blockchain. On the other hand, reading facts is free: all you have to do is run your own site and you will get a full history of facts created by other sites. So to summarize:

  • Reading data for free
  • There is a small fee for adding facts.
  • Block mining brings money in the amount of commission for all the facts included in the block

This is not about real money. As a fact, each blockchain has its own (crypto) currency. It is called bitcoin ( BTC ) on the Bitcoin network, ether ( ETH ) on the Ethereum network, etc. To make a payment on the Bitcoin network, you have to pay a small commission in bitcoins - just as you would have to pay a commission to the bank. However, where did the first money come from?

Miners are rewarded for maintaining network performance and security. Each time they successfully form a block, they receive a fixed amount of cryptocurrency. In Bitcoin, the reward is 25 BTC per block, in Ethereum - 5 ETH per block. Thus, the blockchain itself generates its own money .

Cryptocurrencies quickly becameconvertible to real money . Their nominal value is determined only by supply and demand, so cryptocurrency is an object of speculation. By the time this article was written, the bitcoin mining process still required less energy and equipment expenses than the money you can get by selling coins received for it. That's why people add new miners every day, hoping to turn electricity into money. However, fluctuations in the cost of BTC make mining less and less profitable .

image

Contracts


So far, we mainly talked about the blockchain as a repository of facts, but it can also execute programs . Some blockchains allow each fact to contain a mini program. Such programs are replicated along with the fact, and each node executes them, receiving the fact. In Bitcoin, this is used to complete transactions with conditions , for example: Bob will receive 100 BTC from Alice only if today is February 29th.

Other blockchains allow more complex contracts. For example, in Ethereum, each contract carries a mini-databaseand provides methods for changing its data. Since contracts are replicated across all nodes, so do their databases. Each time the user calls the method from the contract and, accordingly, changes the data, this command is replicated and repeated by the entire network. This allows you to create a distributed consensus to deliver on promises.

This idea of ​​pairing the blockchain with the real world using pre-programmed conditions and transferring them to all nodes is called a smart contract . A contract is a promise that the parties sign in order to secure it legally. A smart contract is the same, only consolidation occurs “technically”, and not “legally”. This eliminates the need for a notary public or any other authorized person recognized by both parties.

Imagine that you want to rent your house for a week for $ 1000 with a 50% prepayment. You and the tenant sign the contract, most likely written by a lawyer. You also need a bank to receive payment. At the beginning of the week you ask for a deposit of $ 5000; the tenant provides you with a check for it. At the end of the week, he refuses to pay the remaining 50%. You will also find out that he broke the window, and the check with the deposit leads to an empty account. Now you need a lawyer to bring your rental agreement to court.

Smart contracts in the blockchain allow you to do without a bank, lawyer, lawyer and court. Just write a program that determines how much money should be transferred in case of certain conditions:

  • Two weeks before rent: transfer of $ 500 from the tenant to the owner
  • Cancellation by the owner: transfer of $ 500 from the owner to the tenant;
  • End of rental period: transfer of $ 500 from the tenant to the owner
  • Proof of mechanical damage after the rental period: transfer of $ 5000 from the tenant to the owner

Add this smart contract to the blockchain and no more problems. By the time specified in the contract, the transfer of money will occur and, if the owner can provide evidence of mechanical damage, he will automatically receive $ 5000 (and there is no need for a deposit).

You are probably wondering how to obtain evidence of mechanical damage. This is where the Internet of Things (IoT) comes in . To interact with the real world, the blockchain needs sensors and drives. Blockchain revolution is impossible without the IoT revolution.

Such applications, based on smart contracts, are called decentralized applications or DApps .

Smart contracts easily extend to smart propertyand many other smart things. You need to remember only one thing: “smart” means “no intermediaries” or “technically executed”. Blockchain is a new way of doing business without intermediaries - just like selling music on the Internet.

image

What is blockchain? Part two


It seems to me that the best way to understand blockchain is to look at it from different angles.

What is he doing? Blockchain allows you to securely distribute and / or process data between several persons through an untrusted network. Data can be anything, but the most interesting option for data is the ability to transmit information that requires a third trusted party. Examples of such information are money (require the participation of a bank), property rights (require the participation of a notary), a loan agreement, etc. In essence, the blockchain eliminates the need for a third party to participate.

How does he work?From a technical point of view, blockchain is an innovation that relies on three concepts: P2P networks, asymmetric cryptography and distributed consensus based on the solution of a mathematical problem. None of these ideas is new in itself. If you do not understand everything, do not worry: few people have the necessary knowledge to develop a blockchain (which is a problem). But the lack of a full understanding of the blockchain does not prevent you from using it, just as you can create web applications without knowing about the slow start of TCP or certificate authorities.

What does he look like?Blockchain can be considered as a (weakly) synchronized database replicated as many times as there are nodes in the network, or as a supercomputer formed by the complex of all CPU / GPU nodes included in it. You can use this supercomputer to store and process data, i.e. as well as you can use the remote API. The only difference is that you do not need to create a backend, and you can be sure that the data is protected and processed properly in the network.

Practical implications


The facts stored on the blockchain cannot be lost. They stay there forever, replicating to every node. Even more so, the blockchain does not just store the final state, it also stores all previous states. Therefore, everyone can verify the correctness of the final state, recounting the facts from the very beginning.

We can trust the facts in the blockchain, because they are technically confirmed by consensus. Even if there are intruders on the network, you can still trust her judgment in general.

Putting data on the blockchain is a rather slow operation since it requires the achievement of distributed consensus.

Hint: If you have 20 free minutes for a deeper understanding, check this out. A great introductory video about Bitcoin that also explains how the blockchain works.

Why is it important


“Blockchain is the most amazing technology I've ever seen.” Salim Ismail
“The most interesting intellectual development on the Internet in the last 5 years.” Julian Assange
“I think the fact that, thanks to the Bitcoin universe, the algorithm replaces the functions of [state] ... it's really very cool. ”Al Gore

These smart people saw huge potential in the blockchain. Potentially, the blockchain can replace all the intermediaries that are necessary to establish trust relationships. Let's take a look at a few applications that are built on the blockchain and implement the idea of ​​refusing an intermediary.

  • Monegraph allows authors to secure the rights to their work and establish rules (and payments) for the use of their work.
  • La Zooz is a decentralized Uber. Offer your car, find a carrier without a fee to Uber.
  • Augur is an online bookmaker. Place bets and get a win.
  • Storj.io is a P2P data warehouse. Give your unused disk space or find the cheapest online storage.
  • Muse is a distributed, open, and transparent database specifically for the music industry.
  • Ripple allows low-cost cross-border payments to banks

Today, many successful Internet enterprises are intermediaries. Think about Google: they managed to become an intermediary between you and the entire Internet. What about Amazon? They became an intermediary between sellers and buyers of any type of goods. This is why technology that eliminates middlemen can explode the Internet.

Will users who no longer need intermediaries to exchange goods and services benefit? It is impossible to say for sure yet. The Internet has previously focused on the absence of intermediaries. However, Google managed to build its market as an intermediary. That's why it is extremely important to invest in the blockchain quickly, because the winners and losers of the next decade are determined right now.

You will not want to create your blockchain


The technologies on which the blockchain is built use advanced cryptography, user network protocols and performance optimization. All this is too complicated to develop it every time a project needs a blockchain. Fortunately, in addition to Bitcoin, there are several more open source blockchain implementations. Here are the most advanced:

  • Ethereum : Ethereum Foundation's Open Source Blockchain Platform
  • Hyperledger : Another open source implementation, only from the Linux Foundation. The first implementation was published recently .
  • Eris Industries: Tools to help use Ethereum, Bitcoin, or fully independent blockchains, mainly to create private networks. Their instructions and guides are a great starting point for a blockchain review.

The complexity of these implementations varies greatly. If you need to create an application right now, we recommend:

  • Eris for small private blockchains or for playing with technology and general familiarization.
  • Ethereum for large blockchain projects

In addition, Bitcoin is not the best choice for a development platform. It was designed for cash transactions, and only though you can program pseudo-smart contracts. The network is currently experiencing a serious growth crisis , with transactions waiting in line for up to an hour to be placed in a block. Miners often choose transactions with the highest commissions, so transferring money to Bitcoin becomes more expensive than at a bank. The development community is on the brink of war, and speculation with cryptocurrency makes face value too volatile.

Figures


How big is blockchain today? Take a look at some numbers.

Bitcoin:



Ethereum:


Conclusion


Blockchain technology is both intriguing and exciting. Will it be the revolution that computer gurus predict? Or is it just an economic bubble based on impractical ideas? After studying a lot of information on this topic, we still cannot form a final opinion.

When we encounter uncertainty, we know a great way to deal with it: try. This is exactly what we decided to do. Read the next article in the series to see what we learned by creating an application running on the blockchain .

Translator's afterword
I translated this article and several other sources to figure out how the blockchain still works. And this series of articles is perhaps the best I've found on the blockchain on the Internet. But nevertheless, this article does not answer the obvious question: “The solution of a complex mathematical problem reduces the likelihood of the simultaneous publication of blocks, but what happens if several nodes solve it at the same time?” The answer is simple: the blockchain will fork, and therefore for such situations in the blockchain there is a special rule: “choose the longest branch”. There is a possibility that several nodes will solve one problem at the same time, but the probability that they will do it two or more times in a row greatly decreases, therefore, sooner or later, the longest branch will appear, which will be placed on the blockchain, i.e. blockchain is quickly stabilizing.

Also popular now: