From d0d264115472680db25a0d76b4ad4b89cfc81436 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 22 Apr 2024 09:50:18 -0500 Subject: [PATCH 1/2] refactor: minimize locking of cs_main in chainlocks.cpp --- src/llmq/chainlocks.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/llmq/chainlocks.cpp b/src/llmq/chainlocks.cpp index c5f3b0f525..1c44c259c3 100644 --- a/src/llmq/chainlocks.cpp +++ b/src/llmq/chainlocks.cpp @@ -620,18 +620,20 @@ void CChainLocksHandler::Cleanup() return; } + { + LOCK(cs); + for (auto it = seenChainLocks.begin(); it != seenChainLocks.end(); ) { + if (GetTimeMillis() - it->second >= CLEANUP_SEEN_TIMEOUT) { + it = seenChainLocks.erase(it); + } else { + ++it; + } + } + } // need mempool.cs due to GetTransaction calls LOCK2(cs_main, mempool.cs); LOCK(cs); - for (auto it = seenChainLocks.begin(); it != seenChainLocks.end(); ) { - if (GetTimeMillis() - it->second >= CLEANUP_SEEN_TIMEOUT) { - it = seenChainLocks.erase(it); - } else { - ++it; - } - } - for (auto it = blockTxs.begin(); it != blockTxs.end(); ) { const auto* pindex = m_chainstate.m_blockman.LookupBlockIndex(it->first); if (InternalHasChainLock(pindex->nHeight, pindex->GetBlockHash())) { From 04ba1640642f84955862645c2f5699467e7a90e8 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 22 Apr 2024 10:04:52 -0500 Subject: [PATCH 2/2] refactor: immediately update lastCleanupTime --- src/llmq/chainlocks.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/llmq/chainlocks.cpp b/src/llmq/chainlocks.cpp index 1c44c259c3..50068f0942 100644 --- a/src/llmq/chainlocks.cpp +++ b/src/llmq/chainlocks.cpp @@ -619,6 +619,7 @@ void CChainLocksHandler::Cleanup() if (GetTimeMillis() - lastCleanupTime < CLEANUP_INTERVAL) { return; } + lastCleanupTime = GetTimeMillis(); { LOCK(cs); @@ -665,8 +666,6 @@ void CChainLocksHandler::Cleanup() ++it; } } - - lastCleanupTime = GetTimeMillis(); } bool AreChainLocksEnabled(const CSporkManager& sporkman)