Proof of work is the concept we come across when it comes to mining, and achieving a hash below the set target value. Without any ado, let’s jump onto the explanation of this naive algorithm to prove your work!

The code in the below Gist is written in JavaScript however, the basic workflow will be the same even if translated in other programming languages. I have shown a sample block of our blockchain which we are going to mine. The initial nonce( magic number ) value doesn’t really have any meaning as we would be changing it constantly to achieve the hash below the target. I have assumed the target to a hash with leading 4 zeros.

The new_proof is initialized with 1 as we would be traversing through all the positive integer nonce values one by one. The check_proof is set to false for which it will check the new_proof unless it is true.

We then are running an infinite loop until we find that nonce value for which we hit the target. Once we get the required hash, it is simply returned with respective nonce.

If you found this blog helpful, please leave some reviews in the contact section.