Skip to content

Commit 7f7b83d

Browse files
committed
[net/refactor] Rework ThreadOpenConnections logic
Make the connection counts explicit and extract into interface functions around m_conn_type. Using explicit counting and switch statements where possible should help prevent counting bugs in the future.
1 parent 35839e9 commit 7f7b83d

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/net.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,21 +1829,27 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18291829
int nOutboundFullRelay = 0;
18301830
int nOutboundBlockRelay = 0;
18311831
std::set<std::vector<unsigned char> > setConnected;
1832+
18321833
{
18331834
LOCK(cs_vNodes);
18341835
for (const CNode* pnode : vNodes) {
1835-
if (!pnode->IsInboundConn() && (pnode->m_conn_type != ConnectionType::MANUAL)) {
1836-
// Netgroups for inbound and addnode peers are not excluded because our goal here
1837-
// is to not use multiple of our limited outbound slots on a single netgroup
1838-
// but inbound and addnode peers do not use our outbound slots. Inbound peers
1839-
// also have the added issue that they're attacker controlled and could be used
1840-
// to prevent us from connecting to particular hosts if we used them here.
1841-
setConnected.insert(pnode->addr.GetGroup(addrman.m_asmap));
1842-
if (pnode->m_tx_relay == nullptr) {
1843-
nOutboundBlockRelay++;
1844-
} else if (pnode->m_conn_type == ConnectionType::OUTBOUND) {
1845-
nOutboundFullRelay++;
1846-
}
1836+
if (pnode->IsFullOutboundConn()) nOutboundFullRelay++;
1837+
if (pnode->IsBlockOnlyConn()) nOutboundBlockRelay++;
1838+
1839+
// Netgroups for inbound and manual peers are not excluded because our goal here
1840+
// is to not use multiple of our limited outbound slots on a single netgroup
1841+
// but inbound and manual peers do not use our outbound slots. Inbound peers
1842+
// also have the added issue that they could be attacker controlled and used
1843+
// to prevent us from connecting to particular hosts if we used them here.
1844+
switch(pnode->m_conn_type){
1845+
case ConnectionType::INBOUND:
1846+
case ConnectionType::MANUAL:
1847+
break;
1848+
case ConnectionType::OUTBOUND:
1849+
case ConnectionType::BLOCK_RELAY:
1850+
case ConnectionType::ADDR_FETCH:
1851+
case ConnectionType::FEELER:
1852+
setConnected.insert(pnode->addr.GetGroup(addrman.m_asmap));
18471853
}
18481854
}
18491855
}

src/net.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,18 @@ class CNode
789789
std::atomic_bool fPauseRecv{false};
790790
std::atomic_bool fPauseSend{false};
791791

792+
bool IsFullOutboundConn() const {
793+
return m_conn_type == ConnectionType::OUTBOUND;
794+
}
795+
792796
bool IsManualConn() const {
793797
return m_conn_type == ConnectionType::MANUAL;
794798
}
795799

800+
bool IsBlockOnlyConn() const {
801+
return m_conn_type == ConnectionType::BLOCK_RELAY;
802+
}
803+
796804
bool IsFeelerConn() const {
797805
return m_conn_type == ConnectionType::FEELER;
798806
}
@@ -805,6 +813,21 @@ class CNode
805813
return m_conn_type == ConnectionType::INBOUND;
806814
}
807815

816+
bool ExpectServicesFromConn() const {
817+
switch(m_conn_type) {
818+
case ConnectionType::INBOUND:
819+
case ConnectionType::MANUAL:
820+
case ConnectionType::FEELER:
821+
return false;
822+
case ConnectionType::OUTBOUND:
823+
case ConnectionType::BLOCK_RELAY:
824+
case ConnectionType::ADDR_FETCH:
825+
return true;
826+
}
827+
828+
assert(false);
829+
}
830+
808831
protected:
809832
mapMsgCmdSize mapSendBytesPerMsgCmd;
810833
mapMsgCmdSize mapRecvBytesPerMsgCmd GUARDED_BY(cs_vRecv);

0 commit comments

Comments
 (0)