in

Help understanding block header for mining/verification

I am learning about Bitcoin mining but having trouble understanding what are the exact input values for the SHA256 function and looking for some help.

What I Think I Know

Mining is the process of adding blocks to the blockchain. New blocks are created by miners and added to the blockchain after completing a Proof of Work.

The Proof of Work is guessing a number (Nonce) that, when added to the end of the block header and hashed twice using SHA256, results in a number smaller than the Difficulty.

Once a block is mined, the Proof of Work is verified by other nodes by hashing the block header twice using SHA256 and checking the value is smaller than the Difficulty.

The block header (before being hashed), consists of the following:

Version + Previous Block Hash + Merkle Root + Timestamp + Difficulty + Nonce

What I’m Confused About

I would like to verify a mined block, but I don’t understand how to put together the block header.

Example:

Block 277316

Version: 0x2

Previous Hash: 0000000000000002a7bbd25a417c0374cc55261021e8a9ca74442b01284f0569

Merkle Root: c91c008c26e50763e9f548bb8b2fc323735f73577effbc55502c51eb4cc7cf2e

Timestamp: 2013-12-27 18:11

Difficulty: 1,180,923,195.26

Bits: 419,668,748

Nonce: 924,591,752

Block Hash: 0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4

I don’t get what format all the parts of the block header need to be to create the block hash successfully. I’ve also heard that the previous hash and Merkle Root need to be reversed?

Can someone break down what the correct block header input to SHA256(SHA256()) for Block 277316 is?

Thanks!

[Block 277316](https://www.blockchain.com/btc/block/0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4)



View Reddit by bccrz_View Source

Leave a Reply

Your email address will not be published.

GIPHY App Key not set. Please check settings

2 Comments

  1. The input is the first 80 bytes as stored in the blockchain, in binary

    `getblockheader` without “verbose” returns hexadecimal so the first 80 bytes is represented as 160 hexadecimal characters …

    bitcoin-cli getblockheader $(bitcoin-cli getblockhash 277316) false

    0200000069054f28012b4474caa9e821102655cc74037c415ad2bba702000000000000002ecfc74ceb512c5055bcff7e57735f7323c32f8bbb48f5e96307e5268c001cc93a09be520ca3031988261c37

    With “verbose” …

    bitcoin-cli getblockheader $(bitcoin-cli getblockhash 277316) true

    {
    “hash”: “0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4”,
    “confirmations”: 434289,
    “height”: 277316,
    “version”: 2,
    “versionHex”: “00000002”,
    “merkleroot”: “c91c008c26e50763e9f548bb8b2fc323735f73577effbc55502c51eb4cc7cf2e”,
    “time”: 1388185914,
    “mediantime”: 1388183675,
    “nonce”: 924591752,
    “bits”: “1903a30c”,
    “difficulty”: 1180923195.258026,
    “chainwork”: “000000000000000000000000000000000000000000000934695e92aaf53afa1a”,
    “nTx”: 419,
    “previousblockhash”: “0000000000000002a7bbd25a417c0374cc55261021e8a9ca74442b01284f0569”,
    “nextblockhash”: “000000000000000010236c269dd6ed714dd5db39d36b33959079d78dfd431ba7”
    }

    The developer guide explains the contents of the header
    https://developer.bitcoin.org/reference/block_chain.html

    As it says

    > The hashes are in internal byte order; the other values are all in little-endian order

    And none of the byte ordering is convenient to discuss in this tiny text box. See below …

    Split the hexadecimal version of the header into its six fields

    02000000
    previous block header hash 69054f28012b4474caa9e821102655cc74037c415ad2bba70200000000000000
    merkle root hash 2ecfc74ceb512c5055bcff7e57735f7323c32f8bbb48f5e96307e5268c001cc9
    time 3a09be52
    nBits 0ca30319
    nonce 88261c37

    You can see visually the byte order of the two hashes. For the other 4 fields, use the nonce as an example. Convert 924591752 to hexadecimal

    dc -e ’16o924591752p’
    371C2688

    and compare

    88261c37
    371C2688

    Just for fun, we’ll convert the timestamp the other way (your timestamp is displayed in your local time and missing the seconds)

    time 3a09be52

    Reverse the byte order

    52BE093A

    Convert to decimal

    dc -e ’16i52BE093Ap’
    1388185914

    UTC timestamp

    date +’%F %T’ -u -d @1388185914
    2013-12-27 23:11:54

    Now if you want to start with

    Version: 0x2
    Previous Hash: 0000000000000002a7bbd25a417c0374cc55261021e8a9ca74442b01284f0569
    Merkle Root: c91c008c26e50763e9f548bb8b2fc323735f73577effbc55502c51eb4cc7cf2e
    Timestamp: 2013-12-27 23:11:54
    Bits: 419,668,748
    Nonce: 924,591,752

    then convert to hexadecimal and reorder the bytes, you’re facing 2 hours of tedium

    Instead, pull the hexadecimal version of the block header from some explorer which will give you hexadecimal
    https://www.blockstream.info/api/block/0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4/header

    or from your own node

    bitcoin-cli getblockheader $(bitcoin-cli getblockhash 277316) false
    0200000069054f28012b4474caa9e821102655cc74037c415ad2bba702000000000000002ecfc74ceb512c5055bcff7e57735f7323c32f8bbb48f5e96307e5268c001cc93a09be520ca3031988261c37

    Convert this to binary and double hash it

    blkhdr=0200000069054f28012b4474caa9e821102655cc74037c415ad2bba702000000000000002ecfc74ceb512c5055bcff7e57735f7323c32f8bbb48f5e96307e5268c001cc93a09be520ca3031988261c37
    echo $blkhdr|xxd -r -p|openssl sha256 -binary|openssl sha256 -binary|perl -e ‘my $header_hash = do { local $/; <> };printf “%*v02xn”, “”, scalar reverse $header_hash’

    This pipes the hexadecimal 80-byte block header into `xxd -r` to convert it to binary, pipes the binary into `openssl` to hash it, pipes the hash into `openssl` to hash it again, and then exploits the `reverse()` function in Perl to reverse the byte order. Perl’s `printf()` converts to hexadecimal for pasting here

    0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4

What do you think?

Gnosis Price Prediction 2021 - Will GNO Hit $350 Soon?

Gnosis Price Prediction 2021 – Will GNO Hit $350 Soon?

Massive Bitcoin Whale Moves Over $875,000,000 Worth of BTC – Here’s Where the Crypto Is Going

Massive Bitcoin Whale Moves Over $875,000,000 Worth of BTC – Here’s Where the Crypto Is Going