mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-28 18:01:27 +02:00
[refactor] Simplify connection type logic in ThreadOpenConnections
Consolidate the logic to determine connection type into one conditional to clarify how they are chosen.
This commit is contained in:
parent
1e563aed78
commit
4829b6fcc6
53
src/net.cpp
53
src/net.cpp
@ -1856,29 +1856,33 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Feeler Connections
|
ConnectionType conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
|
||||||
//
|
int64_t nTime = GetTimeMicros();
|
||||||
// Design goals:
|
|
||||||
// * Increase the number of connectable addresses in the tried table.
|
|
||||||
//
|
|
||||||
// Method:
|
|
||||||
// * Choose a random address from new and attempt to connect to it if we can connect
|
|
||||||
// successfully it is added to tried.
|
|
||||||
// * Start attempting feeler connections only after node finishes making outbound
|
|
||||||
// connections.
|
|
||||||
// * Only make a feeler connection once every few minutes.
|
|
||||||
//
|
|
||||||
bool fFeeler = false;
|
bool fFeeler = false;
|
||||||
|
|
||||||
if (nOutboundFullRelay >= m_max_outbound_full_relay && nOutboundBlockRelay >= m_max_outbound_block_relay && !GetTryNewOutboundPeer()) {
|
// Determine what type of connection to open. Opening
|
||||||
int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
|
// OUTBOUND_FULL_RELAY connections gets the highest priority until we
|
||||||
if (nTime > nNextFeeler) {
|
// meet our full-relay capacity. Then we open BLOCK_RELAY connection
|
||||||
|
// until we hit our block-relay-only peer limit.
|
||||||
|
// GetTryNewOutboundPeer() gets set when a stale tip is detected, so we
|
||||||
|
// try opening an additional OUTBOUND_FULL_RELAY connection. If none of
|
||||||
|
// these conditions are met, check the nNextFeeler timer to decide if
|
||||||
|
// we should open a FEELER.
|
||||||
|
|
||||||
|
if (nOutboundFullRelay < m_max_outbound_full_relay) {
|
||||||
|
// OUTBOUND_FULL_RELAY
|
||||||
|
} else if (nOutboundBlockRelay < m_max_outbound_block_relay) {
|
||||||
|
conn_type = ConnectionType::BLOCK_RELAY;
|
||||||
|
} else if (GetTryNewOutboundPeer()) {
|
||||||
|
// OUTBOUND_FULL_RELAY
|
||||||
|
} else if (nTime > nNextFeeler) {
|
||||||
nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
|
nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
|
||||||
|
conn_type = ConnectionType::FEELER;
|
||||||
fFeeler = true;
|
fFeeler = true;
|
||||||
} else {
|
} else {
|
||||||
|
// skip to next iteration of while loop
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
addrman.ResolveCollisions();
|
addrman.ResolveCollisions();
|
||||||
|
|
||||||
@ -1944,23 +1948,6 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
|||||||
LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
|
LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionType conn_type;
|
|
||||||
// Determine what type of connection to open. If fFeeler is not
|
|
||||||
// set, open OUTBOUND connections until we meet our full-relay
|
|
||||||
// capacity. Then open BLOCK_RELAY connections until we hit our
|
|
||||||
// block-relay peer limit. Otherwise, default to opening an
|
|
||||||
// OUTBOUND connection.
|
|
||||||
if (fFeeler) {
|
|
||||||
conn_type = ConnectionType::FEELER;
|
|
||||||
} else if (nOutboundFullRelay < m_max_outbound_full_relay) {
|
|
||||||
conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
|
|
||||||
} else if (nOutboundBlockRelay < m_max_outbound_block_relay) {
|
|
||||||
conn_type = ConnectionType::BLOCK_RELAY;
|
|
||||||
} else {
|
|
||||||
// GetTryNewOutboundPeer() is true
|
|
||||||
conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, conn_type);
|
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, conn_type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user