Post
Topic
Board Development & Technical Discussion
Merits 11 from 2 users
Re: Understanding Nonce
by
mikeywith
on 01/03/2024, 00:46:04 UTC
⭐ Merited by ABCbits (7) ,ranochigo (4)
Hummm, the purpose of the rehashing is to get a target a hash that is equal or less to the real input hash  so with that example aren't we going to get anything above the hash (if true )

real input hash is irrelevant here, the "hashed" version of the input is what matters to solve a block, let me try to simply the subject as much as I can, when you start hashing a new block you would take the previous blockhash and let that be "1" and the current target is a number that starts with one zero "0" target and difficulty are basically the same thing represented in an invested method to explain the same thing, but let's just say it's a number that starts with a single zero.

Now go to a SHA256 hash tool like https://emn178.github.io/online-tools/sha256.html and input the previous block hash which is "1"



No matter how many times you try to hash that blockheader, you are always going to get
Code:
6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b

But what you need is a hash that starts with 0, so now you add a nonce to it in order to produce a hash that has a single leading zero, so let's try to add 1 and hash 11 (first 1 is the prev blockhash, second one is our nonce)

result = 4fc82b26aecb47d2868c4efbe3581732a3e7cbcc6c2efb32062c08170a05eeb8   = no block
let's try 111

Quote
f6e0a1e2ac41945a9aa7ff8a8aaa0cebc12a3bcc981a929ad5cf810a090e11ae

keep trying randomly, I got mine at 156 so nonce 56 got me a valid block.

Code:
0fecf9247f3ddc84db8a804fa3065c013baf6b7c2458c2ba2bf56c2e1d42ddd4

Now let's assume difficulty has increased, which means the hash needs to start with 00 not just 0

so you take my first block hash which is

Code:
0fecf9247f3ddc84db8a804fa3065c013baf6b7c2458c2ba2bf56c2e1d42ddd4
and add a nonce to it, hash them together, to get 00xxxxxxxxx, I was actually very lucky with this random nonce "3010023340584300011"

hash > 0fecf9247f3ddc84db8a804fa3065c013baf6b7c2458c2ba2bf56c2e1d42ddd4430100233405843 00011 = 00c0a543e028d4e08d8b6e81cf3a5e1f43b57b20723d1c6625e09e788a69e3ce

which is a valid block.


Obviously, since Nonce is pretty small (~ 4 billion value range) it's exhausted in no time by all ASIC miners, the average miner today can do 100TH/s, which is 100 trillion double sha hashes per second, so changing Nonce alone isn't practical and there needs to be something else to reset it.

The good thing about hashing is that any little change you make to the input would generate a completely different output, miners would use Extra Nonce and nTime, as ranochigo explained above if you have

Quote
[nVersion, PrevHash, 28 bytes of merkle root] + [4 bytes of merkleroot, timestamp, difficulty, nonce and padding]

In your input, anything you change there would mean a new set of possibilities, of course, there are things that miners today can't change (given how mining pools operate) but then mining pools would give a different block template to every single miner and the miner would never run out or rehash the same inputs.

To give you another "real example" let's just hash the text I quoted above and start with 1111 once

Quote
[nVersion, PrevHash, 28 bytes of merkle root] + [4 bytes of merkleroot, timestamp, difficulty, 1111 and padding]



let's assume the max nonce I can use is 9999, so I did try up to 9999 but couldn't find a hash that has two leading zeros, what can I do now? I will change a tiny bit of the timestamp chunk and make it timestamp1, even without changing the 1111 once I still get a new different hash which is

Code:
b80e254e1edba8fbc10ab93ceab7eec458ea949b947ce8506b0f543418c29add


Think of Nonce in colors, you paint a picture and somebody is waiting to see a certain final mixed color, your base color can be changed but the colors you add are pretty limited(green and yellow), so you created your base color by mixing blue+red, you added green, it didn't work, you added yellow -- didn't work, you added 0.3 yellow and 0.7 blue it didn't work, you run out of mixing options and the desired color is not there yet, you go back to your base color and add some orange color, now that 0.3 - 0.7 mix would result in a new different final color, hope that makes sense.