How is the Consensus Reached in the Blockchain Network?
Blockchain is a decentralized environment without a central authority to ensure trust between the transacting parties. So, how does blockchain ensure that valid transactions and block creation occur in the network? And how is a consensus reached about the validity of data and transactions?
Blockchain employs a set of validation checks known as consensus mechanisms for the validity of transactions and blocks.
Let’s discuss the consensus in a blockchain.
Consensus Mechanism in Blockchain
In the blockchain, we use mathematical algorithms to verify the transactions and ensure trust between transacting parties. Transacting parties have to trust the output achieved using these mathematical algorithms. We call these algorithms together as the consensus mechanism.
Multiple consensus mechanisms are available to find the consensus for the transactions and the blocks. Let’s discuss them below.
Different types of Consensus Mechanisms
The consensus mechanism is challenging to achieve and easy to verify. Blockchain networks have many consensus mechanisms. The list includes Proof-of-Work, Proof-of-Stake, Proof-of-Authority, Delegated Proof-of-Stake, Proof-of-Capacity, Proof-of-Activity, Proof-of-Elapsed Time, Proof-of-Burn, etc.
This article will cover two prominent consensus mechanisms: “Proof-of-Work” and “Proof-of-Stake”.
Proof-of-Work Consensus Mechanism
In the Proof-of-Work mechanism, the blockchain achieves consensus by using an algorithm where miner nodes must solve a computationally difficult problem to compute the hash for the current block. The network sets a difficult target for the miners. This difficulty level plays a vital role in the consensus and the mining process.
The Bitcoin network has a block creation time set to 10 minutes, and this value has to be maintained. The difficulty value, which is a target hash value, adjusts with every block creation such that the value of block creation remains constant. We calculate the difficulty value by using the following formula:
Difficulty Level = (Previous Difficulty Level * 20160) / (Time taken to mine last 2016 blocks)
In the above formula, 20160 is the time (in minutes) to generate 2016 blocks, considering a block frequency of 10 minutes in the Bitcoin blockchain network.
The Proof-of-Work Consensus for a block takes place if the block follows the following condition:
Hash (header of the block) < Difficulty Level
The hash of a block’s header includes the timestamp, number of transactions, nonce, and Merkle root. To know more about the blockchain’s structure, refer to the post “Blockchain Technology: Architecture, Security and Transactions“.
If the time for the last block is greater than 10 minutes, the new value of the difficulty level is lower than the previous one, so the creation of blocks is faster.
Conversely, if the time for the last block is less than 10 minutes, the new value of the difficulty level is greater than the previous one. What this means is that the list of possible target hash is less. So, the new block creation is slower.
Addition of a block using Proof-of-Work consensus
Suppose that miner nodes have computed the current block’s hash and published it to the entire network. All other blockchain nodes in the network follow rules to validate a block.
Once all the nodes complete checking the block’s validity and find the block to be valid, they give their consensus. In such cases, the new block is added to the blockchain. The functions taking place while validating a node are as follows:
- Check if the block with the same hash has already been received.
- Check if there is a block in the blockchain whose hash is the same as the previous hash of the current block.
- Check if the hash of the current block satisfies the difficulty level.
- Check if the timestamp of the current block is greater than the timestamp of the previous block.
- Update the current state of the blockchain’s new block number, which is the parent block number + 1.
- Broadcast updated blockchain to the network.
After successfully completing the above checks, the block addition takes place in the network. And with this, the consensus is achieved in such a trustless environment.
Let us explore one more consensus algorithm, “proof of stake.”
Proof-of-Stake consensus mechanism
Before Proof-of-Stake (PoS), a popular algorithm to reach Consensus was Proof-of-Work for the Bitcoin blockchain network, as discussed above. But Proof-of-Work has its limitations due to high energy needs. Therefore, the chances of mining the block increase if one entity has high computational resources, giving an unfair advantage. Besides, using such high computation is also hazardous to the environment.
Therefore, Proof-of-Stake mechanisms hold merit in such scenarios. Blockchain networks using Proof-of-Stake include Ethereum (Casper update), Peercoin, etc.
Proof-of-Stake, as the name suggests, is a mechanism where a node on a network stakes an amount of cryptocurrency to participate in new block validation and earn a fee from it. Then, the algorithm chooses the nodes that can participate in validating the new block based on multiple factors to make the selection process fair, as stated below:
- Stake: Amount of cryptocurrency at stake
- Coin-age-based selection: By participating as a validator, older nodes in the network get a preference to be a validator.
- Random block selection: The validator selection occurs based on the lower hash value and higher stakes combination.
Addition of a block using Proof-of-Stake consensus
The steps performed to validate and add the block to the network are as given below:
- All the transactions made by the node are put together, but the PoS algorithm.
- Nodes participate to become a validator. The algorithm selects the validator by combining the stake with Coin-age-based or Random block selection.
- The chosen validator verifies the transactions and publishes the block for the network to verify. The stake is locked, and the validator is not rewarded unless the network confirms the block’s validity.
- After establishing the block’s validity, the validator gets the stake back and the reward fee as a cryptocurrency. Suppose the algorithm uses a coin age-based mechanism to select validators. In that case, the coinage resets to 0, and the validator gets a low priority for the next validator selection.
- If other nodes do not verify the block on the network, the validator loses its stake, and the algorithm marks it as ‘bad.’ The process again starts from step 1 to forge the new block.
The addition of a new block takes place with achieving this consensus in the blockchain by the above steps.
Summary
In this article, we discussed the consensus mechanism, which is a mechanism to establish a verification process in a trustless network. A consensus mechanism is a mathematical algorithm verifying blockchain transactions and blocks. In detail, we discussed two prominent consensus mechanisms, Proof-of-Work and Proof-of-Stake, and established a step-by-step process in the blockchain network to establish the consensus.
Tavish lives in Hyderabad, India, and works as a result-oriented data scientist specializing in improving the major key performance business indicators.
He understands how data can be used for business excellence and is a focused learner who enjoys sharing knowledge.