2

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.

However, I can't manage to get the correct next block hash. By using the block #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?

2 Answers 2

3

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.