<p>The version is wrong:</p>

<p>I have 02000000 </p>

<p>But the one that appears on the block is </p>

<pre><code>Version 0x20000000
</code></pre>

<p>Doing the formatting: </p>

<pre><code>00000020  
</code></pre>

<p>Calculating the hash of the block:</p>

<pre><code>from hashlib import sha256
import hashlib
header = "0000002066720b99e07d284bd4fe67ff8c49a5db1dd8514fcdab610000000000000000007829844f4c3a41a537b3131ca992643eaa9d093b2383e4cdc060ad7dc548118751eb505ac1910018de19b302".decode('hex')
print sha256(sha256(header).digest()).digest()[::-1].encode('hex')
</code></pre>

<p>And the answer is: </p>

<pre><code>00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9
</code></pre>

<p>That is the original hash in the block:</p>

<pre><code>00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9
</code></pre>

CC BY-SA 3.0
2018-01-07T14:17:23.427Z
2018-01-07T14:17:23.427Z
CamiloARG
<p>I'm trying to calculate the hash of block #502871. This is the block:</p>

<pre><code>{
"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
</code></pre>

<p>}</p>

<p>So according to <a href="https://en.bitcoin.it/wiki/Block_hashing_algorithm" rel="noreferrer">https://en.bitcoin.it/wiki/Block_hashing_algorithm</a> I need these values:</p>

<pre><code> versionHex: 20000000
 previousblockhash: 
 00000000000000000061abcd4f51d81ddba5498cff67fed44b287de0990b7266
 merkleroot: 871148c57dad60c0cde483233b099daa3e6492a91c13b337a5413a4c4f842978
 time: 1515252561
 bits: 180091c1
 nonce: 45291998
</code></pre>

<p>Formatting the values I get:</p>

<pre><code>version: 02000000
previousblockhash: 66720b99e07d284bd4fe67ff8c49a5db1dd8514fcdab61000000000000000000
merkleroot: 7829844f4c3a41a537b3131ca992643eaa9d093b2383e4cdc060ad7dc5481187
time: (5A50EB51)_16 -&gt; 51eb505a
bits: c1910018
nonce: (02B319DE)_16 -&gt; de19b302
</code></pre>

<p>So my block header is:  </p>

<p>0200000066720b99e07d284bd4fe67ff8c49a5db1dd8514fcdab610000000000000000007829844f4c3a41a537b3131ca992643eaa9d093b2383e4cdc060ad7dc548118751eb505ac1910018de19b302</p>

<p>And when I calculate the hash:</p>

<pre><code>from hashlib import sha256
import hashlib    
header = "0200000066720b99e07d284bd4fe67ff8c49a5db1dd8514fcdab610000000000000000007829844f4c3a41a537b3131ca992643eaa9d093b2383e4cdc060ad7dc548118751eb505ac1910018de19b302".decode('hex')
print sha256(sha256(header).digest()).digest()[::-1].encode('hex')
</code></pre>

<p>The result is</p>

<pre><code>0a80d313f7c8bab35b1442a194516c3ddff9ddc674c752ccfaffb1c83866a073
</code></pre>

<p>But not the the expected one:</p>

<pre><code>00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9
</code></pre>

<p>Please, what am I doing wrong? Thanks.</p>

CC BY-SA 3.0
2018-01-07T18:05:32.313Z
Please remove the answer from the question. :-)
wizzwizz4
<mining-theory><block-header>
Calculate hash of block header
CamiloARG
# Calculate hash of block header

(Asked by: username_0 on 2018-01-07)

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.

**Tags:** mining-theory, block-header

> **Comments:**
> **username_1**: Please remove the answer from the question. :-)  

---

## 1 answers

### Answer by username_0 on 2018-01-07. Score: 9

The version is wrong:
I have 02000000 
But the one that appears on the block is 
Version 0x20000000

Doing the formatting: 
00000020  

Calculating the hash of the block:
from hashlib import sha256
import hashlib
header = "0000002066720b99e07d284bd4fe67ff8c49a5db1dd8514fcdab610000000000000000007829844f4c3a41a537b3131ca992643eaa9d093b2383e4cdc060ad7dc548118751eb505ac1910018de19b302".decode('hex')
print sha256(sha256(header).digest()).digest()[::-1].encode('hex')

And the answer is: 
00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9

That is the original hash in the block:
00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9


---

