The First Handshake: How the CLI Finds the Node
4. The First Handshake: How the CLI Finds the Node
The moment has arrived. You have typed your command, you have provided your credentials, and you have pressed the "Enter" key. At this exact microsecond, the bitcoin-cli program is loaded into your computer's memory (RAM), and the messenger is "Born." But before the messenger can deliver its JSON note, it must perform the most physically demanding part of its job: it must find the node and shake its hand.
In the world of networking, a "Handshake" is not a social gesture; it is a rigorous technical procedure. It is the moment where two separate programs, living in different parts of the computer or the world, prove to each other that they are alive, they are compatible, and they are ready to talk. If the handshake fails, the entire journey is aborted, and the messenger "Dies" (the program exits) with an error message. It is the "First Breath" of the connection.
The obtain_evhttp_connection_base Function: Dialing the Number
To handle the low-level mechanics of moving data across wires, the Bitcoin developers chose a world-class tool called libevent. This is a "Library"—a collection of specialized code written by some of the best networking engineers in the world. By using libevent, the Bitcoin team ensures that their bridge is built on the strongest possible foundations. It handles the "Plumbing" of the internet so that Bitcoin developers can focus on the "Architecture" of money.
The specific function that initiates the connection is called obtain_evhttp_connection_base. Let's look at how it is invoked in the CallRPC section of src/bitcoin-cli.cpp:
/**
* This section of code is the "Engine Start."
* It is where the messenger reaches out to the node.
*/
// 1. We create the "Connection Object" (evcon).
// We pass it the 'host' (the IP address) and the 'port' (the door number).
// This is the equivalent of the messenger picking up the phone and dialing.
raii_evhttp_connection evcon = obtain_evhttp_connection_base(base.get(), host, port);
// 2. We set the "Watch" (The Timeout).
// We are instructing the messenger: "Do not wait forever."
{
// We look up how much patience the user has configured (default: 900 seconds).
const int timeout = gArgs.GetIntArg("-rpcclienttimeout", 900);
if (timeout > 0) {
// We set the stopwatch. If the node doesnt answer in this time,
// the messenger will hang up the phone.
evhttp_connection_set_timeout(evcon.get(), timeout);
} else {
// If the user says timeout=0, we set a "Nearly Infinite" timeout.
// Even the most patient messenger eventually gives up (after 5 years).
evhttp_connection_set_timeout(evcon.get(), 5 * 31556952);
}
}
Explaining the Handshake for the Non-Coder
-
obtain_evhttp_connection_base: This is the "Search for Existence." The CLI sends a tiny "Packet" of data (a "SYN" packet) across your computer's internal network to the IP address and port we defined in the last chapter. If thebitcoindnode is running and listening on that port, it sees the packet and immediately sends back its own packet saying, "I hear you! I am ready!" This exchange takes less than a millisecond on a local computer. It is the moment the "Bridge" becomes a physical reality. It’s like the messenger knocking on the vault door and hearing the guard on the other side say, "Who's there?" It is the verification of the "Destination's Reality." -
evcon(The Connection Handle): Think ofevconas the "Open Phone Line." As long as this object exists in the computer's memory, the messenger and the node are "Linked." They can send and receive data as if they were in the same room. Theraii_prefix on the name is a technical term that means the connection will "Automatically Close" when the messenger finishes its job. This prevents the computer from leaving "Ghost Connections" open that would slow down your system. It is "Memory Citizenship"—the act of cleaning up after yourself in the digital world. -
evhttp_connection_set_timeout: This is the "Patience of the Machine." Imagine you sent a messenger to a store, but the store was permanently closed. If the messenger had no brain, they might stand in front of that closed door for eternity. Your computer would become clogged with thousands of "Stuck Messengers," eventually leading to a crash. This line of code is a safety valve. It says, "If you don't hear a response in 15 minutes, assume the node is dead and come back home." This is why, if your node is crashed, the CLI will wait for a bit and then say:error: could not connect to the server. It’s not a failure of the CLI; it’s the CLI successfully following its "Patience Instructions." It is the "Pragmatism of the Engine."
The Engineering Logic of the First Touch
Why is this first handshake so important? Because it is the "First Gate." In the world of high-performance software, we want to fail "Fast and Early." If the node is down, there is no point in calculating a complex cryptographic signature or building a 1,000-character JSON message. That would be a waste of your computer's CPU and battery power. It would be like writing a ten-page letter before checking if the post office is still in business.
By performing the handshake first, the CLI confirms that the destination exists before it starts any of the expensive work. It is a "Sanity Check" that ensures the bridge is stable before we try to drive a heavy truck across it. It is the "Foundation Stone" of the communication journey, the moment where the "Abstract" idea of a command meets the "Physical" world of electricity moving through silicon. Without this successful handshake, the Bitcoin network is just a series of disconnected computers; with it, it is a global, living organism. It is the "Genesis" of every single interaction.
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: