You are welcome back to the course on block chain.
And today we've got a tutorial on consensus protocols.
All right.
So here's our map we just ticked of 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 casettes protocols that we're going to look at first the challenges
that they're designed.
So because we've already gotten familiar with the concept of consensus Pericles when we're talking about
the Byzantine fold tolerance and we understand what's what the whole notion is is how the distributed
network agrees on things and in our case it's how does it agree on how to keep growing.
How do you keep adding blocks in which direction.
How do how to know when to add a block and we walked across the whole network.
And so the challenge is that a consensus protocol is as you recall in the bicentennial 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 concessions Perkel to solve that challenge.
In our case the consensus protocol for a blockade 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 attacker and if an attacker tries to like change this block then that's going to be almost impossible
for them because they would have to change all these blogs and then all these blogs on all every single
load.
So that's not the problem we're talking about here.
The problem we talk about here is what happens if an attacker tries to put a block at the end of the
chain tries to add a new blog that is malicious.
And the second challenge that it says is Pericles should solve in a block chain is the challenge of
competing chains.
So in a large block chain because it is distributed across the world.
They can be a lag between nodes especially those that are far away from each other and it couldn't so
happen that two nodes that are far away from each other could find two could successfully mine a block
at the same time like that.
And there's nothing malicious about this there's no attackers.
It's just just so happens because once let's say this one minds the block before the information about
that gets to this one maybe takes like two seconds to get here.
But during the first second this this node already also might a block and from the perspective of each
one of these nodes they have done nothing wrong.
They are in total agreement with how mining works.
But nevertheless for the block chain this is a problem because it needs to be in consensus on how to
keep growing it needs to know how do we keep growing with the orange block or where do we keep growing
if the purple block because as we will discuss further down the course these blocks could actually have
different transaction transactions and Siteman cannot just like access both blogs and split the fee
between the two.
No it has to pick one of the two blocks.
And 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 change you'll have this change will be conflicting and then they'll split up into two and
then later on the block will split up into four and eight and so on and then eventually you just have
a ton of options which are completely separate.
So we don't want that we want an integral block.
So that's the second chance.
And in terms of consensus protocols it's important 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 O.W. or pro-state us.
And what we're going to be talking about is proof of work because that's the one that's the original
one describe the situation Okamoto in his paper and also it's the one that Bitcoin currently uses and
is also the one that if 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 hash hashing and P2P networks and the mutilator we've already
laid a very good foundation for seeing how everything comes together to create this consensus of proof
work.
And so we're going to start with mining.
So here you can see that's where we finished off with the whole mining situation.
That's the main takeaway from here is that miners need to come up with this non-SS with his gold and
loans 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 non-SS.
Hundreds of thousands millions billions of iterations.
They need to go through until they find that not.
And so what does that mean.
That means a lot of time.
That means a lot of hardware investments like into their into their mining rigs or mining computers
and also a lot of electricity they have to pay huge electricity bills.
Some whiners you know like 8 8 2 plus like one I one I heard was.
Eight thousand dollars per day per day just in electricity just my stuff.
And yeah so as we can conclude that's a lot of work on the side of the mine.
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 we can see it's important to see that we do have to put in
a lot of work because this will still be important in the next slide.
So now going back to our distributor network what happens when a miner adds and your book comes there's
that you block the network or the lauching will reward the miner for mining that blog.
So in Bitcoin it's 12 in a heartbeat Quine's.
In theory it's a certain amount of ether for adding a block.
And also the miner will get the fees associated with the transactions that are included in that block.
So there is monetary incentive this financial incentive for miners to mine.
And also there is financial incentive for miners to play fair.
Why is that's because they've put in a lot of work.
So if they 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 found that cryptograms sold in photographic puzzle and they found the correct
hash and they add that block but they've included malicious transactions or they've done.
You know I've done something.
Miss polishers they just won't get that reward and they won't get the fees and therefore they're you
know they're not going to 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 note before that block gets added like that before that
block is propagated to 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 is a couple of
pages long.
And they they check they check the miracle hash they check the proof that the previous block matches
the previous the previous hash field in the mail log.
They check the time stamps they check transaction.
This must not be empty.
They check lots and lots of things so as you can see here it's very very detailed.
And if some of the 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 box will not allow malicious blocks
to be added to the chain.
And that's why there is 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 Perkel that blog.
So once once you see it once the blow comes to you you need to check do all the checks and then you
can only accept or reject it.
And so if the network starts rejecting the block like you've all these notes start rejecting the blog
then that's just the watching will reject it and that minor that block will be valid.
That minor will be penalized.
So that's for the first part.
Now let's talk about the second part of this one.
This one's going to be more interesting.
Oh before we move on.
One thing I always thought about why is thinking like this 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 this cryptographic puzzle you have to go through all these different variations of
the lawns and check the hash and you know like just it's about brute forcing your way through the knots
to get the right hash.
Whereas we've solved it we're checking to verifying.
You just need to check that it matches up so is to go back to you for a second.
So here you'll see that you get to this and also you just have to go through all of these you know iterations
iterations iteration billions of durations but to check to verify is it's extremely trivial you just
take all this out 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 is indeed it.
It is what the miner is saying it is.
So that's the that's the kind of like paradox or irony and say in the cryptographic puzzle that you're
putting in all this work to find this this long.
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've got the hash which
you were looking for.
You know minutes for a long time you know for billions of iterations and they just take one iteration
to verify.
And so all these verifications even though there is a huge list of them it takes a couple of seconds
maximum to conduct these verifications for each node.
So that's it's not a computation only heavy exercise for them they don't spend a lot of pictures or
anything like that in just a routine process that they do.
OK.
So we're clear of that.
That was us talking about prevention of attacks.