How BABE and GRANDPA Achieve Consensus in Polkadot?
Crypto Guide
Live News

Keeping up with all the cryptocurrency news and updates is not an easy task, but we are up to the challenge! This way we can help our readers to keep track of changes in these fast growing currencies. Just like our site, lambo2btc.com is willing to face this challenge and on their site, they compile the features, key elements, and recent news involving bitcoin casino sites and which are the best to try!

How BABE and GRANDPA Achieve Consensus in Polkadot?

February 5, 2021      Anubha Jain

Polkadot, launched in May 2020, is often considered a next-gen blockchain protocol. It is a heterogeneous multi-chain combining an entire network of independent blockchains to provide trust-free cross-chain interoperability. Any data can be sent between these blockchains, and any application deployed on Polkadot can be substantially scaled-out over a number of these parachains. A wide range of real-world use-cases can be built leveraging the best of the multiple blockchains.

A network of heterogeneous and sovereign blockchain shards are called parachains. These chains connect to the Polkadot Relay Chain. It holds the states of each parachain. Supported by proof of stake protocol, it maintains the network’s security, consensus, and cross-chain interoperability.

To understand how consistency is achieved on the Polkadot relay chain, let us dive into its unique set of consensus protocols.

Issues with Existing Consensus Mechanisms

Nakamoto consensus, largely known as Proof of Work, has been part of many blockchains, including Bitcoin. It is known to consume an incredible amount of energy. The miners gain awards for adding blocks accepted by the majority. Nothing is at stake here for miners and their malicious behavior often gets unpunished.

Bitcoin, being a distributed system, prefers availability over consistency and believes the nodes will catch up eventually to the longest blockchain followed by the majority in the system. It only provides probabilistic finality for a block. A block once included in a blockchain in the past is only as safe as the number of blocks that have been built on top of it, expending more computational work behind this particular chain. However, there is no guarantee that this chain containing the block will always remain the canonical chain.

A malicious miner with unlimited resources could potentially build a competing chain and expend enough computational resources to create a chain that did not contain a specific block. In such a situation, the longest chain rule employed in Bitcoin and other proof of work chains would move to this new chain as the canonical one.

Other consensus mechanisms like pBFT provide finality to the fate of every block after making it. More than 2/3rd of the validators approve the inclusion of a block making it final. Here, consistency among nodes is more important than availability.

But these blockchains are vulnerable to stalling in the case of a network partition where more than 2/3rd of the validators get disconnected. In such cases, no majority can be achieved on a block, and the system may stall and may not be able to continue forward.

Consensus Mechanism in Polkadot

Polkadot employs a hybrid consensus mechanism. With some conditions applied, it tries to achieve consistency, availability, and partition tolerance.

It provides provable finality for blocks that can never be reverted after some process of Byzantine agreements has taken place. It allows blocks to be rapidly produced with an Irreversible consensus without risking slower transaction processing or stalling.

It employs a Proof of Stake based mechanism to choose a validator set that participates in consensus using a hybrid protocol referred to as, BABE and GRANDPA.

NPoS (Nominated Proof-of-Stake)

This algorithm selects a validator set from candidates who are interested in maintaining the network by staking DOT (the native token of Polkadot). The selection is repeated every era (a day in Polkadot). Validators assume the role of producing new blocks in BABE, validating parachain blocks, and guaranteeing finality. 

Like other PoS protocols, in case some validator tries to do something malicious or otherwise detrimental to the network, a percentage of their stake is slashed. Validators bonds are kept for longer periods of around 3 months. It allows future misbehavior to be punished, thus mitigating any short-range attacks on the chain. Long-range, nothing-at-stake attacks after validators bond ends are not possible in Polkadot because of finality achieved by GRANDPA protocol.

BABE Protocol

Blind Assignment for Blockchain Extension (BABE). It is a block production mechanism that runs between the validator nodes and determines the authors of new blocks. BABE is similar to a consensus mechanism, Ouroboros Praos (Cardano network will switch to it soon, currently on Ouroboros protocol), with some key differences in chain selection rule and slot time adjustments.

In this mechanism, time is divided into epochs. Each epoch is further split into discrete units of time, called slots, nominally 6 seconds in length. A block is produced in every slot. At the beginning of an epoch, all validators participate in a lottery to determine which slot they would produce the block for.

