mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-28 10:32:39 +02:00
[net processing] Add Peer& arg to MaybeDiscourageAndDisconnect()
Refactor only. No change in behaviour.
This commit is contained in:
@@ -302,9 +302,10 @@ private:
|
|||||||
/** Maybe disconnect a peer and discourage future connections from its address.
|
/** Maybe disconnect a peer and discourage future connections from its address.
|
||||||
*
|
*
|
||||||
* @param[in] pnode The node to check.
|
* @param[in] pnode The node to check.
|
||||||
|
* @param[in] peer The peer object to check.
|
||||||
* @return True if the peer was marked for disconnection in this function
|
* @return True if the peer was marked for disconnection in this function
|
||||||
*/
|
*/
|
||||||
bool MaybeDiscourageAndDisconnect(CNode& pnode);
|
bool MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer);
|
||||||
|
|
||||||
void ProcessOrphanTx(std::set<uint256>& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
|
void ProcessOrphanTx(std::set<uint256>& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
|
||||||
/** Process a single headers message from a peer. */
|
/** Process a single headers message from a peer. */
|
||||||
@@ -3996,43 +3997,39 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode)
|
bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer)
|
||||||
{
|
{
|
||||||
const NodeId peer_id{pnode.GetId()};
|
|
||||||
PeerRef peer = GetPeerRef(peer_id);
|
|
||||||
if (peer == nullptr) return false;
|
|
||||||
|
|
||||||
{
|
{
|
||||||
LOCK(peer->m_misbehavior_mutex);
|
LOCK(peer.m_misbehavior_mutex);
|
||||||
|
|
||||||
// There's nothing to do if the m_should_discourage flag isn't set
|
// There's nothing to do if the m_should_discourage flag isn't set
|
||||||
if (!peer->m_should_discourage) return false;
|
if (!peer.m_should_discourage) return false;
|
||||||
|
|
||||||
peer->m_should_discourage = false;
|
peer.m_should_discourage = false;
|
||||||
} // peer.m_misbehavior_mutex
|
} // peer.m_misbehavior_mutex
|
||||||
|
|
||||||
if (pnode.HasPermission(PF_NOBAN)) {
|
if (pnode.HasPermission(PF_NOBAN)) {
|
||||||
// We never disconnect or discourage peers for bad behavior if they have the NOBAN permission flag
|
// We never disconnect or discourage peers for bad behavior if they have the NOBAN permission flag
|
||||||
LogPrintf("Warning: not punishing noban peer %d!\n", peer_id);
|
LogPrintf("Warning: not punishing noban peer %d!\n", peer.m_id);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pnode.IsManualConn()) {
|
if (pnode.IsManualConn()) {
|
||||||
// We never disconnect or discourage manual peers for bad behavior
|
// We never disconnect or discourage manual peers for bad behavior
|
||||||
LogPrintf("Warning: not punishing manually connected peer %d!\n", peer_id);
|
LogPrintf("Warning: not punishing manually connected peer %d!\n", peer.m_id);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pnode.addr.IsLocal()) {
|
if (pnode.addr.IsLocal()) {
|
||||||
// We disconnect local peers for bad behavior but don't discourage (since that would discourage
|
// We disconnect local peers for bad behavior but don't discourage (since that would discourage
|
||||||
// all peers on the same local address)
|
// all peers on the same local address)
|
||||||
LogPrintf("Warning: disconnecting but not discouraging local peer %d!\n", peer_id);
|
LogPrintf("Warning: disconnecting but not discouraging local peer %d!\n", peer.m_id);
|
||||||
pnode.fDisconnect = true;
|
pnode.fDisconnect = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normal case: Disconnect the peer and discourage all nodes sharing the address
|
// Normal case: Disconnect the peer and discourage all nodes sharing the address
|
||||||
LogPrint(BCLog::NET, "Disconnecting and discouraging peer %d!\n", peer_id);
|
LogPrint(BCLog::NET, "Disconnecting and discouraging peer %d!\n", peer.m_id);
|
||||||
if (m_banman) m_banman->Discourage(pnode.addr);
|
if (m_banman) m_banman->Discourage(pnode.addr);
|
||||||
m_connman.DisconnectNode(pnode.addr);
|
m_connman.DisconnectNode(pnode.addr);
|
||||||
return true;
|
return true;
|
||||||
@@ -4319,11 +4316,12 @@ public:
|
|||||||
bool PeerManagerImpl::SendMessages(CNode* pto)
|
bool PeerManagerImpl::SendMessages(CNode* pto)
|
||||||
{
|
{
|
||||||
PeerRef peer = GetPeerRef(pto->GetId());
|
PeerRef peer = GetPeerRef(pto->GetId());
|
||||||
|
if (!peer) return false;
|
||||||
const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
|
const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
|
||||||
|
|
||||||
// We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
|
// We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
|
||||||
// disconnect misbehaving peers even before the version handshake is complete.
|
// disconnect misbehaving peers even before the version handshake is complete.
|
||||||
if (MaybeDiscourageAndDisconnect(*pto)) return true;
|
if (MaybeDiscourageAndDisconnect(*pto, *peer)) return true;
|
||||||
|
|
||||||
// Don't send anything until the version handshake is complete
|
// Don't send anything until the version handshake is complete
|
||||||
if (!pto->fSuccessfullyConnected || pto->fDisconnect)
|
if (!pto->fSuccessfullyConnected || pto->fDisconnect)
|
||||||
|
Reference in New Issue
Block a user