Skip to content

Commit

Permalink
[Refactor] Add ReloadMapSeen function to reload "seen" votes
Browse files Browse the repository at this point in the history
And clear the orphan maps as well.
  • Loading branch information
random-zebra authored and furszy committed Dec 10, 2021
1 parent d60a105 commit 6f79df3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
22 changes: 21 additions & 1 deletion src/budget/budgetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ std::map<uint256, int64_t> 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()
{
{
Expand Down Expand Up @@ -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__);
Expand Down
9 changes: 3 additions & 6 deletions src/budget/budgetmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,16 @@ 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); }
bool HaveSeenProposalVote(const uint256& voteHash) const { LOCK(cs_votes); return mapSeenProposalVotes.count(voteHash); }
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);

Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 6f79df3

Please sign in to comment.