Skip to content

Commit ce11a03

Browse files
UdjinM6knst
authored andcommitted
refactor: Replace GetAllNodes() + RemoveAsBanned() with single RemoveNodesIf(predicate) method
- Single lock acquisition instead of O(n) locks - No intermediate vector allocation - No redundant map lookups
1 parent cfff7cb commit ce11a03

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

src/llmq/net_signing.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,7 @@ void NetSigning::RemoveBannedNodeStates()
257257
{
258258
assert(m_shares_manager != nullptr);
259259
// Called regularly to cleanup local node states for banned nodes
260-
std::vector<NodeId> nodes = m_shares_manager->GetAllNodes();
261-
for (NodeId node_id : nodes) {
262-
if (m_peer_manager->PeerIsBanned(node_id)) {
263-
m_shares_manager->RemoveAsBanned(node_id);
264-
}
265-
}
260+
m_shares_manager->RemoveNodesIf([this](NodeId node_id) { return m_peer_manager->PeerIsBanned(node_id); });
266261
}
267262

268263
void NetSigning::BanNode(NodeId nodeId)

src/llmq/signing_shares.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,32 +1336,22 @@ void CSigSharesManager::RemoveSigSharesForSession(const uint256& signHash)
13361336
timeSeenForSessions.erase(signHash);
13371337
}
13381338

1339-
std::vector<NodeId> CSigSharesManager::GetAllNodes() const
1340-
{
1341-
std::vector<NodeId> nodes;
1342-
1343-
LOCK(cs);
1344-
nodes.reserve(nodeStates.size());
1345-
for (const auto& [node_id, _] : nodeStates) {
1346-
nodes.push_back(node_id);
1347-
}
1348-
return nodes;
1349-
}
1350-
1351-
void CSigSharesManager::RemoveAsBanned(NodeId node_id)
1339+
void CSigSharesManager::RemoveNodesIf(std::function<bool(NodeId)> predicate)
13521340
{
13531341
LOCK(cs);
1354-
1355-
auto it = nodeStates.find(node_id);
1356-
if (it != nodeStates.end()) {
1357-
// re-request sigshares from other nodes
1358-
// TODO: remove NO_THREAD_SAFETY_ANALYSIS
1359-
// using here template ForEach makes impossible to use lock annotation
1360-
it->second.requestedSigShares.ForEach([this](const SigShareKey& k, int64_t) NO_THREAD_SAFETY_ANALYSIS {
1361-
AssertLockHeld(cs);
1362-
sigSharesRequested.Erase(k);
1363-
});
1364-
nodeStates.erase(it);
1342+
for (auto it = nodeStates.begin(); it != nodeStates.end();) {
1343+
if (predicate(it->first)) {
1344+
// re-request sigshares from other nodes
1345+
// TODO: remove NO_THREAD_SAFETY_ANALYSIS
1346+
// using here template ForEach makes impossible to use lock annotation
1347+
it->second.requestedSigShares.ForEach([this](const SigShareKey& k, int64_t) NO_THREAD_SAFETY_ANALYSIS {
1348+
AssertLockHeld(cs);
1349+
sigSharesRequested.Erase(k);
1350+
});
1351+
it = nodeStates.erase(it);
1352+
} else {
1353+
++it;
1354+
}
13651355
}
13661356
}
13671357

src/llmq/signing_shares.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <util/time.h>
1919

2020
#include <atomic>
21+
#include <functional>
2122
#include <limits>
2223
#include <memory>
2324
#include <optional>
@@ -481,8 +482,7 @@ class CSigSharesManager : public llmq::CRecoveredSigsListener
481482
void RemoveSigSharesForSession(const uint256& signHash) EXCLUSIVE_LOCKS_REQUIRED(cs);
482483

483484
public:
484-
std::vector<NodeId> GetAllNodes() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
485-
void RemoveAsBanned(NodeId node_id) EXCLUSIVE_LOCKS_REQUIRED(!cs);
485+
void RemoveNodesIf(std::function<bool(NodeId)> predicate) EXCLUSIVE_LOCKS_REQUIRED(!cs);
486486
void MarkAsBanned(NodeId nodeId) EXCLUSIVE_LOCKS_REQUIRED(!cs);
487487

488488
private:

0 commit comments

Comments
 (0)