This is the first article of our series on Polkadot’s unique technology and approach to interoperability and scalability in blockchain space. We will dive deep to the various aspects of this network (consensus, relay chain, parachains, staking, and others) and try to explain these technical concepts in an easy way so anyone can understand how Polkadot works under the hoods and why it is one of the most technologically advanced projects in the market.
We will begin with a basic description of what Polkadot is trying to achieve and then jump straight into the Polkadots hybrid approach to consensus algorithms.
As we stated in our introductory article to Polkadot, the big idea here is to create a platform that will allow for interoperability between various blockchains. Polkadot prefers a lot of specialized chains communicating with each other instead of one chain trying to solve every problem imaginable. To achieve this goal, Polkadot uses a Nominated Proof of Stake as its consensus algorithm. There are two main roles in the network — nominators and validators. Validators are nodes elected every couple of hours and their main goal is to produce blocks and participate in consensus. However, these validators have to be backed by a staked DOT tokens and that’s where nominators come into play. Nominators are voting for validators (by bonding their DOT tokens) and there are receiving regular rewards in the form of DOTs.
Collators — those are responsible for collecting the transactions inside the parachains and producing state transition proofs to be validated by the validators
Validators — accept incoming proofs from collators and perform consensus itself for Polkadot network
Nominators — bond their DOT tokens to the validators to increase the security of the network and collect rewards
Relay chain — validators are running on the relay chain to provide security for the parachains and to validate to transactions produced by them
Parachains — separate chains hosted by Polkadot network which are connected to the relay chain. Relay chain provides a secure environment by its shared security model and allows them to communicate with each other
Parathreads — similar to parachains, only with a different economic model
Bridges — serve as a gateway to other blockchains like BTC or ETH to allow true interoperability
We will deal with those separate components in further articles, let’s now focus on the bread and butter of Polkadot which is a consensus.
In order to understand the consensus mechanism, we have to take a look at finality first. Finality is a property that describes a moment in which the transaction becomes irreversible after it has been added to the blockchain. There are actually two kinds of finality and each of them deeply influences the creation of the blocks and how much we can trust that these blocks will remain part of the network.
This is the kind of finality that is used by most of the blockchains — even by the father of cryptocurrencies, the Bitcoin! We call this finality probabilistic because, with enough number of blocks, you can be quite certain that this change is final and block with your transaction will stay on the chain forever.
Let’s use an example — you create a transaction in a BTC network, wait 10 minutes, and get this transaction into a block. But, after one more block is mined, you see that your transaction wasn’t confirmed and it actually disappeared from the blockchain! How can this be? It’s simply because miners are working in parallel and there was some other chain that didn’t contain your block and became the main chain (because, as we know, the longest chain wins — and the chain which contained your transaction was apparently a shorter one :)). That’s why people usually wait for 6 blocks (approximately one hour) before the transaction is considered confirmed. Therefore we can say that a block which is 6 levels deep is probably (and by probably we mean one can be almost sure) final and cannot be changed. This approach has its advantages but it can take quite some time before you can be sure that your transaction won’t be removed from the chain (which can be unacceptable by many businesses).
On the other hand, provable finality means that blocks are immediately considered final whenever they are added to the blockchain. Therefore you know your transaction will never be reverted or modified once it makes it to its block. In other words, blockchain will always contain that finalized block in the future. A practical solution to this problem are pBFT algorithms where a leader can propose a new block and this block has to be approved by ⅔ supermajority of the validators. Examples of those protocols are Tendermint (Cosmos), Casper (Ethereum 2.0), Ouroboros (Cardano), and Polkadots BABE and GRANDPA which we are going to talk about here.
Provable finality sounds like something which is a clear win but there are of course drawbacks as well. The most important one is that as the number of validators grows, the amount of messages which need to be exchanged grow exponentially and harming the performance of the network. That’s why the number of validators will probably be limited to 1000.
Polkadot uses a novel method which is called the hybrid consensus — that means there are actually two algorithms that are used in the network. First of them is BABE (it serves as a block production mechanism) and the second in GRANDPA which adds finality. The main reason to have two algorithms is that we can have fast block production (unlike most of the networks) and finalization which finalizes multiple blocks at once (which is, again, a great performance optimization).
BABE (Blind Assignment for Blockchain Extension)
BABE is the block production mechanism that runs between the validator nodes and determines the authors of the new blocks. The active set of validators is chosen once per era (which is one day at the moment), those validators are then able to validate blocks from the parachain collators (nodes which are producing blocks for parachains). Validators are chosen according to the amount of their stake — there is no minimum, however, the more stake a validator has, the bigger change is that it gets elected for the era.
Validators participate in a lottery in every slot (currently 6 seconds) which will tell them whether they are a block producer candidate. This lottery is based on VRF (Verifiable Random Function) which validators use to compute a random number. This function is also used to prove that the number validator provided was truly valid and it’s thus eligible for a lottery. The validator wins the lottery if v < T where v is the validators random number and T is generated number by the network. After the lottery, several scenarios actually come into play — one validator wins, multiple validators win, and finally a situation where there is no block producer candidate.
This is the most common and easiest scenario — the validator becomes a leader for the current slot and it produces a block that all other validators will validate.
When this happens, competition between the validators begins and all winners are going to produce a block candidate and broadcast it into the network. The validator whose block reaches most of the networks first wins and it becomes a leader for this slot.
This is actually a worst-case scenario as it could lead to the network inefficiency where we could have completely empty blocks with no transactions getting in. That’s why there is a secondary, so-called round-robin algorithm, which is used to always select a “backup” validator in the background. If no-one wins the lottery, the result of the round-robin is used to elect a leader and this leader produces a secondary block. That’s how Polkadot ensures its optimal performance by not leaving any slots empty.
GRANDPA (GHOST-based Recursive ANcestor Deriving Prefix Agreement)
GRANDPA is the second consensus algorithm that adds finality to the network (making all the blocks final right after they are produced). It’s a pBFT based algorithm that presumes that ⅔ of validators are honest and the only ⅓ can be malfunctioning or having malicious intents in the network.
This works very nicely, but what happens where there is a major network hiccup or even an attack on it? Let’s imagine that 40% of validators go offline. In this scenario, block production by BABE will still continue — that means that network will remain available while sacrificing the consistency of the data. When the network is back online, validators are quickly able to determine which is the canonical chain on which the network will continue to produce blocks.
However, there is still one problem remaining and that is what if network partition went on for a long time. In this case, finalizing all the blocks from BABE could take quite some time. That’s why GRANDPA is just not able to vote on a single block, it can actually vote on the whole series of blocks (beginning from the highest block validators think is valid) and transitively apply the validation to all its ancestors. This a great speed improvement upon most of the algorithms which have to validate each block separately (even up to a point of finalizing millions of blocks almost instantly).
This hybrid consensus model allowed Polkadot to build a really fast, interoperable network with provable finality which can hosts potentially thousands of different parachains while providing enough security and decentralization. In the next article of this series, we will look at the core feature of Polkadot and that are the parachains themselves.