Why is the block time on bitcoin 10 mins? A technical explanation of PoW Mining(Part 2)
In part one we explored, at a high level, why bitcoin blocks are mined roughly every 10 mins. However, now it’s time to delve into the maths of what’s really going on under the hood.
To start the mining process, the node must create something called a Block Header. This includes 6 pieces of information about the block:
Version: Just like your phone or computer supports a specific software version, the bitcoin blockchain does too. At the time of writing the most recent version of bitcoin core is 0.22.0
Previous Block Hash: This is the reference to the previous block in the chain and links this candidate block to the existing chain of blocks.
Merkle Root: It would take up too much space to include all transactions within the block so instead we aggregate them all together into a summary value.
Timestamp: The approximate creation time of the block displayed in Unix epoch which is seconds since January 1st 1970 (Why this date? Some engineers back in the day picked it randomly 🤷🏽♀️)
Difficulty Target: The proof-of-work algorithm difficulty target for this block
Nonce: A counter used for the proof-of-work algorithm. This is initially set to zero.
With all these values completed, the miners test billions or trillions of nonce values with the hope that the block header hash (where the block header value is hashed using SHA-256) is less than the difficulty target.
Let’s unpack this a little …
Difficulty Target and Nonce
The difficulty target is the number of leading zeros we’re looking for when we hash the block header value using SHA-256 (a hashing algorithm used throughout the bitcoin protocol). In order to generate a new block header hash, lots of different nonce values are tried and if the resulting hash has more leading zeros than the target the miner has successfully mined the next block (assuming they’re first!).
We can work through this with an example block: 714,830
The target difficulty (shown in the block explorer under the ‘bits’ field is 386,638,367.
We have to convert this to hex: 170BA21F
We then split this into the exponent: 170B and the coefficient: A21F
Using the below formula we can then calculate the target for this block
= 1,114,247,386,799,443,053,935,571,112,778,061,457,902,663,314,832,359,424
That’s a pretty gigantic number but by converting it to hex again (0x0000000000000000000ba21f0000000000000000000000000000000000000000) we can see that we’re looking for a block header hash with 19 or over leading zeros.
Finding the Golden Nonce
Using the block header hash information and different nonce values we can try and calculate a resulting value with at least 19 leading zeros.
The underlying calculation requires each piece of information to be transformed into little-endian format (essentially reversing each bit of information), concatenating (joining the information together in the following order: Version || Previous Block Hash || Merkle Root hash || Time || Bits || Nonce) and then applying the SHA-256 algorithm twice.
Using the information from block 714,830…
Block Header Information
Version: 0x20800004
Previous Block Hash: 00000000000000000007ef49d4208fa6bb6f4512200cbd8db4844ff56b601fa2
Merkle Root: c0ca769aa104227f74ad46755bd7d8b9de9932418c27d8eabf118b4c33ca3bea
Timestamp: 1639934732
Difficulty bits: 0x170ba21f
Nonce: 1
Concatenating in little-endian format
SHA-256 on the result = 4fd12c2af8c4f5fca27d31bb82bb0cf8978d6d0450caa04e6ecaf411f437f39a
Re-applying SHA-256 on this: 9af337f411f4ca6e4ea0ca50046d8d97f80cbb82bb317da2fcf5c4f82a2cd14f
With each different nonce, the result can have a different number of leading zeros. However these don’t sequentially increase so miners who start with very large nonce values may miss the successful nonce, and just because one value has no leading zeros doesn’t mean the next won’t hit the jackpot.
For this block, the successful nonce, referred to as the golden nonce, is 553,889,423.
When the unknown miner found this they were able to broadcast this to the wider network to be verified and then claim the 6.25 newly mined bitcoins as well as the 0.02628513 BTC transaction fees.