Been digging into how mining works and it seems like nonce is super important in block headers. I figured it’s necessary to get a hash that meets the network's difficulty. But I’m a bit confused... does nonce get used while hashing to hit that difficulty target, or only after a block gets added to the chain? Plus, what’s it really doing in terms of mining and block verification?
How Nonce Plays a Role in Blockchain Mining
5 replies 308 views
Nonce is basically just a number miners mess around with, combined with block info to create a hash. They keep tweaking this number until they hit a hash that satisfies the difficulty. Like, a hash starting with a certain number of zeros is what they’re after, ya know? First miner to nail it gets fresh Bitcoins and any fees from that block. And this hash gets included in the next block, so no blocks can get mined too soon.
wizard_2016Full Member
Posts: 127 · Reputation: 575
#3Sep 15, 2018, 07:47 PM
But nonce doesn’t actually identify the block. It’s just a cheap trick to tweak the block hash. Miners will hash the block header with all possible nonce values until they find a valid one. If they can’t find it, they change something in the block and start over. They do the same thing with timestamps too.
Right! Blocks get identified by their hashes, not by their headers like nonce. The nonce is just a random number, not really special... just important since it’s part of the header. We need to get what the Proof of Work does, though. It’s all about putting in that work until they find the right hash result. In Bitcoin, hashing the block header is the work.
Gotcha! So nonce helps change the block hash effectively. I was messing with a script to get my head around it. If I’m trying to find a hash that starts with "0000", I’d start with nonce 0 and keep bumping it up till I hit that target. Seems pretty straightforward when you lay it out like that.
If you’re just playing around to see how this works, that’s cool but you need to know your checks are off. In PoW, we actually convert the hash to an integer in little endian format and check if it’s smaller than or equals the target which also needs converting. Counting zeros isn’t the way to go at all.