What is Proof-of-Work?

Proof-of-Work is a method of adding a new block with transactions to a blockchain. Doing the work means calculating a hash value for that new block by following certain rules. Learn more below!

Home » What is Proof-of-Work?

In this article, we will delve into a bit more detail on what Proof of Work is, and how consensus is reached and maintained in a blockchain. The outline of this article is as follows:

  1. Can data in a block be tampered with?
  2. Introducing the Nonce
  3. How does the Proof-of-Work work? 
  4. Maintaining consensus in the blockchain
  5. Conclusion

Can data in a block be tampered with?

We already examined how blockchain works in a previous article, where we explained that the blocks in a blockchain are linked via cryptographic hashes. Each block contains its own block hash, as well as the block hash of the preceding block in the blockchain.

Blocks of a blockchain linked by hash keys
Blocks in a blockchain are linked by hashes

If a transaction in a block is maliciously tampered with, then that block’s hash would no longer be valid

So if a block in a blockchain is changed in any way, then that block would fail verification. But not only that, the entire blockchain would actually fail verification, since the hashes via which the blocks are linked would no longer match. Remember that a block’s hash is calculated on the combination of that block’s transactions, the hash of the previous block and some other data points. If any of these data points is even slightly changed, the block hash is completely different).

But the question arises, couldn’t one just recalculate the hashes of all the blocks in the blockchain, and thus have a correctly linked chain of blocks and get away with the data tampering?

The answer is unfortunately yes. Even after transactions are placed into a block and the block hash is calculated, it’s still possible for someone to modify a transaction in the block, and then recalculate the hash of that block, as well as the block hashes for all other blocks of the chain. It’s just very inefficient to do so due to the very large number of blocks in a blockchain, which go up to hundreds of thousands of blocks in certain blockchains out there.

Introducing the Nonce

In order to further make it infeasible to change the data in the blockchain, the blockchain is extended to include a new piece of data in the hash calculation process. This new piece of data is called a nonce. The nonce is nothing more than a value that starts at 0 and is incremented until, when combined with the rest of the block data, forms a hash that has a predetermined set of zeros at the beginning. 

This process is known as “solving the hash puzzle” or “solving the Proof of Work”. The goal is to bring the block hash value to start with the predetermined number of 0s, with the help of an incrementing nonce. Once this hash puzzle has been solved, we then link the blocks together in precisely the same way as before. 

So the Proof of Work makes it difficult and expensive to calculate a block hash. Because you don’t just calculate the hash according to a well-known algorithm. Rather, you redo the hash calculation process over and over again by incrementing the nonce, until a satisfactory hash value is obtained.

On the other hand, the Proof of Work makes it very easy to verify if that hash is actually correct. We calculate the hash only once, by including the current value of the nonce available in that block. If we do obtain a hash that has the correct number of zeros in front, we have successfully verified that block.

So what makes a good Proof of Work in the case of blockchain is something that makes hash calculation difficult (so that you can’t easily tamper with the data), but hash verification easy (so that it can be easily verified that no one has tampered with the data).

Let’s illustrate this with an example. If calculating the correct block hash takes, say, 15 minutes for one block with the Proof of Work, then once that block is added to the chain, verification that the hash is correct would be very quick.

So if anyone changes some data in a block in the blockchain, they’d then have to recalculate the proof of work (or “solve the hashing puzzle”) for that block and any blocks after it. So if there are 5000 blocks, and it takes 15 minutes to calculate the hashing puzzle (Proof-of-Work) for each block, then it would take 1250 hours of effort, or 52 days. for the hashes of all blocks to be recalculated. 

How exactly does the Proof-of-Work work? 

Let’s again revisit the nonce. The idea is actually very straightforward. This new piece of data, which is essentially a number, is added to a block. The goal is that the hash of the block (which now includes the nonce, along with the rest of the data points included in the hash calculation) starts with a predetermined number of the zero digit, as depicted below: 

