本文由币乎社区(http://bihu.com)内容支持计划奖励。 本节讲的就是大名鼎鼎的 Proof-of-Work 机制啦。这一节初步回答了两个常见的关于比特币(区块链)机制的疑问——常说的「挖矿」挖的是什么?计算机硬件不断发展,系统如何保证挖矿的难度随之提升,以至十分钟左右产生一个区块?
注:本节的第3段讲的「共识问题」在第11节会有详细阐述,故先跳过。
1 译文为了在点对点的基础上完成一个分布式时间戳服务器,我们需要使用一个类似于类似于Adam Back 提出的哈希现金(Hashcash)的工作量证明系统,而不是以前的新闻组及论坛的机制。「工作量证明」牵涉到对一个值的搜寻。这个值用SHA-256等算法进行hash操作后,得到的hash值从一定数量的0字节开始。搜寻这个值的平均工作量随着0字节的增加呈现指数级增长,但是校验这个值只需要执行一次hash操作。
为了时间戳服务器网络的运行,我们实施了这样一种工作量证明机制——找到一个被称为nounce的随机数,找到后在区块中增加它。nounce能让区块的hash值以一连串所需数量的0字节开头。CPU不断运算,直到完成了一个「工作量证明」。那时,这个区块将不能被修改,除非重新完成这个工作量。当之后的区块链接到这个区块后,改变这个区块要做的工作还包括重做(redo)这个区块之后的所有区块。
![]()
这个工作量系统也解决了集体决策谁代表大多数的问题。如果大多数是基于一个IP一个投票权的话,这个系统能被分配了大量的IP地址的人破坏。工作量证明本质上来说是「一个CPU一个投票权」。大多数的决策被最长的链条所代表,也代表了最大的「工作量证明」的结果的投入。如果绝大多数CPU算力被诚实的节点所控制,那么最诚实的链会增长得最快,同时超过所有和它竞争的链。为了修改过去的区块,一个攻击者将不得不把块和之后所有的块里所有的工作量重做,然后追上并超过诚实节点的工作。我们将在之后证明,随着随后的区块被添加,一个较慢的攻击者赶上的成功概率将呈指数化递减。
为了补偿硬件运算速度的增长,及节点参与网络的程度的起伏,工作量证明的难度取决于一个移动的均数——每小时产生的平均区块数量。如果产生得太快,那么难度就会增加。
2 概念解析本节核心的概念就是Nounce这个神奇的值。
[h1]2.1 Nounce的概念[/h1]区块中一个32位(4字节)的字段,有了这个字段,区块的hash值会包含一连串的0。这个字段和其它字段是独立的,也即不会影响到其它的字段。
[h1]2.2 Nounce代表了算力[/h1]找到这个值需要消耗CPU进行大量的随机碰撞,因此这个值可以用来衡量算力。
[h1]2.3 控制「找到Nounce」的难度[/h1]为了确保固定10分钟生成一个区块,找到Nounce的难度会随着「每小时产生的平均区块数量」而增加。控制难度的方式是——控制区块生成的hash值的开头的0的数量。
3 图解区块链[h1]3.1 挖矿是什么?[/h1]↓有一座矿山,矿山由许多32位2进制数构成……
![]()
↓矿工前去挖矿,如果挖到的数字不能让「区块生成的哈希值」达到要求,就摒弃。
![]()
↓挖啊挖啊挖啊挖,挖到一个符合要求的数!
![]()
↓这个数就是所谓的Nounce啦
![]()
[h1]3.2 区块头是如何产生的?[/h1]↓包括Nounce在内的6个字段混合起来,“hash”一下,就得到了这个区块的区块头。
![]()
4 总结现在,我们对本篇开头提出的问题就有了清楚的答案啦。
(1)常说的「挖矿」挖的是什么?
挖的是一个被称为Nounce的值,这个值可以让「区块生成的哈希」以若干个0开头。找到这个值需要消耗CPU进行大量的随机碰撞,因此这个值可以用来衡量算力。
(2)计算机硬件不断发展,系统如何保证固定十分钟产生一个区块?
为了确保固定10分钟生成一个区块,找到Nounce的难度会随着「每小时产生的平均区块数量」而增加。控制难度的方式是——控制区块生成的hash值的开头的0的数量。
5 原文To implement a distributed timestamp server on a peer-to-peer basis, we will need to use a proofof-work system similar to Adam Back’s Hashcash, rather than newspaper or Usenet posts. The proof-of-work involves scanning for a value that when hashed, such as with SHA-256, the hash begins with a number of zero bits. The average work required is exponential in the number of zero bits required and can be verified by executing a single hash.
For our timestamp network, we implement the proof-of-work by incrementing a nonce in the block until a value is found that gives the block’s hash the required zero bits. Once the CPU effort has been expended to make it satisfy the proof-of-work, the block cannot be changed without redoing the work. As later blocks are chained after it, the work to change the block would include redoing all the blocks after it.
![]()
The proof-of-work also solves the problem of determining representation in majority decision making. If the majority were based on one-IP-address-one-vote, it could be subverted by anyone able to allocate many IPs. Proof-of-work is essentially one-CPU-one-vote. The majority decision is represented by the longest chain, which has the greatest proof-of-work effort invested in it. If a majority of CPU power is controlled by honest nodes, the honest chain will grow the fastest and outpace any competing chains. To modify a past block, an attacker would have to redo the proof-of-work of the block and all blocks after it and then catch up with and surpass the work of the honest nodes. We will show later that the probability of a slower attacker catching up diminishes exponentially as subsequent blocks are added.
To compensate for increasing hardware speed and varying interest in running nodes over time, the proof-of-work difficulty is determined by a moving average targeting an average number of blocks per hour. If they’re generated too fast, the difficulty increases.
|
|