Blockchain Getting Started Tutorial

Source: Internet
Author: User
Tags blockchain blockchain protocol cryptocurrencies blockchain network

The blockchain is a big hit at the moment, and the news media has reported a lot, claiming that it will create the future.

However, there are very few easy-to-understand introductory articles. What is the blockchain, what is special, there is very little explanation.


Below, I will try and write a tutorial on the best blockchain. After all, it is not a very difficult thing. The core concept is very simple and can be clearly stated in a few words. I hope that after reading this article, you can not only understand the blockchain, but also understand what is mining, why mining is getting harder and so on.

It should be noted that I am not an expert in this area. Although very early attention, but a careful understanding of the blockchain, still starting from the beginning of this year. The mistakes and inaccuracies in the text, welcome everyone to correct me.


1. Nature of the blockchain

What is the blockchain? In a word, it is a special kind of distributed database.

First, the main role of the blockchain is to store information. Any information that needs to be saved can be written to the blockchain or read from it, so it is a database.

Second, anyone can set up a server and join the blockchain network to become a node. In the world of blockchains, there are no central nodes, and each node is equal, and the entire database is preserved. You can write/read data to any node because all nodes will be synchronized at the end, ensuring that the blockchain is consistent.


2. Biggest feature of the blockchain

Distributed databases are not new inventions, and such products are already on the market. However, the blockchain has a revolutionary feature.

The blockchain has no administrators and it is completely uncentered. Other databases have administrators, but blockchains do not. If someone wants to add an audit to the blockchain, it will not be realized because it is designed to prevent the emergence of a centrally located management.

It is precisely because of the inability to manage that the blockchain can be controlled. Otherwise, once the big company group controls the management rights, they will control the entire platform, and other users must obey them.

However, without an administrator, everyone can write data to it. How can we ensure that the data is trustworthy? What should I do if I am changed by a bad person? Please read on, this is the wonderful place for the blockchain.


3. Block

A blockchain consists of blocks. A block is much like a database record. Each time you write data, you create a block.


Each block contains two parts.

Head: Record the meta information of the current block.

Body: Actual data

The block header contains multiple meta-information for the current block.

Generation time

Hash of actual data (ie block body)

Hash of the previous block

...

Here, you need to understand what Hash is, which is necessary to understand the blockchain.

The so-called Hash is that the computer can calculate a feature value of the same length for any content. The Hash length of the blockchain is 256 bits, which means that no matter what the original content is, a 256-bit binary number is finally calculated. And it can be guaranteed that the corresponding Hash must be different as long as the original content is different.

For example, the hash of string 123 is a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0 (hexadecimal), which is 256 bits in binary, and only 123 can get this hash.

Therefore, there are two important inferences.

Corollary 1: The Hash of each block is different, and the block can be identified by Hash.

Corollary 2: If the content of the block changes, its Hash will definitely change.


4. Immutability of Hash

The block is in one-to-one correspondence with Hash, and the Hash of each block is calculated for the "Head".

Hash = SHA256 (block header)

The above is the calculation formula of the block Hash, Hash is determined by the block header uniquely, and SHA256 is the Hash algorithm of the blockchain.

As mentioned earlier, the block header contains a lot of content, including the Hash of the current block (note the "block" of the block, not the entire block), and the Hash of the previous block. This means that if the contents of the current block change, or if the Hash of the previous block changes, it will definitely cause a Hash change in the current block.

This is of great significance to the blockchain. If someone modifies a block, the Hash of that block changes. In order for the latter block to be connected to it, the person must modify all subsequent blocks at the same time, otherwise the changed block will be detached from the blockchain. For reasons to be mentioned later, Hash calculations are time consuming, and it is almost impossible to modify multiple blocks at the same time, unless someone has mastered more than 51% of the computing power of the entire network.

It is through this linkage mechanism that the blockchain guarantees its own reliability, and once the data is written, it cannot be tampered with. It's like history, it happened, it happened, and it can't change again.

Each block is connected to the previous block, which is also the origin of the name "blockchain".


5. Mining

Since the synchronization between nodes must be guaranteed, the new block cannot be added too fast. Imagine that you have just synchronized a block and are ready to generate the next block based on it, but in this case another node has a new block generated, you have to give up half of the calculation and synchronize again. Because each block is followed by only one block, you can only generate the next block after the latest block. So, you have no choice but to synchronize immediately as soon as you hear the signal.

