Block chain Introductory Tutorial __ block chain

Source: Internet
Author: User
Tags constant

The Block chain (blockchain) is now a big hit, with a lot of news media reporting that it will create the future.

But there are very few introductory articles that are easy to understand. What is so special about the block chain is that there are few explanations.

Next, I will try to write a piece of the best understanding of the block chain tutorial. After all, it is not a difficult thing, the core concept is very simple, a few words can be said clearly. I hope that after reading this article, you can not only understand the block chain, but also understand what is digging, why mining more and more difficult problems.

It should be explained that I am not an expert in this field. Although very early attention, but a careful understanding of the block chain, or from the beginning of this year. The mistakes and inaccuracies in the article are welcome. the nature of the block chain

Block chain is what. In a word, it is a kind of special distributed database.

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

Second, anyone can set up a server, join the block chain network, become a node. Block chain of the world inside, there is no central node, each node is equal, all save the entire database. You can write/read data to any node, because all nodes will be synchronized at the end to ensure that the block chain is consistent. the biggest characteristic of block chain

The distributed database is not a new invention, and the market has such products early on. However, the block chain has a revolutionary character.

Block chain without administrator, it is completely without center. All other databases have administrators, but there is no block chain. If someone wants to add an audit to the block chain, it will not be achieved, because its design goal is to prevent the emergence of a central authority.

It is because of the inability to manage the block chain can not be controlled. Otherwise, once big corporations control their management, they will control the entire platform, and other users will have to take orders from them.

However, without the administrator, everyone can write to the inside data, how to ensure that the data is trustworthy. How to be changed by the bad guys. Read on, this is where the chain of blocks is wonderful. third, block

Block chain consists of blocks (block). Chunks are like database records, and each time you write data, you create a chunk.

Each block contains two parts. Block Header (head): Record the eigenvalues of the current block (body): Actual data

The area header contains a number of eigenvalues for the current block. Generate the hash of a chunk on the hash of the actual data (that is, the block body) of the time ...

Here, you need to understand what hash is called, which is necessary to understand the block chain.

The so-called "hash" is that a computer can compute a characteristic value of the same length for any content. The hash length of a block chain is 256 bits, which means that, regardless of the original content, a 256-bit binary number is computed. It is also guaranteed that, as long as the original content is different, the corresponding hash must be different.

For example, the hash of string 123 is a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0 (hexadecimal), the binary is 256 bits, and only 123 can get the hash. (Theoretically, other strings may also get this hash, but the probability is extremely low and can be approximated as unlikely to happen.) )

Therefore, there are two important inferences. Inference 1: The hash of each chunk is different, and the chunk can be identified by hashing. Inference 2: If the contents of a chunk change, its hash will change. the non-modification of Hash

A chunk corresponds to a hash of one by one, and the hash of each chunk is computed for the block header (head). That is, the various eigenvalues of the block headers are concatenated together in order to form a very long string, and then the hash is computed for the string.

Hash = SHA256 (area header)

The above is the calculation formula of the chunk hash, SHA256 is the hash algorithm of the block chain. Note that the formula contains only the block header, not the block body, that is, the hash is only determined by the block size,

As I said before, the area header contains a lot of content, including a hash of the current block body, and a hash of the previous block. This means that if the contents of the current block body are changed, or if the hash of the previous block changes, the hash of the current block is bound to change.

This is of great significance to the block chain. If someone modifies a block, the hash of the chunk is changed. In order for the following block to be connected to it (because the next block contains a hash of the previous block), the person must modify all the subsequent blocks, otherwise the broken block is detached from the block chain. Because of the reasons to be mentioned later, hashing is time-consuming, and it is almost impossible to modify multiple blocks in a short period of time, unless someone has mastered more than 51% of the computing power of the network.

It is through this linkage mechanism that the block chain guarantees its own reliability, and once the data has been written, it cannot be tampered with. It's like history, it's happening, it's never going to change.

Each block is attached to the previous block, which is the origin of the name "block chain". v. Mining

Because of the need to ensure synchronization between nodes, the new block can not be added too fast. Imagine that you have just synchronized a block, ready to generate the next block based on it, but then another node has a new block generation, you have to give up half of the calculation, to sync again. Since each block is followed by only one block, you can always generate the next block only after the newest block. So you have no choice but to sync at once when you hear the signal.

Therefore, the block chain of the inventor Ching (this is a pseudonym, the real identity is unknown) deliberately let add new area block, become very difficult. His design is that on average every 10 minutes, the whole network can generate a new block, an hour is six.

