We briefly discussed in earlier section different consensus algorithms like Proof-of-Work (PoW), Proof-of-Stake (PoS) and Practical Byzantine Fault Tolerance (PBFT). Ethereum currently uses the PoW algorithm and the plan is to move to PoS in a later release. Let’s understand the PoW consensus algorithm in more detail and what problem it is trying to solve in a decentralized public network.
Ethereum blockchain uses permissionless consensus. Permissionless here means that any node can join and participate in the network and execute or validate the transactions. Out of all the public nodes in the network, majority of the nodes, say 51% out of 100 must agree on the validity of the transaction to arrive at a consensus. Once consensus is reached, the transaction is committed to Ethereum. Now, in the absence of a central governing authority, how can one assure that 51% of these nodes are genuine and no one has added or manipulated nodes to drive transaction in their favor. That’s where the PoW algorithm is applied to drive consensus throughout the network.
The following points explains the PoW algorithm in simple terms
- In a PoW based Ethereum network, each node needs to solve a mathematical puzzle to propose their transaction or to be more precise, an intent to create and commit the block. To solve this mathematical puzzle, it requires lot of computation, power (energy) and time in terms of CPU resources. The nodes solving this puzzle are referred to as miners.
- The mathematical puzzle is all about finding a message digest based on input message + the digest of the previous block.
Note – A message digest is a cryptographic hash function containing a set of digits created by a one-way hashing formula. Digest basically ensures data is not altered and the integrity of the message is maintained between the sender and receiver.
- The mathematical puzzle should be less than the difficulty level of the system. The difficulty level is an arithmetically derived number set by the system. For more details, refer to the example below.
- Once the puzzle is solved, all other nodes should agree to the solution and apply the same formula to arrive at a consensus.
- The node that is the first to solve the mathematical puzzle, and eventually verified and agreed by all other nodes is rewarded for its work. In the Ethereum network, this would imply a node being rewarded with some ethers for solving the puzzle.
Given below is an example of PoW algorithm.
Let’s assume there is a chain of 2 blocks already committed on the network. A new block 3 needs to be added to the existing chain. Let’s understand the sequence of flow as to how this third block of transaction would be committed:
Every node in the network will take up the challenge of creating the third block and committing the same to the blockchain. For this to achieve, each of the nodes will try to solve a mathematical equation. In the context of Ethereum, it is a digest value that needs to be created and it has to be less than some difficulty level preset by the system. The mathematical equation could be something like the following:
Problem to solve: Calculate digest (3) which should be < (less than) the difficulty level
The equation and its context:
Let’s assume the difficulty level is preset as 000000000000000….. 781. Now the digest (3) is calculated using hash algorithm (say sha256) and by creating the hash of the values: block 2, digest 2 and nonce
Note: Nonce can be any arbitrary value that is incremented every time the node makes an attempt to find the correct digest(3) value. In short, nonce is incremented to find a solution to the equation.
Overview of the solution:
Now, each node would start solving this equation by incrementing the nonce value. Let’s say B3 is the new block to be committed, B2 is the previous block i.e. block 2 and D is the difficulty level. If equation is solved the result is Yes, else it is No. The following snippet shows how a node tries to solve the equation:
- Iteration 1 – Sha256 (Block 3, Block 2, 1) < D Outcome – No
- Iteration 2 – Sha256 (Block 3, Block 2, 2) < D Outcome – No
- Iteration 2000008 – Sha256 (Block 3, Block 2, 4000008) < D Outcome – Yes
The miner node will keep trying until the outcome is Yes. It is like a rat race where every node attempts to be the first to get the outcome as ‘Yes’. As you can see from the above snippet, at nonce value 4000008, the equation is solved, i.e., the digest(3) value is less than the difficulty level. The node that solves this equation propagates the nonce value of 4000008 to all the other nodes in the network for verification. All other nodes would simply take this nonce value and verify if it is indeed less than the difficulty level. The other nodes just need one-step to verify this equation with least computing power.
Once verified, the block will get committed, i.e., chained to the previous block and the miner is rewarded with ethers for the work done.
Note – As you can see in above example, the blockchain is a chain of hashes, each linked to previous one, i.e., Digest of 3 contains Digest 2, Digest of 2 contains Digest 1 and so on. Adding a new block in between by an intruder implies changing the entire hash sequence till the very beginning, which seems virtually impossible.