tip: 547
title: Connection precheck before P2P communication
author: wb_bupt@163.com
status: Final
type: Standards Track
category: Networking
date: 2023-05-22
This TIP is to specify how to establish a p2p connection effectively and quickly through the node precheck.
The node to be connected is selected by the order of the node update time, but it is impossible to know whether the other party can receive the connection. In the actual scene, the probability of the connection being rejected is rather high. The main reason for the rejection is the great number of peers in the network. To solve this problem, the node detection function is developed, which can detect whether the node can be connected in advance, avoiding invalid connections all through, which greatly improves the efficiency of connection establishment.
Through the node precheck, the node status is negotiated in advance to achieve effective and fast connection establishment and avoid a large number of invalid connections, which affect the efficiency of block synchronization.
- The function is able to obtain the status of nodes in advance.
- Selecting nodes according to their status so that improves the efficiency of connection establishment.
- Try to establish a connection with a node that has a relatively large number of connections remaining.
Try to establish a TCP connection with the node in advance, check whether the node is still online and see if the connection can be successfully established. After the TCP connection is established, exchange a pair of status messages to obtain node-related information, such as maxConnections
, currentConnections
, to determine whether the node accepts other connections
The status message is designed as follows:
message StatusMessage {
Endpoint from = 1;
int32 version = 2;
int32 network_id = 3;
int32 maxConnections = 4;
int32 currentConnections = 5;
int64 timestamp = 6;
}
version
: the p2p version of the node
network_id
: the network id of the node
maxConnections
: the maximum number of connections for the node
currentConnections
: the number of connections the node has established
Constant settings:
- Set the maximum detection of nodes to 300.
- The node detection thread is scheduled every 5s. According to the latest node update time, 3 or 10 nodes are detected each time accordingly.
- If the detection fails, it would be added to the detection failure cache, and the node could not be detected for 1 hour.
Functional logic: When the system starts, a new queue is created to store nodes that are being detected and a thread is started to do node detection.
- When the queue size is less than 200, fast detection is performed, and 100 nodes are detected each time.
- When the queue size is more than 200, slow detection is performed, and confirm the number of nodes detected each time according to the update time of the last node.
- If the update time of the last updated node is less than 5 minutes, detect 3 nodes each time
- If the update time of the last updated node is more than 5 minutes, detect 10 nodes each time
- If the detection fails or the node’s connections are full, then remove the node from the queue, write to the detection failure cache, and stop detecting the node for 1 hour
External Interface:
public List<Node> getConnectableNodes()
returns a list of nodes that have successfully detected.