When you are at the learning stage in Ethereum, definitely this would have been buzz word and more frequently people use the word “POW”. Though it looks simple to understand from a 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 a very nice programming concept I inspired from Udemy Course.
Let’s consider, that you are submitting a transaction with some data, as soon as you submit, the Tx(transaction) will be lined up in the 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 does it actually work behind the scene? is a mystery(if you don’t understand). Let’s crack that mystery now.
I have a data “HelloWorld”, as soon as I submit the transaction the encryption algorithm (SHA256) will be applied and a Tx Hash will be generated. Usually, txHash will contain 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 a very time & power-consuming process. Since the leading zeros are not static and it changes from transaction to transaction, we call it “Difficulty”. When miners figure out the leading zeros well before the average time, then the difficulty level auto adjusted for the next transaction, thus miners will always have a tough time figuring out this.
How does it work?
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
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.
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.