Skip to content

Commit fed2f49

Browse files
refactor(net): merge duplicate peer discouragement query
PeerManager exposed the same check twice: IsBanned() on PeerManager and PeerIsBanned() on PeerManagerInternal, the latter only forwarding to the former. Neither name described the check: it does not consult BanMan, it only reports whether the live Peer has crossed the discouragement threshold and SendMessages() has not consumed m_should_discourage yet, and it returns false for peers that are unknown or already finalized. Both declarations also required cs_main, which the lookup never uses; it only takes m_peer_mutex and the peer's m_misbehavior_mutex. Replace both with a single PeerManagerInternal::PeerShouldBeDiscouraged() without the cs_main requirement, and rename NetSigning's RemoveBannedNodeStates() to RemoveDiscouragedNodeStates() to match. Behavior is unchanged, including returning false for missing peers. Add peer_discouragement assertions covering the flag before and after crossing the threshold, after SendMessages() consumes it, below the threshold, and after FinalizeNode(). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 8819507 commit fed2f49

5 files changed

Lines changed: 18 additions & 23 deletions

File tree

‎src/llmq/net_signing.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,10 @@ void NetSigning::WorkThreadSigning()
297297
}
298298
}
299299

300-
void NetSigning::RemoveBannedNodeStates()
300+
void NetSigning::RemoveDiscouragedNodeStates()
301301
{
302302
assert(m_shares_manager != nullptr);
303-
// Called regularly to cleanup local node states for banned nodes
304-
m_shares_manager->RemoveNodesIf([this](NodeId node_id) { return m_peer_manager->PeerIsBanned(node_id); });
303+
m_shares_manager->RemoveNodesIf([this](NodeId node_id) { return m_peer_manager->PeerShouldBeDiscouraged(node_id); });
305304
}
306305

307306
void NetSigning::BanNode(NodeId nodeId, bool mark_shares_banned)
@@ -324,7 +323,7 @@ void NetSigning::WorkThreadCleaning()
324323
assert(m_shares_manager);
325324

326325
while (!workInterrupt) {
327-
RemoveBannedNodeStates();
326+
RemoveDiscouragedNodeStates();
328327

329328
m_shares_manager->SendMessages();
330329
m_shares_manager->Cleanup();

‎src/llmq/net_signing.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class NetSigning final : public NetHandler, public CValidationInterface
7171
std::unordered_map<NodeId, std::vector<CSigShare>>&& sigSharesByNodes,
7272
std::unordered_map<std::pair<Consensus::LLMQType, uint256>, CQuorumCPtr, StaticSaltedHasher>&& quorums);
7373

74-
void RemoveBannedNodeStates();
74+
void RemoveDiscouragedNodeStates();
7575
//! Score the peer with 100 misbehavior points and drop its not-yet-verified pending recovered
7676
//! sigs. mark_shares_banned additionally suppresses the peer's sig-share channel and must stay
7777
//! false for recovered-sig-only failures: NoBan/manual peers survive the misbehavior score, and

‎src/net_processing.cpp‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,6 @@ class PeerManagerImpl final : public PeerManager
622622
EXCLUSIVE_LOCKS_REQUIRED(!m_object_request_mutex, !m_peer_mutex, !m_recent_confirmed_transactions_mutex,
623623
!m_most_recent_block_mutex, g_msgproc_mutex);
624624
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
625-
bool IsBanned(NodeId pnode) override EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex);
626625
size_t GetRequestedObjectCount(NodeId nodeid) const override EXCLUSIVE_LOCKS_REQUIRED(!m_object_request_mutex);
627626

628627
/** Implements external handlers logic */
@@ -635,7 +634,7 @@ class PeerManagerImpl final : public PeerManager
635634

636635
/** Implement PeerManagerInternal */
637636
void PeerMisbehaving(const NodeId pnode, const int howmuch, const std::string& message = "") override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
638-
bool PeerIsBanned(const NodeId node_id) override EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex);
637+
bool PeerShouldBeDiscouraged(const NodeId node_id) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
639638
void PeerEraseObjectRequest(const NodeId nodeid, const CInv& inv) override
640639
EXCLUSIVE_LOCKS_REQUIRED(!m_object_request_mutex, ::cs_main);
641640
bool PeerConsumeObjectRequest(NodeId nodeid, const CInv& inv) override
@@ -1954,16 +1953,13 @@ void PeerManagerImpl::Misbehaving(Peer& peer, int howmuch, const std::string& me
19541953
peer.m_id, score_before, score_now, warning, message_prefixed);
19551954
}
19561955

