Blockchain （blockchain） It's a big hit right now , The news media reported a lot , Claim that it will create the future .
But , There are few easy to understand introductory articles . What is blockchain , What's special , There is little explanation .
below , I'll try , Write a best understanding of the blockchain tutorial . After all, it's not very difficult , The core concept is very simple , You can make it clear in a few words . I hope to finish reading this article , You can not only understand blockchain , And understand what mining is 、 Why is mining more and more difficult .
It should be noted that , I'm not an expert on this . Although I have been concerned about , But take a closer look at the blockchain , Or from the beginning of this year . Mistakes and inaccuracies in the text , Welcome to correct .
One 、 The essence of blockchain
What is blockchain ？ In a word , It's a special kind of distributed database .
First , The main function of blockchain is to store information . Any information that needs to be saved , Can be written to the blockchain , You can also read from it , So it's a database .
secondly , Anyone can set up a server , Join the blockchain network , Become a node . In the world of blockchain , There is no central node , Every node is equal , They hold the entire database . You can go to any node , write in / Reading data , Because all nodes will eventually synchronize , Ensure that the blockchain is consistent .
Two 、 The biggest characteristics of blockchain
Distributed database is not a new invention , There have been such products in the market . however , Blockchain has a revolutionary feature .
Blockchain has no administrator , It's completely centerless . Other databases have Administrators , But blockchain doesn't have . If someone wants to add audit to blockchain , It doesn't work , Because it's designed to prevent the emergence of central management .
It's because of the inability to manage , Blockchain can not be controlled . Otherwise, once large companies and groups control the management power , They will control the whole platform , Other users have to obey them .
however , Without the Administrator , Everyone can write data into it , How can we ensure that the data is credible ？ What to do if you are changed by a bad person ？ Please read on , That's the magic of blockchain .
3、 ... and 、 block
A blockchain consists of blocks （block） form . Blocks are like records in a database , Every time you write data , It's creating a block .
Each block contains two parts .
- Block head （Head）： Record the eigenvalues of the current block
- Block body （Body）： Actual data
The block head contains multiple eigenvalues of the current block .
- Generation time
- Actual data （ That is block body ） Hash
- Hash of the previous block
here , You need to understand what Hash （hash）, This is necessary to understand the blockchain .
So-called " Hash " That is, the computer can handle any content , Calculate an eigenvalue of the same length . Block chaining The hash length is 256 position , That is to say , No matter what the original content is , At the end of the day, we'll figure out a 256 Binary number of bits . And it can guarantee , As long as the original content is different , The corresponding hashes must be different .
for instance , character string
123 The hash of is
a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0（ Hexadecimal ）, To binary is to 256 position , And only
123 You can get this hash .（ Theoretically , Other strings can also get this hash , But the probability is very low , It can be approximated that it can't happen .）
therefore , There are two important corollaries .
- inference 1： The hash of each block is different , Blocks can be identified by hashing .
- inference 2： If the content of the block changes , Its hash is bound to change .
Four 、 Hash The immutability of
Blocks and hashes are one-to-one correspondence , The hash of each block is for " Block head "（Head） Calculated . in other words , Put the eigenvalues of the block into the matrix , Connect in order , Make up a very long string , Then hash the string .
Hash = SHA256( Block head )
The above is the calculation formula of block hash ,
SHA256 It's the hash algorithm of blockchain . Be careful , This formula only contains block heads , Does not contain block bodies , in other words , The hash is uniquely determined by the block header ,
As I said before , The block header contains a lot of content , There is the hash of the current block body , And the hash of the last block . It means , If the content of the current block body changes , Or the hash of the previous block has changed , It will definitely cause the hash of the current block to change .
This is of great significance to the blockchain . If someone modifies a block , The hash of the block changes . So that the blocks behind can still connect to it （ Because the next block contains the hash of the previous block ）, The person has to modify all the following blocks in turn , Otherwise, the changed block will be separated from the blockchain . For the reasons to be mentioned later , Hashing is time consuming , It is almost impossible to modify multiple blocks in a short time , Unless someone's got the whole network 51% The above computing power .
It is through this linkage mechanism that , Blockchain ensures its own reliability , Once the data is written , It can't be tampered with . It's like history , What happened is what happened , From then on, there is no way to change .
Every block is connected , This is also " Blockchain " The origin of the name .
5、 ... and 、 mining
Because the synchronization between nodes must be guaranteed , So the speed of adding new blocks should not be too fast . Just imagine , You just synced a block , Ready to generate the next block based on it , But at this time, other nodes have new block generation , You have to give up half the calculation , To synchronize again . Because behind each block , You can only follow one block , You can always be behind the latest block , Generate the next block . therefore , You have no choice , On hearing the signal , You have to synchronize immediately .
therefore , Nakamoto, the inventor of blockchain （ It's a pseudonym , The real identity is still unknown ） Intentionally let new blocks be added , It becomes very difficult . His design is , The average 10 minute , Only the whole network can generate a new block , Six in an hour .
This output speed is not achieved by command , It's deliberately setting up massive computing . in other words , Only through an extremely large amount of calculation , To get a valid hash of the current block , So as to add new blocks to the blockchain . Because of the amount of calculation , So I can't get up soon .
This process is called mining （mining）, Because of the difficulty of calculating a valid hash , It's like in the sand all over the world , Find a grain of sand that meets the criteria . The machine that calculates the hash is called a miner , The man who operates the mining machine is called a miner .
6、 ... and 、 The difficulty coefficient
Read here , You may have a question , People say it's hard to mine , But mining is to calculate a hash by computer , That's what computers are good at , How can it be difficult , It's too late to come out ？
It turns out that not any hash can , Only those hashes that meet the criteria will be blocked . This condition is particularly harsh , Most hashes do not meet the requirements , Must recalculate .
original , The block head contains a The difficulty coefficient （difficulty）, This value determines the difficulty of hash calculation . for instance , The first 100000 Block The coefficient of difficulty is 14484.16236122.
Block chain protocol , Use a constant divided by the difficulty coefficient , You can get the target value （target）. obviously , The greater the difficulty coefficient , The smaller the target value .
The validity of hash is closely related to the target value , Only hashes smaller than the target value are valid , Otherwise the hash is invalid , Must recalculate . Because the target value is very small , The chance of a hash less than that value is extremely slim , It's possible to calculate 10 100 million times , Only once . That's why mining is so slow .
As I said before , The hash of the current block is uniquely determined by the block header . If you want to hash the same block over and over again , Means , The area has to change all the time , Otherwise, it's impossible to work out a different hash . All the eigenvalues in the block are fixed , To make the block head change , Nakamoto deliberately added a random term , be called Nonce.
Nonce It's a random value , The role of miners is to guess Nonce Value , So that the hash of the block header can be smaller than the target value , So that you can write to the blockchain .Nonce It's very hard to guess , At present, we can only try and make mistakes one by one through the exhaustive method . According to the agreement ,Nonce It's a 32 Binary value of bit , That is, it can reach the maximum 21.47 Billion . The first 100000 Block Nonce The value is
274148111, Can be interpreted as , Miners from 0 Start , I've been calculating 2.74 100 million times , To get an effective Nonce value , So that the hash can satisfy the condition .
With luck , Maybe we'll find it soon Nonce. Bad luck , Maybe it's over 21.47 100 million times , Nothing Nonce, That is, it is impossible for the current block body to calculate the hash that meets the conditions . At this time , The agreement allows miners to change the block body , Start a new calculation .
7、 ... and 、 Dynamic adjustment of difficulty coefficient
As we said in the previous section , Mining is random , There is no guarantee that a block will be produced in exactly ten minutes , Sometimes it comes out in a minute , Sometimes it doesn't work out for hours . overall , With the improvement of hardware equipment , And the increase in the number of mining machines , It's going to be faster and faster .
To keep the output rate constant for ten minutes , Nakamoto also designed the dynamic adjustment mechanism of difficulty coefficient . He stipulated that , The difficulty factor is every two weeks （2016 Block ） Adjust once . If in two weeks , The average block generation speed is 9 minute , It means faster than the legal speed 10%, So the next difficulty factor is going to be raised 10%; If the average generation rate is 11 minute , It means it's slower than the legal speed 10%, So the next difficulty factor will be lowered 10%.
The higher the difficulty coefficient is （ The target value is getting smaller and smaller ）, It makes mining more and more difficult .
8、 ... and 、 The fork of the blockchain
Even if the blockchain is reliable , Now there's another problem that hasn't been solved ： If two people write data to the blockchain at the same time , in other words , At the same time, two blocks are added , Because they're all connected to the previous block , It's a bifurcation . Which block should be adopted at this time ？
Now the rule is , New nodes always use the longest blockchain . If the blockchain has forks , We'll see which branch is behind the bifurcation point , Arrive first 6 A new block （ be called " Six confirmations "）. according to 10 One block per minute , It'll be confirmed in an hour .
Because the generation speed of new blocks is determined by the computing power , So the rule is that , The branch that has most of the computing power , It's an authentic blockchain .
Nine 、 summary
Blockchain as an unmanaged distributed database , from 2009 It's been running for years 8 year , No big problems . This proves that it works .
however , In order to ensure the reliability of data , Blockchains also have their own costs . One is efficiency , Data is written to the blockchain , Wait at least ten minutes , All nodes synchronize data , It takes more time ; The second is energy consumption , The formation of blocks requires countless meaningless calculations by miners , It's very energy intensive .
therefore , Applicable scenarios of blockchain , In fact, it's very limited .
- There is no authority that all members trust
- The data written is not required to be used in real time
- The benefits of mining can make up for its own costs
If the above conditions cannot be met , So the traditional database is a better solution .
at present , The biggest application scenario of blockchain （ Maybe the only application scenario ）, It is the cryptocurrency represented by bitcoin . Next article , I will introduce Introduction to bitcoin .
Ten 、 Reference link
- How does blockchain really work?, by Sean Han
- Bitcoin mining the hard way: the algorithms, protocols, and bytes, by Ken Shirriff
（ End ）