Ethereum: How Proof of Work Algorithm works?

Ethereum: How Proof of Work Algorithm works?

Ethereum: How Proof of Work Algorithm works?

When you are at learning stage in Ethereum, definitely this would have been buzz word and more frequently people uses this word “POW”. Though it looks simple to understand from theory standpoint, but not actually when you relate this with mining the transactions. In this article, I just want to explain how this works with very nice programming concept I inspired from Udemy Course.

Let’s consider, you are submitting a transaction with some data, as soon as you submit, the Tx(transaction) will be lined up in queue for mining which miners will have to perform some puzzle cracking game and figure out your transaction is valid or not, but how they are doing that ? how it actually works behind the scene? is mystery(if you don’t understand). Let’s crack that mystery now..

I have a data “HelloWorld”, as soon as I submit the transaction a encryption algorithm (SHA256) will be applied and a Tx Hash will be generated. Usually txHash will contain a leading zeros(prefixed). Prediction of this leading zero’s is the game for Miners. What?, are you serious? I read your mind.. Yes this seems easy but actually very time & power consuming process. Since the leading zero’s are not static and it changes from transaction to transaction, this we call it as “Difficulty”. When miners figure out the leading zero’s well before the average time, then the difficulty level auto adjusted for next transaction, thus miners will always have tough time in figuring out this.

How it works?

Let’s say my txHash is 0000000000001x2xhf2sdkfhsdfh2ksdfhksdfhsdf (rough value)

below function solves the purpose,

function puzzle(data,prefix){
var nonce = ‘NOT-FOUND’;
var start = DATE.now();
var end = start;

for(tryNonce = 0; tryNonce < 1000000000; tryNonce++){
var y = blockdata + tryNonce;
var sha = SHA256(y);

if(sha.startsWith(prefix)){
var end = Date.now();
nonce = tryNonce;
console.log(SHA256(blockdata + tryNonce));
break;
}
}
var timetaken = end — start;
if(timetaken ==0)
{
console.log(“not found”);
}else{
console.log(“Prefix”, prefix, “TimeTaken”, timetaken);
}

}

var data = “helloworld”;

puzzle(data,”0");
puzzle(data,”00");
puzzle(data,”000");
puzzle(data,”0000");
puzzle(data,”00000");
puzzle(data,”000000");
puzzle(data,”0000000");
puzzle(data,”00000000");

How puzzle function works?

> it runs through the loop and add the “tryNonce” with “data” we provided and create hash key using SHA256

> Verify if generated hash key is starting with the “prefix” we passed as a parameter.

> it breaks and come out of loop if it finds the prefix and that’s the NONCE value to be added in TxN (Nonce -> number used once)

> So more the prefix value, higher number of loop will executes, this will consume more energy

DifficultyPrefix will auto adjusted every time & you can find the chart in https://www.coinwarz.com/difficulty-charts/ethereum-difficulty-chart realtime

This is the reason, why miners are being paid a small transaction fee, they spend time & energy(power) to figure out whether the transaction is valid or not.

I got this algorithm explained clearly by Mr. Rajeev Sakhuja in Udemy. Credit goes to him..

Thanks for taking a min to read this article, hope you got some clarity on how POW works?, if not put your questions in comment section below.

Note : Like this article?, give Logeswaran a thumbs-up(Claps) & follow him on Linkedin / Twitter

What's Your Reaction?

like
1
dislike
0
love
0
funny
1
angry
0
sad
0
wow
0