How Proof of Work is used to establish consensus in blockchain
In the previous article, we saw how we use a puzzle to achieve consensus. In this article, we will see how we use Proof of Work as the puzzle and how it works.
As we have already seen, to create an ordered list of transactions, we have to hash a transaction and refer to that hash in the following transaction. This way, we end up creating a chain of transactions using hashes. We will be using these hashes to create the puzzle. Let’s see how.
Proof of Work
We assign a target hash value for all transactions. The hash of a transaction has to be less than or equal to the target hash value. Remember, a hash produces a 256-bit output. So, the maximum value a hash output can take is 2256-1 = 5.7896045e+76. The target value will be less than this.
Now, what if the output hash value of our transaction is greater than the target? Enter nonce. A nonce is a random number added to the transaction to change the output hash. Therefore, the goal is to obtain a hash value less than or equal to the target by adjusting the nonce. Since there is no way by which we can calculate the nonce that can produce the right hash value, we have to find the right nonce for a transaction through trial and error. So, this is the puzzle that nodes have to crack to have their transactions accepted. We call this puzzle Proof of Work because producing the right hash serves as proof that you have worked to solve the puzzle.
We now know how the puzzle, which we call Proof of Work, works. Let’s now see how we can adjust the difficulty of Proof of Work. Later, we shall see why we need to adjust the difficulty.
We adjust the difficulty of Proof of Work by adjusting the target hash value. When the target value is closer to the maximum possible value, we will have a higher chance of finding a valid hash. To give your a better picture, let’s take a simple example. Let’s say the maximum possible output is 100. Now, the target value should be less than or equal to this value. Let’s set the target to 100 first. Now, the number I generate will be anywhere between 0 and 100. Since my target is 100, I have a 100% chance of getting the output correct.
Now, let’s lower our target to 80. Even though the target has been reduced, still the number I generate would be between 0 and 100 since when it comes to hashing, there is no way I can control the output. Now, I will have an 80% chance of getting it right.
As you can see, the lower the target is, the lower the chance of producing a valid output. In other words, the lower the target, the higher the difficulty.
Why do we need to adjust the difficulty of Proof of Work?
Now, why do we need to adjust the difficulty? Remember, our goal is to make sure that the tcreate value is long enough. We use Proof of Work to ensure that nodes take tcreate time to create a transaction. In Bitcoin’s case, this time is approximately 10 minutes. In reality, Bitcoin takes 10 minutes to produce a block and transactions are much faster. We shall see about blocks later but for the time being let’s assume it takes 10 minutes to produce a transaction. So, the target value should be such that it takes 10 minutes to produce the next transaction.
But as more people join the blockchain network, the time taken to solve Proof of Work decreases. For example, let’s say the current computers in the network can all produce 100 hashes per second combined. We call this the hash rate. At this hash rate, let’s assume it takes 10 minutes to find a hash lower than or equal to the target. Now, let’s assume a few more machines join the network, or the existing machines become more powerful that the hash rate goes up to 1000. When 1000 hashes are produced in a second, the network will be able to find the right hash value quicker.
The more the number of guesses the faster the solution
To give you a better understanding, let’s take our previous example. We shall assume that the target value is 50 and that we have the ability to produce 10 numbers in a second. At this rate, let’s assume, it takes 2 seconds to find the right value. Now, let’s get a friend of ours to produce numbers along with us, increasing the numbers we produce to 20 per second. Yes, there is a good chance we may both produce the same number but still, the number of unique numbers we produce will be higher. As the number of guesses that we make per second now has gone up, we will be able to guess the right number a lot quicker.
Thus, when the hash rate of the network increases, the time taken to crack Proof of Work goes down. This means the difficulty of Proof of Work has to be increased. We increase the difficulty by reducing the target value because, as we saw earlier, when we reduce the target, the difficulty goes up. So, by adjusting the target value, we ensure that the time taken to produce a transaction remains fairly constant.
Now that we understand how Proof of Work works, let’s try to understand how the target is adjusted to keep the time taken to create a transaction almost constant in the next article.
[…] looked at the Proof-of-Work algorithm in the previous article, and in this article, we shall look at how we adjust the target hash based on the difficulty value […]