Blockchain nodes use consensus engines to agree on the blockchain's state. This document covers the process of generating a block including 3 steps:
- Block production
- Block finality
- Block import
BABEis a slot-based block production mechanism that uses a VRF (Verifiable Random Function) to randomly perform the slot allocation. On every slot, all the authorities generate a new random number with the VRF function and if it is lower than a given threshold (which is proportional to their weight/stake) they have a right to produce a block.
VRFa verifiable random function is a mathematical operation that takes some input and produces a random number along with proof of authenticity that this random number was generated by the submitter. The proof can be verified by any challenger to ensure the random number generation is valid.
VRFis used to make sure that all validators will be treated fairly, and everyone will have the same opportunity to produce a block.
Slot-based consensusalgorithms must have a known set of validators who are permitted to produce blocks. Time is divided up into discrete slots, and during each slot, only some of the validators may produce a block.
GRANDPAprovides block finalization. It has a known weighted authority set like BABE. However, GRANDPA does not author blocks; it just listens to gossip about blocks that have been produced by some authoring engine.
A fork choice ruleis an algorithm that takes a blockchain and selects the "best" chain, and thus the one that should be extended. As a primitive, a block contains a header and a batch of extrinsic. The header must contain a reference to its parent block such that one can trace the chain to its genesis. Forks occur when two blocks reference the same parent. Forks must be resolved such that only one, canonical chain exists.
A fork choice rule is an algorithm that takes a blockchain and selects the "best" chain, and thus the one that should be extended. Glitch allows you to write a custom fork choice rule, or use one that comes out of the box. For example:
Longest Chain Rule
The longest chain rule simply says that the best chain is the longest chain. Substrate provides this chain selection rule with the
LongestChainstruct. GRANDPA uses the longest-chain rule for voting.
- Each validator is assigned a weight for an epoch. This epoch is broken up into slots and the validator evaluates its VRF at each slot. For each slot that the validator's VRF output is below its weight, it is allowed to author a block.
- Glitch process block validation on the slot-based mechanism. This process includes the following tasks:
- The validators are picked for claiming slot to receive transactions which are processed and put into the block until the slot duration ends (so-called block time).
- Validator stores the transaction’s implementation data
- Check the duration of slot base based on block time information, then compute and pack blocks.
- Validators sign for confirming block and proof to process block finality step.
- Using the block authoring schemes and fork choice rules, transactions are never entirely finalized. There is always a chance that a longer (or heavier) chain will come along and revert your transaction. However, the more blocks are built on top of a particular block, the less likely it is to ever be reverted. In this way, block authoring along with a proper fork choice rule provides probabilistic finality.
- When deterministic finality is desired, a finality gadget can be added to the blockchain's logic. Members of a fixed authority set cast finality votes, and when enough votes have been cast for a certain block, the block is deemed final.
- GRANPA validators vote on a block that they consider "best" and their votes are applied transitively to all previous blocks. Once more than 2/3 of the GRANDPA authorities have voted for a particular block, it is considered final.
- The import queue is an abstract worker queue present in every node. The import queue collects incoming elements from the network and stores them in a pool. The elements are later checked for validity and discarded if they are not valid. Elements that are valid are then imported into the node's local state.
- The most fundamental piece of information that the import queue processes is blocks themselves, but it is also responsible for importing consensus-related messages such as justifications and, in light clients, finality proofs.