Consensus is the state of reaching a general agreement among a group of people. For example, if you’re going to paint your house, there will be different opinions regarding the color of the paint, but you can only paint one color, so there must be a generally agreed-upon color among your family members. This general agreement is known as “consensus.”
Likewise, in a blockchain, the majority of the nodes in the network must agree upon a particular state of the blockchain in order for the network to run smoothly. A blockchain contains different layers like the Application layer, Consensus layer, Data layer, Network layer, etc.
Consensus on a blockchain can be achieved through the use of various mechanisms known as consensus algorithms or consensus mechanisms.
Types of consensus mechanisms
There are several types of consensus algorithms used today, each with its own pros and cons. The most popular ones are:
- Proof-of-Work (PoW)
- Proof-of-Stake (PoS)
We’ve already written a non-detailed article on the top 5 consensus algorithms, but in this article, we dive a little deeper into PoW and PoS.
Proof-of-Work (PoW)
PoW is probably the most popular consensus mechanism out there. Popularly known for its usage in Bitcoin and previously in Ethereum. It is also one of the most secure and decentralized consensus algorithms.
Proof-of-Work is the process by which one node proves to the other nodes that a certain amount of computation power was used to complete a task. So, the miner needs to expend a specific amount of computational power to solve the puzzle and mine the block. The puzzle is similar to guessing a random number, and all miners compete to find it; once a miner finds the number, they get to mine the new block on the blockchain, while other miners simply verify the newly mined block.
The number they need to find can vary from 1 to 2^256 (in the case of Bitcoin) according to the mining difficulty of the network. Mining difficulty can vary depending on various factors like the number of miners, total computational power, etc.
More miners/more computational power = Higher mining difficulty.
Similarly, fewer miners/less computational power = Lower mining difficulty.
In Bitcoin, this number is generated by hashing the header of the block being added with the hash function SHA-256. The header contains important information that is used to secure the block. The miners alter this number every time they try to guess it. If the hash number is greater than the difficulty target, the process is repeated until a number less than the value is found. The greater the target value, the greater the likelihood of correctly guessing the number, and vice versa.
The process is more like a “trial-and-error” method. Miners with more computational power have a better chance of correctly guessing the number and thus mining the block because they can generate more hashes per given time and try it.
This process of miners guessing the right number takes up to 10 minutes per block in the Bitcoin network, so in Bitcoin, a new block is mined every 10 minutes. The time can vary according to the network. For example, Dogecoin takes only 1 minute to mine a block, Monero takes 2 minutes, and Litecoin takes 2.5 minutes.
The miners are usually paid for the work they did in the form of the native crypto of the network they mined for, which is $BTC in Bitcoin. The main source of income for the miners is from the block rewards issued for mining each block. For example, miners in the Bitcoin network get paid 6.25 $BTC per block, this 6.25 $BTC goes to the miner who solved the corresponding puzzle and mined the block. This reward also varies depending on the blockchain – Dogecoin pays 10,000 $DOGE per block, Litecoin pays 12.5 $LTC per block, Monero pays 1.26 $XMR per block, etc.
This reward system also plays a major role in keeping the chain secure and decentralized, as only people validating truthfully will receive a reward for their efforts. If nodes attempt to attack the blockchain, tamper with data, or engage in double-spending, they will not receive the reward and will be wasting resources.
The main two factors affecting mining are:
- Hash rate: The number of hashes generated by all the miners per second is known as the “hash rate” of that particular blockchain.
- Difficulty: The hardness of finding a hash is known as difficulty. In Bitcoin, the difficulty is adjusted after every 2,016 blocks. An average of 2,016 blocks needs to be mined in a time window of 2 weeks.
After the 2 weeks, if the number of blocks mined in 2 weeks > 2,016, the difficulty will be increased and vice versa.
So, in short, the actual process of mining looks like this:
- The miner will generate the block header and convert it into a data string.
- The miner adds a nonce (a random 32-bit number used by miners as a base for their hash calculations) to the data string and inputs it into the hashing algorithm (SHA-256 in Bitcoin).
- Get the resulting hash & execute a test to see if the condition, i.e. the target hash, is satisfied.
- If the miner gets the hash correctly, he/she gets to mine the block or if they got the hash wrong, they repeat the same process with a different nonce until the correct hash is found.
The above steps are typically repeated billions of times before the miner gets the right hash.
Pros of PoW:
- More secure
- More decentralized
- Prevents constant forking
Cons of PoW:
- Slow transaction speed
- Higher transaction fees
- High energy usage
Proof-of-Stake
PoS is one of the most widely used consensus mechanisms, famous for its low energy consumption, low entry barrier for validators, etc. PoS is the most preferred consensus mechanism out there for the same reasons. Last year, the second biggest blockchain after Bitcoin, Ethereum, switched over to PoS from PoW.
In PoS, the miners are known as “validators”. The validators don’t need expensive hardware equipment or large energy requirements, as opposed to PoW. Any node that needs to be a validator needs to stake a particular amount of the native crypto of the network, which varies depending on the blockchain. For example, to be a validator in Ethereum, one needs to stake 32 $ETH. There are also chains with no minimum staking requirements, such as Cardano.
The working of PoS is completely different when compared to PoW:
- Validators need to put up a security deposit beforehand (stake) as opposed to PoW.
- In PoW, miners compete with each other to find the right hash and mine the block but in PoS, each time a block needs to be validated, a random validator is chosen from the network to validate the block.
- If the validator validates blocks truthfully, they will be rewarded or else they’ll lose a part of their stake or even the full stake, which will greatly reduce foul plays from validators.
In PoS, the validators are not always selected randomly; the selection process of validators may vary across different networks. For example, Ethereum selects validators completely randomly, but Algorand prefers validators with high stakes. So, the higher the stake, the greater your chances of being chosen as a validator in Algorand.
The validator who gets selected makes sure that all the transactions in that particular block are valid and if everything checks out, the node approves the block and adds it to the blockchain, creating a new block.
The validators are rewarded with the transaction fees associated with the block they validated. So, as long as the validators’ stake is higher than the transaction fees earned from the block, we can be sure that the validator plays by the rules, or they will be losing their stake, which is higher, for a lower amount. No sane man does that. The process of draining the stake of validators who try to tamper with the block is known as “slashing”.
Validators who choose to withdraw their stake would need to wait a certain amount of time known as “cool-off time” before they withdraw their stake as the chains need to make sure that you have not played badly and get away with it. This varies according to the blockchain. For example, Cardano has no cool-off time, Tezos have a cool-off time of 23 days.
There are different variants of PoS that are being used today like Delegated Proof-of-stake (DPoS), Liquid Proof-of-stake (LPoS), Nominated Proof-of-stake (NPoS), Hybrid Proof-of-stake (HPoS) etc.
Pros of PoS:
- Low energy consumption. To put that into perspective, Ethereum reduced its energy consumption by 99.98% after switching PoS from PoW.
- Lower chances of a 51% attack as the attacker would need to own 51% supply of that particular crypto to attack.
- Doesn’t require powerful hardware equipment.
Cons of PoS:
- May lead to centralization due to staking pools.
- Impermanent loss due to the lock-up period.
Conclusion
Consensus is a critical element of a blockchain network and can be achieved through various consensus algorithms, such as Proof-of-Work (PoW) and Proof-of-Stake (PoS). The most widely used consensus algorithm, PoW, is a secure and decentralized system that compels miners to use their computing power to solve a puzzle in order to create new blocks. Contrarily, PoS is a more energy-efficient consensus algorithm that doesn’t require miners to use their computing power but rather relies on validators holding a set quantity of cryptocurrency to take part in the consensus process. The choice of consensus algorithm depends on the particular needs of the blockchain network, even if both PoW and PoS have advantages and disadvantages.