This output speed is not achieved through the command, but deliberately set a huge amount of calculation. In other words, only through extremely large number of computations can we get the valid hash of the current block, thus adding the new block to the block chain. The amount of calculation is too great to get up.

This process is called mining (mining), because the difficulty of calculating an effective hash is like finding a grain of sand in the world of sand. The machine that computes the hash is called the mine machine, and the man who operates the mine is called the miner.

vi.. Difficulty coefficient

Read here, you may have a doubt, people say that mining is difficult, but mining is not the computer to calculate a hash, which is the strength of the computer Ah, how can become difficult, delayed calculation.

The original is not any hash can be, only the hash that satisfies the condition will be accepted by the block chain. This condition is particularly harsh, making most hashes do not meet the requirements and must be calculated.

Originally, the area header contains 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 Block chain protocol stipulates that the target value can be obtained using a constant divided by the difficulty factor. Obviously, the greater the difficulty coefficient, the smaller the target value.

The validity of a hash is closely related to the target value, and only Hachicai that are less than the target value are valid, otherwise the hash is invalid and must be calculated. Because the target value is very small, the chance that the hash is less than the value is extremely slim and may be calculated 1 billion times before it is counted once. That is the root cause of such a slow mining.

As I said before, the hash of the current chunk is only determined by the size of the zone. If you want to compute the hash repeatedly for the same block, it means that the block size must change constantly, otherwise it is impossible to calculate a different hash. All the eigenvalues are fixed, in order for the block head to change, the Ben Cong intentionally adds a random term called Nonce.

Nonce is a random value, the role of the miner is to guess the value of the Nonce, so that the hash of the block header can be less than the target value, thus able to write block chain. Nonce is very difficult to guess, at present only through the poor method of trial and error. According to the protocol, the Nonce is a 32-bit binary value, that is, the maximum can reach 2.147 billion. The Nonce value of the 100,000th block is 274148111, it can be understood that the miners start from 0, has been calculated 274 million times, only to obtain a valid Nonce value, so that the calculation of the hash can meet the conditions.

If you are lucky, you may find Nonce in a few. If you have bad luck, you may have done it 2.147 billion times, and have found no Nonce, that is, the current block body cannot calculate the hash that satisfies the condition. At this point, the agreement allows the miners to change the block and start a new calculation. dynamic adjustment of difficulty coefficient

As mentioned in the previous section, mining is random and there is no guarantee that a block will be produced in exactly 10 minutes, sometimes even if it comes out in a minute, sometimes for hours. Overall, with the upgrading of hardware equipment, as well as the number of mining machines, the calculation speed must be faster and quicker.

In order to keep the output rate constant at 10 minutes, the dynamic regulation mechanism of difficulty coefficient is also designed in Ben Cong. He stipulates that the difficulty coefficient is adjusted once every two weeks (2016 blocks). If within the two weeks, the average generation speed of the block is 9 minutes, which means faster than the legal speed of 10%, so the next difficulty coefficient must be adjusted up 10%; If the average generation speed is 11 minutes, it means a 10% slower than the legal speed, so the next difficulty factor will be reduced by 10%.

The higher the difficulty coefficient (the lower the target), the more difficult the mining is. the bifurcation of the block chain

Even if the block chain is reliable, there is still one problem that is not resolved: If two people write data to the chunk chain at the same time, that is to say, two blocks are joined because they are all connected to the previous block and the bifurcation is formed. Which block should be adopted at this time?

The rule now is that the new node always takes the longest block chain. If the chunk chain has a fork, it will see which branch is behind the fork point, reaching 6 new blocks (called "Six Acknowledgements") first. Based on a 10-minute block, you can confirm it in one hour.

Because the speed of building blocks is determined by the computational power, the rule is that the branch with the most computational power is the authentic block chain. ix. Summary

Block chain as an unmanned distributed database, since 2009 has been running for 8 years, there is no big problem. This proves that it is feasible.

However, in order to ensure the reliability of the data, the block chain also has its own price. First, the efficiency, data writing block chain, to wait at least 10 minutes, all the nodes are synchronized data, it will take more time; second, energy consumption, the formation of blocks need miners to carry out countless meaningless calculations, which is very energy-intensive.

Therefore, the application of the block chain is actually very limited. There is no trust in all members of the administration to write the data not to be realistic when using the proceeds 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 (and possibly the only application scenario) of the block chain is the encrypted currency represented by the 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.