From 794f2717db37a06ec1f9e513ae05a1e9503b82a6 Mon Sep 17 00:00:00 2001 From: pasta Date: Mon, 16 Oct 2023 09:12:47 -0500 Subject: [PATCH 1/3] refactor: move DecreaseScores method to be inside CDeterministicMNList class --- src/evo/deterministicmns.cpp | 49 ++++++++++++++++-------------------- src/evo/deterministicmns.h | 6 ++--- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index 87df855cae1cc..ad051cd7d9306 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -366,17 +366,30 @@ void CDeterministicMNList::PoSePunish(const uint256& proTxHash, int penalty, boo UpdateMN(proTxHash, newState); } -void CDeterministicMNList::PoSeDecrease(const uint256& proTxHash) -{ - auto dmn = GetMN(proTxHash); - if (!dmn) { - throw(std::runtime_error(strprintf("%s: Can't find a masternode with proTxHash=%s", __func__, proTxHash.ToString()))); +void CDeterministicMNList::DecreaseScores() { + 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 +736,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const C } }); - DecreasePoSePenalties(newList); + newList.DecreaseScores(); bool isMNRewardReallocation = llmq::utils::IsMNRewardReallocationActive(pindexPrev); @@ -1002,24 +1015,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..0049781e041ad 100644 --- a/src/evo/deterministicmns.h +++ b/src/evo/deterministicmns.h @@ -274,6 +274,7 @@ class CDeterministicMNList } } } +public: /** * Prefer ForEachMN. Execute a callback on all masternodes in the mnList. @@ -385,12 +386,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 +610,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); From 7d212560e87f5c3fbee64038dd6a21cf392dd934 Mon Sep 17 00:00:00 2001 From: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:17:33 -0500 Subject: [PATCH 2/3] Update deterministicmns.h Co-authored-by: UdjinM6 --- src/evo/deterministicmns.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/evo/deterministicmns.h b/src/evo/deterministicmns.h index 0049781e041ad..8af688c822897 100644 --- a/src/evo/deterministicmns.h +++ b/src/evo/deterministicmns.h @@ -274,7 +274,6 @@ class CDeterministicMNList } } } -public: /** * Prefer ForEachMN. Execute a callback on all masternodes in the mnList. From 7de2941b789f9fe4fab82493f14b174a692dc761 Mon Sep 17 00:00:00 2001 From: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:17:41 -0500 Subject: [PATCH 3/3] Update deterministicmns.cpp Co-authored-by: UdjinM6 --- src/evo/deterministicmns.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index ad051cd7d9306..12baa3cfcec96 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -366,7 +366,8 @@ void CDeterministicMNList::PoSePunish(const uint256& proTxHash, int penalty, boo UpdateMN(proTxHash, newState); } -void CDeterministicMNList::DecreaseScores() { +void CDeterministicMNList::DecreaseScores() +{ std::vector toDecrease; toDecrease.reserve(GetAllMNsCount() / 10); // only iterate and decrease for valid ones (not PoSe banned yet)