<p>That isn't the way a block header is created. The block header is constructed as </p>

<ol>
<li>the version</li>
<li>the hash of the previous block</li>
<li>the merkle root</li>
<li>the timestamp</li>
<li>the nBits field</li>
<li>and the nonce.</li>
</ol>

<p>Once serialized in bytes, <strong>the header is hashed twice</strong> with SHA256 not just once.</p>

<p>A common mistake is to hash the string of hex representation of the bytes, <strong>you must hash the bytes themselves</strong>.</p>

<p>See here for more info: <a href="https://en.bitcoin.it/wiki/Block_hashing_algorithm" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Block_hashing_algorithm</a></p>

<p>The hash is the hash of <em>that block</em>. It does <em>not</em> give the hash of the next block following it, <strong>there is no way to know what the next block hash will be</strong> and there are many possible hashes it could be.</p>

CC BY-SA 4.0
2018-01-21T21:08:12.163Z
2020-04-28T18:33:52.053Z
2020-04-28T18:33:52.053Z
meshcollider
<p>Copied below is a short python script that walks through the verification, using input values for block 505400 from the blockchain.com block explorer:</p>
<pre class="lang-python prettyprint-override"><code>    import json
    import hashlib

    block={}

    #inputs values (from https://www.blockchain.com/explorer/blocks/btc/505400)
    block['blocknumber']=505400
    block['version']='20000000'
    block['hashPrevBlock']='00000000000000000022a664b3ff1e4f85140eddeebd0efcbe6a543d45c4135f'
    block['hashMerkleRoot']='a3defcaa713d267eacab786c4cc9c0df895d8ac02066df6c84c7aec437ae17ae'
    block['time']=1516561306
    block['bits']=394155916
    block['nonce']=2816816696

    #prepare values
    block['versionprepared']=bytes.fromhex(block['version'])[::-1].hex()
    block['hashPrevBlockprepared']=bytes.fromhex(block['hashPrevBlock'])[::-1].hex()
    block['hashMerkleRootprepared']=bytes.fromhex(block['hashMerkleRoot'])[::-1].hex()
    block['timeprepared']=int(block['time']).to_bytes(4, byteorder='little').hex()
    block['bitsprepared']=int(block['bits']).to_bytes(4, byteorder='little').hex()
    block['nonceprepared']=int(block['nonce']).to_bytes(4, byteorder='little').hex()

    #concatentate prepared values to create input to double sha256 hash function
    block['hashinput']=block['versionprepared'] + block['hashPrevBlockprepared'] + block['hashMerkleRootprepared'] + block['timeprepared'] + block['bitsprepared'] + block['nonceprepared']

    #double sha256 hash
    block['hashoutcomputed']=(hashlib.sha256(hashlib.sha256(bytes.fromhex(block['hashinput'])).digest()).digest())[::-1].hex()

    #print results
    print(json.dumps(block, indent=4))
</code></pre>
<p>The double sha256 hash computed is: <code>00000000000000000023b89dd18f6be5a6c03a71cd864ccbdf024683114b9ce3</code></p>
<p>As expected, this result has 18 leading zeroes (as required to meet the difficulty requirement for this block), and matches the hash for block 505400 according to the block explorer.</p>

CC BY-SA 4.0
2024-02-23T01:03:41.023Z
2024-05-29T15:22:35.233Z
2024-05-29T15:22:35.233Z
mti2935
<p>From my understanding, you simply need to use a SHA256 function to hash the block's header (Block hash and merkle root) then add the nonce at the end like <a href="https://chrispacia.wordpress.com/2013/09/02/bitcoin-mining-explained-like-youre-five-part-2-mechanics/" rel="nofollow noreferrer">this article explains</a>. </p>

<p>However, I can't manage to get the correct next block hash.
By using the block <a href="https://blockchain.info/block-height/505400" rel="nofollow noreferrer">#505400</a> :</p>

<p><code>Hash : 00000000000000000023b89dd18f6be5a6c03a71cd864ccbdf024683114b9ce3</code></p>

<p><code>Merkle root :  a3defcaa713d267eacab786c4cc9c0df895d8ac02066df6c84c7aec437ae17ae</code></p>

<p><code>Nonce : 2816816696</code></p>

<p><code>Next block hash : 0000000000000000007625ff74171cf53e301c3e5013bf2184e2629a2da0fc8d</code></p>

