Source: Ruan Yi Feng
Www.ruanyifeng.com/blog/2017/12/blockchain-tutorial.html
This article has been entered in the July online AI question Bank, as the first AI bank, July online bank contains the vast majority of machine learning and in-depth study of the written interview questions, knowledge points, can be used as machine learning test questions, can also be as a check leakage fill the database. July Online website and July online app portal See "read the original."
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): Log the meta information for the current block
Block (body): Actual data
The block header contains multiple meta information for the current block.
Build time
Hash of actual data (i.e. block body)
Hash of a previous block
...
Here, you need to understand what is called Hash, which is necessary to understand the block chain.
So-called Hash is that the computer can compute a characteristic value of the same length for any content. The Hash length of the block chain is 256 bits, which means that no matter what the original content is, a 256-bit binary number is computed at the end. And it can be 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.
Therefore, there are two important inferences.
Inference 1: The hash of each block is different and can be identified by the hash block.
Inference 2: If the contents of the chunk change, its Hash will change. the non-modification of Hash
The chunk is one by one corresponding to the hash, and the hash of each block is computed for the "Block Header" (head).
Hash = SHA256 (area header)
The above is the calculation formula of the hash of the block, the hash is only decided by the block size, SHA256 is the hash algorithm of the chunk chain.
As I said before, the area header contains a lot of content, including the hash of the current chunk (note "Chunk" hash, not the whole block), and the hash on the previous block. This means that if the contents of the current block change, or 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 block chain. If someone modifies a block, the Hash of the chunk is changed. In order for the back block to be connected to it, the person must modify all the blocks at the same time, or the broken blocks will be left out of the block chain. Because of the reasons to be mentioned later, Hash calculation is time-consuming, while it is almost impossible to modify multiple blocks, unless someone has mastered the network more than 51% of computing power.
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 a very large number of calculations to get the effective Hash of the current block, so that the new area block to add 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 a valid Hash is like finding a grain of sand in the world of sand. The machine that calculates the Hash is called the mine machine, the person who operates the mine machine is called the miner.
vi.. Difficulty coefficient
Read here, you may have a doubt, people say that mining is difficult, but mining is not using computers 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 to meet the conditions of the hash will be the block chain to accept. This condition is particularly harsh, so that most of the Hash does not meet the requirements, must be counted.
Originally, the block size 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 the hash is closely related to the target value, only the hash that is less than the target value is valid, otherwise the hash is invalid and must be counted. Because the target value is very small, the chance for the hash to be less than this 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.
There is also a Nonce value in the block header, which records the number of Hash counts. The Nonce value of the 100,000th block is 274148111, which is calculated 274 million times before a valid Hash can be added to the block chain. dynamic adjustment of difficulty coefficient
Even if mining is difficult, 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 these two weeks, the average generation speed of the block is 9 minutes, which means faster than the legal speed of 10%, so the 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 difficulty coefficient should be lowered 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 bit 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 authority that all members trust.
Write data not to be realistic when used
The proceeds of mining can make up for its 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 scenario) of the block chain is the encrypted currency represented by the bit currency. In the next article, I'll introduce you to the basics of Bitcoin.