Few days ago, a member asked about this in Bitcoin Discussion here:
Mining process - SHA256 - Probability. I thought it would be better if I create a new thread for the reply I posted for him so more people who don't know the process can read it.
So if you read various beginners' guide on Bitcoin and blockchain on internet then will you surely come across a topic on Proof of Work (PoW) algorithm. Almost of all of them describes PoW as a consensus algorithm where miners try to find a solution to a
mathematical puzzle and the first one to get the solution mines the block! So this thread is all about what that 'Mathematical Puzzle' is because the solution of this mathematical puzzle is what known as the hash of the block.
Finding block hash is nothing but finding a value which is less than the target value. Target value is adjusted after every 2,016 blocks. Current target value (as on 22nd August) is:
0x000000000000000000109bac0000000000000000000000000000000000000000 (hexadecimal form)
or
1590739304116800001454600275103718494518067345886281728 (decimal/integer number)
Now in order to find the block hash, you as a miner need to find an integer value less than the above number. But this is not very simple because you need to include 6 values in your computation together known as Block Header:
> Version
> Hash of previous block
> Merkle Root
> Time of mining
> Bits (difficulty in compact form)
> Nonce (an incrementing number)
So the process of mining involves calculating double SHA-256 of these 6 values in such a way that result hexadecimal hash is less than the current target. The whole process is defined here:
o_e_l_e_o's reply to this post. Now let's take an example of Block: 644,731 and try to verify the solution provided by the miner:
For block 644,731, we have:
Version = 20400000 (hexadecimal)
Hash of Previous Block = 000000000000000000083d157a8a2a589a74fc2f7b91245cb3a415c23d7cc79a
Merkle Root = 9453cfc708a1cc84f3f36c2bae68202b03ad2866bb02da511d7e603a4e9d1fd1
Time = 16:42:15 as per GMT
Bits = 386,964,396 (decimal)
Nonce = 2035916310 (decimal)
Converting everything in hexadecimal form (little-endian), we will have following value,
0x
000040209ac77c3dc215a4b35c24917b2ffc749a582a8a7a153d08000000000000000000d11f9d4e3a607e1d51da02bb6628ad032b2068ae2b6cf3f384cca108c7cf539467f93f5fac9b1017169e5979(text is colored to differentiate six parameters)
The final step involves performing SHA-256 hashing twice on the above value and the result will be:
811802676f41d6aff67ad767517c466c4c8d3b87c81009000000000000000000
Converting it in big-endian, we have:
0000000000000000000910c8873b8d4c6c467c5167d77af6afd6416f67021881 which will become block hash of the block.
Now you can compare,
Target Value = 0x000000000000000000109bac0000000000000000000000000000000000000000 or 1590739304116800001454600275103718494518067345886281728
Block Hash (644,731) = 0000000000000000000910c8873b8d4c6c467c5167d77af6afd6416f67021881 or 868308124812842907393685749016650546999869456114653313
Since 868308124812842907393685749016650546999869456114653313 is smaller than 1590739304116800001454600275103718494518067345886281728, it is accepted as a correct solution and will become the block hash.