Skip to content
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
26 changes: 23 additions & 3 deletions src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,35 @@ std::vector<CDeterministicMNCPtr> CDeterministicMNList::GetProjectedMNPayees(int
if (nCount < 0 ) {
return {};
}
nCount = std::min(nCount, int(GetValidMNsCount()));
nCount = std::min(nCount, int(GetValidWeightedMNsCount()));

std::vector<CDeterministicMNCPtr> result;
result.reserve(nCount);

auto remaining_hpmn_payments = 0;
CDeterministicMNCPtr hpmn_to_be_skipped = nullptr;
ForEachMNShared(true, [&](const CDeterministicMNCPtr& dmn) {
result.emplace_back(dmn);
if (dmn->pdmnState->nLastPaidHeight == nHeight) {
// We found the last MN Payee.
// If the last payee is a HPMN, we need to check its consecutive payments and pay him again if needed
if (dmn->nType == MnType::HighPerformance && dmn->pdmnState->nConsecutivePayments < dmn_types::HighPerformance.voting_weight) {
remaining_hpmn_payments = dmn_types::HighPerformance.voting_weight - dmn->pdmnState->nConsecutivePayments;
for ([[maybe_unused]] auto _ : irange::range(remaining_hpmn_payments)) {
result.emplace_back(dmn);
hpmn_to_be_skipped = dmn;
}
}
}
return;
});

ForEachMNShared(true, [&](const CDeterministicMNCPtr& dmn) {
if (dmn == hpmn_to_be_skipped) return;
for ([[maybe_unused]] auto _ : irange::range(GetMnType(dmn->nType).voting_weight)) {
result.emplace_back(dmn);
}
});
std::sort(result.begin(), result.end(), [&](const CDeterministicMNCPtr& a, const CDeterministicMNCPtr& b) {
std::sort(result.begin() + remaining_hpmn_payments, result.end(), [&](const CDeterministicMNCPtr& a, const CDeterministicMNCPtr& b) {
return CompareByLastPaid(a.get(), b.get());
});

Expand Down
9 changes: 9 additions & 0 deletions src/evo/deterministicmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <immer/map.hpp>

#include <numeric>
#include <unordered_map>
#include <utility>

Expand Down Expand Up @@ -241,6 +242,14 @@ class CDeterministicMNList
return ranges::count_if(mnMap, [](const auto& p) { return p.second->nType == MnType::HighPerformance && IsMNValid(*p.second); });
}

[[nodiscard]] size_t GetValidWeightedMNsCount() const
{
return std::accumulate(mnMap.begin(), mnMap.end(), 0, [](auto res, const auto& p) {
if (!IsMNValid(*p.second)) return res;
return res + GetMnType(p.second->nType).voting_weight;
});
}

/**
* Execute a callback on all masternodes in the mnList. This will pass a reference
* of each masternode to the callback function. This should be preferred over ForEachMNShared.
Expand Down