diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index 5e056cd3b796..89997142a884 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -778,10 +778,6 @@ bool CDeterministicMNManager::ProcessBlock(const CBlock& block, gsl::not_null current && !to_cleanup.compare_exchange_weak(current, nHeight)) { - // Loop continues if compare_exchange_weak failed (another thread changed it) (current is updated to the new value in to_cleanup) - } return true; } @@ -837,6 +833,10 @@ CDeterministicMNList CDeterministicMNManager::GetListForBlockInternal(gsl::not_n AssertLockHeld(cs); + if (tipIndex != nullptr && mnListsCache.size() > MAX_CACHED_LISTS) { + CleanupCache(tipIndex->nHeight); + } + std::list listDiffIndexes; while (true) { @@ -1015,15 +1015,6 @@ void CDeterministicMNManager::CleanupCache(int nHeight) //end -void CDeterministicMNManager::DoMaintenance() { - LOCK(cs_cleanup); - int loc_to_cleanup = to_cleanup.load(); - if (loc_to_cleanup <= did_cleanup) return; - LOCK(cs); - CleanupCache(loc_to_cleanup); - did_cleanup = loc_to_cleanup; -} - bool CDeterministicMNManager::IsMigrationRequired() const { // Check if there are any legacy format diffs in the database diff --git a/src/evo/deterministicmns.h b/src/evo/deterministicmns.h index 234c26acaeff..efda69005f6c 100644 --- a/src/evo/deterministicmns.h +++ b/src/evo/deterministicmns.h @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -762,15 +761,15 @@ class CDeterministicMNManager // keep cache for enough disk snapshots to have all active quourms covered static constexpr int DISK_SNAPSHOTS = llmq_max_blocks() / DISK_SNAPSHOT_PERIOD + 1; static constexpr int LIST_DIFFS_CACHE_SIZE = DISK_SNAPSHOT_PERIOD * DISK_SNAPSHOTS; + //! Lists the cache may hold before a lookup runs CleanupCache(). Cleanup retains only the + //! tip and the bases of live quorums, a few dozen lists; the rest is scratch that historical + //! requests (a full list read from disk plus its mini-snapshots) and connected blocks added. + //! The budget caps what peers can make the node hold through historical requests, while + //! sequential historical work keeps its intermediate lists until it outgrows the budget. + static constexpr size_t MAX_CACHED_LISTS{64}; private: Mutex cs; - Mutex cs_cleanup; - // We have performed CleanupCache() on this height. - int did_cleanup GUARDED_BY(cs_cleanup) {0}; - - // Main thread has indicated we should perform cleanup up to this height - std::atomic to_cleanup {0}; CEvoDB& m_evoDb; CMasternodeMetaMan& m_mn_metaman; @@ -809,8 +808,6 @@ class CDeterministicMNManager // Test if given TX is a ProRegTx which also contains the collateral at index n static bool IsProTxWithCollateral(const CTransactionRef& tx, uint32_t n); - void DoMaintenance() EXCLUSIVE_LOCKS_REQUIRED(!cs, !cs_cleanup); - // Recalculate and optionally repair diffs between snapshots struct RecalcDiffsResult { int start_height{0}; diff --git a/src/init.cpp b/src/init.cpp index 7dd9849ec950..0d2b5fe683a6 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2333,7 +2333,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) node.scheduler->scheduleEvery(std::bind(&CNetFulfilledRequestManager::DoMaintenance, std::ref(*node.netfulfilledman)), std::chrono::minutes{1}); node.scheduler->scheduleEvery(std::bind(&CMasternodeUtils::DoMaintenance, std::ref(*node.connman), std::ref(*node.dmnman), std::ref(*node.mn_sync), node.cj_walletman.get()), std::chrono::minutes{1}); - node.scheduler->scheduleEvery(std::bind(&CDeterministicMNManager::DoMaintenance, std::ref(*node.dmnman)), std::chrono::seconds{10}); node.peerman->ScheduleHandlers(*node.scheduler); if (node.active_ctx) { diff --git a/src/test/evo_deterministicmns_tests.cpp b/src/test/evo_deterministicmns_tests.cpp index f80be41ac420..625a0c9eabf3 100644 --- a/src/test/evo_deterministicmns_tests.cpp +++ b/src/test/evo_deterministicmns_tests.cpp @@ -271,7 +271,6 @@ void FuncV19Activation(TestChainSetup& setup) ++nHeight; BOOST_CHECK_EQUAL(tip_height(), nHeight); sync_dmn_tip(); - dmnman.DoMaintenance(); auto tip_list = dmnman.GetListAtChainTip(); BOOST_REQUIRE(tip_list.HasMN(tx_reg_hash)); auto pindex_create = tip_index(); @@ -289,7 +288,6 @@ void FuncV19Activation(TestChainSetup& setup) ++nHeight; BOOST_CHECK_EQUAL(tip_height(), nHeight); sync_dmn_tip(); - dmnman.DoMaintenance(); tip_list = dmnman.GetListAtChainTip(); BOOST_REQUIRE(tip_list.HasMN(tx_reg_hash)); diffs.push_back(base_list.BuildDiff(tip_list)); @@ -308,7 +306,6 @@ void FuncV19Activation(TestChainSetup& setup) ++nHeight; BOOST_CHECK_EQUAL(tip_height(), nHeight); sync_dmn_tip(); - dmnman.DoMaintenance(); diffs.push_back(tip_list.BuildDiff(dmnman.GetListAtChainTip())); tip_list = dmnman.GetListAtChainTip(); BOOST_REQUIRE(!tip_list.HasMN(tx_reg_hash)); @@ -320,7 +317,6 @@ void FuncV19Activation(TestChainSetup& setup) ++nHeight; BOOST_CHECK_EQUAL(tip_height(), nHeight); sync_dmn_tip(); - dmnman.DoMaintenance(); diffs.push_back(tip_list.BuildDiff(dmnman.GetListAtChainTip())); tip_list = dmnman.GetListAtChainTip(); BOOST_REQUIRE(!tip_list.HasMN(tx_reg_hash)); @@ -332,7 +328,6 @@ void FuncV19Activation(TestChainSetup& setup) ++nHeight; BOOST_CHECK_EQUAL(tip_height(), nHeight); sync_dmn_tip(); - dmnman.DoMaintenance(); diffs.push_back(tip_list.BuildDiff(dmnman.GetListAtChainTip())); tip_list = dmnman.GetListAtChainTip(); BOOST_REQUIRE(!tip_list.HasMN(tx_reg_hash)); @@ -353,7 +348,6 @@ void FuncV19Activation(TestChainSetup& setup) DeploymentActiveAfter(tip_index(), chainman.GetConsensus(), Consensus::DEPLOYMENT_V19)); BOOST_CHECK_EQUAL(tip_height(), nHeight + 1 + i); sync_dmn_tip(); - dmnman.DoMaintenance(); diffs.push_back(tip_list.BuildDiff(dmnman.GetListAtChainTip())); tip_list = dmnman.GetListAtChainTip(); BOOST_REQUIRE(!tip_list.HasMN(tx_reg_hash));