In this article, you will learn how to implement ‘Proof of Work’ to your blockchain.

Also, newbies if you’re wondering what we are talking about? I strongly recommend you to check Part 1 one of this article, where you will get a walkthrough on how to create a blockchain step by step.

Implementing Proof-of-Work [PoW]:

So what is Proof-of-Work? PoW is an agreement mechanism that prevents tampering and abuse on a blockchain by requiring a particular amount of computer power to solve a mathematical puzzle. In the crypto world, we call this technique as ‘mining’. Here the mining power is the key to prevent tampering and spam. So trying to tamper a blockchain does not make sense as it requires a lot of [hell lot] computing power that would cost someone millions.

Now, let’s start with implementing PoW on the newly built blockchain!

STEP 1: Changes that we are going to make in the Block

The first addition to the existing blockchain is ‘nonce’


this.nonce = 0;

So what’s the need for a ‘nonce’ variable here? Nonce variable is used to calculate the hash value inside the block.

There is something called difficulty in mining, which means PoW requires hash of a block that starts with the specific number of zero’s. I know it’s hard to understand now, but, in the end, you will get the point!

As of now, just remember including ‘nonce’ variable is to calculate zeros.

And also we included the nonce variable in calculatehash() function to calculate the hash.

STEP 2: Including mineblock() method inside our Block

Note: Remember we are implementing this method inside the class block.

Class Block: Contains all the data inside block. eg: Blockno, timestamp, previoushash etc.

This method helps to increment the nonce value until we get a valid hash. Here this block works according to the difficulty parameter. As the valid hash value depends on the difficulty.

If we throw it all together the block looks something like this:-


class Block{








      this.nonce = 0;




      return SHA256(this.index + this.previoushash + this.timestamp + JSON.stringify( + this.nonce).toString()



      while(this.hash.substring(0,difficulty) !== Array(difficulty + 1).join(“0”)){


          this.hash = this.calculatehash();


      console.log(“Block Mined: ” + this.hash);



STEP 3: Modification inside Blockchain

It’s time to play inside the blockchain. Let’s set difficulty = 4

Now the blocks have nonce and are ready to be mined. You have to make sure that your chain supports this new behavior. And now we are implementing new property to the blockchain that keeps track of the difficulty.

We will start by setting it to 4 (which means hashes of our block should start with 4 zero’s)

In our previous article, the add block function contains only the property to add the blocks. But now we are going to implement the mining mechanism in it, which means before adding the current block it will cross check with mineblock() function to add the particular block to the chain.

STEP 4: Testing our Blockchain

All the corrections are done, so now our blockchain is ready with proof-of-work to protect against attackers who try to tamper with it.

Note: Make the above changes in the code by cleaning out the previous one.

We are creating a new instance for our blockchain and also we are adding two simple blocks to it. Now let’s see what effects PoW has on adding a new block to our blockchain.


let maheshcoin = new Blockchain();

console.log(‘Mining block 1…’);

maheshcoin.addblock(new Block(1,”10/07/2017″,{amount: 4}));

console.log(‘Mining block 2…’);

maheshcoin.addblock(new Block(2,”12/07/2017″,{amount: 10}));

STEP 5: Output

Currently, we are mining the block with Difficulty = 4

The output looks like

Note: The above hash contains 4 zero digits.

Let’s change the difficulty = 6 and let’s see what happens

The output looks like

Note: The above hash contains 6 zero digits.

Explanation in simple terms:

The proof of work includes many technical concepts like consensus, distributed, trustless and lot more. Here, I’ll explain to you guys the meaning of these concepts with some simple examples!

Let’s assume you have Rs 10/-  in your account and I add one zero to it, which now makes it Rs 100/-. And if again I add one zero to the value it becomes 1000 INR.

So here the addition of zero increases the value of money. Similarly, let’s take Bitcoin as an example. Every new block hash has some amount of ‘zeros’ in front, which defines the difficulty of the block. More the zero, more the difficulty will be. Which means you need a certain amount of computer power to tamper the block.

Here, a hacker who has a minimal amount of computing power will not be in a position to tamper the blockchain because they won’t be able to match with the hash value.

Now, let’s convert this Rupees example into hash power. Let say computer A has 10 h/s.

And computer B has 25h/s. Now computer B has more probability to solve the block because it can solve the puzzle efficiently and way faster with that additional 15h/s compared to computer A.

The hash rate number can be increased by adding more graphics card (GPU) onto the system.

The hash rate (h/s) can be increased by including more GPU processors. GPU’ will provide additional computing power to the network. Which helps to solve the block and in return miners get the reward, and this is how Proof-of-Work mechanism works in the blockchain.

Leave a comment

My Newsletter

Sign Up For Updates & Newsletters