Block Header � Deep Dive
The block header is the most critical 80-byte structure in blockchain. It contains everything needed to verify a block's validity and chain linkage. The block hash that miners race to find is the SHA-256 of this exact 80 bytes.
Bitcoin Block Header Fields (80 bytes total)
# Bitcoin Block Header � exact binary layout
# Total: 80 bytes, hashed twice with SHA-256 to get block hash
import struct
import hashlib
class BitcoinBlockHeader:
"""Exact representation of Bitcoin's 80-byte block header"""
def __init__(self, version, prev_block_hash, merkle_root,
timestamp, bits, nonce):
self.version = version # 4 bytes: block format version
self.prev_block_hash = prev_block_hash # 32 bytes: hash of parent header
self.merkle_root = merkle_root # 32 bytes: Merkle root of transactions
self.timestamp = timestamp # 4 bytes: Unix time in seconds
self.bits = bits # 4 bytes: compact difficulty target
self.nonce = nonce # 4 bytes: mining variable
def serialize(self) -> bytes:
"""Pack into 80 bytes � exactly as Bitcoin nodes transmit"""
return struct.pack(
"<I32s32sIII", # little-endian format
self.version,
bytes.fromhex(self.prev_block_hash),
bytes.fromhex(self.merkle_root),
self.timestamp,
self.bits,
self.nonce,
)
def double_sha256(self) -> str:
"""Bitcoin hashes the 80-byte header twice"""
raw = self.serialize()
first_hash = hashlib.sha256(raw).digest()
final_hash = hashlib.sha256(first_hash).digest()
# Bitcoin reverses byte order for display
return final_hash[::-1].hex()
# Bitcoin Block #800000 header (approximate values for illustration)
header = BitcoinBlockHeader(
version=0x20000000,
prev_block_hash="0000000000000000000abc123" + "0" * 39,
merkle_root="a1b2c3d4e5f6" + "0" * 52,
timestamp=1691629823, # Aug 10 2023
bits=0x17034219, # Compact difficulty target
nonce=3141592653,
)
print(f"Header size: {len(header.serialize())} bytes")
print(f"Block hash: {header.double_sha256()[:20]}...")What Each Header Field Does
- Version (4 bytes): Signals which consensus rules this block follows. Miners use version bits to signal readiness for soft fork upgrades (BIP-9). Bitcoin has had versions 1 through 4 plus SegWit (0x20000000)
- Previous Block Hash (32 bytes): The double-SHA256 of the parent's 80-byte header. This is what 'chains' blocks together. Any change to any ancestor block changes this field, invalidating the chain
- Merkle Root (32 bytes): Summary of all transactions. Changing, adding, or removing any transaction changes this field, invalidating the block
- Timestamp (4 bytes): Approximate time of block creation in Unix seconds. Accepted if within 2 hours of network-adjusted time and greater than the median of last 11 blocks
- Bits (4 bytes): Compact encoding of the target threshold. A valid block hash must be numerically less than this target. Adjusted every 2016 blocks (~2 weeks)
- Nonce (4 bytes): The only field miners change freely. Iterated 0 to 2^32 searching for a hash below the target
Common Mistakes
- Thinking the block hash is stored in the header � the hash is NOT a field in the header. It is computed FROM the header when needed. This is why changing any header field changes the block's identity
- Confusing block height with block hash � height is the sequential position (0, 1, 2...). Hash is the cryptographic identifier (000000000019d668...). Multiple blocks can compete at the same height (temporary forks)
Tip
Tip
Practice Block Header Deep Dive in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Each block references the previous block's hash, forming an immutable chain
Practice Task
Note
Practice Task — (1) Write a working example of Block Header Deep Dive from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Block Header Deep Dive is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready blockchain code.
Key Takeaways
- The block header is the most critical 80-byte structure in blockchain.
- Version (4 bytes): Signals which consensus rules this block follows. Miners use version bits to signal readiness for soft fork upgrades (BIP-9). Bitcoin has had versions 1 through 4 plus SegWit (0x20000000)
- Previous Block Hash (32 bytes): The double-SHA256 of the parent's 80-byte header. This is what 'chains' blocks together. Any change to any ancestor block changes this field, invalidating the chain
- Merkle Root (32 bytes): Summary of all transactions. Changing, adding, or removing any transaction changes this field, invalidating the block