Glossary

Headers Message (Bitcoin P2P)

A headers message delivers a sequence of block headers to a peer during initial block download or chain synchronization.

Key Takeaways

  • A headers message is a Bitcoin P2P network response that delivers up to 2,000 block headers to a peer that requested them with a getheaders message, allowing the receiver to build its chain map before downloading full blocks.
  • Headers messages are the backbone of headers-first synchronization: a node first downloads the entire header chain (roughly 68 MB at 850,000 blocks), identifies the heaviest chain, then fetches block data only for the winning chain.
  • Combined with assumevalid and parallel block download, headers messages make initial block download practical even as the blockchain exceeds 600 GB.

What Is a Headers Message?

A headers message is one of the data-relay messages in Bitcoin's peer-to-peer protocol. When a Bitcoin node needs to learn about blocks it has not yet seen, it sends a getheaders request containing a block locator (a list of known block hashes). The receiving peer replies with a headers message carrying an ordered sequence of block headers: up to 2,000 per response.

The getheaders and headers message pair was introduced in Bitcoin protocol version 31800 (Bitcoin Core 0.3.18). It became the primary synchronization mechanism when headers-first sync shipped in Bitcoin Core 0.10.0 in February 2015, replacing the older getblocks/inv flow that downloaded full blocks sequentially from a single peer.

How It Works

The headers message follows a request-response pattern. A node that needs chain data initiates the exchange, and the peer responds with a batch of headers.

The Getheaders/Headers Exchange

  1. The requesting node constructs a getheaders message containing a block locator: a set of block hashes at exponentially increasing depth (tip, tip-1, tip-2, tip-4, tip-8, ... genesis)
  2. The receiving peer scans its chain for the first hash in the locator that matches a block it knows, identifying the fork point
  3. Starting from the block after the fork point, the peer serializes up to 2,000 consecutive block headers and sends them in a headers message
  4. The requesting node validates each header (checking proof-of-work, difficulty targets, and timestamps), appends valid headers to its chain, and sends another getheaders if more remain
  5. The loop continues until the peer responds with zero headers, indicating the requesting node is caught up

Wire Format

Every Bitcoin P2P message is wrapped in a 24-byte envelope containing the network magic bytes, a 12-byte command name (headers), the payload size, and a checksum. The headers message payload itself consists of:

Headers Message Payload
├── count        compactSize uint   (number of headers, max 2000)
└── headers[]    repeated:
    ├── version        4 bytes     (block version)
    ├── prev_block    32 bytes     (hash of previous block header)
    ├── merkle_root   32 bytes     (merkle root of transactions)
    ├── timestamp      4 bytes     (block timestamp)
    ├── bits           4 bytes     (difficulty target, nBits)
    ├── nonce          4 bytes     (mining nonce)
    └── txn_count      compactSize uint (always 0 in headers msg)

Each header entry is 81 bytes: the standard 80-byte block header plus a one-byte txn_count field that is always zero. The txn_count field exists because the header serialization format reuses the same structure as the beginning of a full block message, keeping the parser compatible across message types. With the maximum batch of 2,000 headers, a single headers message payload is approximately 162 KB.

Block Locator Strategy

The getheaders message includes a block locator object: a vector of block hashes that helps the peer identify the fork point efficiently. Rather than sending every known hash, the locator uses an exponential backoff pattern:

// Block locator construction (simplified)
// Start from the tip and step back exponentially
hashes = []
step = 1
height = tip_height
while height > 0:
    hashes.append(block_hash_at(height))
    if len(hashes) >= 10:
        step *= 2
    height -= step
hashes.append(genesis_hash)

This yields roughly 30 hashes for a chain of any length, keeping the getheaders message small while still allowing the peer to find the common ancestor quickly. The first 10 hashes step back one block at a time (catching recent forks), then the step size doubles with each subsequent entry.

Role in Headers-First Synchronization

Headers messages are the mechanism that makes headers-first sync possible. The sync process has two stages:

  1. Header download: the node repeatedly exchanges getheaders/headers messages with a peer until it has the complete header chain. At roughly 850,000 blocks, the entire header chain requires about 425 round trips (850,000 / 2,000) and transfers approximately 68 MB.
  2. Block download: once the best chain is identified by total chain work, the node fetches full blocks in parallel from multiple peers using getdata messages, downloading only blocks on the winning chain.