Two hash values
A hash value before and after Proof of Work

Initially, the nonce starts at 0. Then the SHA-256 hash is calculated on the block. If the resulting hash doesn’t start with the desired number of 0s, then add 1 to the nonce, and re-calculate the hash. If the hash still doesn’t start with the desired number of 0s, repeat the process again.

Keep on going by increasing the nonce by 1 at a time, until a hash with the predetermined number of 0s at the beginning is obtained. This process of calculating the hash puzzle is also known as mining the block.

Once the Proof-of-Work is solved by a node and a new block is mined (added to the blockchain), the node receives a reward, usually paid in cryptocurrency. This is the incentive for the nodes to spend the time and computing resources to do the Proof-of-Work and add a new block.

A block and its constituent parts, including the nonce
The Nonce value in a block

Maintaining consensus in a blockchain

Each node of the blockchain works in two states:

  1. Verifying blocks that were created by other nodes, or
  2. Working to create a new block, for which it must solve the hashing puzzle (i.e., do the Proof-of-Work).

These blockchain nodes are also known as miners, or powerful computers because they try to create new blocks in the chain in order to earn a reward.

At any moment, there are multiple miners trying to solve the hashing puzzle (do the Proof-of-Work) to create the next node. So it may happen that in a given moment, more than one node successfully solves the hashing puzzle and thus creates a new node. Any new node must be appended to the last node of the blockchain, and then this new version of the chain must be transmitted to all participating nodes. But due to network latencies, t may happen that different nodes end up having different versions of the blockchain.

Let’s look at this through an example, by starting with the case where all nodes of the network agree on what the blockchain looks like, i.e. there exists a consensus:

Three consecutive blocks in a blockchain
The initial structure of the blockchain

The initial blockchain pictured above contains three blocks: blocks one, two and three. Each numbered box represents a block, even though the block structure is more complicated, as we have seen in the previous sections of this article. 

Now, let’s imagine a node in the network has finally solved the hashing puzzle and has successfully created a new node in the blockchain, represented with the newly added box:

The blockchain after the addition of a new node

This would be a best case scenario where only one node wins the race to solve the hashing puzzle and create a new block. So again, all of the nodes have a consensus on the blockchain. 

But what happens if the winning block (Block 4) is delayed getting through to all the other nodes of the blockchain? And during this delay, a second node solves the hashing puzzle, and starts distributing the new block (Block 5) to the other peer nodes? Once this happen and the blocks finish propagating, we would end up with the following blockchain:

The blockchain now has a fork

The blockchain now has a fork where both Block 4 and Block 5 are linked to Block 3. So it looks more like a block tree than a block chain.

And now consensus cannot be reached, since both branches have the same length so the notion of “longest chain” fails to resolve the issue. In this case, a node can decide for itself which branch to extend. It can extend both blocks 4 and 5; both would be valid decisions.

So again the nodes go off and all try to create new blocks. Let’s say that at around the same time, again two nodes solve the hashing puzzle and broadcast their winning blocks (Block 6 and Block 7). Let’s assume now both of the nodes build their block on top of Block 4, and we have the following situation:

A new fork is introduced in the blockchain

Now we can rule out chain 1, 2, 3, 5, as it is a shorter chain. But we still do not have a consensus on the remaining blocks, as both chains 1, 2, 3, 4 and 6, as well as 1, 2, 3, 4 and 7 have the same lengths. The nodes continue to race on the creation of the next block, which they can either construct by extending Block 6, or Block 7. 

Eventually one of the nodes wins the race and sends the created block (Block 8) across all the blockchain nodes. This winning block, Block 8 was constructed on top of Block 7, which means that finally we can apply the longest chain principle and reach consensus. So the consensus is that the blockchain is constructed of the following blocks: 1, 2, 3, 4, 7 and 8, as this is the longest chain. 

The final blockchain structure

