Skip to content

Commit

Permalink
refactor: move DecreaseScores method to be inside CDeterministicMNLis…
Browse files Browse the repository at this point in the history
…t class
  • Loading branch information
PastaPastaPasta committed Oct 16, 2023
1 parent bf84e37 commit 794f271
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
49 changes: 22 additions & 27 deletions src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CDeterministicMNCPtr> 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<CDeterministicMNState>(*dmn->pdmnState);
void CDeterministicMNList::PoSeDecrease(const CDeterministicMN& dmn)
{
assert(dmn.pdmnState->nPoSePenalty > 0 && !dmn.pdmnState->IsBanned());

auto newState = std::make_shared<CDeterministicMNState>(*dmn.pdmnState);
newState->nPoSePenalty--;
UpdateMN(proTxHash, newState);
UpdateMN(dmn, newState);
}

CDeterministicMNListDiff CDeterministicMNList::BuildDiff(const CDeterministicMNList& to) const
Expand Down Expand Up @@ -723,7 +736,7 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const C
}
});

DecreasePoSePenalties(newList);
newList.DecreaseScores();

bool isMNRewardReallocation = llmq::utils::IsMNRewardReallocationActive(pindexPrev);

Expand Down Expand Up @@ -1002,24 +1015,6 @@ void CDeterministicMNManager::HandleQuorumCommitment(const llmq::CFinalCommitmen
}
}

void CDeterministicMNManager::DecreasePoSePenalties(CDeterministicMNList& mnList)
{
std::vector<uint256> 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);
Expand Down
6 changes: 3 additions & 3 deletions src/evo/deterministicmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ class CDeterministicMNList
}
}
}
public:

/**
* Prefer ForEachMN. Execute a callback on all masternodes in the mnList.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 794f271

Please sign in to comment.