Therefore, the inventor of the blockchain, Nakamoto Satoshi (this is a pseudonym, whose true identity is still unknown) deliberately makes it difficult to add new blocks. His design is that every 10 minutes, the entire network can generate a new block, six in an hour.

This rate of output is not achieved by command, but deliberately set a massive calculation. That is to say, only through a very large number of calculations can the effective block of the current block be obtained, thereby adding the new block to the blockchain. Because the amount of calculation is too large, it will not get up.

This process is called mining, because the difficulty of calculating effective Hash is like finding a qualified sand in the sand all over the world. The machine that calculates the Hash is called the mining machine, and the person who operates the mining machine is called the miner.


6. Difficulty coefficient

Read this, you may have a question, people say that mining is difficult, but mining is not to use a computer to calculate a Hash, this is the strength of the computer, how can it become difficult, delay can not be counted out ?

It turns out that not all Hash can be, only the Hash that satisfies the condition will be affected by the block link. This condition is particularly demanding, so that most of the Hash does not meet the requirements and must be recalculated.

Originally, the block header contained a difficulty factor (difficulty), which determines the difficulty of calculating the Hash. For example, the difficulty factor for the 100,000th block is 14484.16236122.


The blockchain protocol stipulates that a target value can be obtained by dividing a constant by the difficulty factor. Obviously, the larger the difficulty factor, the smaller the target value.

The validity of the Hash is closely related to the target value. Only Hash smaller than the target value is valid. Otherwise, the Hash is invalid and must be recalculated. Since the target value is very small, the chance of Hash being less than this value is extremely embarrassing, and it is possible to calculate 1 billion times. This is the root cause of mining so slow.

There is also a Nonce value in the block header that records the number of Hash recalculations. The Nonce value of the 100000th block is 274148111, that is, 274 million times are calculated, and a valid Hash is obtained, and the block can be added to the blockchain.


7. Dynamic adjustment of the difficulty coefficient

Even if mining is difficult, there is no guarantee that it will produce a block in just ten minutes, sometimes it will be calculated in one minute, and sometimes it may not be possible in a few hours. Overall, with the improvement of hardware equipment and the increase in the number of mining machines, the calculation speed will be faster and faster.

In order to keep the output rate constant for ten minutes, Nakamoto also designed a dynamic adjustment mechanism for the difficulty factor. He stipulated that the difficulty factor should be adjusted every two weeks (2016 blocks). If the average generation speed of the block is 9 minutes in the past two weeks, it means 10% faster than the legal speed, so the difficulty factor will be increased by 10%; if the average generation speed is 11 minutes, it means more than the legal The speed is 10% slower, so the difficulty factor is reduced by 10%.

The higher the difficulty factor (the smaller the target value), the more difficult it is to mine.


8. Fork of the blockchain

Even if the blockchain is reliable, there is still a problem that is not solved: if two people write data to the blockchain at the same time, that is, two blocks are added at the same time, because they are all connected to the previous block. A fork is formed. Which block should I adopt at this time?

The rule now is that the new node always uses the longest blockchain. If the blockchain has a fork, you will see which branch is behind the fork point and reach 6 new blocks (called "six confirmations"). According to a block calculation of 10 minutes, it can be confirmed in one hour.

Since the speed at which new blocks are generated is determined by the computing power, this rule means that the branch with most computing power is the authentic bit chain.


9. Summary

Blockchain, as an unmanaged distributed database, has been in operation for eight years since 2009, with no major problems. This proves that it is feasible.

However, in order to ensure the reliability of the data, the blockchain also has its own cost. First, efficiency, data is written into the blockchain, waiting at least ten minutes, all nodes are synchronizing data, it takes more time; second, energy consumption, block generation requires miners to carry out countless meaningless calculations, this is Very energy consuming.

Therefore, the application scenario of the blockchain is actually very limited.

  • There are no authorities that all members trust

  • The data written does not require real-time use

  • The benefits of mining can make up for its own costs

If the above conditions are not met, then the traditional database is a better solution.


At present, the largest application scenario of the blockchain (and possibly the only application scenario) is the cryptocurrency represented by bitcoin. In the next article, I will introduce the introduction to Bitcoin.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.