By Matt Zand, in collaboration with Brian Wu
Blockchain technology is gaining tremendous popularity among developers and cloud architects as its use cases are growing among enterprise applications. Currently, the top 3 most popular blockchain platforms include: Ethereum, Hyperledger, and Corda. Hyperledger is slowly becoming the number one choice among private enterprise blockchains. Also, Hyperledger Family consists of many projects (like Hyperledger Fabric), libraries and tools. Regardless of the choice of platform, every blockchain will have common components like smart contracts, consensus algorithms, and just to name a few.
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 blockchain systems:
- Proof of Work (used by Bitcoin and Ethereum)
- Proof of Stake (used by Ethereum)
- Byzantine Fault Tolerance (used by Hyperledger)
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. We won’t be focusing on the technologies behind blockchain and its use cases in this article, but you can check out this article if you are interested to learn more.
What Is a Consensus?
A 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
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.
How PoW Works
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 2³² has the same chance to solve the puzzle, therefore a practical approach is to loop through from 0 to 2³² 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.
Targets and Difficulty
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.
Incentives and Rewards
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:
- The first transaction goes through the PoW and is added to the blockchain when the second transaction is submitted. In this case, the second one will be rejected when miners pull it from the transaction pool and validate it against all parent blocks.
- Both transactions are submitted simultaneously and both go into the unconfirmed pool of transactions. In this case, only the first transaction gets a confirmation and will be added in the next block. Her second transaction will not be confirmed as per validation rules.
- Both get confirmed and are added into competing blocks. This happens when miners take both transactions from the pool and put them into competing blocks. The competing blocks form a temporary fork on the blockchain. Whichever transaction gets into the longest chain will be considered valid and spent, and the other one within the block on the short chain will be recycled. When it is reprocessed, it will be rejected since it is already spent. In this case, it may take a few blocks to get the other one recognized as the double-spent one.
Advantages and Disadvantages of PoW
However, there are a few drawbacks with the PoW algorithm, thanks to the economic cost of maintaining the blockchain network safety:
- Energy consumption: PoW consensus, which uses a network of powerful computers to secure the network, is extremely expensive and energy-intensive. Miners need to use specialized hardware with high computing capacity in order to perform mining and get rewards. A large amount of electricity is required to run these mining nodes continuously. Some people also claim these cryptographic hash calculations are useless as they can’t produce any business value. At the end of 2018, the Bitcoin network across the Globe used more power than Denmark.
- Vulnerability: PoW consensus is vulnerable to 51% attacks, which means, in theory, dishonest miners could gain a majority of hashing power and manipulate the blockchain to their advantage.
- Centralization: Winning a mining game requires specified and expensive hardware, typically an ASIC type of machine. Expenses grow unmanageable, and mining becomes possible only for a small number of sophisticated miners. The consequence of this is a gradual increase in the centralization of the system, as it becomes a game of riches.
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.
As briefly discussed in the previous section, one of major barriers for widespread adoption of blockchain technology is its intensive use of resources. Luckily, the rapid development of Blockchain as a Service (BaaS) poses an ideal compromise to the problem at hand and Alibaba cloud Blockchain-as-a-Service (BaaS) is one of the solution.
Alibaba Cloud BaaS is an enterprise-level PaaS (Platform as a Service) based on leading blockchain technologies, and is powered by Ant Financial Blockchain Team. This service helps you build a stable, secure blockchain environment, and manage the deployment, operation, maintenance, and development of blockchains easily and that’s will enables business companies and users to focus on business innovation. It plans to address the issues faced by sectors such as healthcare, retail, finance, pharmaceutical, insurance, logistics, and manufacturing. As an add-on feature to secure the network, Alibaba’s BaaS uses chip encryption technology.
Currently BaaS supports Linux Foundation Hyperledger Fabric, Ant Blockchain technologies from Ant Financial, and Enterprise Ethereum of Quorum by J.P. Morgan.
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.
About the Author
Matt Zand is a serial entrepreneur and the founder of 3 tech startups: DC Web Makers, Coding Bootcamps and High School Technology Services. He is a leading author of Hands-on Smart Contract Development with Hyperledger Fabric book by O’Reilly Media. He has written more than 100 technical articles and tutorials on blockchain development for Hyperledger, Ethereum and Corda R3 platforms. At DC Web Makers, he leads a team of blockchain experts for consulting and deploying enterprise decentralized applications. As chief architect, he has designed and developed blockchain courses and training programs for Coding Bootcamps. He has a master’s degree in business management from the University of Maryland. Prior to blockchain development and consulting, he worked as senior web and mobile App developer and consultant, angel investor, business advisor for a few startup companies. You can connect with him on LI: https://www.linkedin.com/in/matt-zand-64047871
This article is written in collaboration with Brian Wu.