Every validator has their own pair of public and secret block signing keys. When an epoch starts, all the validators use their secret keys and some randomness (uses VRF, verifiable random function) from previous epochs (2 epochs past). They generate a pseudo-random number using these values. If the value produced is less than a target, then that validator is selected as block producer for the slot.

This process is deterministic as it is based upon the blockchain data from previous epochs, so other validators can verify it. For this, the block producer creates verifiable proof that he has in fact generated a pseudo-random number using blockchain data and has actually won a slot in the lottery to create the block by using block signing keys.

Because of this randomness mechanism, multiple validators could be candidates for the same slot. Other times, a slot could be empty (rare with more validators onboarding Polkadot). These situations are handled by the following mechanisms:

  • Multiple validators per slot- When multiple validators are selected as block producers in a given slot, they all will produce their blocks and broadcast them to the network. What follows is a Bitcoin kind of mechanism, where depending upon the network topology and latency, the validator whose block reaches most of the network first wins. Both the chains will continue to build individually until finalization kicks in using the GRANDPA protocol and resolves the fork.
  • No validator selected in a slot- A slot can remain seemingly blockless when no validators have generated a pseudo-random number low enough to qualify for block production. Such a situation is avoided by running a secondary, round-robin style validator selection algorithm in the background. The validators selected to produce blocks through this algorithm always produce blocks. These secondary blocks are ignored if the same slot also produces a primary block from a VRF-selected validator. Thus, a slot can have either a primary or a secondary block, making sure that there is a block per slot.

All other validators collect blocks from chosen validators for a slot and verify the block (verify signatures, check if the validator is a slot leader). If correct, they add this block to the end of the blockchain, which they consider the best.

Best chain selection in BABE: Of all the forks available, validators chose the longest chain with the most primary blocks following the last finalized block by GRANDPA as the canonical blockchain, thus providing probabilistic finality.

Image source: https://wiki.polkadot.network/docs/en/learn-consensus

 

READ  Moonstake Wallet Adds Staking Support for Polkadot (DOT)

As shown in the above image, blocks marked as (1) are the primary blocks, and (2) are the secondary blocks. The black ones are the blocks finalized by the GRANDPA protocol.

  1. Chain 1 is built on top of it but does contain some secondary blocks.
  2. Chain 2 is also extending the last finalized block and contains all primary blocks.
  3. Chain 3, also on the last finalized block contains some secondary blocks.
  4. Chain 4 is not built on the last finalized block, thus cannot be considered as the best chain.

From these blockchains, Chain 2 (black blocks followed by block 1-1-1) is considered to be the canonical chain. Please note that even if it is less in height than Chain 1 and 3, it is built using the highest number of primary blocks. 

In the case of a network partition when we cannot achieve a consensus of more than 2/3rd of validators using GRANDPA protocol, the blockchain production continues with BABE on individual chains. Here, availability is achieved at the cost of consistency. When the network reattaches, then finalizing is done using GRANDPA, and consistency is achieved.

GRANDPA Protocol

GHOST-based Recursive ANcestor Deriving Prefix Agreement (GRANDPA). It is the finality gadget that is implemented for the Polkadot Relay Chain, which ensures that a block is in the canonical blockchain. It works in a partially synchronous network model as long as 2/3rd of the nodes are honest. 

GRANDPA reaches agreements on the chains rather than blocks, greatly speeding up the finalization process. As soon as more than two-thirds of the validators attest to a chain containing a certain block, all blocks leading up to that one are finalized at once.

Conclusion

Co-founded by Ethereum’s founder Dr. Gavin Wood and developed by Parity Technologies, Polkadot is designed to work with the public, private, and enterprise chains.

Polkadot is being considered as a stronger competitor of Ethereum and other industry heavyweights, but we believe it has much more to offer than them. It has the potential to use the best of any blockchain world for the rise of future decentralized economies.

#BABE Protocol #Byzantine fault tolerance (BFT). #Consensus algorithm #GRANDPA Protocol #Interoperability-focused blockchain platform #Polkadot
Anubha Jain
Anubha Jain

Anubha is a keen follower of the manifold applications of blockchain technology. She is a doctoral candidate researching privacy constraints of the blockchain and is working to enhance user privacy in cryptocurrencies. She is a strong supporter of the vision of user-owned internet and believes blockchain as the key to it.