Consensus Mechanisms in Blockchain
Table of contents
As a Web2 developer transitioning to Web3, you would have heard about how Web3 is decentralized, permissionless, trustless, and all of that. Coming from Web2, you might ask, since the blockchain is not governed by a single/central entity, how then is the state of the blockchain decided? How is it decided what transactions are valid? or what block should be added to the blockchain? In Web3, we call this Consensus. Generally, the term 'Consensus' simply means agreement, and it is achieved through a consensus mechanism. In this article, you will learn what a consensus mechanism is, why blockchains use a consensus mechanism, and the different consensus mechanisms used by blockchains.
What is a Consensus Mechanism?
In Blockchain, a Consensus mechanism is simply a way for the participants of a blockchain network to come to an agreement on the state of the network. According to Vitalik Buterin; the founder of Ethereum, the purpose of a consensus mechanism is to allow for the secure updating of the state according to some specific state transition rules, where the right to perform the state transition rules is distributed among some economic set. This simply means that the purpose of a consensus mechanism is to ensure that all the nodes in a blockchain network agree on the current state of the network, as well as any upcoming transactions that will update the state of the network.
When a transaction is initiated on a blockchain network, a group of nodes on the network come together to validate the transaction, in order to decide if it should be added to the next block. If a majority of the nodes agree on the validity of the transaction, all nodes can add the transaction to their block. When a node adds a set of validated transactions to its block, other nodes will have to agree to add that block to the blockchain. If it is agreed on by the majority, each node then adds the new block to their copy of the blockchain on their machine, and the state of the blockchain network is updated. You might be wondering, since a particular group of people have to validate each transaction, doesn't it mean control is still centralized? It would, except that anybody can participate in this process. Every blockchain has a method it uses to achieve consensus, and anybody can participate in this process.
Consensus mechanisms are designed to reward honest participation in a Blockchain network and punish malicious actors who might be trying to falsify data in transactions or harm the network. Usually, If a block proposed by a node is added to a blockchain, there is a reward for that node. Reversely, if a node proposes a block with false data in it, there is a penalty for the node. This way, the nodes are encouraged towards good behavior, and the integrity of the network is maintained. If you're wondering what happens if a group of nodes come together to agree on adding a block with false transactions to the blockchain, we'll cover that in the section on the different consensus mechanisms.
Why do Blockchains need a consensus mechanism?
A blockchain is a publicly distributed ledger, which means control does not rest on a single entity or a central authority. Having a trusted third party control the state of the blockchain, completely defeats its purpose. As such, the need for the third party, and trust among the network participants is eliminated by the implementation of a consensus mechanism. Seeing as the network is governed by its participants, they need a way to come to an agreement on things such as:
Valid transactions and blocks
State of the network from the Genesis block, to the current block
All of the above can be achieved with the implementation of a consensus mechanism.
Importance of consensus mechanisms
Consensus is the foundation on which the blockchain is built. It is what truly allows for the decentralization, trustlessness, distribution, and other features a blockchain offers. The implementation of a consensus mechanism allows for the following:
Immutability: Historical data on the blockchain cannot be changed or tampered with because it is difficult to remove or manipulate a block that has already been added to the blockchain.
Trustlessness: Consensus mechanisms enable you to securely interact with other participants, or smart contracts without the need to trust them. The algorithm behind a consensus mechanism promotes transparency and removes the need for a third party.
Decentralization: Consensus mechanisms are designed to distribute the validation power among a large number of participants. This way, a single entity or group cannot gain control over the network. It will be quite difficult to convince the majority of nodes to act maliciously since there are a lot of them.
Network stability and Fault Tolerance: Since there are a large number of nodes running the network, the network cannot fail from a single point. That is, even if some nodes are unresponsive, the network will still function properly.
Security: Since a majority of the nodes have to come together to validate blocks and transactions, it makes it difficult to manipulate the data contained thereby fortifying the network.
Governance: In certain Blockchain networks such as Ethereum, consensus mechanisms are used by stakeholders to decide on what proposals to implement, as well as other decisions to be made concerning the network's future.
Types of Consensus Mechanisms
Practical Byzantine Fault Tolerance(PBFT)
If you've heard about the Byzantine Generals' problem, then this should be easy to understand, as it is loosely based on it. Any fault exhibiting various symptoms to various observers can be referred to as a byzantine fault. Byzantine fault tolerance in blockchain is the ability of a blockchain network to keep functioning when some of its nodes fail or act maliciously. This means that if a percentage of the nodes on a network agree to act maliciously in regard to the network's data, the network will keep functioning as long as that percentage is not more than 1/3 of the network's nodes, and honestly, that'll be extremely difficult to achieve. This mechanism is usually used in permissioned/private blockchains, and functions on the condition that the maximum number of malicious nodes in a network should not be more than a third of the nodes in that network. As the name implies, it has high fault tolerance, because the network can tolerate a third of its participants being malicious, and still be fully functional. This also implies that the more participants on the network, the more secure the network becomes. This way, it is difficult to influence a third of the network's nodes to act maliciously. An example of a blockchain that uses the PBFT mechanism is Hyperledger Fabric.
Proof of Work (PoW)
This mechanism was first introduced by Bitcoin. It involves nodes known as miners solving a complex cryptographic puzzle, where the first miner to solve this puzzle gets to add their block to the blockchain, and receives the block reward. PoW is highly secure, because for a malicious actor to add a manipulated block, they will have to expend resources competing against the other miners who want to add their own block. Additionally, changing existing data on the blockchain will require even more energy since the person will have to redo the work it took to add those blocks in first place. The energy consumption in this mechanism is a cause for concern, as it is not very efficient or sustainable. Examples of blockchains that use the PoW mechanism include Bitcoin (of course), Dogecoin, and Litecoin.
Proof of Stake (PoS)
In this mechanism, the nodes that validate transactions are known as Validators. To become a validator, you have to deposit a certain amount of the network's token. This is known as a stake. It is to keep validators in line, as they can lose a percentage of their stake if they harm the network. This mechanism uses a random selection algorithm to decide on the validator to add the next block. There is also a penalty if a validator is selected, and they are not online to add their block. There are also staking pools, where nodes who want to be a part of the validation process, but do not have enough tokens to stake, or just want to increase their chances of being selected to add the next block to the blockchain, can pool their resources together and share the validators reward based on their percentage in the staking pool. Proof of Stake is considered more energy efficient than proof of work since it does not require the computations that consume energy. PoS is considered to be a bit centralized as the staking pools with more tokens have a higher risk of being chosen as validators. Examples of blockchains that use the PoS mechanism include Ethereum, Solana, and Algorand.
Delegated Proof of Stake (DPoS)
This mechanism is a type of PoS where the nodes vote and elect a group of delegates who will be responsible for validating transactions and adding new blocks. Its main purpose is to reduce the time taken to add a new block. Since there is no algorithm to decide the next validator, and nobody has to spend time solving a cryptographic puzzle, it takes less time to add a new block to the blockchain. However, this mechanism is seen as more centralized since a significant amount of power rests on a small number of delegates. Examples of blockchains that use the DPoS mechanism include EOS and Tron.
Proof of Authority (PoA)
This is kind of like the opposite of DPos. Here, a central entity selects and authorizes the block validators. This mechanism is energy efficient, more scalable, and the time taken to add a new block is lesser. It is usually used by private and consortium blockchains. However, it is considered to be centralized, since the block validators are controlled by a central entity. Examples of blockchains that use the PoA mechanism include Bitgert and Xodex.
In this article, we have covered what a consensus mechanism is, the need and importance of consensus mechanisms, as well as the common consensus mechanisms used by blockchain networks. As the blockchain ecosystem improves, new networks may come with their own consensus mechanisms, and existing networks may adopt new consensus mechanisms as well to support and enhance the network's functionality. With the knowledge gained from this article, developers can make informed decisions concerning building and participation on the blockchain.