TeachMeBitcoin

The Handshake: How Two Nodes Become Peers

From TeachMeBitcoin, the free encyclopedia Reading time: 5 min

12. The Handshake: How Two Nodes Become Peers

Opening a connection between two nodes is not as simple as "Plugging in a Cord." It is a formal, multi-step process called the Handshake. Before any blocks or transactions can be shared, the two nodes must "Introduce" themselves, "Agree" on the rules of the language they will speak, and "Verify" that they are both compatible with the network's current laws. It is the "Protocol of First Contact." It is the process of turning a "Stranger" into a "Peer." It is the "Sanctity of the Introduction." It is the "Treaty of the Node," the birth of the link.

The handshake is a critical "Gatekeeper" for security. It prevents old, broken, or malicious nodes from entering the nervous system and causing chaos. If a node tries to connect but speaks an "Obsolete Dialect" (an old protocol version) or belongs to an "Alien Chain" (a fork of Bitcoin like Signet or Testnet), the handshake will fail, and the connection will be closed immediately. It is the "Customs Control" of the digital border. It is the "Filter of Compatibility," ensuring the nervous system stays pure and functional. It is the protection of the handshake.

Analyzing the "Handshake" Logic in the Core: The Formal Declaration of Identity

In the source code (src/net_processing.cpp), we find the logic for the VERSION message. This is the "First Word" spoken by every node in the world. It contains a wealth of information about the node's capabilities, its software version, and its view of the current time. It is the "Opening Statement of the Relationship." It is the "Declaration of Reality." It is the "Handshake of the Core," the introduction of the sovereign.

/**
 * This snippet shows the node "Reading the Introduction" from a brand new stranger.
 * We check their credentials and version before we allow them to become a peer.
 */
void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, ...)
{
    if (msg_type == NetMsgType::VERSION) {
        // 1. Read the Peer's "Passport" (The version and services data).
        int nVersion;
        vRecv >> nVersion;

        // 2. Check the "Validity of the Language."
        // If they are speaking an old, unsupported dialect, we say "Goodbye" instantly.
        if (nVersion < MIN_PEER_PROTO_VERSION) {
            // "You are too old to understand the modern vault laws. Disconnecting."
            pfrom.fDisconnect = true;
            return;
        }

        // 3. Acknowledge the Introduction (The VERACK message).
        // "I have heard your identity, and I accept your rules and version."
        connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::VERACK));
    }
}

Explaining the Logic to a Non-Coder: The First Formal Meeting and the Rules

The "Services" Flag: What Skills and Data do you Have?

During the handshake, nodes also share their Services flag. This is a bitmask (a set of on/off switches) that tells the other node what "Skills" they have. For example, some nodes are "Full Nodes" (they have every block in history), while some are "Pruned Nodes" (they only have the last few days). Some nodes support "Bloom Filters," and some support "BIP 157" compact filters for privacy. This allows nodes to find the "Right Peer" for the right task. If you need to download the whole blockchain, you won't waste time talking to a pruned node. It is the "Portfolio of the Peer," the "Capability Statement," the resume of the connection.

By understanding the handshake, you are learning the "Rules of Digital Diplomacy." You are seeing that the Bitcoin network is not just a "Jumble of Random Connections," but a "Structured Society" built on mutual agreement, formal introductions, and rigorous verification. You are the "Master of the Handshake," the one who knows exactly why your node accepts some peers and rejects others. You are ensuring that your "Nervous System" is composed only of compatible, modern, and capable partners. It is the "Sovereignty of the Introduction." You are the "Gatekeeper of the Web," the "Guardian of the Handshake," the master of the link.

The Sovereign's View: RPC Reflection

When you look at the version field in getpeerinfo, you are seeing the "Age of the Mind" for that peer. As an architect, you must ask: Is my neighborhood "Stagnant"? If all your peers are on old versions, you are at risk if the network activates a new feature. This command is your "Modernity Report." It tells you if you are living in the past or the future. You are the "Master of the Version." You are the auditor of the mind.


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