Block Chain - Proof of Work

What is Proof of Work?

In a blockchain system, a miner is required to give a proof that he did some work, only then his mined block is added to the block chain. A very simple example of proof of work can be generating a hash value for the block with four consecutive ‘a’ in the beginning. Hash of the block is varied by varying the nonce. A miner will keep on varying the nonce till the time a hash of pattern “aaaa…….” is not generated. The hash function used should be deterministic.

Wikipedia: A proof-of-work (POW) system (or protocol, or function) is an economic measure to deter denial of service attacks and other service abuses such as spam on a network by requiring some work from the service requester, usually meaning processing time by a computer.

In the above example, four consecutive ‘a’ in the beginning of the hash is a common consensys on which all the nodes agree. Other nodes verify this condition before accepting a block mined by some miner. The difficulty of mining a block depends on the proof of work function. The above example can be made more difficult by making six consecutive ‘a’ as the common consensys.

Proof of work solves many problems associated with Crptocurrency and distributed databases in general:

At this stage, it is very easy for us to include proof of work in our block chain. We just have to change the addBlock() function. We need to add a loop so that it keeps on re-calculating the hash by varying the nonce till the time a hash which fulfills the common condition is obtained. For our block chain, we will keep the condition as first four letters of the hash will be 'a'.

Now, replace the addBlock() function with this

function addBlock(data){
if( data == undefined || data == "" )
console.log("No data found.");
var timeStamp = new Date().getTime()/1000;
var previousBlock = blockChain[blockChain.length-1];
let nonce = 0; //will get value in proof of work
let hash = getHash( previousBlock.index + 1, previousBlock.hash, timeStamp, data, nonce );
if(hash.substr(0,4) == "aaaa"){
var temp = new block( previousBlock.index + 1, previousBlock.hash, timeStamp, data, hash, nonce );
console.log("New Block Added");
nonce = nonce+1;

Give it a read: What is the difference between let and var

Again add the following code in the end of the file and run the file.

addBlock("Second block with proof of work");

Analyze the output, the hash value of the second block will have 4 consecutive ‘a’. Also, because we increment the nonce by 1 and start it from 0, nonce here represents the number of hashes calculated before we obtained this hash value. Try to tweak the proof of work condition and make it 5 consecutive 'a'. How much time did it take? Now make it 6 consecutive 'a'. Now how much time did it take?

What is hashing power?

It is the estimated number of hashes a system can compute in one second. Professional setups for mining have hashing power in order of petahash.

Now we have a working block chain on one node but we need to develop some way of communication between multiple nodes. We will see that in the next part.

Next -> Block Chain - Peer to Peer Communication

Previous -> Block Chain - Utility Functions