Ethereum: How Bitcoin Finds Lookalikes?
When starting a Bitcoin client from scratch, one of the most critical components is peer discovery on the network. In this article, we will explore how Bitcoin finds its competitors and explore the intricacies behind this process.
Bitcoin Core Codebase
To understand how Bitcoin finds peers, let’s first look at the code base. The Bitcoin core code is open source under the MIT license, which provides access to the underlying algorithms and data structures. Here are some key components that enable peer discovery:
NetworkManager
The NetworkManager is a critical component responsible for managing connections between nodes on a network. It is responsible for creating and maintaining connections with other nodes.
// src/daemon/main.c (Line 123)
void daemon_addnode(int argc, char *argv[]) {
// ...
NetworkManager(network_manager, NULL);
}
NetworkManager creates a new network connection using CreateConnection():
// src/daemon/main.c (Line 143)
int CreateConnection(const char hostname, const char port) {
// ...
return AddPeer(hostname, port, NULL, NULL);
}
int AddPeer(const char hostname, int port, const char path, const void *data) {
// ...
return peer_addpeer(network_manager->nodes[0], hostname, port, path, data);
}
The AddPeer() function is called by a client to add a new node to the network. It takes the hostname, port, and optional path and data as arguments.
PeerList
PeerList' is an array of peers currently connected to the network:
// src/daemon/main.c (Line 172)
struct peer_list {
struct peer *peers;
size_t npeers;
};
The AddPeer()function creates a newpeerstructure and adds it to thePeerList.
PeerManager
ThePeerManageris responsible for managing multiple connections. Provides methods to create, remove, and manage peers:
// src/daemon/main.c (Line 193)
struct peer_manager {
struct network *net;
struct peer_list *peers;
};
The AddPeer()function creates a new peer and adds it to the list.
PeerData
PeerDatais used to store metadata about each peer:
// src/daemon/main.c (Line 201)
struct peer_data {
uint256 balance;
uint256 number_of_transactions;
};
How does Bitcoin find peers?
Bitcoin uses a combination of algorithms and data structures to find similar users online. Here's a simplified overview of the process:
- Node Discovery: When you start the Bitcoin client, it sends a discovery packet to all nodes on the network using GetDiscoveryPacket()
.
- Peer List Update
![Ethereum: How does bitcoin find peers? [duplicate]](https://segalsoftplay.com/wp-content/uploads/2025/02/7bded78d.png)
: A node receiving a discovery packet updates its peer list and adds any new peers from the packet to the list.
- Query for PeerList: The client periodically queries the peer list for available peers:
// src/daemon/main.c (Line 221)
struct peer_list *peer_list_getpeerlist();
void peer_list_query(struct peer_list *peers) {
// ...
}
- Node Selection: Based on the available peers, the client selects a node to connect to using SelectNode()
.
- Connection Establishment: Once connected, the client establishes a connection to the selected node usingConnectToNode()
.
- Peer List Update: Once a connection is established, the peer list is updated to reflect any changes in the network:
// src/daemon/main.c (Line 236)
void peer_list_update() {
// ...
}
Conclusion
In short, Bitcoin's peer discovery engine relies on NetworkManager,PeerList,PeerData` and other components to manage connections between nodes.
