diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index 87df855cae1cc..12baa3cfcec96 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -366,17 +366,31 @@ void CDeterministicMNList::PoSePunish(const uint256& proTxHash, int penalty, boo UpdateMN(proTxHash, newState); } -void CDeterministicMNList::PoSeDecrease(const uint256& proTxHash) +void CDeterministicMNList::DecreaseScores() { - auto dmn = GetMN(proTxHash); - if (!dmn) { - throw(std::runtime_error(strprintf("%s: Can't find a masternode with proTxHash=%s", __func__, proTxHash.ToString()))); + std::vector toDecrease; + toDecrease.reserve(GetAllMNsCount() / 10); + // only iterate and decrease for valid ones (not PoSe banned yet) + // if a MN ever reaches the maximum, it stays in PoSe banned state until revived + ForEachMNShared(true /* onlyValid */, [&toDecrease](auto& dmn) { + // There is no reason to check if this MN is banned here since onlyValid=true will only run on non-banned MNs + if (dmn->pdmnState->nPoSePenalty > 0) { + toDecrease.emplace_back(dmn); + } + }); + + for (const auto& proTxHash : toDecrease) { + PoSeDecrease(*proTxHash); } - assert(dmn->pdmnState->nPoSePenalty > 0 && !dmn->pdmnState->IsBanned()); +} - auto newState = std::make_shared(*dmn->pdmnState); +void CDeterministicMNList::PoSeDecrease(const CDeterministicMN& dmn) +{ + assert(dmn.pdmnState->nPoSePenalty > 0 && !dmn.pdmnState->IsBanned()); + + auto newState = std::make_shared(*dmn.pdmnState); newState->nPoSePenalty--; - UpdateMN(proTxHash, newState); + UpdateMN(dmn, newState); } CDeterministicMNListDiff CDeterministicMNList::BuildDiff(const CDeterministicMNList& to) const @@ -723,7 +737,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const C } }); - DecreasePoSePenalties(newList); + newList.DecreaseScores(); bool isMNRewardReallocation = llmq::utils::IsMNRewardReallocationActive(pindexPrev); @@ -1002,24 +1016,6 @@ void CDeterministicMNManager::HandleQuorumCommitment(const llmq::CFinalCommitmen } } -void CDeterministicMNManager::DecreasePoSePenalties(CDeterministicMNList& mnList) -{ - std::vector toDecrease; - toDecrease.reserve(mnList.GetAllMNsCount() / 10); - // only iterate and decrease for valid ones (not PoSe banned yet) - // if a MN ever reaches the maximum, it stays in PoSe banned state until revived - mnList.ForEachMN(true /* onlyValid */, [&toDecrease](auto& dmn) { - // There is no reason to check if this MN is banned here since onlyValid=true will only run on non-banned MNs - if (dmn.pdmnState->nPoSePenalty > 0) { - toDecrease.emplace_back(dmn.proTxHash); - } - }); - - for (const auto& proTxHash : toDecrease) { - mnList.PoSeDecrease(proTxHash); - } -} - CDeterministicMNList CDeterministicMNManager::GetListForBlockInternal(const CBlockIndex* pindex) { AssertLockHeld(cs); diff --git a/src/evo/deterministicmns.h b/src/evo/deterministicmns.h index e7a889ea5bf1d..8af688c822897 100644 --- a/src/evo/deterministicmns.h +++ b/src/evo/deterministicmns.h @@ -385,12 +385,12 @@ class CDeterministicMNList */ void PoSePunish(const uint256& proTxHash, int penalty, bool debugLogs); + void DecreaseScores(); /** * Decrease penalty score of MN by 1. * Only allowed on non-banned MNs. - * @param proTxHash */ - void PoSeDecrease(const uint256& proTxHash); + void PoSeDecrease(const CDeterministicMN& dmn); [[nodiscard]] CDeterministicMNListDiff BuildDiff(const CDeterministicMNList& to) const; [[nodiscard]] CDeterministicMNList ApplyDiff(const CBlockIndex* pindex, const CDeterministicMNListDiff& diff) const; @@ -609,7 +609,6 @@ class CDeterministicMNManager bool BuildNewListFromBlock(const CBlock& block, const CBlockIndex* pindexPrev, BlockValidationState& state, const CCoinsViewCache& view, CDeterministicMNList& mnListRet, bool debugLogs) EXCLUSIVE_LOCKS_REQUIRED(cs); static void HandleQuorumCommitment(const llmq::CFinalCommitment& qc, const CBlockIndex* pQuorumBaseBlockIndex, CDeterministicMNList& mnList, bool debugLogs); - static void DecreasePoSePenalties(CDeterministicMNList& mnList); CDeterministicMNList GetListForBlock(const CBlockIndex* pindex) LOCKS_EXCLUDED(cs) { LOCK(cs);