Skip to content

Commit 655b195

Browse files
committed
[net processing] Continue SendMessages processing if not disconnecting peer
If we don't disconnect a peer in MaybeDiscourageAndDisconnect because it has NOBAN permissions or it's a manual connection, continue SendMessages processing rather than exiting early. The previous behaviour was that we'd miss the SendMessages processing on this iteration of the MessageHandler loop. That's not a problem since SendMessages() would just be called again on the next iteration, but it was slightly inefficient and confusing.
1 parent a49781e commit 655b195

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

src/net_processing.cpp

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3557,32 +3557,49 @@ void ProcessMessage(
35573557
return;
35583558
}
35593559

3560+
/** Maybe disconnect a peer and discourage future connections from its address.
3561+
*
3562+
* @param[in] pnode The node to check.
3563+
* @return True if the peer was marked for disconnection in this function
3564+
*/
35603565
bool PeerLogicValidation::MaybeDiscourageAndDisconnect(CNode& pnode)
35613566
{
3562-
LOCK(cs_main);
3563-
CNodeState &state = *State(pnode.GetId());
3567+
NodeId peer_id{pnode.GetId()};
3568+
{
3569+
LOCK(cs_main);
3570+
CNodeState &state = *State(peer_id);
35643571

3565-
if (state.m_should_discourage) {
3572+
// There's nothing to do if the m_should_discourage flag isn't set
3573+
if (!state.m_should_discourage) return false;
3574+
3575+
// Reset m_should_discourage
35663576
state.m_should_discourage = false;
3567-
if (pnode.HasPermission(PF_NOBAN)) {
3568-
LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode.addr.ToString());
3569-
} else if (pnode.m_manual_connection) {
3570-
LogPrintf("Warning: not punishing manually-connected peer %s!\n", pnode.addr.ToString());
3571-
} else if (pnode.addr.IsLocal()) {
3572-
// Disconnect but don't discourage this local node
3573-
LogPrintf("Warning: disconnecting but not discouraging local peer %s!\n", pnode.addr.ToString());
3574-
pnode.fDisconnect = true;
3575-
} else {
3576-
// Disconnect and discourage all nodes sharing the address
3577-
LogPrintf("Disconnecting and discouraging peer %s!\n", pnode.addr.ToString());
3578-
if (m_banman) {
3579-
m_banman->Discourage(pnode.addr);
3580-
}
3581-
connman->DisconnectNode(pnode.addr);
3582-
}
3577+
} // cs_main
3578+
3579+
if (pnode.HasPermission(PF_NOBAN)) {
3580+
// Peer has the NOBAN permission flag - log but don't disconnect
3581+
LogPrintf("Warning: not punishing noban peer %d!\n", peer_id);
3582+
return false;
3583+
}
3584+
3585+
if (pnode.m_manual_connection) {
3586+
// Peer is a manual connection - log but don't disconnect
3587+
LogPrintf("Warning: not punishing manually connected peer %d!\n", peer_id);
3588+
return false;
3589+
}
3590+
3591+
if (pnode.addr.IsLocal()) {
3592+
// Peer is on a local address. Disconnect this peer, but don't discourage the local address
3593+
LogPrintf("Warning: disconnecting but not discouraging local peer %d!\n", peer_id);
3594+
pnode.fDisconnect = true;
35833595
return true;
35843596
}
3585-
return false;
3597+
3598+
// Normal case: Disconnect the peer and discourage all nodes sharing the address
3599+
LogPrintf("Disconnecting and discouraging peer %d!\n", peer_id);
3600+
if (m_banman) m_banman->Discourage(pnode.addr);
3601+
connman->DisconnectNode(pnode.addr);
3602+
return true;
35863603
}
35873604

35883605
bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)

0 commit comments

Comments
 (0)