Babe and Grandpa Protocol in Substrate
The substrate framework is a modular blockchain framework that is constructed on top of existing runtime modules. It provides the ability to select various consensus mechanisms, modes of execution, and cryptography for your app chain.
Speaking of the amazing features of Substrate, two consensus mechanisms are particularly noteworthy: Babe and Grandpa. Babe is a block production mechanism that is responsible for creating new blocks in the blockchain, while Grandpa is a finality gadget that is used to determine the final state of the blockchain.
These two mechanisms work together seamlessly to ensure the security and efficiency of the blockchain network. Babe is designed to be flexible, allowing developers to choose between various block production strategies, while Grandpa provides a finality mechanism that ensures that the network reaches consensus quickly and efficiently.
In this article, we will delve deeper into the mechanics of Babe and Grandpa and how they contribute to the overall functionality of the Substrate framework.
Babe Protocol
An analogy to explain the Babe protocol is a group project where each member is assigned a specific role in a fixed time slot. The project leader selects members using a random function and a deterministic function based on the previous project update. Each member has a fixed period to complete their task, and if they do it correctly, they are rewarded, but if they fail or produce poor work, their contribution is penalized.
Babe protocol is a consensus mechanism that provides a secure and scalable approach for block production and validation in the Substrate blockchain framework. It operates on a set of slots, with each slot representing a fixed period of time, and divides time into epochs consisting of a fixed number of slots. During each epoch, Babe selects a set of validators to produce and validate blocks using a verifiable random function (VRF) and a deterministic function based on the previous block hash. Once a validator is selected for a slot, it has a fixed period of time to produce a block. If the validator produces a valid block, it is added to the blockchain, and the validator is rewarded with a block reward. However, if the validator fails to produce a block, or produces an invalid block, it is penalized by having its stake slashed.
This diagram shows a sequence of events for two validators, one for Slot 1 and another for Slot 2, producing blocks using the Babe protocol. The Substrate Runtime is responsible for coordinating the interactions between the validators and the blockchain.
The sequence starts with the Validator for Slot 1 being selected by the Babe protocol, and the Substrate Runtime sending details of Slot 1 to the validator. The validator then produces a block for Slot 1, which is validated by the Block Producer and added to the blockchain.
The sequence then continues with the Validator for Slot 2 being selected by the Babe protocol, and the Substrate Runtime sending details of Slot 2 to the validator. The validator then produces a block for Slot 2, which is validated by the Block Producer and added to the blockchain.
Grandpa Protocol
GRANDPA is built on the GHOST (Greedy Heaviest Observed Subtree) protocol and works in conjunction with the block production mechanism, such as BABE (Blind Assignment for Blockchain Extension), to provide both liveness and safety to the blockchain. Liveness refers to the ability of the network to continue producing blocks, while safety ensures that once a block is finalized, it cannot be reverted.
The key advantage of using the GRANDPA protocol in Substrate is that it allows for faster finality, as it can finalize multiple blocks at once, depending on network conditions. This is beneficial in situations where there is a large number of validators, or when the network experiences high latency. Additionally, GRANDPA provides better security against long-range attacks and other malicious activities, which helps maintain the overall health and stability of the blockchain network.
To understand the workings of the Grandpa protocol in the Substrate blockchain framework, think of a referee in a game of soccer. Just as a referee makes sure that the game progresses in a fair and organized manner, the Grandpa protocol ensures that the blockchain network reaches a final and agreed-upon state. It is a finality gadget that confirms the validity of blocks in the blockchain and helps to prevent the possibility of a fork. Grandpa works alongside the Babe protocol to ensure that the network reaches consensus quickly and efficiently. The Grandpa protocol in Substrate uses a Byzantine Fault Tolerant (BFT) consensus algorithm that allows for fast finality and high security. With Grandpa, blockchain developers can have confidence that their network will reach a secure and final state, just like soccer players can rely on the referee to enforce the rules of the game.
Babe and Grandpa Protocol
This diagram shows a sequence of events for two validators, one for Slot 1 and another for Slot 2, producing blocks using the Babe protocol, which are then finalized using the Grandpa protocol.
The sequence starts with the Validator for Slot 1 being selected by the Babe protocol, and the Substrate Runtime sending details of Slot 1 to the validator. The validator then produces a block for Slot 1, which is validated by the Block Producer and added to the blockchain.
The sequence then continues with the Grandpa Authority finalizing the block using a finality certificate, which is added to the Substrate Runtime. The Grandpa Authority then finalizes the blocks, which are added to the blockchain.
The sequence then continues with the Validator for Slot 2 being selected by the Babe protocol, and the Substrate Runtime sending details of Slot 2 to the validator. The validator then produces a block for Slot 2, which is validated by the Block Producer and added to the blockchain.
The sequence concludes with the Grandpa Authority finalizing the block using a finality certificate, which is added to the Substrate Runtime. The Grandpa Authority then finalizes the blocks, which are added to the blockchain.
Finality Certificate
Finality gadgets: The Grandpa protocol uses a set of finality gadgets to determine which blocks should be finalized and added to the longest chain in the Substrate blockchain. These gadgets include a finality voter, a finality detector, and a finality gadget selector.
Grandpa authorities: The Grandpa protocol requires a set of trusted authorities to participate in the finality process. These authorities are selected based on their stake in the blockchain and their ability to contribute to the security of the network. The authorities are responsible for signing and validating finality certificates.
Agreement threshold: The Grandpa protocol requires a threshold of authorities to agree on the finality of a block before it can be considered final. This threshold is set by the network and can be adjusted based on the level of security and finality required.
Finalization process: Once a block has been added to the Substrate blockchain by the Babe protocol, it becomes eligible for finalization by the Grandpa protocol. The finalization process involves the following steps:
Finality detector: The finality detector checks the block's ancestry to ensure that it is part of the longest chain in the blockchain. If the block is part of a fork, the detector will wait for the fork to be resolved before proceeding.
Finality voter: The finality voter collects and verifies signatures from the Grandpa authorities to create a signed message that contains the block header hash and the signatures of the authorities who have agreed to finalize the block.
Finality gadget selector: The finality gadget selector determines which blocks should be finalized based on the signatures collected by the finality voter. If the threshold of signatures is met, the block is considered final and a finality certificate is generated.
Finality certificate: The finality certificate is a signed message that contains the block header hash and the signatures of the Grandpa authorities who have agreed to finalize the block. The certificate is added to the Substrate Runtime and included in subsequent blocks as part of the block header.
By using a set of finality gadgets, trusted authorities, and an agreement threshold, the Grandpa protocol provides a secure and scalable method for adding finality to the Substrate blockchain. The finality certificate serves as proof that the block has been finalized and ensures that the blockchain is secure and resistant to attacks.
This diagram shows the interactions between the different components of the Grandpa protocol, the Substrate Runtime, and the Block Producer in producing a finality certificate for a block in the Substrate blockchain.
The sequence starts with the Finality Detector checking the block's ancestry to ensure that it is part of the longest chain in the blockchain. If the block is part of a fork, the detector will wait for the fork to be resolved before proceeding.
Once the block has been verified, the Finality Voter collects and verifies signatures from the Grandpa authorities to create a signed message that contains the block header hash and the signatures of the authorities who have agreed to finalize the block.
The Finality Gadget Selector then determines which blocks should be finalized based on the signatures collected by the finality voter. If the threshold of signatures is met, the block is considered final and a finality certificate is generated.
The Grandpa Authority signs the finality certificate, which is added to the Substrate Runtime and included in subsequent blocks as part of the block header.
Overall, this sequence diagram provides a detailed overview of how the Grandpa protocol produces a finality certificate for a block in the Substrate blockchain, which provides strong security guarantees and ensures that the blockchain is secure and resistant to attacks.
Conclusion
The article has not only helped you understand these two consensus mechanisms on a high level but has also given you valuable insights into the technical aspects and benefits of using them in real-world blockchain applications. By the end of this article, you should be feeling like a substrate guru, ready to take on any challenge that comes your way.
So, what are you waiting for? Move forward with confidence, knowing that you have the knowledge and expertise to create secure and efficient blockchain applications using Babe and Grandpa protocols. And don't forget to show off your new-found skills to your friends and colleagues – they'll be impressed by your blockchain prowess!
Subscribe to my newsletter
Read articles from Faisal Ahmed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Faisal Ahmed
Faisal Ahmed
As a developer, I have been working with Rust for several years and have gained significant experience in building secure and reliable systems. I have also worked extensively with cryptography and have a deep understanding of its principles and application in various domains. In my recent projects, I have implemented advanced cryptographic protocols such as Threshold ECDSA Signing, Multi Party ECDSA Key Generation, BLS Signature Aggregation, and Threshold Signing with BLS Signatures using Rust. I have used Rust libraries such as rust-secp256k1-zkp, pairing, and blst to ensure their security and efficiency. In addition to my expertise in cryptography and Rust, I have extensive experience in Substrate development. I have been working with Substrate for over a year and have gained a deep understanding of its architecture. I have developed several projects using its framework and have extensively used the client side of Substrate node to make use of std libraries and optimize performance. I have also developed several host functions for pallets to call functions outside the WASM boundary, which has significantly improved the performance and functionality of the projects.