How blockchain works!
If you are new to the field of information technology, taking our Introduction to Information Technology and its Career Roadmaps and Introduction to Cloud Technology with DevOps, Docker and Kubernetes (free self-paced) courses is highly recommended. Also, for a comprehensive coverage of blockchain concepts and its use cases, taking our Introduction to Blockchain Technology course is highly recommended.
In our previous article (The role and types of consensus mechanism in blockchain), we learn about the role and types of consensus algorithms that are widely used in the blockchain applications.
In this article, we discuss how Proof of Stake or PoS consensus algorithm works in blockchain.
Blockchain technology is gaining tremendous popularity among developers and cloud architects as its use cases are growing among enterprise applications. Here is a great article for a compressive review of blockchain technology use cases in many industries. Currently there are 3 most popular blockchain platforms: Ethereum, Hyperledger, and Corda. Hyperledger is becoming choice number 1 among private enterprise blockchains. Also, Hyperledger Family consists of many projects (like Hyperledger Fabric), libraries and tools. Regardless of choice of platform, every blockchain have common components like smart contracts, consensus algorithm, etc.
A fundamental problem in large-scale distributed systems is how to achieve overall system reliability in the presence of failures. Systems need to be fault-tolerant. This requires a process for distributed, often heterogeneous systems to reach a consensus and agree on the network state, whether it is a database commit or an action to take. The following 3 consensus algorithms are widely used in the blockchain systems:
In this article, we start off by briefly explaining what a consensus is and then we will discuss what the Proof of Work algorithm is and how it works. We move on covering more topics like how Proof of Work was invented to solve double-spend issues and what are main advantages and disadvantages of using Proof of Work.
Consensus in a blockchain is the process by which a network of mutually distrusted nodes reaches an agreement on the global state of the chain of blocks. In blockchain, transactions or data are shared and distributed across the network. Every node has the same copy of the blockchain data. Consensus allows all of the network nodes to follow the same rules to validate transactions and add new blocks to the chain, and therefore allows it to maintain uniformity in all of the copies of a blockchain.
Proof-of-work, also referred to as PoW, is the most popular consensus algorithm used by blockchain and cryptocurrencies such as Bitcoin and Ethereum, each one with its own differences.
In general, PoW is like a race between miners to solve a cryptographic puzzle; upon solving the puzzle, they win the chance to add the block to the chain and get rewarded. As shown in the following screenshot, miners collect all pending transactions from the decentralized network and compete with each other to solve the puzzle. Whoever solves the puzzle will generate a block and push that block into the network for verification from other nodes, after which, the other nodes can add that block to their own copy of the blockchain:
The cryptographic puzzle that miners race to solve is identifying the value of the nonce. A nonce is an attribute in the block header structure. In the beginning, each miner guesses a number to start with, checking whether the resulting hash value is less than the blockchain specific target. Bitcoin uses the SHA-256 algorithm for this. SHA-256 outputs a fixed-length number. Every number between 0 to 232 has the same chance to solve the puzzle, therefore a practical approach is to loop through from 0 to 232 until a number can meet the criteria, as shown in the following diagram:
Once a miner finds the nonce, the results, including the previous block's hash value; the collection of transactions; the Merkle root of all transactions in the block; and the nonce are broadcasted to the network for verification. Upon being notified, the other nodes from the network automatically check whether the results are valid. If the results are valid, they add the block to their copies of the blockchain, stop the mining work in hand, and move on to the next block.
A target is a blockchain-specific 256-bit number that the network sets up for all miners. The SHA-256 hash of a block's header—the nonce plus the rest of the block header—must be lower than or equal to the current target for the block to be accepted by the network.
The difficulty of a cryptographic puzzle depends on the number of leading zeros in the target. The lower the target, the more difficult it is to generate a block. Adding leading zeros in the target number will increase the difficulty of finding such a nonce exponentially. As you can imagine, the higher the difficulty setting, the more difficult it will be to evaluate the nonce. Adding one leading zero in the target will reduce by 50% the chance of finding the nonce. The difficulty is decided by the blockchain network itself. The basic rule of thumb is to set the difficulty proportionally to the total effort on the network. If the number of miner nodes doubles, the difficulty will also double. The difficulty is periodically adjusted to keep the block time around the target time. In Bitcoin, it is 10 minutes.
The winner of the cryptographic puzzle needs to expend huge energy and crucial CPU time to find the nonce and win the chance to create new blocks in the blockchain. The reward for such actions depends on the blockchain itself. In a Bitcoin blockchain, the winner will be rewarded with Bitcoin, the cryptocurrency in the Bitcoin blockchain.
The PoW consensus is a simple and yet reliable mechanism to maintain the state of the blockchain. It is simple to implement. It is a democratic lottery-based system that lets you participate in the game of mining and get the rewards, where every node can join and higher CPU power may not translate into higher rewards. Currently, the winning miner is rewarded with 12.5 BTC for each block created in the Bitcoin blockchain.
Satoshi's original intention in using a PoW mechanism is to solve double-spend issues and ensure the integrity of the global state of the Bitcoin blockchain network. Let's say Alice sends 10 BTC to Bob, and at the same time or later on she pays Catherine the same 10 BTC. We could end up with the following three situations:
However, there are a few drawbacks with the PoW algorithm, thanks to the economic cost of maintaining the blockchain network safety:
On the flip side, it requires huge computing power and electricity to take over the PoW- based blockchain. Therefore, PoW is perceived as an effective way to prevent Denial-of-Service (DoS) and Distributed Denial-of-Service(DDoS) attacks on the blockchain.
By now, you have a better understanding of why consensus algorithms play a very vital role in the operation of a blockchain system. In short, in this article we explained what a consensus and the Proof of Work algorithm are. We also covered the nitty gritty of Proof of Work for adding a new block of data to the blockchain network. At the end, we discussed other topics like how Proof of Work was invented to solve double-spend issues and what are main advantages and disadvantages of using Proof of Work.
In the next article, we discuss how Proof of Stake consensus algorithm works in blockchain.
This article is written in collaboration with Brian Wu who is a leading author of “Learn Ethereum: Build your own decentralized applications with Ethereum and smart contracts” book. He has written 7 books on blockchain development.
Here is the list of our free webinars that are highly recommended:
Here is the list of our 10 free self-paced courses that are highly recommended:
If you like to learn more about Hyperledger Fabric, Hyperledger Sawtooth, Ethereum or Corda, taking the following self-paced classes is highly recommended:
If you want to master Hyperledger Fabric, Ethereum or Corda, taking the following live classes is highly recommended:
If you like to learn more about blockchain technology and how it works, reading the following articles is highly recommended:
If you like to learn more about blockchain development in Ethereum with Solidity, reading the following articles and tutorials is highly recommended:
If you like to learn more about blockchain development with Hyperledger, reading the following articles and tutorials is highly recommended:
If you like to learn more about blockchain development on Corda , reading the following articles and tutorials is highly recommended:
If you like to learn more about blockchain development in other platforms, reading the following articles and tutorials is highly recommended:
We offer private custom tutoring classes both online and in DC, MD and VA for almost all of our courses or bootcamps. Give us a call or email us to discuss your needs.
$90 Regular
$50 Limited Offer
REGISTER NOW