Conceptually this is not a difficult algorithm. We just need to continuously reevaluate the chains to find the longest chain, and with time eventual consistency across all nodes is reached. 

Now let’s talk about what happens with the eliminated blocks, blocks 5 and 6, also called as orphaned blocks. They contained actual transactions, which of course can’t just be eliminated or removed from the blockchain. They are instead placed back into the pool of transactions and they are given the opportunity to be mined again as part of another block. 

Because of this, some transactions that once appeared as if they are already in the blockchain, suddenly seem to disappear for a while, until they are added again as part of a new block. So the system is eventually consistent. Once transactions are placed in a node and that node is working on solving the hashing puzzle, there is no guarantee that they will be part of the blockchain – even if the node is the winner. This is due to the fact that there still exists a possibility for the winning node to end up as an orphan due to a fork in the blockchain. But the transaction will make it to the block eventually.

The miners that successfully solve the hashing or proof of work puzzle get rewarded for the time and  computational power spent. However, if that block is subsequently orphaned from the blockchain, the mining reward gets reclaimed. But the miners continue to have a chance at trying to mine the next block.

Conclusion

Proof of work is a mechanism present in some types of blockchains that requires miners (nodes in the blockchain network) to solve a mathematical “puzzle” before they can add a new block in the blockchain. The node or miner that successfully solves the mining puzzle wins a certain amount of money for their computational effort. This reward is usually paid in cryptocurrency money. 

The nonce is an additional piece of data added to a block. Its goal is to bring the hash of that block to a value that starts with a predetermined number of zeros. So a miner that is trying to solve the hashing puzzle (i.e., is doing the proof of work) essentially increments the nonce by one, until the hash calculated on the block forms a value that starts with a predetermined number of 0s in front.

The nonce value starts at 0, and is incremented by 1 until the hash reaches a value with the predetermined number of 0s. For example, if the hash of the block initially has the value EYEd2Nqkf57Cna112rk9UCprZtCrhiT3QcRhS1BQJHA, it’s nonce value is repeatedly incremented by 1 until the block hash reaches a value of 0000032c57M3MYWkrnzHNV+efN7SJULrrnzRgDrXXQ (assuming that the predetermined number of 0s is 5). This process of repeatedly calculating the hash of the block until it reaches the value with the predetermined number of 0s in front is what constitutes the process of Proof of Work.

Since every node in the blockchain network keeps a copy of the blockchain, consensus between these copies must be ensured. As new blocks are added to the blockchain and these changes are propagated onto the blockchain, it might happen temporarily that different nodes contain slightly different versions of the blockchain. However, by continuously following the rule of the “largest chain is the valid one”, consistency is eventually reached across all nodes, as explained via the illustrations in the article.

Frequently Asked Questions:

What is Proof-of-Work?

Proof of Work is a consensus mechanism that requires a miner to solve a mathematical “puzzle” in order to be able to add a new block in the blockchain. The winner node that is successful in solving this puzzle earns a reward that is usually paid in cryptocurrency.

What is a nonce?

The nonce is a value added to a block that starts at 0 and is incremented by 1 until the calculated hash of that block contains a predetermined number of 0s in front.

How is consensus maintained in the blockchain?

Each node in the blockchain contains a copy of the complete blockchain. As new blocks are added to the blockchain, these changes are propagated to all the nodes of the blockchain. If temporarily two nodes contain different versions of the blockchain, these differences are resolved by following the “largest chain is the valid one” rule.

bitcoinBitcoin
$ 97,434.115.3%
ethereumEthereum
$ 3,121.150.39%
tetherTether
$ 1.000.04%
solanaSolana
$ 238.631.56%
bnbBNB
$ 610.440.3%
xrpXRP
$ 1.112.17%
dogecoinDogecoin
$ 0.3846760.98%
usd-coinUSDC
$ 1.000.04%
staked-etherLido Staked Ether
$ 3,115.960.22%
cardanoCardano
$ 0.7871380.45%