Skip to content

Commit

Permalink
[Refactoring] Add IsPassing function to CBudgetProposal
Browse files Browse the repository at this point in the history
Makes the code easier to read and allows for reusing of this functions
in other places.
  • Loading branch information
Warrows committed Apr 26, 2019
1 parent 3ec04f7 commit bda0322
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/masternode-budget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,23 +793,19 @@ std::vector<CBudgetProposal*> CBudgetManager::GetBudget()

int nBlockStart = pindexPrev->nHeight - pindexPrev->nHeight % Params().GetBudgetCycleBlocks() + Params().GetBudgetCycleBlocks();
int nBlockEnd = nBlockStart + Params().GetBudgetCycleBlocks() - 1;
int mnCount = mnodeman.CountEnabled(ActiveProtocol());
CAmount nTotalBudget = GetTotalBudget(nBlockStart);


std::vector<std::pair<CBudgetProposal*, int> >::iterator it2 = vBudgetPorposalsSort.begin();
while (it2 != vBudgetPorposalsSort.end()) {
CBudgetProposal* pbudgetProposal = (*it2).first;

LogPrint("mnbudget","CBudgetManager::GetBudget() - Processing Budget %s\n", pbudgetProposal->strProposalName.c_str());
//prop start/end should be inside this period
if (pbudgetProposal->fValid && pbudgetProposal->nBlockStart <= nBlockStart &&
pbudgetProposal->nBlockEnd >= nBlockEnd &&
pbudgetProposal->GetYeas() - pbudgetProposal->GetNays() > mnodeman.CountEnabled(ActiveProtocol()) / 10 &&
pbudgetProposal->IsEstablished()) {

if (pbudgetProposal->IsPassing(pindexPrev, nBlockStart, nBlockEnd, mnCount)) {
LogPrint("mnbudget","CBudgetManager::GetBudget() - Check 1 passed: valid=%d | %ld <= %ld | %ld >= %ld | Yeas=%d Nays=%d Count=%d | established=%d\n",
pbudgetProposal->fValid, pbudgetProposal->nBlockStart, nBlockStart, pbudgetProposal->nBlockEnd,
nBlockEnd, pbudgetProposal->GetYeas(), pbudgetProposal->GetNays(), mnodeman.CountEnabled(ActiveProtocol()) / 10,
nBlockEnd, pbudgetProposal->GetYeas(), pbudgetProposal->GetNays(), mnCount / 10,
pbudgetProposal->IsEstablished());

if (pbudgetProposal->GetAmount() + nBudgetAllocated <= nTotalBudget) {
Expand Down Expand Up @@ -1558,6 +1554,29 @@ bool CBudgetProposal::IsEstablished()
return nTime < GetAdjustedTime() - Params().GetProposalEstablishmentTime();
}

bool CBudgetProposal::IsPassing(const CBlockIndex* pindexPrev, int nBlockStartBudget, int nBlockEndBudget, int mnCount)
{
if (!fValid)
return false;

if (!pindexPrev)
return false;

if (this->nBlockStart > nBlockStartBudget)
return false;

if (this->nBlockEnd < nBlockEndBudget)
return false;

if (GetYeas() - GetNays() <= mnCount / 10)
return false;

if (!IsEstablished())
return false;

return true;
}

bool CBudgetProposal::AddOrUpdateVote(CBudgetVote& vote, std::string& strError)
{
std::string strAction = "New vote inserted:";
Expand Down
1 change: 1 addition & 0 deletions src/masternode-budget.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ class CBudgetProposal
bool IsValid(std::string& strError, bool fCheckCollateral = true);

bool IsEstablished();
bool IsPassing(const CBlockIndex* pindexPrev, int nBlockStartBudget, int nBlockEndBudget, int mnCount);

std::string GetName() { return strProposalName; }
std::string GetURL() { return strURL; }
Expand Down

0 comments on commit bda0322

Please sign in to comment.