Mapping the Connections: Deep Dive into `getpeerinfo`
4. Mapping the Connections: Deep Dive into getpeerinfo
If getnetworkinfo is the "Compass" that tells you the general direction, then getpeerinfo is the "High-Resolution Map" that shows you every individual street, building, and citizen. This command provides a detailed list of every single peer your node is currently talking to. For each peer, it gives you a wealth of "Granular Data": their IP address, how long they've been connected, how many blocks they've sent you, and even the "Latency" (the time it takes for a message to travel between you). It is the "Deep Audit of the Web." It is the "Census of the Neighbors." It is where the "Abstract Network" becomes a "Collection of Individuals." It is the micro-analysis of your digital social circle.
For the sovereign architect, this is the most important command in the network toolkit. It allows you to "Vet" your neighbors. Are you connected to high-quality, fast nodes that are giving you the latest information? Or are you connected to slow, "Leeching" nodes that are taking your data but giving nothing back? getpeerinfo provides the "Evidence" you need to make these decisions. It is the "Transparency of the Nervous System." It allows you to see the "Face of the Peer," ensuring that your connection to the truth is supported by reliable and honest actors. It is the "Audit of the Witness," the proof of the neighbor.
Analyzing the "Peer Audit" Logic in the Core: The Inspector
In the source code workshop (src/rpc/net.cpp), the node iterates through its internal list of "Peer Objects" (CNode). For each node, it extracts a snapshot of its current performance and identity. This process is protected by "Locks" to ensure that the network engine isn't disturbed while the audit is taking place. It is a process of "Taking a Census without Stopping the World." It is the "Precision of the Record," ensuring that the data you see is a "Frozen Moment" of the machine's reality. It is the high-fidelity capture of the web.
/**
* This function is the "Peer Inspector."
* It provides a detailed snapshot of every individual neighbor in the nervous system.
*/
static UniValue getpeerinfo(const JSONRPCRequest& request)
{
// 1. Access the Connection Manager (CConnman).
// This is the "Registry" of all active sockets and peers.
NodeContext& node = EnsureAnyNodeContext(request.context);
CConnman& connman = *node.connman;
// 2. Prepare the result list (A list of JSON objects).
UniValue ret(UniValue::VARR);
// 3. Ask the manager to provide a snapshot of all active nodes.
// We use "CopyNodeStats" to ensure we don't slow down the network engine
// while we are reading the data. It's a "Safe Snapshot."
std::vector<CNodeStats> vstats;
connman.GetNodeStats(vstats);
// 4. Iterate through every peer and format their detailed report.
for (const CNodeStats& stats : vstats) {
UniValue obj(UniValue::VOBJ);
// The unique local ID of the peer (1, 2, 3...).
obj.pushKV("id", stats.nodeid);
// The IP and Port (The physical location in the web).
obj.pushKV("addr", stats.addrName);
// The "Ping" time (The speed of the nerve in seconds).
obj.pushKV("pingtime", stats.m_ping_usec / 1000000.0);
// The volume of the conversation (Bytes).
obj.pushKV("bytessent", stats.nSendBytes);
obj.pushKV("bytesrecv", stats.nRecvBytes);
// How long have we known them? (In seconds).
obj.pushKV("conntime", stats.nTimeConnected);
// ... (The complete profile of the neighbor)
ret.push_back(obj);
}
return ret;
}
Explaining the Peer Data to a Non-Coder: The Neighbor’s Detailed Profile
-
id: Every time a peer connects to your node, they are assigned a unique "Node ID" (a simple number like 1, 2, or 3). This ID is local to your node and is used as a shorthand for management commands. If you want to disconnect a peer, you can just say "Disconnect Node 5" instead of typing their whole, long IP address. It is the "Temporary Name of the Stranger." It is how you address the individual in the crowd without getting lost in the numbers. It is the "Identity of the Connection," the local handle of the web. -
addr: This is the "Physical Address" (IP and Port) of the peer. It tells you where in the world the node is located. If you see many connections from the same IP range, it might indicate that a single company or person is trying to "Surround" your node. This is a red flag for an "Eclipse Attack." An architect uses this data to ensure their neighbors are "Diverse." You want friends from all over the world, not just from the same building. It is the "Identity of the Source," the coordinate of the individual. It is the map of the neighbor. -
pingtime: This is the "Latency" or "Round-Trip Time." It measures how many seconds it takes for a tiny "Ping" message to travel from your node to the peer and back again. A low ping (e.g., 0.05 seconds) means the peer is "Close" and "Fast." A high ping (e.g., 2.0 seconds) means the peer is "Far," "Lagging," or perhaps running over a slow satellite link. In the world of high-speed block propagation, every millisecond counts. You want the fastest nerves possible to stay synced with the truth. It is the "Speed of the Nerve." It is the pulse of the relationship. -
bytessentandbytesrecv: These numbers show the "Volume of the Conversation." If you have sent a peer 500 MB but they have only sent you 1 MB, that peer is a "Leech." They are using your bandwidth but giving nothing back to your view of the ledger. While Bitcoin is a generous network, a master architect will eventually prune these leeches to make room for peers who contribute more to the "Global Consensus." It is the "Audit of the Relationship," the proof of contribution. It is the ledger of the social link.
The Power of the High-Resolution Audit: Pruning for Quality
By running getpeerinfo, the architect can identify "Bad Neighbors." You can see who is slow, who is silent, and who is consuming too much of your resources. You can see which peers are running old software and which ones are up-to-date. This command turns the "Black Box" of the internet into a "Clear Map of Relationships." You are seeing the "Individual Units" of the global network and deciding exactly how much you want to rely on each one. You are the "Master of the Web," the one who knows exactly who is supporting your node and how well they are doing their job.
This command is the ultimate expression of "Network Transparency." It allows you to prove the health of your connection with data, not just faith. You are the "Master of the Map," the one who knows exactly who is supporting your node and how well they are doing their job. You are no longer just "Connected"; you are "Curated." It is the "Foundation of Network Sovereignty." You are the "Inspector of the Individual." You are the "Judge of the Web." You are the auditor of the crowd.
The Sovereign's View: RPC Reflection
When you look at getpeerinfo, look for the inflight field. It shows you which blocks the peer has promised to send you. If a peer has 10 blocks "In-Flight" but hasn't delivered them for minutes, they are "Stalling" your node. As an architect, you must ask: Is this peer "Dead-Weight"? Sovereignty means having the courage to "Disconnect" the unproductive. You are the "Guardian of the Queue." Every millisecond they stall you is a millisecond you are blind to the newest truth. You are the "Master of the Moment."
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: