I'm trying to calculate the hash of block #502871. This is the block:
{
"result": {
"hash": "00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9",
"confirmations": 35,
"strippedsize": 976261,
"size": 1064277,
"weight": 3993060,
"height": 502871,
"version": 536870912,
"versionHex": "20000000",
"merkleroot": "871148c57dad60c0cde483233b099daa3e6492a91c13b337a5413a4c4f842978",
"tx": [
"6fef5a38c0a76d4bfb6644afed809fe760abcbe9c1e030b7b09bd876f8d28765",
"2506559cd75c9537108c63adddf6910eaa2c12e9e129b75528dd5dbff8e6cef3",
...
"b136d220a049fd9a42261153c7d5e21e12b791c27eed9cb9e41acaea672a6af5",
"7dbaf4d2b31d4a000b9f2028dd350e46da427b8c1721f030af67d5e47f322da5"
],
"time": 1515252561,
"mediantime": 1515249802,
"nonce": 45291998,
"bits": "180091c1",
"difficulty": 1931136454487.716,
"chainwork": "000000000000000000000000000000000000000000e0ef1d9aa7eeab21ce8628",
"previousblockhash": "00000000000000000061abcd4f51d81ddba5498cff67fed44b287de0990b7266",
"nextblockhash": "00000000000000000075e23616edab2b743425a064c282a7745ad38d05806e80"
},
"error": null,
"id": null
}
So according to https://en.bitcoin.it/wiki/Block_hashing_algorithm I need these values:
versionHex: 20000000
previousblockhash:
00000000000000000061abcd4f51d81ddba5498cff67fed44b287de0990b7266
merkleroot: 871148c57dad60c0cde483233b099daa3e6492a91c13b337a5413a4c4f842978
time: 1515252561
bits: 180091c1
nonce: 45291998
Formatting the values I get:
version: 02000000
previousblockhash: 66720b99e07d284bd4fe67ff8c49a5db1dd8514fcdab61000000000000000000
merkleroot: 7829844f4c3a41a537b3131ca992643eaa9d093b2383e4cdc060ad7dc5481187
time: (5A50EB51)_16 -> 51eb505a
bits: c1910018
nonce: (02B319DE)_16 -> de19b302
So my block header is:
0200000066720b99e07d284bd4fe67ff8c49a5db1dd8514fcdab610000000000000000007829844f4c3a41a537b3131ca992643eaa9d093b2383e4cdc060ad7dc548118751eb505ac1910018de19b302
And when I calculate the hash:
from hashlib import sha256
import hashlib
header = "0200000066720b99e07d284bd4fe67ff8c49a5db1dd8514fcdab610000000000000000007829844f4c3a41a537b3131ca992643eaa9d093b2383e4cdc060ad7dc548118751eb505ac1910018de19b302".decode('hex')
print sha256(sha256(header).digest()).digest()[::-1].encode('hex')
The result is
0a80d313f7c8bab35b1442a194516c3ddff9ddc674c752ccfaffb1c83866a073
But not the the expected one:
00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9
Please, what am I doing wrong? Thanks.