Translator's Preface
I offer Habrahabr readers 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. The article is an excellent introduction to the blockchain technology "from scratch", but it may be interesting for those who are already "in the subject." 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.An Explanation of the Blockchain for Web Developers, Part One: Theory
Blockchain is a new popular technology. Even if you have not heard of it, you most likely know about Bitcoin. Blockchain is one of the fundamental technologies on which Bitcoin is based. Experts say that the 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 telling about the Blockchain phenomenon. We will discuss the theory, show the actual code and share our experience based on a real project.
To begin with, let's try to understand what Blockchain is all about.
What is Blockchain, Part One
Despite the fact that the blockchain was created to support
Bitcoin , the idea of a blockchain can be defined independently of the Bitcoin ecosystem. Literature usually defines a blockchain as follows:
Blockchain is a fact-based journal replicated to several computers connected to a peer-to-peer network (P2P). Facts can be anything from cash transactions to content signing. Network members are anonymous entities 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 take a look a little deeper.
')
Ordering facts
Decentralized 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 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 should have rules for determining which fact is considered correct.
Take, for example, the problem of double spending : Alice has $ 10 and she sends them to Bob and Charlie twice. 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, the one that will be the first recorded will win.
In P2P networks, two facts sent at about the same time may arrive in different orders at remote nodes. Then how can the whole network agree on what fact came first? To ensure integrity in a P2P network, you need a way to reconcile the order of facts. You need a consensus system.
Algorithms for consensus for distributed systems is a
very active field for research . You may have heard of the Paxos or Raft algorithms. The blockchain implements a different algorithm, a consensus based on proof of work (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 replicated throughout the network. Each block refers to the previous one. That is, if the fact F is in block 21, and the fact E in block 22, then the fact E is considered by the entire network as following the fact F. Before adding to the block, the facts
are under consideration , i.e. not confirmed.

Mining
Some nodes in the chain create new local blocks with unconfirmed facts. They compete to find out if their local unit will become the next block in the chain for the entire network, by throwing the 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 check that the block is correct, add it to their chain copy and try to build a new block with new unconfirmed facts.

But in fact, the knots do not just throw a pair of dice. The task that miners solve in the blockchain involves throwing a huge amount of dice. By design, detecting a random key to test a block is
unlikely . This prevents fraud and makes the network secure (as long as the attacker does not have control of more than half of the nodes in the network). As a result, new blocks will be published to the chain at a fixed time interval. In Bitcoin, blocks are published, on average, every 10 minutes.
In Bitcoin, a task is a double SHA-256 hash of unchecked facts, an identifier of the previous block and a random string. A node wins if its hash contains at least n leading zeros.
// Bitcoin
787308540121f4afd2ff5179898934291105772495275df35f00cc5e44db42dd
// Bitcoin, n=10
00000000009f766c17c736169f79cb0c65dd6e07244e9468bc60cde9538b551e
The number n is adjusted from time to time 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 GPUs from being used for computing (for example, they require large amounts of memory to be transferred).
The process of finding blocks is called
mining . The thing is that, like gold mining (mining), mining blocks brings economic reward - some form of money. That is why people who have nodes in the blockchain are called
miners .
Note: By default, a node does not mine; it simply receives blocks that other nodes mine. This is a voluntary process - turning a node into a miner node.
Money and cryptocurrency
Every second, each miner tests thousands of random lines 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 is why
you have to pay to record facts on the blockchain. On the other hand, reading the facts for free: you just have to run your own node and you will receive the full history of the facts created by other nodes. So let's summarize:
- Read data for free
- There is a small commission for adding facts.
- Mining block brings money in the amount of commission for all the facts included in the block
This is not about real money. In fact, each blockchain has its own (crypto) currency. It is called bitcoin (
BTC ) in the Bitcoin network, ether (
ETH ) in the Ethereum network, etc. To make a payment on the Bitcoin network, you must pay a small bitcoin commission - 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 work and network 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 became
convertible into real money . Their nominal value is determined only by supply and demand, so the cryptocurrency is the object of speculation. By the time this article was written, the bitcoin mining process still required less energy and equipment costs than the money you can get by selling the coins you received for it. That is why people add new miners every day, hoping to turn electricity into money. However, fluctuations in the value of BTC make mining
less and less profitable .

Contracts
Until now, we mostly 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 performs them, receiving the fact. In Bitcoin, this is used to perform
conditional transactions , 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-database and provides methods for changing its data. Since contracts are replicated across all nodes, so are their databases. Each time a user calls a method from a contract and, accordingly, changes data, this command is replicated and repeated throughout the network. This allows you to create a distributed consensus to fulfill promises.
This idea of interfacing 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 to legally secure it. A smart contract is the same, only the consolidation takes place "technically" and not "legally". This eliminates the need for a notary 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 a 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 gives you a check for him. At the end of the week, he refuses to pay the remaining 50%. You will also learn that he broke the window, and a check with the deposit leads to an empty account. Now you need a lawyer to submit your rental agreement to the court.
Smart contracts in the blockchain allow you to do without a bank, a lawyer, a lawyer and a court. Just write a program that determines how much money should be transferred in case of certain conditions:
- Two weeks before renting: transfer of $ 500 from the tenant to the owner
- Cancellation by the owner: transfer of $ 500 from the owner to the lessee;
- End of rental period: transfer of $ 500 from the leasing to the owner
- Proof of mechanical damage after the rental period: transfer of $ 5000 from the lessee 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 $ 5,000 (and there is no need for a deposit).
You are probably wondering how to get evidence of mechanical damage. This is where the
“Internet of Things” (IoT) comes into play. Sensors and actuators are needed to interact with the real world of the blockchain. Blockchain revolution is impossible without IoT revolution.
Such applications relying on smart contracts are called
decentralized applications or
DApps .
Smart contracts are easily extended to
smart property and many other smart things. Remember only one thing: "smart" means "no intermediaries" or "executed technically." Blockchain is a new way of doing business without intermediaries - as well as selling music on the Internet.

What is blockchain. Part two
I think the best way to understand the blockchain is to look at it from different angles.
What is he doing? Blockchain allows you to securely distribute and / or process data between multiple individuals through an untrusted network. Data can be anything, but the most interesting version of the data is the ability to transfer information, which requires a third trusted party. Examples of such information are money (require the participation of a bank), rights to property (require the participation of a notary), a loan agreement, etc. In essence, the blockchain eliminates the need for the participation of a third trustee.
How does he work? From a technical point of view, the blockchain is an innovation based on three concepts: P2P networks, asymmetric cryptography, and distributed consensus based on solving a mathematical problem. None of these ideas is new in itself. If you do not understand everyone, do not worry: few people have the necessary knowledge to develop blockchains (which is a problem). But the lack of a complete 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? A blockchain can be viewed as a (weakly) synchronized database replicated as many times as there are nodes in a network, or as a supercomputer formed by a 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 remote api. The only difference is that you do not need to create a backend, and you can be sure that the data is reliably protected and properly processed on the network.
Practical implications
Facts stored on the blockchain cannot be lost. They remain there forever, replicating to each node. Moreover, 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 by recounting the facts from the very beginning.
We can trust the facts on the blockchain, since they are technically supported by consensus. Even if there are intruders on the network, you can still trust her judgment as a whole.
Placing data on a blockchain is quite a slow operation, since it requires achieving a distributed consensus.
Tip: If you have 20 free minutes for a deeper understanding, watch
this excellent introductory video about Bitcoin, which 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 is that thanks to the Bitcoin universe, the algorithm replaces the functions of [the state] ... this is really very cool.” Al Gore
These smart people saw a huge potential in the blockchain. Potentially, the blockchain can replace all 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 abandoning the middleman.
- Monegraph allows authors to secure 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 payment to Uber.
- Augur is an online bookmaker. Place bets and get a win.
- Storj.io is a P2P data store. Hand over 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. And Amazon? They became an intermediary between sellers and buyers of all types of goods. That is why technology that eliminates middlemen can explode the Internet.
Will users benefit who no longer need intermediaries to exchange goods and services? While it is impossible to say for sure. The Internet has previously focused on the absence of intermediaries. Nevertheless, Google managed to build its market, being an intermediary. That is 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 do not want to create your blockchain
The technologies on which the blockchain is built use advanced cryptography, custom network protocols, and performance optimization. All this is too difficult to develop it every time a blockchain is required for a project. Fortunately, in addition to Bitcoin, there are several open-source blockchain implementations. Here are the most advanced:
- Ethereum : an open source blockchain platform from the Ethereum Foundation
- Hyperledger : another open source implementation only from the Linux Foundation. The first implementation was published quite recently .
- Eris Industries: Tools to help use Ethereum, Bitcoin, or fully independent blockchains, mainly for creating private networks. Their instructions and guides are a great starting point for reviewing the blockchain.
The complexity of these implementations varies greatly. If you need to create an application right now, we recommend:
- Eris for small private blockchains or to play around with technology and general information.
- 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, although you can program pseudo-smart contracts. Currently, the network is experiencing a
serious growth crisis , transactions are waiting in line for up to an hour to be placed in the unit. Miners often choose transactions with the highest commissions, so transferring money to Bitcoin becomes more expensive than at a bank. The developer community is on the verge of war, and cryptocurrency speculation makes the face value too volatile.
Numbers
How big is the blockchain today? Take a look at some numbers.
Bitcoin:
Ethereum:
Conclusion
Blockchain technology is both intriguing and exciting. Will it be a revolution predicted by computer gurus? 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 face 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 epilogueI performed the translation of this article and several other sources in order to find 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 still, this article does not answer the obvious question: “Solving a complex mathematical problem reduces the likelihood of 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 there is a special rule in the blockchain: “choose the longest branch”. There are chances that several nodes will solve one task at the same time, but the probability that they will do it two or more times in a row is greatly reduced, therefore, sooner or later, the longest branch will appear, which will be placed on the blockchain, i.e. blockchain quickly stabilized. You can learn more about this and the impact of branching on transaction security from a video that has already been suggested before.