The Peer Discovery: How nodes find each other (`ADDR`/`GETADDR`)
5. The Peer Discovery: How nodes find each other (ADDR/GETADDR)
A new node wakes up in a "Dark Room." It doesn't know where any of its friends are. To find the rest of the network, it must perform Peer Discovery. This is the process of building a "Contact List" of IP addresses. This list is managed by the Address Manager (AddrMan) and the messages GETADDR and ADDR.
For the Sovereign Architect, Peer Discovery is the "Social Map" of the network. It is how your node "Spreads its Wings" and finds a diverse group of peers across the globe. A node with a good contact list is "Resilient"—it is much harder for a hacker to "Isolate" it from the truth.
Analyzing the Discovery: GETADDR and ADDR
In the source code (src/net.cpp and src/addrman.h), we see how nodes "Gossip" about their friends.
/**
* PEDAGOGICAL ANALYSIS: THE SOCIAL GOSSIP
* This logic allows nodes to share their "Contact Lists" with each other.
*/
void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, ...)
{
// 1. If a peer sends a "GETADDR", we send them a random
// selection of addresses from our contact list.
if (msg_type == NetMsgType::GETADDR) {
PushAddress(pfrom);
}
// 2. If a peer sends an "ADDR", we add those new
// addresses to our "Address Manager" (AddrMan).
if (msg_type == NetMsgType::ADDR) {
std::vector<CAddress> vAddr;
vRecv >> vAddr;
m_addrman.Add(vAddr, pfrom.GetAddr());
}
}
Explaining the Discovery: The Word of Mouth
-
GETADDR: Imagine walking into a room and saying: "Does anyone know where I can find more Bitcoiners?" That is aGETADDRmessage. It is the Request for the Community. -
PushAddress: When your node responds to aGETADDR, it doesn't send its entire contact list. It sends a "Random Sample." This is a security feature—it prevents a hacker from "Mapping" the entire network by just asking one node. It is the Privacy of the Gossip. -
m_addrman.Add: The Address Manager is the "Address Book" on your disk. When it hears about a new peer, it doesn't immediately try to connect. Instead, it "Notes" the address and assigns it a "Reliability Score." Over time, the addresses that stay "Alive" move to the "Tried" bucket, while new addresses stay in the "New" bucket. It is the Memory of the Neighborhood.
The Sovereignty of the Map
A node is only as strong as its connections. By building a diverse contact list, your node ensures it is "Connected to the World." As a Sovereign Architect, you know that a "Global Map" is your best defense against censorship. By enforcing the gossip rules, your node ensures that the "Digital Web" of Bitcoin remains dense, chaotic, and impossible to break. You are the "Master of the Map," the one who ensures the "Digital Social Circle" of your machine is always expanding.
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: