TeachMeBitcoin

The First Handshake: How the CLI Finds the Node

From TeachMeBitcoin, the free encyclopedia Reading time: 5 min

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

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.


☕ 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!