Blockchain 8 min read

Inside a Bitcoin Block: Structure, Headers, and Merkle Tree Explained

This article breaks down the anatomy of a Bitcoin block, detailing its header, identifier, transaction list, and the Merkle tree construction with JavaScript code examples to illustrate how hashes are combined into a root hash.

21CTO
21CTO
21CTO
Inside a Bitcoin Block: Structure, Headers, and Merkle Tree Explained

Block Structure

A block is a container data structure in Bitcoin, typically holding over 500 transactions and averaging about 1 MB in size; Bitcoin Cash can reach up to 8 MB. Each block consists of a header and a long list of transactions.

Block Header

The header stores metadata in three parts:

Hash of the previous block, linking blocks together in the chain.

Mining data (timestamp, nonce, difficulty) that produces a valid hash (not covered in depth here).

A Merkle root that summarizes all transactions in the block.

Block Identifier

A block is identified by a cryptographic hash generated by applying SHA‑256 twice to the block header, often accompanied by a digital signature. Example block URL: https://blockchain.info/en/block/000000000000000000301fcfeb141088a93b77dc0d52571a1185b425256ae2fb.

The block’s hash is:

000000000000000000301fcfeb141088a93b77dc0d52571a1185b425256ae2fb

The previous block’s hash is:

0000000000000000004b1ef0105dc1275b3adfd067aed63a43324929bed64fd7

Block height indicates its position in the chain; the example block is at height 500312, meaning 500,311 blocks precede it.

Merkle Trees

Transactions are organized in a Merkle tree (binary hash tree). Each leaf is a double‑SHA‑256 hash of a transaction; internal nodes are double‑SHA‑256 hashes of the concatenation of their child hashes, culminating in a single Merkle root stored in the block header.

Example with four transactions:

const tA = 'Hello'
const tB = 'How are you?'
const tC = 'This is Thursday'
const tD = 'Happy new Year'
const sha256 = require('js-sha256').sha256
// double hash each transaction
const hA = sha256(sha256(tA))
const hB = sha256(sha256(tB))
const hC = sha256(sha256(tC))
const hD = sha256(sha256(tD))
// results
52c87cd40ccfbd7873af4180fced6d38803d4c3684ed60f6513e8d16077e5b8e // hA
426436adcaca92d2f41d221e0dd48d1518b524c56e4e93fd324d10cb4ff8bfb9 // hB
6eeb307fb7fbc0b0fdb8bcfdcd2d455e4f6f347ff8007ed47475481a462e1aeb // hC
fd0df328a806a6517e2eafeaacea72964f689d29560185294b4e99ca16c63f8f // hD
// pair hA and hB
const hAB = sha256(sha256(hA + hB)) // 5dc23d1a2151665e2ac258340aa9a11ed227a4cc235e142a3e1738333575590b
// pair hC and hD
const hCD = sha256(sha256(hC + hD)) // ff220daefda29821435691a9aa07dd2c47ca1d2574b8b77344aa783142bae330
// pair hAB and hCD to get the root
const hABCD = sha256(sha256(hAB + hCD)) // 301faf21178234b04e1746ee43e126e7b2ecd2188e3fe6986356cc1dd7aa1377

The resulting root hash is stored as the Merkle root in the block header.

If the number of transactions is odd, the last hash is duplicated to keep the tree balanced.

Changing any single transaction alters its leaf hash, which propagates up the tree, changing the Merkle root and thus the block hash, making tampering evident.

Proof of inclusion can be demonstrated by providing a Merkle path of log₂(N) hashes, where N is the number of transactions.

Bitcoin block illustration
Bitcoin block illustration
Merkle tree with 16 leaves
Merkle tree with 16 leaves

The diagram shows a 16‑leaf Merkle tree built by repeatedly pairing leaf hashes upward, illustrating how a small set of hashes can prove a transaction’s inclusion.

Hashblockchaincryptographymerkle treeBlock
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.