Skip to content

Commit 2f2e13b

Browse files
committed
[net/refactor] Simplify multiple-connection checks
Extract logic that check multiple connection types into interface functions & structure as switch statements. This makes it very clear what touch points are for accessing `m_conn_type` & using the switch statements enables the compiler to warn if a new connection type is introduced but not handled for these cases.
1 parent 7f7b83d commit 2f2e13b

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ void CConnman::ThreadDNSAddressSeed()
16481648
{
16491649
LOCK(cs_vNodes);
16501650
for (const CNode* pnode : vNodes) {
1651-
nRelevant += pnode->fSuccessfullyConnected && !pnode->IsFeelerConn() && !pnode->IsAddrFetchConn() && !pnode->IsManualConn() && !pnode->IsInboundConn();
1651+
if (pnode->fSuccessfullyConnected && pnode->IsOutboundOrBlockRelayConn()) ++nRelevant;
16521652
}
16531653
}
16541654
if (nRelevant >= 2) {
@@ -1758,7 +1758,7 @@ int CConnman::GetExtraOutboundCount()
17581758
{
17591759
LOCK(cs_vNodes);
17601760
for (const CNode* pnode : vNodes) {
1761-
if (!pnode->IsInboundConn() && !pnode->IsManualConn() && !pnode->IsFeelerConn() && !pnode->fDisconnect && !pnode->IsAddrFetchConn() && pnode->fSuccessfullyConnected) {
1761+
if (pnode->fSuccessfullyConnected && !pnode->fDisconnect && pnode->IsOutboundOrBlockRelayConn()) {
17621762
++nOutbound;
17631763
}
17641764
}

src/net.h

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

792+
bool IsOutboundOrBlockRelayConn() const {
793+
switch(m_conn_type) {
794+
case ConnectionType::OUTBOUND:
795+
case ConnectionType::BLOCK_RELAY:
796+
return true;
797+
case ConnectionType::INBOUND:
798+
case ConnectionType::MANUAL:
799+
case ConnectionType::ADDR_FETCH:
800+
case ConnectionType::FEELER:
801+
return false;
802+
}
803+
804+
assert(false);
805+
}
806+
792807
bool IsFullOutboundConn() const {
793808
return m_conn_type == ConnectionType::OUTBOUND;
794809
}

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
829829

830830
static bool IsOutboundDisconnectionCandidate(const CNode& node)
831831
{
832-
return !(node.IsInboundConn() || node.IsManualConn() || node.IsFeelerConn() || node.IsAddrFetchConn());
832+
return node.IsOutboundOrBlockRelayConn();
833833
}
834834

835835
void PeerLogicValidation::InitializeNode(CNode *pnode) {
@@ -2324,7 +2324,7 @@ void ProcessMessage(
23242324
{
23252325
connman.SetServices(pfrom.addr, nServices);
23262326
}
2327-
if (!pfrom.IsInboundConn() && !pfrom.IsFeelerConn() && !pfrom.IsManualConn() && !HasAllDesirableServiceFlags(nServices))
2327+
if (pfrom.ExpectServicesFromConn() && !HasAllDesirableServiceFlags(nServices))
23282328
{
23292329
LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom.GetId(), nServices, GetDesirableServiceFlags(nServices));
23302330
pfrom.fDisconnect = true;

0 commit comments

Comments
 (0)