Skip to content

Commit 4829b6f

Browse files
committed
[refactor] Simplify connection type logic in ThreadOpenConnections
Consolidate the logic to determine connection type into one conditional to clarify how they are chosen.
1 parent 1e563ae commit 4829b6f

File tree

1 file changed

+24
-37
lines changed

1 file changed

+24
-37
lines changed

src/net.cpp

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,28 +1856,32 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18561856
}
18571857
}
18581858

1859-
// Feeler Connections
1860-
//
1861-
// Design goals:
1862-
// * Increase the number of connectable addresses in the tried table.
1863-
//
1864-
// Method:
1865-
// * Choose a random address from new and attempt to connect to it if we can connect
1866-
// successfully it is added to tried.
1867-
// * Start attempting feeler connections only after node finishes making outbound
1868-
// connections.
1869-
// * Only make a feeler connection once every few minutes.
1870-
//
1859+
ConnectionType conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
1860+
int64_t nTime = GetTimeMicros();
18711861
bool fFeeler = false;
18721862

1873-
if (nOutboundFullRelay >= m_max_outbound_full_relay && nOutboundBlockRelay >= m_max_outbound_block_relay && !GetTryNewOutboundPeer()) {
1874-
int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
1875-
if (nTime > nNextFeeler) {
1876-
nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1877-
fFeeler = true;
1878-
} else {
1879-
continue;
1880-
}
1863+
// Determine what type of connection to open. Opening
1864+
// OUTBOUND_FULL_RELAY connections gets the highest priority until we
1865+
// meet our full-relay capacity. Then we open BLOCK_RELAY connection
1866+
// until we hit our block-relay-only peer limit.
1867+
// GetTryNewOutboundPeer() gets set when a stale tip is detected, so we
1868+
// try opening an additional OUTBOUND_FULL_RELAY connection. If none of
1869+
// these conditions are met, check the nNextFeeler timer to decide if
1870+
// we should open a FEELER.
1871+
1872+
if (nOutboundFullRelay < m_max_outbound_full_relay) {
1873+
// OUTBOUND_FULL_RELAY
1874+
} else if (nOutboundBlockRelay < m_max_outbound_block_relay) {
1875+
conn_type = ConnectionType::BLOCK_RELAY;
1876+
} else if (GetTryNewOutboundPeer()) {
1877+
// OUTBOUND_FULL_RELAY
1878+
} else if (nTime > nNextFeeler) {
1879+
nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1880+
conn_type = ConnectionType::FEELER;
1881+
fFeeler = true;
1882+
} else {
1883+
// skip to next iteration of while loop
1884+
continue;
18811885
}
18821886

18831887
addrman.ResolveCollisions();
@@ -1944,23 +1948,6 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
19441948
LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
19451949
}
19461950

1947-
ConnectionType conn_type;
1948-
// Determine what type of connection to open. If fFeeler is not
1949-
// set, open OUTBOUND connections until we meet our full-relay
1950-
// capacity. Then open BLOCK_RELAY connections until we hit our
1951-
// block-relay peer limit. Otherwise, default to opening an
1952-
// OUTBOUND connection.
1953-
if (fFeeler) {
1954-
conn_type = ConnectionType::FEELER;
1955-
} else if (nOutboundFullRelay < m_max_outbound_full_relay) {
1956-
conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
1957-
} else if (nOutboundBlockRelay < m_max_outbound_block_relay) {
1958-
conn_type = ConnectionType::BLOCK_RELAY;
1959-
} else {
1960-
// GetTryNewOutboundPeer() is true
1961-
conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
1962-
}
1963-
19641951
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, conn_type);
19651952
}
19661953
}

0 commit comments

Comments
 (0)