TeachMeBitcoin

The Postman (P2P Network)

From TeachMeBitcoin, the free encyclopedia Reading time: 5 min

The Postman (src/net.cpp): How Nodes Talk to Each Other

If Bitcoin is a global conversation, the code in src/net.cpp is the Postman. He handles the physical connections between your computer and thousands of other computers around the world. Without the Postman, your node would be "deaf and dumb"—it wouldn't know about new transactions being sent in Tokyo or new blocks being mined in Reykjavik. The networking architecture is what turns a single computer into a vital organ of the global Bitcoin body.

In a world where trust is non-existent, the Postman must be as suspicious as the Watchman, ensuring that no one can flood your house with junk mail or "clog" your phone line.

The Peer-to-Peer (P2P) Miracle: No Central Server

Most programs you use (like Facebook, Netflix, or your Bank app) have a "Central Server." Your computer talks to a giant building owned by a corporation. If that building catches fire, the service dies. Bitcoin is different. It uses a Peer-to-Peer (P2P) network. Your computer talks directly to other people's computers, who talk to more computers, and so on.

The Postman has three main responsibilities in this decentralized world:

  1. Peer Discovery (Finding Friends): How does your computer find other Bitcoin nodes when you first turn it on? The Postman uses "DNS Seeds" (pre-set digital address books) and local databases of addresses from the last time you were online. He tries to find at least 8 "Outbound" peers to ensure a stable connection.

  2. The Handshake (Verification): When your Postman meets another Postman, they don't just start trading money. They perform a "Handshake." Your node says: "Hi, I'm running Bitcoin Version 27.0, and my clock says it's 10:00 AM. Who are you?" If the other node is running an incompatible version, the Postman politely hangs up.

  3. Inventory Relay (The "Hey, I Heard a Secret" System): Instead of sending every transaction to everyone (which would crash the internet), the Postman sends a tiny "INV" (Inventory) message. It’s like saying: "Hey, I just heard about a new transaction with ID #ABC. Do you want to see it?" If the other node says "Yes, please," the Postman sends the full data. This makes the network incredibly efficient.

Managing the Crowd: The CConnman (Connection Manager)

The Postman is constantly monitoring the "health" of its connections. This logic is handled by the CConnman (The Connection Manager). He is like a bouncer at a club who is also checking everyone's temperature and making sure nobody is starting a fight.

// src/net.cpp - The Postman checking if the house is full
void CConnman::AcceptConnection(const AcceptParam& accept_param) {
 // 1. A new node is knocking on the door.
 // 2. We check our current number of connections.
 // 3. If we are already at our limit (e.g., 125 nodes), we say no.
 if (m_nodes.size() >= (size_t)m_max_connections) {
 // "Sorry, we're at capacity! Try the node next door."
 return;
 }
 // 4. We check if this person has been "banned" before.
 if (IsBanned(accept_param.addr)) {
 return; // "You're not welcome here!"
 }
}

The Non-Coder's Technical Deep Dive: Imagine you're hosting a global dinner party. Your house only has 10 chairs. When the 11th person knocks on the door, the Postman (the host) checks the chairs and politely tells them there's no room.

But what if someone keeps knocking on the door 100 times a second? This is called a Denial of Service (DoS) Attack. The Postman is trained to recognize this behavior. He sees that the same person is being annoying, and he "Bans" their IP address for 24 hours. He effectively blocks their phone number so they can't even knock anymore. This protects your computer from being overwhelmed by hackers or bots.

Traffic Control and Throttling: The Good Citizen

The Postman is also a "Traffic Cop." He makes sure that Bitcoin doesn't use all your internet bandwidth. If you've ever used a program that made your whole internet slow, it's because it didn't have a good Postman.

The "Eclipse" Protection: Avoiding the Dark Room

A dangerous attack in Bitcoin is called an Eclipse Attack. This is where a hacker surrounds your node with 8 of their nodes. You think you're talking to the world, but you're actually only talking to the hacker. They can then lie to you about the price of Bitcoin or whether a transaction was sent. The Postman prevents this by:

The Netgroup Logic: Ensuring Global Diversity

The Postman uses something called "Netgroups" to ensure he isn't talking to 8 people who all live in the same house (or the same data center). He checks the "Address" of every peer and tries to pick a diverse set. This ensures that even if one part of the internet (like a whole country) goes offline, your node stays connected to the rest of the world.

Summary of Section 6

The Postman in src/net.cpp is what makes Bitcoin a "Network" rather than just a piece of software. By managing thousands of simultaneous connections, discovery peers, and relaying data efficiently, the Postman ensures that the global ledger remains synchronized across every continent, in real-time, forever. He is the guardian of the network's connectivity and the primary defense against digital isolation.



☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!