What are Consensus and its role in Blockchain
A consensus in a blockchain is a set of rules to be followed to maintain a blockchain.
Consensus used in blockchain to achieve an agreement on adding new data value in distributed servers or node in the network. Blockchain consensus plays a very important role in a blockchain, basically its purpose to achieve agreement among the nodes in the distributed network.
Blockchain consists of several blocks of information and it is distributed over the network. The server in the decentralized network which has a copy of blockchain called as a node. Each node in the network has the same copy of blockchain and they follow same consensus rules to validate and generate a new block. Each block in blockchain has information of many transactions.
Without consensus, blockchain is simply a way of storing encrypted/unencrypted data. Consensus allows it to decentralized because all the nodes in network follow the same rules and it will maintain uniformity in all the copies of a blockchain. So each and every change in a single blockchain is verified and adopt by another blockchain in the network.
Blockchain has distributed nature that means that in the network all the node has a copy of the same blockchain that makes blockchain immutable but at the same time to share the same kind of information all the nodes must have same agreement. Here to share the same kind of information means all the node must have the same copy of blockchain and when a new block gets added in a chain that block must be added in all the copies of the chain. In case of POW to add a new block all the nodes in the network try to solve complex cryptographic puzzles and the one which solves it first will add the new block and other nodes update their blockchain to adopt this change.
This agreement maintained by implementing the consensus algorithm. It enables all the node in the network for a common agreement that’s why consensus algorithm plays a very important role in the blockchain.
Popular Consensus Algorithms
Every Blockchain has there consensus algorithm. Some of them are as follow:
- Proof of Work
- Proof of Stake
- Delegated Proof of Stake
- Practical Byzantine fault-tolerant (PBFT) mechanism
- Proof of Identity
- Proof of Elapsed Time
- Proof of Importance
Proof of work (POW)
Proof of Work is one of the consensus algorithms. As the name suggests for validating a transaction a node should publicly prove that it did a certain amount of work. Node has to show proof of its work and they do it by solving a complex cryptographic puzzle.
Before going into details about POW, let's see some of the important terms used in POW. We can take Bitcoin blockchain as an example :
Block: A block consist of a set of transactions, nonce, timestamp, hash of the previous block, the answer of the puzzle(Nonce) and index value. Each block in blockchain is linked to its previous block.
Miners: In bitcoin blockchain POW is conducted through miners, miners are the people keeping the blockchain running by providing a huge amount of computing resources competing to solve a cryptographic puzzle and upon solving the puzzle they generate a block and they also get rewarded. Miners compete with each other to generate a valid block of transactions. Miners collect all pending transactions from the decentralized network then they guess a random number (nonce) to solve cryptographic puzzle, on successfully solving the puzzle they generate a block then they push that block into the network for verification from other nodes so that other nodes after verification can add that block in there copy of blockchain.
Nonce: The cryptographic puzzle that miners solve is to identify the value of nonce. A nonce is a random number which can be used only one time. Mostly it is a random number with combination of some data. Blockchain adds a value called nonce in each block. This nonce is like a salt added to the contents of a block. By adding nonce, the hash output of the contents of the block will change.
Hash: Hashing is a cryptographic technique which maps input data to data of a fixed size output. Bitcoin uses the SHA-256 algorithm for it. SHA-256 output a fixed length number. A slight change in input would change the complete output but the output would always of the same length.
In POW blockchain, all nodes validate transactions to create next block and only the first node that successfully computes it will add the new block. As soon as the other nodes in the network receive it, they will stop trying to build it and move on to the next block. Proof of work is costly and time-consuming to produce but easy for others to verify. The hash of the last recorded block in nonce isn’t known to anyone. The miner needs to find it, and that’s the cryptographic puzzle. The miner needs to try one number after another. The cryptographic puzzle is asymmetric that means puzzle is hard for the miners to solve but easy for the nodes to review.
Each block that is added to the blockchain must follow consensus rules if any block doesn't follow those rules will be rejected by the network nodes. Bitcoin uses Proof of Work to ensure miner has the correct copy of the blockchain and validated all transaction to be in sync with all other nodes in the network. Once a miner solves their proof of work puzzle, the results including the previous block’s address, the collection of transactions in the block, and the nonce are published to the network for verification. The other nodes from the network automatically check whether the results are valid. If the results are valid, the other nodes add the block to their copies of the blockchain.
The difficulty of cryptographic puzzle increase by adding leading zeroes. As a single change in the string of nonce completely change its output hash value so adding leading zeroes increases the difficulty of the puzzle. This difficulty is decided by the Blockchain network itself. As you can imagine, the higher the difficulty setting, the more difficult it will be to evaluate the nonce. There is no formula to calculate nonce. It is just a series of hit and trials. This evaluation of nonce to achieve a hash value of the block data is the "work" that the miners perform and the right output of the puzzle is the “proof”.
Advantages of POW
- A complete decentralization achieved by it.
- Nodes are easy to implement.
- A huge cost and computing power required to destroy the system.
- Protection from DDoS-attacks (Distributed Denial of Service)
Disadvantages of POW
- Waste of energy and resource as miners perform work on the creation of blocks that consume a lot of computing power and only one miner will get reward who resolve the puzzle first.
- For resolving a proof of work problem miner must have a computer with a lot of processing power and electricity with that other peripherals to maintain a moderate temperature which is expensive.
Where POW used
POW is used in many blockchain some of them are as follow:
- Bitcoins: Bitcoin is the first cryptocurrencey and widely used. In bitcoin each transaction gets recorded in a public ledger.
- Etherium: Ethereum is an open-source platform based on blockchain. It is a distributed blockchain network. Smart contract is one of the most popular feature of Etherium.
- Bitcoin Cash: Bitcoin Cash is also a kind of cryptocurrency. In Bitcoin cash block size is increased from 1 MB to 8 MB.
- Litecoin: Litecoin is a decentralized online cryptocurrency. It can be used to purchase services and products. It is a peer-to-peer cryptocurrency.
- Zcash: Zcash is one of the new privacy cryptocurrency. Its main goal is to provide enhanced privacy.
In short Poof of work is to show/prove that miners did the work and they will be allowed to create the new block and get rewarded. For mining miner required powerful hardware such as Graphics Processing Units (GPUs). The mining processes required a high amount of electricity. Rewards for mining is to ensure that a blockchain network keeps increase with tough competition to generate new blocks.