diff --git a/src/budget/budgetmanager.cpp b/src/budget/budgetmanager.cpp index 0a78b3c5a8cd8..3e3cd390a1ad4 100644 --- a/src/budget/budgetmanager.cpp +++ b/src/budget/budgetmanager.cpp @@ -28,6 +28,26 @@ std::map askedForSourceProposalOrBudget; // Used to check both proposals and finalized-budgets collateral txes bool CheckCollateral(const uint256& nTxCollateralHash, const uint256& nExpectedHash, std::string& strError, int64_t& nTime, int nCurrentHeight, bool fBudgetFinalization); +void CBudgetManager::ReloadMapSeen() +{ + const auto reloadSeenMap = [](auto& mutex1, auto& mutex2, const auto& mapBudgets, auto& mapSeen, auto& mapOrphans) { + LOCK2(mutex1, mutex2); + mapSeen.clear(); + mapOrphans.clear(); + for (const auto& b : mapBudgets) { + for (const auto& it : b.second.mapVotes) { + const auto& vote = it.second; + if (vote.IsValid()) { + mapSeen.emplace(vote.GetHash(), vote); + } + } + } + }; + + reloadSeenMap(cs_proposals, cs_votes, mapProposals, mapSeenProposalVotes, mapOrphanProposalVotes); + reloadSeenMap(cs_budgets, cs_finalizedvotes, mapFinalizedBudgets, mapSeenFinalizedBudgetVotes, mapOrphanFinalizedBudgetVotes); +} + void CBudgetManager::CheckOrphanVotes() { { @@ -1031,7 +1051,7 @@ void CBudgetManager::NewBlock() // Once every 2 weeks (1/14 * 1/1440), clean the seen maps if (masternodeSync.IsSynced() && GetRandInt(1440) == 0) { - ClearSeen(); + ReloadMapSeen(); } LogPrint(BCLog::MNBUDGET,"%s: PASSED\n", __func__); diff --git a/src/budget/budgetmanager.h b/src/budget/budgetmanager.h index 4ae39b77f01bc..6a7c2a8a6c697 100644 --- a/src/budget/budgetmanager.h +++ b/src/budget/budgetmanager.h @@ -66,12 +66,6 @@ class CBudgetManager : public CValidationInterface CBudgetManager() {} - void ClearSeen() - { - WITH_LOCK(cs_votes, mapSeenProposalVotes.clear(); ); - WITH_LOCK(cs_finalizedvotes, mapSeenFinalizedBudgetVotes.clear(); ); - } - void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override; bool HaveProposal(const uint256& propHash) const { LOCK(cs_proposals); return mapProposals.count(propHash); } @@ -79,6 +73,9 @@ class CBudgetManager : public CValidationInterface bool HaveFinalizedBudget(const uint256& budgetHash) const { LOCK(cs_budgets); return mapFinalizedBudgets.count(budgetHash); } bool HaveSeenFinalizedBudgetVote(const uint256& voteHash) const { LOCK(cs_finalizedvotes); return mapSeenFinalizedBudgetVotes.count(voteHash); } + // Clears and reloads seen votes in the maps, and clears orphan votes + void ReloadMapSeen(); + void AddSeenProposalVote(const CBudgetVote& vote); void AddSeenFinalizedBudgetVote(const CFinalizedBudgetVote& vote); diff --git a/src/init.cpp b/src/init.cpp index 4e087207cf028..12a2ff5f71db4 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1819,7 +1819,7 @@ bool AppInitMain() //flag our cached items so we send them to our peers g_budgetman.ResetSync(); - g_budgetman.ClearSeen(); + g_budgetman.ReloadMapSeen(); RegisterValidationInterface(&g_budgetman);