Blockchain: Building The Trust — Part2

Kartik
Coinmonks
8 min readJan 30, 2019

--

This is the second part of the “Blockchain: Building The Trust”. I recommend you read the 1st part about how Blockchain evolved and built to gain trust.

In this post, we will discuss more on Mining and Consensus protocol.

Let's get started with Mining.

4. Mining

Here we will discuss what mining is. What is the competition that everybody is participating in mining? why there are so many resources allocated to mining.

Let’s consider this block.

This block has block number, some data then we got previous hash and hash of the current block. And how do we get the hash of the current block? Well, we just take block number, data, and previous hash and we run it across the hashing algorithm, sounds simple, right? If it’s that simple what’s the fuss about mining? How come there are thousands of mining rigs running, energy and so much computing power is dedicated to them?

Well, actually there is another field in the block called “Nonce” and it stands for Number used Only once. Miners try to change this nonce field which actually which controls the hash. This nonce gives extra control and extra flexibility over the hash. For every change in the nonce, the hash drastically changes. That’s where we can observe the avalanche effect in action. A nonce is a number which can go like till a billionth number.

So how does this tie in with money? So here is an example of a hash pool.

The above is the proper SHA256 hashes of 64 digits which is a hexadecimal number and below them are their respective decimal representation. Let’s draw a hypothetical pool of hashes. We consider all those smallest hashes at the bottom and the largest at the top. Let us map these three hashes into the pool. Say smaller at the bottom and larger at the top. So, what’s the fuss here? We will come to that soon.

So how mining works in the blockchain system are, the blockchain system sets a target for the miners to accomplish a certain target. Let’s consider the target position as shown above.

The way it works is, any hash above the target does not count. The important thing is that the target is completely arbitrary. There is no logical or mathematical reason for it. It’s just to put hurdles in the minor’s way and create a challenge for them. A good way of thinking about the target is actually in terms of leading zeroes. So, in the block, a miner cannot change anything in the block which makes the blockchain an immutable ledger but there is one field ‘nonce’ which can be varied to get the hash which is below the target. Miners just sit there and they change the nonce field in order to try to guess a value called a “Golden Nonce”. The important thing here is that the Avalanche effect. By changing the nonce a tiny bit, the hash changes drastically. By looking at the picture above one can think that you just need to lower the nonce value in order to find the hash which is below the target. That’s not the case in here and that’s where it becomes harder to find that number because of the important feature of Avalanche effect. And we are using the hashing the algorithm so that it cannot be reversed back to get the value what a miner wants. Otherwise, the blockchain would not have existed. That’s why the miners put hours and hours of machine work in order to get the right nonce or hash below the target.

5. Consensus Protocol

So now let’s discuss another buzz we hear around the blockchain world which is the Consensus protocol. But before directly diving into that let me take you through one another interesting concept which is a ‘Byzantine fault tolerant network’ which is the condition of a computer system, particularly distributed computing systems, where components may fail and there is imperfect information on whether a component has failed. The term takes its name from an allegory, the “Byzantine Generals’ Problem”, developed to describe this condition, where actors must agree on a concerted strategy to avoid catastrophic system failure, but some of the actors are unreliable.

The enthusiast began by noting that the Generals’ Problem can be reduced to solving a “Commander and Lieutenants” problem where loyal Lieutenants must all act in unison and that their action must correspond to what the Commander ordered in the case that the Commander is loyal

One solution considers scenarios in which messages may be forged, but which will be Byzantine-fault-tolerant as long as the number of traitorous generals does not equal or exceed one-third of the generals. The impossibility of dealing with one-third or more traitors ultimately reduces to proving that the one Commander and two Lieutenants problem cannot be solved, if the Commander is traitorous. To see this, suppose we have a traitorous Commander A, and two Lieutenants, B, and C: when A tells B to attack and C to retreat, and B and C send messages to each other, forwarding A’s message, neither B nor C can figure out who is the traitor, since it is not necessarily A — another Lieutenant could have forged the message purportedly from A.

We will consider the same situation when we talk about the Blockchain network.

So, we got familiarized with the Byzantine fault tolerance and we understood what is the whole notion or how the network among the generals agrees on consensus.

The consensus protocol for a blockchain must solve two main challenges-

  1. Protect the network from attackers: We talked about the situation when an attacker tries to add a block on the middle of the chain and we got the answers in discussing distributed P2P. Now the situation is what if the attacker tries to add the block which is a malicious block at the end of a block? A serious problem it is now! How do we solve this now?
  2. The protocol should solve for the competing chains: Our blockchain network is a large distributed P2P network which exists all around the globe. Let’s say there are two mining pools, say pool A with 4 computers which are close to each other and pool B with 2 computers which are close to each other but far away from pool A. Let’s assume both got successful in mining a block. This is a problem now because it needs to in consensus on where to keep growing, with the pool A’s mined block or pool B’s mined block! It must pick one of the two blocks. To solve these types of problems the main type of Consensus protocol which is called Proof-of-Work takes up its part.

So where does this term proof of work come from?

Well, we now have the knowledge of how the hashing, mining, distributed P2P and the Consensus work each other we can relate all these and discuss how POW works!

We will start with mining.

The miners need to come with a nonce, a Golden nonce which will create a hash which is under the target and then they will be allowed to create the block. That’s the cryptographic challenge that they solve. They must go through millions, billions of iterations until they find that nonce. That means that’s a lot of time, a lot of hardware investment and a lot of electricity consumption. For that one number, they have to put in a lot of work which is important because to get that huge reward for mining a valid block. In Bitcoin, it is 12.5 BTC reward for mining a block and for Litecoin it is 25 coins etc. It’s necessary to play a fair valid game here because what if the block they added got rejected? All those hard work put in goes for nothing, and that’s a huge loss!

How will the network get to know that a malicious block is being added? It is actually simple. before adding any block, every single node will conduct a series of checks and these checks are very rigorous. Here is one of the links to the protocol rules for the blockchain. And if some of the checks don’t go through then the blockchain system reject the block.

Now let’s get into the second part of the problem, the conflict in the Competing chains.

Let’s assume the same mining pools and their computing sources. In the middle of the mining process, again the nodes have mined the block at the same time and now the conflict again rises to where the next blocks to add? And now we have two versions of chains here. What to do now? The answer is simple. Actually, we wait! The pool which mines the immediate next block will add first and that chain wins.

We wait until which pools will mine the next block and considering from our consensus protocol and taking Byzantine protocol the nodes which have the highest blocks wins the challenge and continues to add the next block. The key feature here is that the network which has the highest hashing power will eventually generate the longest chain. The blocks which are rejected from the pool B are now called the Orphaned Blocks.

So as considered in Byzantine Generals’ problem where the network cannot come to a consensus and the majority ratio number is 70% of approval. Unlike in Blockchain, it is more than 50 % approval. As long as you have more than 50% of hashing power in the network, your node will win the consensus for approval of the blocks to add. This is somewhat becoming the problem, right? That’s why I before mentioned about the 51% attack. It’s a nice, interesting concept which I’ll try to talk about in my next post.

In the next post, we will implement an Ethereum application using Solidity and Javascript in which we can watch the transaction happening in real-time.

Thank you all for making up to this point. Hope this post helped you for understanding how Blockchain works.

Get Best Software Deals Directly In Your Inbox

--

--

Kartik
Coinmonks

Onboarding people to NFT | Developer 👨‍💻 JPEG Collector | Love Spreading Knowledge | Are you new to NFT? Always happy to help.