Skip to content

Commit 0b3562c

Browse files
Mrs-XFuzzbawls
authored andcommitted
[Budget] Improved double-payment-handling.
1 parent 7328ca5 commit 0b3562c

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

src/masternode-budget.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,11 @@ bool CBudgetManager::IsBudgetPaymentBlock(int nBlockHeight)
664664
return false;
665665
}
666666

667-
bool CBudgetManager::IsTransactionValid(const CTransaction& txNew, int nBlockHeight)
667+
TrxValidationStatus CBudgetManager::IsTransactionValid(const CTransaction& txNew, int nBlockHeight)
668668
{
669669
LOCK(cs);
670670

671+
TrxValidationStatus transactionStatus = TrxValidationStatus::InValid;
671672
int nHighestCount = 0;
672673
int nFivePercent = mnodeman.CountEnabled(ActiveProtocol()) / 20;
673674
std::vector<CFinalizedBudget*> ret;
@@ -694,7 +695,7 @@ bool CBudgetManager::IsTransactionValid(const CTransaction& txNew, int nBlockHei
694695
/*
695696
If budget doesn't have 5% of the network votes, then we should pay a masternode instead
696697
*/
697-
if (nHighestCount < nFivePercent) return false;
698+
if (nHighestCount < nFivePercent) return TrxValidationStatus::InValid;
698699

699700
// check the highest finalized budgets (+/- 10% to assist in consensus)
700701

@@ -713,9 +714,10 @@ bool CBudgetManager::IsTransactionValid(const CTransaction& txNew, int nBlockHei
713714
LogPrint("mnbudget","CBudgetManager::IsTransactionValid - GetVoteCount() > nCountThreshold passed\n");
714715
if (nBlockHeight >= pfinalizedBudget->GetBlockStart() && nBlockHeight <= pfinalizedBudget->GetBlockEnd()) {
715716
LogPrint("mnbudget","CBudgetManager::IsTransactionValid - GetBlockStart() passed\n");
716-
if (pfinalizedBudget->IsTransactionValid(txNew, nBlockHeight)) {
717+
transactionStatus = pfinalizedBudget->IsTransactionValid(txNew, nBlockHeight);
718+
if (transactionStatus == TrxValidationStatus::Valid) {
717719
LogPrint("mnbudget","CBudgetManager::IsTransactionValid - pfinalizedBudget->IsTransactionValid() passed\n");
718-
return true;
720+
return TrxValidationStatus::Valid;
719721
}
720722
else {
721723
LogPrint("mnbudget","CBudgetManager::IsTransactionValid - pfinalizedBudget->IsTransactionValid() error\n");
@@ -731,7 +733,7 @@ bool CBudgetManager::IsTransactionValid(const CTransaction& txNew, int nBlockHei
731733
}
732734

733735
//we looked through all of the known budgets
734-
return false;
736+
return transactionStatus;
735737
}
736738

737739
std::vector<CBudgetProposal*> CBudgetManager::GetAllProposals()
@@ -2107,20 +2109,20 @@ bool CFinalizedBudget::IsPaidAlready(uint256 nProposalHash, int nBlockHeight)
21072109
return true;
21082110
}
21092111

2110-
bool CFinalizedBudget::IsTransactionValid(const CTransaction& txNew, int nBlockHeight)
2112+
TrxValidationStatus CFinalizedBudget::IsTransactionValid(const CTransaction& txNew, int nBlockHeight)
21112113
{
2114+
TrxValidationStatus transactionStatus = TrxValidationStatus::InValid;
21122115
int nCurrentBudgetPayment = nBlockHeight - GetBlockStart();
21132116
if (nCurrentBudgetPayment < 0) {
21142117
LogPrint("mnbudget","CFinalizedBudget::IsTransactionValid - Invalid block - height: %d start: %d\n", nBlockHeight, GetBlockStart());
2115-
return false;
2118+
return TrxValidationStatus::InValid;
21162119
}
21172120

21182121
if (nCurrentBudgetPayment > (int)vecBudgetPayments.size() - 1) {
21192122
LogPrint("mnbudget","CFinalizedBudget::IsTransactionValid - Invalid last block - current budget payment: %d of %d\n", nCurrentBudgetPayment + 1, (int)vecBudgetPayments.size());
2120-
return false;
2123+
return TrxValidationStatus::InValid;
21212124
}
21222125

2123-
bool found = false;
21242126
bool paid = false;
21252127

21262128
BOOST_FOREACH (CTxOut out, txNew.vout) {
@@ -2136,18 +2138,18 @@ bool CFinalizedBudget::IsTransactionValid(const CTransaction& txNew, int nBlockH
21362138
vecBudgetPayments[nCurrentBudgetPayment].nAmount, vecBudgetPayments[nCurrentBudgetPayment].nProposalHash.Get32());
21372139
// No matter what we've found before, stop all checks here. In future releases there might be more than one budget payment
21382140
// per block, so even if the first one was not paid yet this one disables all budget payments for this block.
2139-
found = false;
2141+
transactionStatus = TrxValidationStatus::DoublePayment;
21402142
break;
21412143
}
21422144
else {
2143-
found = true;
2145+
transactionStatus = TrxValidationStatus::Valid;
21442146
LogPrint("mnbudget","CFinalizedBudget::IsTransactionValid - Found valid Budget Payment of %d for proposal %d\n",
21452147
vecBudgetPayments[nCurrentBudgetPayment].nAmount, vecBudgetPayments[nCurrentBudgetPayment].nProposalHash.Get32());
21462148
}
21472149
}
21482150
}
21492151

2150-
if (!found) {
2152+
if (transactionStatus == TrxValidationStatus::InValid) {
21512153
CTxDestination address1;
21522154
ExtractDestination(vecBudgetPayments[nCurrentBudgetPayment].payee, address1);
21532155
CBitcoinAddress address2(address1);
@@ -2156,7 +2158,7 @@ bool CFinalizedBudget::IsTransactionValid(const CTransaction& txNew, int nBlockH
21562158
address2.ToString(), vecBudgetPayments[nCurrentBudgetPayment].nAmount, nCurrentBudgetPayment);
21572159
}
21582160

2159-
return found;
2161+
return transactionStatus;
21602162
}
21612163

21622164
void CFinalizedBudget::SubmitVote()

src/masternode-budget.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class CTxBudgetPayment;
3131
#define VOTE_YES 1
3232
#define VOTE_NO 2
3333

34+
enum class TrxValidationStatus {
35+
InValid, /** Transaction verification failed */
36+
Valid, /** Transaction successfully verified */
37+
DoublePayment /** Transaction successfully verified, but includes a double-budget-payment */
38+
};
39+
3440
static const CAmount PROPOSAL_FEE_TX = (50 * COIN);
3541
static const CAmount BUDGET_FEE_TX_OLD = (50 * COIN);
3642
static const CAmount BUDGET_FEE_TX = (5 * COIN);
@@ -235,7 +241,7 @@ class CBudgetManager
235241
bool UpdateProposal(CBudgetVote& vote, CNode* pfrom, std::string& strError);
236242
bool UpdateFinalizedBudget(CFinalizedBudgetVote& vote, CNode* pfrom, std::string& strError);
237243
bool PropExists(uint256 nHash);
238-
bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
244+
TrxValidationStatus IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
239245
std::string GetRequiredPaymentsString(int nBlockHeight);
240246
void FillBlockPayee(CMutableTransaction& txNew, CAmount nFees, bool fProofOfStake);
241247

@@ -338,7 +344,7 @@ class CFinalizedBudget
338344
int GetBlockEnd() { return nBlockStart + (int)(vecBudgetPayments.size() - 1); }
339345
int GetVoteCount() { return (int)mapVotes.size(); }
340346
bool IsPaidAlready(uint256 nProposalHash, int nBlockHeight);
341-
bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
347+
TrxValidationStatus IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
342348
bool GetBudgetPaymentByBlock(int64_t nBlockHeight, CTxBudgetPayment& payment)
343349
{
344350
LOCK(cs);

src/masternode-payments.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ bool IsBlockValueValid(const CBlock& block, CAmount nExpectedValue, CAmount nMin
224224

225225
bool IsBlockPayeeValid(const CBlock& block, int nBlockHeight)
226226
{
227+
TrxValidationStatus transactionStatus = TrxValidationStatus::InValid;
228+
227229
if (!masternodeSync.IsSynced()) { //there is no budget data to use to check anything -- find the longest chain
228230
LogPrint("mnpayments", "Client not synced, skipping block payee checks\n");
229231
return true;
@@ -234,18 +236,25 @@ bool IsBlockPayeeValid(const CBlock& block, int nBlockHeight)
234236
//check if it's a budget block
235237
if (IsSporkActive(SPORK_13_ENABLE_SUPERBLOCKS)) {
236238
if (budget.IsBudgetPaymentBlock(nBlockHeight)) {
237-
if (budget.IsTransactionValid(txNew, nBlockHeight))
239+
transactionStatus = budget.IsTransactionValid(txNew, nBlockHeight);
240+
if (transactionStatus == TrxValidationStatus::Valid) {
238241
return true;
242+
}
239243

240-
LogPrint("masternode","Invalid budget payment detected %s\n", txNew.ToString().c_str());
241-
if (IsSporkActive(SPORK_9_MASTERNODE_BUDGET_ENFORCEMENT))
242-
return false;
244+
if (transactionStatus == TrxValidationStatus::InValid) {
245+
LogPrint("masternode","Invalid budget payment detected %s\n", txNew.ToString().c_str());
246+
if (IsSporkActive(SPORK_9_MASTERNODE_BUDGET_ENFORCEMENT))
247+
return false;
243248

244-
LogPrint("masternode","Budget enforcement is disabled, accepting block\n");
245-
return true;
249+
LogPrint("masternode","Budget enforcement is disabled, accepting block\n");
250+
}
246251
}
247252
}
248253

254+
// If we end here the transaction was either TrxValidationStatus::InValid and Budget enforcement is disabled, or
255+
// a double budget payment (status = TrxValidationStatus::DoublePayment) was detected
256+
// In both cases a masternode will get the payment for this block
257+
249258
//check for masternode payee
250259
if (masternodePayments.IsTransactionValid(txNew, nBlockHeight))
251260
return true;

0 commit comments

Comments
 (0)