Bitcoin miners technically must find a blockhash which satisfies the requirements of the protocol. So it can be really very interesting to check a block header of an already mined block and manually calculate the hash to see what is normally verified by nodes after receiving blocks by miners. This is very useful to see if the procedure is correctly understood.
Let’s take the block of height 577316. This block has nothing special, we just use this as an example.
What we are going do to is: 1) retrieve the blockhash from the blockchain, 2) retrieve the block header and 3) calculate the blockhash from the blockheader and check if it is really matching what we expected.
The calculation of the hash is going to be performed with a little python script because we need to change formats to data. What follows has been calculated on a bitcoin core full node, which was installed on an ubuntu distribution machine.
1) first of all get the blockhash of that block with height 577316
dev@zulu:~$ bitcoin-cli getblockhash 577316
2) now get the blockhash in raw format:
dev@zulu:~$ bitcoin-cli getblockheader 0000000000000000001d476635098d9eda148caae22965044380bb74c78ca80a false
3) now I retrieve the same header in json format (easily human readable)
dev@zulu:~$ bitcoin-cli getblockheader 0000000000000000001d476635098d9eda148caae22965044380bb74c78ca80a
4) Now we want to check that the sha256 hash of the block header is really the blockhash shown in data. So we want to calculate it ourselves. For this purpose we execute commands using python
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
>>> import binascii, hashlib
as we can see the blockhash is as expected, infact the value of calculated hash is
that matches with the blockhash from the blockchain. For making calculations we used the hashlib in python with those few lines of code. This calculation has been easy to verify because we already had a confirmed block. Infact the miner had to try many and many times billions of different header combinations (changing the nonce) and then hash the resulting header, in order to find a blockhash satisfing the rules of POW. So what we have just done is more similar to verifications done by nodes but different from what is performed by hashing machines which have to identify the correct values creating the right hash result.