The Blockchain Chief Bitcoin Book / Part IV: The Network
Chapter 08

P2P Networking

How Bitcoin nodes find each other, exchange blocks and transactions, synchronize the chain, and protect against attacks on the peer-to-peer network.

Network Architecture

Bitcoin is a peer-to-peer network with no central server. Every node is equal; there's no hierarchy. The network's job is to propagate blocks and transactions to every node as quickly as possible.

๐Ÿ”Œ

net.cpp

Low-level connection management. TCP sockets, reading/writing bytes, managing the peer list.

๐Ÿ“จ

net_processing.cpp

High-level message handling. Interprets protocol messages, coordinates block/tx relay.

๐Ÿ“‹

AddrMan

Address manager. Stores known peer addresses, handles DNS seeding, peer discovery.

Connection Types

Bitcoin Core maintains different types of connections:

Full Outbound8 connections we initiate, our primary peers for block relay
Block-Only Outbound2 connections for blocks only (no tx relay), increases connectivity
InboundUp to 117 connections initiated by others, we serve data to them
FeelerShort-lived connections to test if addresses are reachable
AnchorPersisted connections we reconnect to on restart (eclipse attack protection)

Peer Discovery & Connection

How does a Bitcoin node find other nodes?

  1. DNS Seeds: On first start, the node queries hardcoded DNS seeds (e.g., seed.bitcoin.sipa.be) to get a list of active nodes.
  2. ADDR messages: Connected peers share addresses of other nodes they know about.
  3. Hardcoded seeds: As a final fallback, a list of IP addresses is compiled into the binary.
  4. Manual (-addnode): Users can specify peers directly.

The Version Handshake

Every new connection starts with a version handshake:

โ†’ VERSIONInitiator sends protocol version, services, best height, local address
โ†“
โ† VERSIONResponder sends their VERSION back
โ†“
โ†’ WTXIDRELAY, SENDADDRV2Negotiate feature support before VERACK
โ†“
โ†” VERACKBoth sides acknowledge, connection is now active
โ†“
โ†’ SENDCMPCT, SENDHEADERS, FEEFILTERPost-handshake feature negotiation

P2P Message Types

The Bitcoin P2P protocol uses ~25 message types. Here are the most important ones:

Block-Related

HEADERSUp to 2,000 block headers (for headers-first sync)
GETHEADERSRequest headers starting from a known block hash
BLOCKA full block (header + all transactions)
CMPCTBLOCKCompact block, header + short tx IDs (BIP 152)
GETBLOCKTXNRequest missing transactions for a compact block
BLOCKTXNResponse with the requested transactions

Transaction-Related

INVAnnounce available transactions/blocks (inventory vectors)
GETDATARequest specific transactions or blocks by hash
TXA full transaction
NOTFOUNDResponse when requested data isn't available

Network Management

ADDR / ADDRV2Share known peer addresses (ADDRV2 supports Tor, I2P)
PING / PONGKeepalive and latency measurement
FEEFILTERTell peers your minimum relay fee rate (avoid sending txs you'll reject)

Block Synchronization

When a node starts up (or falls behind), it needs to download and validate the entire blockchain. This uses headers-first synchronization:

Phase 1: Download Headers

  1. Send GETHEADERS with our best known block hash
  2. Peer responds with up to 2,000 headers
  3. Validate each header's PoW and chain continuity
  4. Repeat until we reach the peer's tip
  5. This is fast, headers are only 80 bytes each

Phase 2: Download Blocks

  1. Now that we know the full header chain, request blocks in parallel from multiple peers
  2. Download blocks out of order for speed, but validate in order
  3. Use GETDATA messages to request specific blocks
  4. Each block is validated and connected as it arrives (if in sequence)
โœ… AssumeValid Optimization

During initial sync, Bitcoin Core skips script verification for blocks below the assumevalid hash (a recent block known to be valid). This dramatically speeds up IBD, UTXO updates are still performed, but expensive signature checks are skipped.

Compact Blocks (BIP 152)

Once synced, new blocks propagate using compact block relay:

  1. Sender transmits a CMPCTBLOCK, block header + short 6-byte IDs for each transaction
  2. Receiver reconstructs the block using transactions it already has in its mempool
  3. If any transactions are missing, request them with GETBLOCKTXN
  4. This typically avoids transmitting the full block entirely, since most transactions are already in the mempool

High-Bandwidth vs. Low-Bandwidth Mode

Transaction Relay

Transaction propagation is carefully designed for both speed and privacy:

Relay Process

  1. Node validates a new transaction (ATMP succeeds)
  2. Sends INV messages to all peers it wants to announce to
  3. Interested peers respond with GETDATA
  4. Node sends the full TX message

Privacy Protections

Address Manager (AddrMan)

AddrMan stores peer addresses using a bucketed structure designed to resist eclipse attacks:

Encrypted P2P (BIP 324)

BIP 324 (v2 transport protocol) adds encryption to Bitcoin P2P connections:

Encryption doesn't provide authentication (you don't know who you're talking to), but it prevents passive eavesdroppers from seeing which transactions you relay - important for privacy.

DoS Protection

The P2P layer includes several mechanisms to protect against denial-of-service attacks: