# Consensus Algorithms in Blockchain

When we talk about the blockchain, the first thing that came up in our mind is the security and the security because of the blockchain consensus algorithm. Those who know about the blockchain know that we keep the ledger transactions synchronized across the network to ensure that ledgers only update when the appropriate participants approve transactions and that when ledgers do update, they update with the same transactions in the same order is called consensus. Here we will discuss the three different consensus algorithms.

Practical Byzantine fault tolerance

Imagine that several divisions of the Byzantine army are camped outside an enemy city, each division commanded by its own general. The generals can communicate with one another only by messenger. After observing the enemy, they must decide upon a common plan of action. However, some of the generals may be traitors, trying to prevent the loyal generals from reaching an agreement. The generals must decide on when to attack the city, but they need a strong majority of their army to attack at the same time. The generals must have an algorithm to guarantee that (a) all loyal generals decide upon the same plan of action, and (b) a small number of traitors cannot cause the loyal generals to adopt a bad plan. The loyal generals will all do what the algorithm says they should, but the traitors may do anything they wish. The algorithm must guarantee condition (a) regardless of what the traitors do. The loyal generals should not only reach an agreement but should agree upon a reasonable plan.

Above story represents the Byzantine General’s Problem. There are many solutions for this problem, but we will talk about Practical Byzantine fault tolerance (PBFT).

In 1999, Miguel Castro and Barbara Liskov introduced the “Practical Byzantine Fault Tolerance” (PBFT) algorithm, which provides high-performance Byzantine state machine replication, processing thousands of requests per second with sub-millisecond increases in latency.

We here talking about only PBFT from the number of other solution is because PBFT the only potential solution to  Byzantine General’s Problem. IBM backed Hyperledger uses this consensus algorithm. In PBFT each node maintains an internal storage. When a node receives, messages coming through the node is signed by the node to verify its format. Once enough same responses are reached, then a consensus is met that the message is a valid transaction.

Proof of Work

One of the most known algorithms of the all is Proof of Work (PoW). This algorithm is used by one of the strongest crypto-currency that is Bitcoins. This is one of the most commonly used consensuses mechanism. Unlike PBFT, PoW doesn’t require every node on the network to submit their message to reach any consensus. Instead in PoW, an individual can provide the conclusions to reach consensus.

Individual also known as miner calculates the hash of his block header and checks whether the conclusion is correct or not. If the conclusion is wrong, then the miner modify the nonce and then try again.

For example, let’s say we are going to work on a string “blockchain” and our target is to find a variation of the variation of it that SHA-256 hashes to a value beginning with ‘0000’. We vary the string by adding an integer value to the end called a nonce and incrementing it each time.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

 blockchain0 — bd4824d8ee63fc82392a6441444166d22ed84eaa6dab11d4923075975acab938 blockchain1 — db0b9c1cb5e9c680dfff7482f1a8efad0e786f41b6b89a758fb26d9e223e0a10 blockchain2 — 8f0532cd22055fb7599aa48f38501dcd46e61712ab49a02f840f5545830e9260 blockchain3 — eb61c3724d6da33605084d2d232bba0563cb82f4ad82c101b42f23c2e86277ef blockchain4 — 1af101f70897bf501779b7b2e413ae7144aba5b97e24890c71ba2a1d9c518d20 . . . blockchain1038 — 2eee57eaae45cc6a47c341facfe6cd1368e632cc065df9cb2c37fbe65478e29e blockchain1039 — 305c971ed5272a33940a09b72b2c101fdf51f36b96c77c0732ad2ed75319592d blockchain1040 — 3f1f04f2146bce225366fbe65da38a5acbde429777b2801e3ba0e6ae3d5c197a blockchain1041 — 00007f73e777e83b01302b5fd5bc9905960c6398c7b24d0f2cc6a3e0c5cd3522
view raw

PoW-SHA256

hosted with ❤ by GitHub

Finding a match for “blockchain” takes us 1042 tries.

Proof of Stake

And the last one is known as Proof of Stake (PoS). Here the mining is done by a stakeholder who is selected by network based on their stake. Unlike PoW, there is no reward for the block miner in the PoS system. The miners get the transaction fees instead. Crypto-currencies like Peercoin, BlackCoin, Nav Coin uses the PoS system.

So these are the well know three different consensus algorithms. Each one has their pros and cons.

References:

https://en.bitcoin.it/wiki/Proof_of_work

https://medium.com/@chrshmmmr/consensus-in-blockchain-systems-in-short-691fc7d1fefe

#### Written by Prabhat Kashyap

I am from India and developing Scala Microservices :)