<p>What I am expecting : sha256(Hash + Merkle Root + Nonce) = Next Block Hash</p>

<p>However I get this : <code>10345cb85ffed406a47e1faf31eb8ba1160aac0fbaae3cae0cd66889f9c9f49e</code></p>

<p>What did I do wrong?</p>

<blockchain><mining-theory><hash><block-validity>
How do you verify a block manually?
Cedric Martens
# How do you verify a block manually?

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

From my understanding, you simply need to use a SHA256 function to hash the block's header (Block hash and merkle root) then add the nonce at the end like [this article explains](https://chrispacia.wordpress.com/2013/09/02/bitcoin-mining-explained-like-youre-five-part-2-mechanics/).

However, I can't manage to get the correct next block hash.
By using the block [#505400](https://blockchain.info/block-height/505400) :

`Hash : 00000000000000000023b89dd18f6be5a6c03a71cd864ccbdf024683114b9ce3`

`Merkle root : a3defcaa713d267eacab786c4cc9c0df895d8ac02066df6c84c7aec437ae17ae`

`Nonce : 2816816696`

`Next block hash : 0000000000000000007625ff74171cf53e301c3e5013bf2184e2629a2da0fc8d`

What I am expecting : sha256(Hash + Merkle Root + Nonce) = Next Block Hash

However I get this : `10345cb85ffed406a47e1faf31eb8ba1160aac0fbaae3cae0cd66889f9c9f49e`

What did I do wrong?

**Tags:** blockchain, mining-theory, hash, block-validity


---

## 2 answers

### Answer by username_1 on 2018-01-21. Score: 3 | Accepted answer

That isn't the way a block header is created. The block header is constructed as

1. the version
2. the hash of the previous block
3. the merkle root
4. the timestamp
5. the nBits field
6. and the nonce.

Once serialized in bytes, **the header is hashed twice** with SHA256 not just once.

A common mistake is to hash the string of hex representation of the bytes, **you must hash the bytes themselves**.

See here for more info: <https://en.bitcoin.it/wiki/Block_hashing_algorithm>

The hash is the hash of *that block*. It does *not* give the hash of the next block following it, **there is no way to know what the next block hash will be** and there are many possible hashes it could be.


---

### Answer by username_2 on 2024-02-23. Score: 2

Copied below is a short python script that walks through the verification, using input values for block 505400 from the blockchain.com block explorer:
    import json
    import hashlib

    block={}

    #inputs values (from https://www.blockchain.com/explorer/blocks/btc/505400)
    block['blocknumber']=505400
    block['version']='20000000'
    block['hashPrevBlock']='00000000000000000022a664b3ff1e4f85140eddeebd0efcbe6a543d45c4135f'
    block['hashMerkleRoot']='a3defcaa713d267eacab786c4cc9c0df895d8ac02066df6c84c7aec437ae17ae'
    block['time']=1516561306
    block['bits']=394155916
    block['nonce']=2816816696

    #prepare values
    block['versionprepared']=bytes.fromhex(block['version'])[::-1].hex()
    block['hashPrevBlockprepared']=bytes.fromhex(block['hashPrevBlock'])[::-1].hex()
    block['hashMerkleRootprepared']=bytes.fromhex(block['hashMerkleRoot'])[::-1].hex()
    block['timeprepared']=int(block['time']).to_bytes(4, byteorder='little').hex()
    block['bitsprepared']=int(block['bits']).to_bytes(4, byteorder='little').hex()
    block['nonceprepared']=int(block['nonce']).to_bytes(4, byteorder='little').hex()

    #concatentate prepared values to create input to double sha256 hash function
    block['hashinput']=block['versionprepared'] + block['hashPrevBlockprepared'] + block['hashMerkleRootprepared'] + block['timeprepared'] + block['bitsprepared'] + block['nonceprepared']

    #double sha256 hash
    block['hashoutcomputed']=(hashlib.sha256(hashlib.sha256(bytes.fromhex(block['hashinput'])).digest()).digest())[::-1].hex()

    #print results
    print(json.dumps(block, indent=4))

The double sha256 hash computed is: 00000000000000000023b89dd18f6be5a6c03a71cd864ccbdf024683114b9ce3
As expected, this result has 18 leading zeroes (as required to meet the difficulty requirement for this block), and matches the hash for block 505400 according to the block explorer.


---

