Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move DecreaseScores method to be inside CDeterministicMNList class #5615

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 +737,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 +1016,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
5 changes: 2 additions & 3 deletions src/evo/deterministicmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading