Blockchain networks rely on consensus algorithms to reach an agreement among distributed nodes. A consensus mechanism such as proof of work (PoW) or proof of stake (PoS) secures the network and prevents unauthorized users from validating bad transactions. Thus a consensus algorithm protects us by
Prevents Attacks
Solves Competing Chain Problem
And is basically of two types that are famous
Proof Of Work (POW)
Proof Of Stake (POS)
Others
So in the blog Immutable Ledger, we learnt that if some hacker tries to inject a block of data in between somewhere in the blockchain then as he would have just changed one node, the rest of the other nodes would easily track the anomaly and protect the blockchain from the attacker.
But what if the hacker tries to add a malicious block of data at the end of the ledger?
In that case, he has not broken the chain so the concept of the immutable ledger won’t be able to detect the injection.
Here comes the concept of consensus protocol.
If a miner wishes to add a block to our blockchain then to do so the miner needs to solve a mathematical problem to mine a block. Solving that complex mathematical problem is time-consuming and hence miner needs to put a lot of effort to do so along with spending money on electricity as well as building an environment that keeps the system cool. This works as proof of their work. The solution to the complex mathematical problem is the proof of work done by the miner to add the block to the blockchain. And for solving the problem i.e. to establish a valid proof of work, the miners are rewarded with a particular amount of bitcoin (in the case of bitcoin blockchain) along with a transaction fee. Thus if a miner tries to add a malicious block neither will he get compensation/ reward for his work also his efforts, time as well as money to do so will go in vain.
Also, it’s worth noting that not suddenly all the nodes add the mined block to their blockchain. First, an algorithm is run simultaneously on all the nodes to validate the authenticity of the mined block that is to be added to the blockchain. The algorithm is quite big and includes verifications like
1. Transactions list must not be empty
2. Previous hash present or not etc…
Once all the steps of the algorithm verify the block in all the nodes only then it is added to the blockchain. This way it prevents the attack. Also, verification of the block isn’t as time-consuming as the process of mining the block. Consider an example: It takes time to solve a 4x4 Rubiks Cube but it takes just a few seconds to validate that all the colours are perfectly paired.
Where Proof Of Work relies majorly on 3 factors, namely:
Better Computer == Higher chances of a win
More number of computers == Higher chances of a win
Luck
Proof of stake relies on stake i.e. some money that needs to be put so that in case you do something wrong, your stake gets dissolved as a punishment.
So, in proof of stake, every node puts up a stake to be chosen to be a validator and if that particular node which gets selected to be a validator, fails to validate then he loses the stake by the process called Slashing ( losing coins that you locked up initially ).
To be chosen a validator, there exists different models:
The more you stake, the more reliable you are
The longer you have been staking, the more reliable you are
Some models take randomness into consideration for fair game
Often the models combine all of this and if you wish to know the specifics then you need to read the white paper of that particular blockchain.
→ Solana works on POS and POH (Proof Of History)
→ Ethereum 2.0 is the transition of the Ethereum blockchain from proof of work to proof of stake.
Anatoly Yakovenko proposed Proof Of History to be functional for Solana. Now, this is not completely a consensus protocol but it is an implementation of POS with added timestamps to place a specific time and date on the blocks. This is done for fast sequencing the validators so that they know their order without having to communicate back and forth.
I will be explaining more about it in a later blog where I will talk about Solana.
Now let’s see how consensus protocol helps solve competing chain problem
Consensus protocol states that in a network the node with the longest chain will get accepted.
Let’s understand this with an example
Here we can see two groups. Let the first group have node 1,2,3 in the network and they mined a block shown with the colour green and the second group is of node 4,5 having mined a block depicted with the colour red.
Initially, 1 mined a block and verified it with 2 and 3 and similarly, 4 mined a block and verified it with 5. The issue arises now that which of the 2 blocks should be considered worth adding to the network. The solution to this is the statement mentioned earlier. The group of nodes that has the longest chain, its blocks will be considered valid to be added to the network. So, as the first group has 3 nodes it will be able to mine another block (shown in green colour) before nodes 4 and 5 mine a red block. Hence the green block will be considered valid and all 5 nodes will have it in their blockchain.
In brief :
Competing chains mean that two different sides of the network now hold two different chains, and attempt to extend them further. At some point, some miner will solve the hash puzzle and will propagate his next new valid block. He may be arbitrarily on either side of the network — so either he will extend Miner A’s block, or Miner B’s. And the solution to this is “The ‘main chain’ at any time is whichever valid chain of blocks has the most cumulative proof-of-work associated with it. Under most circumstances, this is also the chain with the most blocks in it”.