Skip to content

Commit 12d411f

Browse files
committed
[Refactor] Decouple CBudgetProposal::UpdateValid checks
1 parent 683fd6b commit 12d411f

File tree

2 files changed

+74
-33
lines changed

2 files changed

+74
-33
lines changed

src/masternode-budget.cpp

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,14 +1387,17 @@ void CBudgetProposal::SyncVotes(CNode* pfrom, bool fPartial, int& nInvCount) con
13871387
}
13881388
}
13891389

1390-
bool CBudgetProposal::UpdateValid(int nCurrentHeight, bool fCheckCollateral)
1390+
bool CBudgetProposal::IsHeavilyDownvoted()
13911391
{
1392-
fValid = false;
13931392
if (GetNays() - GetYeas() > mnodeman.CountEnabled(ActiveProtocol()) / 10) {
13941393
strInvalid = "Proposal " + strProposalName + ": Active removal";
1395-
return false;
1394+
return true;
13961395
}
1396+
return false;
1397+
}
13971398

1399+
bool CBudgetProposal::CheckStartEnd()
1400+
{
13981401
if (nBlockStart < 0) {
13991402
strInvalid = "Invalid Proposal";
14001403
return false;
@@ -1405,23 +1408,38 @@ bool CBudgetProposal::UpdateValid(int nCurrentHeight, bool fCheckCollateral)
14051408
return false;
14061409
}
14071410

1411+
int nProposalEnd = nBlockStart + (Params().GetConsensus().nBudgetCycleBlocks + 1) * GetTotalPaymentCount();
1412+
if (nBlockEnd != nProposalEnd) {
1413+
strInvalid = "Proposal " + strProposalName + ": Invalid nBlockEnd (mismatch with payments count)";
1414+
return false;
1415+
}
1416+
1417+
return true;
1418+
}
1419+
1420+
bool CBudgetProposal::CheckAmount(const CAmount& nTotalBudget)
1421+
{
1422+
// check minimum amount
14081423
if (nAmount < 10 * COIN) {
1409-
strInvalid = "Proposal " + strProposalName + ": Invalid nAmount";
1424+
strInvalid = "Proposal " + strProposalName + ": Invalid nAmount (too low)";
14101425
return false;
14111426
}
14121427

1413-
if (address == CScript()) {
1414-
strInvalid = "Proposal " + strProposalName + ": Invalid Payment Address";
1428+
// check maximum amount
1429+
// can only pay out 10% of the possible coins (min value of coins)
1430+
if (nAmount > nTotalBudget) {
1431+
strInvalid = "Proposal " + strProposalName + ": Invalid nAmount (too high)";
14151432
return false;
14161433
}
14171434

1418-
if (fCheckCollateral) {
1419-
int nConf = 0;
1420-
std::string strError;
1421-
if (!IsBudgetCollateralValid(nFeeTXHash, GetHash(), strError, nTime, nConf)) {
1422-
strInvalid = "Proposal " + strProposalName + ": Invalid collateral (" + strError + ")";
1423-
return false;
1424-
}
1435+
return true;
1436+
}
1437+
1438+
bool CBudgetProposal::CheckAddress()
1439+
{
1440+
if (address == CScript()) {
1441+
strInvalid = "Proposal " + strProposalName + ": Invalid Payment Address (null)";
1442+
return false;
14251443
}
14261444

14271445
/*
@@ -1432,35 +1450,49 @@ bool CBudgetProposal::UpdateValid(int nCurrentHeight, bool fCheckCollateral)
14321450
return false;
14331451
}
14341452

1435-
//if proposal doesn't gain traction within 2 weeks, remove it
1436-
// nTime not being saved correctly
1437-
// -- TODO: We should keep track of the last time the proposal was valid, if it's invalid for 2 weeks, erase it
1438-
// if(nTime + (60*60*24*2) < GetAdjustedTime()) {
1439-
// if(GetYeas()-GetNays() < (mnodeman.CountEnabled(ActiveProtocol())/10)) {
1440-
// strError = "Not enough support";
1441-
// return false;
1442-
// }
1443-
// }
1453+
return true;
1454+
}
14441455

1445-
//can only pay out 10% of the possible coins (min value of coins)
1446-
if (nAmount > budget.GetTotalBudget(nBlockStart)) {
1447-
strInvalid = "Proposal " + strProposalName + ": Payment more than max";
1448-
return false;
1456+
bool CBudgetProposal::IsWellFormed(const CAmount& nTotalBudget)
1457+
{
1458+
return CheckStartEnd() && CheckAmount(nTotalBudget) && CheckAddress();
1459+
}
1460+
1461+
bool CBudgetProposal::IsExpired(int nCurrentHeight)
1462+
{
1463+
if (nBlockEnd < nCurrentHeight) {
1464+
strInvalid = "Proposal " + strProposalName + ": Proposal expired";
1465+
return true;
14491466
}
1467+
return false;
1468+
}
14501469

1451-
// Calculate maximum block this proposal will be valid, which is start of proposal + (number of payments * cycle)
1452-
int nProposalEnd = GetBlockStart() + (Params().GetConsensus().nBudgetCycleBlocks * GetTotalPaymentCount());
1470+
bool CBudgetProposal::UpdateValid(int nCurrentHeight, bool fCheckCollateral)
1471+
{
1472+
fValid = false;
14531473

1454-
if (nCurrentHeight <= 0) {
1455-
strInvalid = "Proposal " + strProposalName + ": Tip is NULL";
1456-
return true;
1474+
if (IsHeavilyDownvoted()) {
1475+
return false;
1476+
}
1477+
1478+
// Checks that don't change. !TODO: remove from here, they should be done only once.
1479+
if (!IsWellFormed(budget.GetTotalBudget(nBlockStart))) {
1480+
return false;
14571481
}
14581482

1459-
if(nProposalEnd < nCurrentHeight) {
1460-
strInvalid = "Proposal " + strProposalName + ": Invalid nBlockEnd (" + std::to_string(nProposalEnd) + ") < current height (" + std::to_string(nCurrentHeight) + ")";
1483+
if (IsExpired(nCurrentHeight)) {
14611484
return false;
14621485
}
14631486

1487+
if (fCheckCollateral) {
1488+
int nConf = 0;
1489+
std::string strError;
1490+
if (!IsBudgetCollateralValid(nFeeTXHash, GetHash(), strError, nTime, nConf)) {
1491+
strInvalid = "Proposal " + strProposalName + ": Invalid collateral (" + strError + ")";
1492+
return false;
1493+
}
1494+
}
1495+
14641496
fValid = true;
14651497
strInvalid.clear();
14661498
return true;

src/masternode-budget.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,15 @@ class CBudgetProposal
540540
bool fValid;
541541
std::string strInvalid;
542542

543+
// Functions used inside UpdateValid() (!TODO: move) - setting strInvalid
544+
bool IsHeavilyDownvoted();
545+
bool IsExpired(int nCurrentHeight);
546+
bool CheckStartEnd();
547+
bool CheckAmount(const CAmount& nTotalBudget);
548+
bool CheckAddress();
549+
// Static checks that should be done only once
550+
bool IsWellFormed(const CAmount& nTotalBudget);
551+
543552
protected:
544553
std::map<uint256, CBudgetVote> mapVotes;
545554
std::string strProposalName;

0 commit comments

Comments
 (0)