blockchainconsensuspolkadot

How does the fork choice in Polkadot work?


Polkadot has GRANDPA for finalizing and BABE for block production. BABE is said to be able to keep producing blocks even without GRANDPA and even without a majority of the validators online. How does BABE know which chain to keep building on?


Solution

  • BABE is the block production algorithm in Polkadot's relay chain. It uses a VRF, Verifiable Random Function, to pick the block production candidate among the set of validators. This block producer is primary. At the same time, BABE runs a round-robin-like selection algorithm in the background to select a secondary validator from the set to produce a block, if the VRF didn't result in any validators being selected as primaries. Using this double selection method, BABE maintains a constant block-time - primaries if selected, secondary if not.

    It is possible that VRF qualifies several validators to produce blocks, in which case both blocks are valid. At that point, it'll depend on network latency how fast the rest of the network will see each block - this will determine the winner.

    Under GRANDPA, when 2/3 or more of validators agree on a block, that block and all blocks leading up to it are finalized immediately.

    So what happens when BABE is forking and building on two separate chains?

    The rule in Polkadot is as follows:

    If there is still a tie, the ambiguity continues until two thirds of the validators finalize a block in one of the forks and amputate the other.