One of three minutes clarifying the block chain [/] Bitcoin: Getting Started tutorial block chain

Block chain (blockchain) is the favorite right now, a lot of news media reports, claiming it will create the future. However, straightforward how-to articles are few. What block chain in the end is what is special, there is little explanation.
Here Insert Picture Description
Now, I will try to write a tutorial block chain best understand. After all, it is not hard stuff, the core concept is very simple, a few words can say. I hope reading this article, you can not only understand the block chain, but also understand what mining is why more and more difficult issues such as mining.

It should be noted that I am not an expert in this area. Although very early attention, but a closer understanding of the block chain, or start from the beginning of this year. The text of the errors and inaccuracies, welcome to correct me.

First, the nature of the block chain

What block chain? In short, it is a special kind of distributed database.

First of all, the main role is to store information block chain. Any information needs to be saved, you can write block chain, it can be read from the inside, so it's database.

Secondly, anyone can set up the server, block chain to join the network as a node. World block chain, there's no central node, each node is equal, we have preserved the entire database. You can ask any one of the nodes, the write / read data, because all nodes will be the last synchronization, ensure consistent block chain.

Second, the most important feature of the block chain

Distributed database is not a new invention, has long been on the market for such products. However, the block chain has a revolutionary features.

Block chain is not an administrator, it is completely free center. Other databases are an administrator, but not block chain. If someone wants to add a review of the block chain, can not achieve, because of its design goals is to prevent central to the authorities appeared.

It is because they can not manage, do not block the chain can be controlled. Otherwise, if large companies and control over the management of the group, they will control the entire platform, other users will have to take orders from them.

However, there is no administrator, everyone can write data entered, how to ensure that data can be trusted it? The bad guys changed how do? Please read on, this is a wonderful place to block chain.

Third, the block

A block chain of blocks (block) composition. Record blocks like database each time data is written, is to create a block.

Each block comprises two portions.

  • Header regions (Head): Record feature value of a current block;
  • Block body (Body): actual data;

Area header contains the current number of eigenvalues ​​block.

  • Generation time
  • 实际数据(即区块体)的哈希
  • 上一个区块的哈希

这里,你需要理解什么叫哈希(hash),这是理解区块链必需的。

所谓"哈希"就是计算机可以对任意内容,计算出一个长度相同的特征值。区块链的 哈希长度是256位,这就是说,不管原始内容是什么,最后都会计算出一个256位的二进制数字。而且可以保证,只要原始内容不同,对应的哈希一定是不同的。

举例来说,字符串123的哈希是a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0(十六进制),转成二进制就是256位,而且只有123能得到这个哈希。(理论上,其他字符串也有可能得到这个哈希,但是概率极低,可以近似认为不可能发生。)

因此,就有两个重要的推论。

  • 推论1:每个区块的哈希都是不一样的,可以通过哈希标识区块。
  • 推论2:如果区块的内容变了,它的哈希一定会改变。

四、 Hash 的不可修改性

区块与哈希是一一对应的,每个区块的哈希都是针对"区块头"(Head)计算的。也就是说,把区块头的各项特征值,按照顺序连接在一起,组成一个很长的字符串,再对这个字符串计算哈希。

Hash = SHA256(区块头)

上面就是区块哈希的计算公式,SHA256是区块链的哈希算法。注意,这个公式里面只包含区块头,不包含区块体,也就是说,哈希由区块头唯一决定,

前面说过,区块头包含很多内容,其中有当前区块体的哈希,还有上一个区块的哈希。这意味着,如果当前区块体的内容变了,或者上一个区块的哈希变了,一定会引起当前区块的哈希改变。
Here Insert Picture Description
这一点对区块链有重大意义。如果有人修改了一个区块,该区块的哈希就变了。为了让后面的区块还能连到它(因为下一个区块包含上一个区块的哈希),该人必须依次修改后面所有的区块,否则被改掉的区块就脱离区块链了。由于后面要提到的原因,哈希的计算很耗时,短时间内修改多个区块几乎不可能发生,除非有人掌握了全网51%以上的计算能力。

正是通过这种联动机制,区块链保证了自身的可靠性,数据一旦写入,就无法被篡改。这就像历史一样,发生了就是发生了,从此再无法改变。
Here Insert Picture Description
每个区块都连着上一个区块,这也是"区块链"这个名字的由来。

五、采矿

由于必须保证节点之间的同步,所以新区块的添加速度不能太快。试想一下,你刚刚同步了一个区块,准备基于它生成下一个区块,但这时别的节点又有新区块生成,你不得不放弃做了一半的计算,再次去同步。因为每个区块的后面,只能跟着一个区块,你永远只能在最新区块的后面,生成下一个区块。所以,你别无选择,一听到信号,就必须立刻同步。