Why Headers Before Blocks?

The older blocks-first approach downloaded full blocks sequentially from a single peer. This had several problems:

  • Bandwidth waste on orphan chains: a peer could feed the node blocks from a low-difficulty fork, consuming gigabytes of bandwidth before the node realized it was on the wrong chain
  • Memory exhaustion from orphan blocks: blocks that arrived before their parent had to be stored in memory, creating a DoS vector
  • Single-peer bottleneck: syncing from one peer meant the entire process was limited by that peer's upload speed
  • Weak-chain attacks: an attacker could generate millions of small, low-difficulty blocks and keep the node busy validating a worthless fork

By downloading only 80-byte headers first, the node can verify proof-of-work and identify the heaviest chain at a fraction of the bandwidth cost, then commit to downloading full blocks only along that chain. For a detailed comparison, see the assumevalid fast sync deep dive.

Headers Messages During Normal Operation

Headers messages are not limited to initial sync. After BIP 130 (deployed in Bitcoin Core 0.12.0), nodes can signal the sendheaders preference during the version handshake. When this flag is active, peers announce new blocks by sending a headers message directly instead of an inv message, eliminating one round trip from the block relay sequence:

// Without sendheaders (pre-BIP 130):
Peer A -> inv(block_hash) -> Peer B
Peer B -> getheaders(locator) -> Peer A
Peer A -> headers(new_header) -> Peer B
Peer B -> getdata(block_hash) -> Peer A
Peer A -> block(full_block) -> Peer B

// With sendheaders (BIP 130):
Peer A -> headers(new_header) -> Peer B
Peer B -> getdata(block_hash) -> Peer A
Peer A -> block(full_block) -> Peer B

This is especially useful for block propagation: in the time saved by skipping the inv/getheaders round trip, blocks can reach the entire network faster. Modern nodes also use compact blocks (BIP 152) alongside headers for even more efficient relay.

Use Cases

Initial Block Download

The primary use case is bootstrapping a new Bitcoin node. The node exchanges headers messages with its peers to build the full header chain, then downloads blocks in parallel. Combined with assumevalid, which skips signature verification for historical blocks, this allows a fresh node to sync the full blockchain in hours rather than days. For more on this process, see the full node cost and requirements analysis.

Chain Reorganization Detection

When a node receives headers that fork from its current best chain, it compares total chain work to decide whether to switch. Headers messages let the node evaluate a competing chain's proof-of-work before downloading any of its block data, preventing bandwidth waste on weaker forks.

Light Client Verification

SPV clients and compact block filter clients rely on headers messages to maintain a verified header chain without downloading full blocks. By checking proof-of-work on each header, a light client can verify that transactions are included in the heaviest chain using Merkle proofs against the Merkle root in the corresponding header.

Risks and Considerations

Header Flooding Attacks

A malicious peer could attempt to overwhelm a node by sending massive numbers of headers for low-difficulty chains. Bitcoin Core mitigates this by requiring each header to meet its declared difficulty target. Since generating valid headers requires real proof-of-work, this makes flooding economically expensive. The node also limits how many header chains it tracks, discarding branches that fall too far behind the best known chain.

Eclipse Attacks

If an attacker controls all of a node's peer connections (an eclipse attack), they can present a fabricated header chain that the node will accept as the heaviest chain. The node would then request blocks from the attacker, receiving a complete but false view of the blockchain. Defenses include connecting to diverse peers across different network segments and using manual peer additions. For more on network-level threats, see the P2P network topology resilience analysis.

Duplicate Header Bandwidth

During normal operation, multiple peers may announce the same new block by sending unsolicited headers messages simultaneously. While this redundancy is harmless to correctness, it consumes extra bandwidth proportional to (number of peers minus 1) multiplied by 81 bytes per new block. For typical peer counts this overhead is negligible, but it increases during periods of rapid block production or when a node has many connections.

Protocol Version Compatibility

The getheaders/headers message pair requires protocol version 31800 or higher. All modern Bitcoin implementations support this, but custom or legacy software that speaks an older protocol version may fall back to the less efficient getblocks/inv flow. The sendheaders preference (BIP 130) requires protocol version 70012 or higher.

This glossary entry is for informational purposes only and does not constitute financial or investment advice. Always do your own research before using any protocol or technology.