1957-
bool PeerManagerImpl::IsBanned(NodeId pnode)
1956+
bool PeerManagerImpl::PeerShouldBeDiscouraged(const NodeId node_id)
19581957
{
1959-
PeerRef peer = GetPeerRef(pnode);
1960-
if (peer == nullptr)
1961-
return false;
1958+
PeerRef peer = GetPeerRef(node_id);
1959+
if (peer == nullptr) return false;
1960+
19621961
LOCK(peer->m_misbehavior_mutex);
1963-
if (peer->m_should_discourage) {
1964-
return true;
1965-
}
1966-
return false;
1962+
return peer->m_should_discourage;
19671963
}
19681964

19691965
bool PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
@@ -6904,11 +6900,6 @@ void PeerManagerImpl::PeerMisbehaving(const NodeId pnode, const int howmuch, con
69046900
if (peer) Misbehaving(*peer, howmuch, message);
69056901
}
69066902

6907-
bool PeerManagerImpl::PeerIsBanned(const NodeId node_id)
6908-
{
6909-
return IsBanned(node_id);
6910-
}
6911-
69126903
void PeerManagerImpl::PeerEraseObjectRequest(const NodeId nodeid, const CInv& inv)
69136904
{
69146905
// Completing only this peer's announcement is deliberate: an invalid or unusable object must

‎src/net_processing.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ class PeerManagerInternal
8484
{
8585
public:
8686
virtual void PeerMisbehaving(const NodeId pnode, const int howmuch, const std::string& message = "") = 0;
87-
virtual bool PeerIsBanned(const NodeId node_id) = 0;
87+
/** Whether the peer crossed the discouragement threshold and SendMessages() has not acted on it
88+
* yet. Does not consult BanMan; returns false for peers that are not (or no longer) known. */
89+
virtual bool PeerShouldBeDiscouraged(const NodeId node_id) = 0;
8890
/** Complete this peer's pending announcement of the inv, so it is not requested from them
8991
* again. Announcements of the same inv by other peers are unaffected: an invalid or unusable
9092
* object must not stop us from fetching it from honest peers.
@@ -224,8 +226,6 @@ class PeerManager : public CValidationInterface, public NetEventsInterface, publ
224226
/** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
225227
virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
226228

227-
virtual bool IsBanned(NodeId pnode) = 0;
228-
229229
virtual size_t GetRequestedObjectCount(NodeId nodeid) const = 0;
230230

231231
virtual void AddExtraHandler(std::unique_ptr<NetHandler>&& handler) = 0;

‎src/test/denialofservice_tests.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,11 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
339339
peerLogic->InitializeNode(*nodes[0], NODE_NETWORK);
340340
nodes[0]->fSuccessfullyConnected = true;
341341
connman->AddTestNode(*nodes[0]);
342+
BOOST_CHECK(!peerLogic->PeerShouldBeDiscouraged(nodes[0]->GetId()));
342343
peerLogic->UnitTestMisbehaving(nodes[0]->GetId(), DISCOURAGEMENT_THRESHOLD); // Should be discouraged
344+
BOOST_CHECK(peerLogic->PeerShouldBeDiscouraged(nodes[0]->GetId()));
343345
BOOST_CHECK(peerLogic->SendMessages(nodes[0]));
346+
BOOST_CHECK(!peerLogic->PeerShouldBeDiscouraged(nodes[0]->GetId())); // Consumed by SendMessages
344347
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
345348
BOOST_CHECK(nodes[0]->fDisconnect);
346349
BOOST_CHECK(!banman->IsDiscouraged(other_addr)); // Different address, not discouraged
@@ -359,6 +362,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
359362
nodes[1]->fSuccessfullyConnected = true;
360363
connman->AddTestNode(*nodes[1]);
361364
peerLogic->UnitTestMisbehaving(nodes[1]->GetId(), DISCOURAGEMENT_THRESHOLD - 1);
365+
BOOST_CHECK(!peerLogic->PeerShouldBeDiscouraged(nodes[1]->GetId()));
362366
BOOST_CHECK(peerLogic->SendMessages(nodes[1]));
363367
// [0] is still discouraged/disconnected.
364368
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
@@ -400,6 +404,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
400404

401405
for (CNode* node : nodes) {
402406
peerLogic->FinalizeNode(*node);
407+
BOOST_CHECK(!peerLogic->PeerShouldBeDiscouraged(node->GetId()));
403408
}
404409
connman->ClearTestNodes();
405410
}

0 commit comments

Comments
 (0)