所以,区块链的发明者中本聪(这是假名,真实身份至今未知)故意让添加新区块,变得很困难。他的设计是,平均每10分钟,全网才能生成一个新区块,一小时也就六个。

这种产出速度不是通过命令达成的,而是故意设置了海量的计算。也就是说,只有通过极其大量的计算,才能得到当前区块的有效哈希,从而把新区块添加到区块链。由于计算量太大,所以快不起来。

这个过程就叫做采矿(mining),因为计算有效哈希的难度,好比在全世界的沙子里面,找到一粒符合条件的沙子。计算哈希的机器就叫做矿机,操作矿机的人就叫做矿工。

六、难度系数

读到这里,你可能会有一个疑问,人们都说采矿很难,可是采矿不就是用计算机算出一个哈希吗,这正是计算机的强项啊,怎么会变得很难,迟迟算不出来呢?

原来不是任意一个哈希都可以,只有满足条件的哈希才会被区块链接受。这个条件特别苛刻,使得绝大部分哈希都不满足要求,必须重算。

原来,区块头包含一个难度系数(difficulty),这个值决定了计算哈希的难度。举例来说,第100000个区块的难度系数是 14484.16236122。
Here Insert Picture Description
区块链协议规定,使用一个常量除以难度系数,可以得到目标值(target)。显然,难度系数越大,目标值就越小。
Here Insert Picture Description
哈希的有效性跟目标值密切相关,只有小于目标值的哈希才是有效的,否则哈希无效,必须重算。由于目标值非常小,哈希小于该值的机会极其渺茫,可能计算10亿次,才算中一次。这就是采矿如此之慢的根本原因。

前面说过,当前区块的哈希由区块头唯一决定。如果要对同一个区块反复计算哈希,就意味着,区块头必须不停地变化,否则不可能算出不一样的哈希。区块头里面所有的特征值都是固定的,为了让区块头产生变化,中本聪故意增加了一个随机项,叫做 Nonce。

Nonce 是一个随机值,矿工的作用其实就是猜出 Nonce 的值,使得区块头的哈希可以小于目标值,从而能够写入区块链。Nonce 是非常难猜的,目前只能通过穷举法一个个试错。根据协议,Nonce 是一个32位的二进制值,即最大可以到21.47亿。第 100000 个区块的 Nonce 值是274148111,可以理解成,矿工从0开始,一直计算了 2.74 亿次,才得到了一个有效的 Nonce 值,使得算出的哈希能够满足条件。

运气好的话,也许一会就找到了 Nonce。运气不好的话,可能算完了21.47亿次,都没有发现 Nonce,即当前区块体不可能算出满足条件的哈希。这时,协议允许矿工改变区块体,开始新的计算。

七、难度系数的动态调节

正如上一节所说,采矿具有随机性,没法保证正好十分钟产出一个区块,有时一分钟就算出来了,有时几个小时可能也没结果。总体来看,随着硬件设备的提升,以及矿机的数量增长,计算速度一定会越来越快。

For the production rate constant at ten minutes, Nakamoto dynamic adjustment mechanism designed to degree of difficulty. He prescribed degree of difficulty every two weeks (2016 block) adjusted once. If the average generation rate of two weeks inside, the block is nine minutes, it means faster than the statutory rate of 10%, so the next degree of difficulty will increase by 10%; if the average production rate is 11 minutes, it means slower than the 10% statutory rate, so the next degree of difficulty going down 10%.

The more stressed the higher difficulty factor (smaller target), led mining more difficult.

Eight, block chain bifurcation

Even block chain is reliable, and now there is a problem is not resolved: if two people simultaneously writing data to the block chain, that is to say, while adding two blocks, one block before because they are attached, on the formation of split ends. Then a block which should adopt it?
Here Insert Picture Description
Now the rule is that the new node always uses the longest piece of block chain. If the block chain bifurcation, which branch will look after the bifurcation point, reached first six new blocks (called "six times to confirm"). A calculation block according to 10 minutes, it was confirmed that an hour.

Since the new block production rate is determined by computing power, so this rule is to say, with a piece of branch most computing power, is authentic block chain.

Nine, summary

Block chain as a distributed database unattended, starting in 2009, has been running for eight years, no major problems arise. 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 is written to block chain, at least to wait ten minutes, all nodes synchronize data, you need more time; the second is to generate energy, the block needs to be meaningless to calculate countless miners, which is consuming energy.

Therefore, application scenarios block chain, in fact, very limited.

  • There is no trust all members of regulatory authorities;
  • It does not require real-time data is written to use;
  • Mining revenue to make up the cost itself;

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


In addition, the block chain as the most typical applications, Bitcoin is what stuff, you know? Are interested can access the second in this series: [three minutes clarifying the block chain / Bitcoin] II: Bitcoin introductory tutorial !

Guess you like

Origin blog.csdn.net/LEON1741/article/details/96888960