（戏剧音乐） (dramatic music)
指导老师：大家好，欢迎回来 Instructor: Hello and welcome back
区块链的课程。 to the course on Blockchain.
今天，我们有一个关于共识协议的教程。 And today, we've got a tutorial on Consensus Protocols.
好了，这是我们的地图。 All right, so here's our map.
我们刚刚完成了采矿， We've just ticked off mining,
最后，我们达成了共识协议， and finally, we are at consensus protocols,
我们传奇的最后一步。 the last step in our saga.
好吧，让我们看看我们在谈论什么。 All right, so let's have a look at what we're talking about.
我们将讨论，根据共识协议， We're going to discuss, in terms of consensus protocols,
首先，我们要看看挑战 we're going to look at, first, the challenges
他们要解决的问题。 that they're designed to solve.
因为我们已经很熟悉了 Because we've already gotten familiar
共识协议的概念 with the concept of consensus protocols
当我们谈到拜占庭容错时。 when we were talking about the Byzantine fault tolerance.
我们知道整个概念是什么， And we understand what the whole notion is,
这就是分布式网络在事情上达成一致的方式。 it's how the distributed network agrees on things.
在我们的例子中，它是如何同意的 And in our case, it's how does it agree
如何不断增长，如何不断增加积木， on how to keep growing, how to keep adding blocks,
在哪个方向，如何知道何时添加块， in which direction, how to know when to add a block,
以及在整个网络中添加哪个块。 and which block to add across the whole network.
所以挑战在于一个共识协议， And so the challenge is that a consensus protocol,
正如您在拜占庭容错示例中回忆的那样， as you recall in the Byzantine fault tolerance example,
将军们面临的挑战 the challenge for the generals
就是明白该听哪个命令， was to understand which command to listen to,
比如是进攻还是撤退。 like whether to attack or retreat.
所以他们必须想出一个共识协议 So they had to come up with a consensus protocol
来解决这个挑战。 to solve that challenge.
在我们的例子中，区块链的共识协议 In our case, the consensus protocol for a blockchain
必须解决两个主要挑战。 has to solve two main challenges.
第一是保护网络免受攻击者的攻击。 Number one is protect the network from attackers.
我们已经谈过了 And we already talked about the situation
攻击者试图攻击的地方 where a attacker tries to attack
在链条中间的某个地方。 somewhere in the middle of the chain.
所以如果攻击者试图改变这个块， So if an attacker tries to change this block,
那对他们来说几乎是不可能的 then that's gonna be almost impossible for them
因为他们必须改变所有的街区 because they would have to change all these blocks
然后每个节点上的所有这些块。 and then all these blocks on every single node.
所以这不是我们在这里讨论的问题。 So that's not the problem we're talking about here.
我们在这里讨论的问题 The problem we're talking about here
如果攻击者 is what happens if an attacker
试图在链条的末端放一块积木， tries to put a block at the end of the chain,
尝试添加恶意的新块。 tries to add a new block that is malicious.
第二个挑战 And the second challenge
共识协议应该在区块链中解决的问题 that a consensus protocol should solve in a blockchain
是竞争链的挑战。 is the challenge of competing chains.
所以在一个巨大的区块链中， So in a large blockchain,
因为它分布在世界各地， because it is distributed across the world,
节点之间可能会有延迟， there can be a lag between nodes,
尤其是相距较远的节点。 especially nodes that are far away from each other.
这不可能发生 And it couldn't so happen
两个相距很远的节点 that two nodes that are far away from each other
可以像那样同时成功开采一个区块。 could successfully mine a block at the same time, like that.
这并没有什么恶意， And there's nothing malicious about this,
没有袭击者。 there's no attackers.
只是因为有一次， It just so happens because once,
假设这个人开采了这个街区， let's say this one mines the block,
在这个消息传到这里之前， before the information about that gets to this one,
也许需要两秒钟才能到达这里。 maybe it takes like two seconds to get here.
但在第一秒， But during the first second,
此节点也已经挖掘了一个块。 this node already also mined a block.
从每个节点的角度来看， And from the perspective of each one of these nodes,
他们没有做错什么。 they have done nothing wrong.
他们完全同意采矿的工作方式。 They're in total agreement with how mining works.
但是，尽管如此，对于区块链来说，这是一个问题 But, nevertheless, for the blockchain, this is a problem
因为它需要就如何保持增长达成共识。 because it needs to be in consensus on how to keep growing.
它需要知道，我们是不是一直和橙色块一起成长 It needs to know, do we keep growing with the orange block
还是我们继续和紫色块一起成长？ or do we keep growing with the purple block?
因为我们会在后面讨论， Because as we'll discuss further down the course,
这些街区实际上可以 these blocks could actually have
里面有不同的交易。 different transactions inside them.
它不能同时接受两个块 It cannot just like accept both blocks
并在两者之间平分费用。 and split the fee between the two.
不，它必须从两个街区中选一个。 No, it has to pick one of the two blocks.
是的，这就是挑战。 And yeah, so that's the challenge.
网络如何达成共识 How does the network come to a consensus
如何保持成长？ on how to keep growing?
因为如果不能达成共识， Because if it doesn't come to a consensus,
比如如果共识协议没有到位， like if the consensus protocol is not in place,
你会得到这条链子， what'll happen is you'll have this chain,
你会有这条链子，它会有冲突， you'll have this chain, it'll be conflicting,
然后他们会分裂成两个， and then they'll split up into two,
然后，区块链会分裂成四个， and then later on, the blockchain will split up into four,
还有八个等等， and eight and so on,
最后，你就有了一大堆区块链 and then eventually, you just have a ton of blockchains
它们是完全分开的。 which are completely separate.
所以我们不想那样。我们想要一个完整的区块链。 So we don't want that. We want an integral blockchain.
这是第二个挑战。 So that's the second challenge.
就共识协议而言， And in terms of consensus protocols,
在这一点上需要注意的是 it's important to note at this point
它们是多种不同的类型 that they are multiple different types
共识协议。 of consensus protocols.
在本教程的最后，我将推荐一篇文章 And at the end of this tutorial, I'll recommend an article
这突出了其中的几个，这可能是有用的。 which highlights a couple of them, which can be useful.
但最著名的两个是工作证明， But the two most famous ones are Proof-of-Work, PoW,
或利害关系证明。 or Proof-of-Stake, PoS.
我们将要讨论的是 And the one we're going to be talking about
是工作证明，因为那是原件 is Proof-of-Work, because that's the original one
中本聪在他的论文中描述了。 described by Satoshi Nakamoto in his paper.
而且，这也是比特币目前使用的一种。 And also, it's the one that Bitcoin currently uses.
这也是以太坊仍在使用的， And it's also the one that Ethereum still uses,
尽管他们计划转向国家证明。 even though they're planning on moving to Proof-of-State.
这就是我们要讨论的问题。 So that's the one we're going to be talking about.
那么工作证明这个术语从何而来呢？ So where does this term Proof-of-Work come from?
好消息是通过谈论 Well, the great news is that by talking about
我们谈论的信息或事情 the information or the things that we talked about
在前面的教程中，比如挖掘，比如哈希， in the previous tutorials, such as mining, such as hashing,
P2P网络和不可变账本， and P2P networks and the immutable ledger,
我们已经打下了很好的基础 we've already laid a very good foundation
看到一切是如何结合在一起的 for seeing how everything comes together
来建立这种工作证明的共识。 to create this consensus of Proof-of-Work.
所以我们要从采矿开始。 And so we're gonna start with mining.
所以在这里你可以看到 So here you can see
在那里我们完成了整个采矿情况， where we finished off with the whole mining situation,
这里的主要收获 that the main takeaway from here
矿工们需要想出这个随机数， was that miners need to come up with this nonce,
有了这个金色的随机数，它将创建一个散列， with this golden nonce, which will create a hash,
在目标之下， which is under the target,
然后他们将被允许创建一个块。 and then they will be allowed to create a block.
这就是他们正在解决的密码挑战。 So that's the cryptographic challenge that they're solving.
他们必须经历很多很多的迭代 And they have to go through lots and lots of iterations
为了达到那个时刻， to get to that nonce,
几十万，几百万，几十亿次迭代 hundreds of thousands, millions, billions of iterations
他们需要通过直到他们找到那个nonce。 they need to go through until they find that nonce.
那这意味着什么呢？ And so what does that mean?
这意味着很多时间， That means a lot of time,
这意味着大量的硬件投资 that means a lot of hardware investments
比如他们的采矿设备或者采矿电脑， like into their mining rigs or mining computers,
还有大量的电力。 and also a lot of electricity.
他们不得不支付巨额电费。 They have to pay huge electricity bills.
有些矿工，你知道，像八个以上， Some miners, you know, like eight plus,
就像我听说的每天8000美元， like one I heard was like $8,000 per day,
只是每天用电来开采这些东西。 just per day just in electricity just to mine this stuff.
是的，所以我们可以得出结论， And yeah, so as we can conclude,
矿工方面的工作量很大。 that's a lot of work on the side of the miner.
最后， And this hash that they have, in the end,
密码学难题的解决 the solution to the cryptographic puzzle
是工作证明， is the proof of work,
是他们投入了这么多工作的证明 is the proof that they've put in all this work
为了解决密码学的挑战。 in order to solve the cryptographic challenge.
这就是这个术语的来源。这是第一位的。 So that's where the term comes from. That's number one.
第二，重要的是要看到 And number two is that it's important to see
他们必须投入大量的工作， that they do have to put in a lot of work,
因为这在下一张幻灯片中很重要。 because this will be important in the next slide.
现在，回到我们的分布式网络， So now, going back to our distributed network,
当矿工添加新块时会发生什么？ what happens when a miner adds a new block?
来了，新街区来了。 There it comes, there's the new block.
网络或区块链将奖励矿工 The network or the blockchain will reward the miner
开采那个区块。 for mining that block.
所以在比特币中，是12.5个比特币。 So in Bitcoin, it's 12 and a half Bitcoins.
在以太坊里，是一定量的以太 In Ethereum, it's a certain amount of Ether
添加那个块。 for adding that block.
此外，矿工将获得费用 And also, the miner will get the fees
与交易记录关联的 associated with the transactions
包含在该块中的。 that are included in that block.
所以有金钱上的激励， So there's monetary incentive,
矿工采矿有经济激励。 there's financial incentive for miners to mine.
此外，还有经济上的激励 And also, there's financial incentive
让矿工们公平竞争。 for miners to play fair.
为什么会这样？因为他们做了很多工作。 Why is that? Because they've put in a lot of work.
所以如果他们投入了大量的工作 So if they've put in a lot of work
然后他们添加一个被拒绝的块， and then they add a block that is rejected,
或者像他们加了一个块， or like they add a block that is,
你知道，他们解决了密码难题 you know, they solved the cryptographic puzzle
他们找到了正确的散列，然后添加了那个块 and they found the correct hash and they add that block
但其中包括恶意交易 but they've included malicious transactions
或者他们做了一些恶意的事情， or they've done something malicious,
他们就是拿不到报酬，也拿不到费用， they just won't get that reward and they won't get the fees,
因此，他们无法 and therefore, they're not gonna be able
来支付他们的电费。 to pay for their electricity bill.
所以基本上，他们会受到惩罚， So basically, they'll get penalized,
他们不想冒这个险，所以他们必须公平竞争。 and they don't want to risk that so they have to play fair.
网络怎么会知道 And how will the network know
如果他们添加了恶意块？ if they're adding a malicious block?
嗯，其实很简单。 Well, it's actually very simple.
每一个节点，在那个块被添加之前， Every single node, before that block gets added, like that,
在该块通过网络传播之前， before that block is propagated through the network,
每个节点都将进行一系列检查。 every single node will conduct a series of checks.
而且这一系列的检查非常严格。 And this series of checks is very rigorous.
所以这只是部分支票的截图。 So this is just a screenshot of some of the checks.
这个清单还在继续。 The list just keeps going.
我想它有几页长。 I think it's a couple of pages long.
他们检查了Merkle的杂烩， And they check the Merkle hash,
他们检查前一个街区 they check that the previous block
匹配主块中的前一个哈希字段， matches the previous hash field in the main block,
他们检查时间戳， they check the timestamps,
他们检查交易清单不得为空。 they check transaction list must not be empty.
他们检查了很多很多东西。 They check lots and lots of things.
正如你在这里看到的，它非常非常详细。 So as you can see here, it's very, very detailed.
如果支票没有通过， And if a check doesn't go through,
然后他们拒绝阻止。 then they reject the block.
所以基本上，在一天结束的时候， And so basically, at the end of the day,
网络不允许，区块链不允许 the network will not allow, the blockchain will not allow
要添加到链中的恶意块。 malicious blocks to be added to the chain.
这就是为什么有一个财政激励 And that's why there's a financial incentive
按规则比赛。 to play according to the rules.
这就是我们解决第一个挑战的方法。 So that's how we solve challenge number one.
这就是共识协议的一部分。 So that's that part of the consensus protocol, that block.
所以一旦有障碍来找你，你需要做所有的检查， So once a block comes to you, you need to do all the checks,
然后你只能接受或者拒绝。 and then you can only accept or reject it.
所以如果网络开始拒绝屏蔽， And so if the network starts rejecting the block,
就像如果所有这些节点都开始拒绝块， like if all these nodes start rejecting the block,
基本上，区块链会拒绝它 then that's just basically, the blockchain will reject it
这个块将是无效的， and that block will be invalid,
那个矿工将受到处罚。 that miner will be penalized.
这是第一部分。 So that's the first part.
现在，我们来谈谈第二部分。 Now, let's talk about the second part.
这个会更有趣。 This one's gonna be more interesting.
哦，在我们继续之前，有一件事我差点忘了。 Oh, before we move on, one thing I almost forgot.
我在想我有什么想说的。 I was thinking like there's something I wanted to say.
这个，密码谜题：难解，易验证。 This, cryptographic puzzles: hard to solve, easy to verify.
所以重要的是要明白 So it's important to understand that there's a difference
在挖掘和验证之间。 between mining and then verifying things.
因为在挖掘中，你要做很多迭代。 Because in mining, you have to do a lot of iterations.
你必须解决这个密码难题。 You have to solve that cryptographic puzzle.
你必须通过 You have to go through
所有这些随机数的不同变化， all these different variations of the nonce,
检查哈希，你知道，就像， and check the hash, and you know, like just,
这是关于暴力强迫你通过nonce的方式 it's about brute forcing your way through the nonce
得到正确的散列。 to get the right hash.
而通过验证， Whereas with verifying,
你只需要检查它是否匹配。 you just need to check that it matches up.
所以让我们回到这里一会儿。 So let's just go back here for a second.
所以在这里你会看到，为了得到这个随机数， So here you'll see that to get to this nonce,
你只需要通过 you just have to go through
所有这些迭代，迭代，迭代， all of these iterations, iterations, iterations,
数十亿次迭代。 billions of iterations.
但是去检查，去验证，是极其琐碎的。 But to check, to verify, it's extremely trivial.
你只要把这些信息， You just take all this information,
你把它放到哈希算法中，你得到了哈希， you put it into the hashing algorithm and you get the hash,
你只要检查它是否确实在目标之下。 and you just check that indeed it's under the target.
就是这样。 That's it.
或者它确实是矿工所说的那样。 Or it's indeed it is what the miner is saying it is.
所以这是一种悖论或讽刺，我会说， So that's the kind of paradox or irony, I would say,
在密码谜题中， in the cryptographic puzzles,
你花了这么多心思来寻找这个随机数， that you're putting in all this work to find this nonce,
然后有人来验证， and then for somebody to verify,
他们根本不需要做任何工作。 they don't have to do any of that work at all.
他们就像拿着这个，然后，砰， They just like take this and then, bam,
他们把它放到哈希算法中 they put it into the hash algorithm
然后一秒钟后，他们得到了杂碎， and then a second later, they got the hash,
你找了几分钟，很长一段时间， which you were looking for minutes, for a long time,
你知道，数十亿次迭代， you know, for billions of iterations,
他们只需要一次迭代来验证它。 and they just take one iteration to verify it.
所以所有这些验证， And so all these verifications,
尽管他们的名单很长， even though there's a huge list of them,
最多需要几秒钟 it takes a couple seconds maximum
为了对每个节点进行这些验证， to conduct these verifications for each node,
所以对他们来说这不是一个计算量很大的练习。 so it's not a computationally heavy exercise for them.
他们不花很多电或类似的东西。 They don't spend a lot of electricity or anything like that.
这只是他们做的一个常规过程。 It's just a routine process that they do.
好吧。所以我们很清楚这一点。 Okay. So we're clear with that.
我说的是预防攻击。 I was talking about prevention of attacks.