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

[Consensus] Single superblock #2858

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add function to check that all proposals are paid
  • Loading branch information
panleone committed Apr 27, 2023
commit 21ef3f10240dd118e6888bf4ba12944e64e88b7b
15 changes: 15 additions & 0 deletions src/budget/budgetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,21 @@ TrxValidationStatus CBudgetManager::IsTransactionValid(const CTransaction& txNew
return fThreshold ? TrxValidationStatus::InValid : TrxValidationStatus::VoteThreshold;
}

bool CBudgetManager::IsValidSuperBlockTx(const CTransaction& txNew, int nBlockHeight) const
{
assert(Params().GetConsensus().IsSuperBlock(nBlockHeight));

int nFivePercent = mnodeman.CountEnabled() / 20;

const auto highest = GetBudgetWithHighestVoteCount(nBlockHeight);
const CFinalizedBudget* pfb = highest.m_budget_fin;
if (pfb == nullptr || highest.m_vote_count <= nFivePercent) {
// No finalization or not enough votes. Nothing to check.
return true;
}
return pfb->AllBudgetsPaid(txNew);
}

std::vector<CBudgetProposal*> CBudgetManager::GetAllProposalsOrdered()
{
LOCK(cs_proposals);
Expand Down
4 changes: 3 additions & 1 deletion src/budget/budgetmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ class CBudgetManager : public CValidationInterface

bool UpdateProposal(const CBudgetVote& vote, CNode* pfrom, std::string& strError);
bool UpdateFinalizedBudget(const CFinalizedBudgetVote& vote, CNode* pfrom, std::string& strError);
TrxValidationStatus IsTransactionValid(const CTransaction& txNew, const uint256& nBlockHash, int nBlockHeight) const;
TrxValidationStatus IsTransactionValid(const CTransaction& txNew, const uint256& nBlockHash, int nBlockHeight) const; // legacy (multiple SB)
bool IsValidSuperBlockTx(const CTransaction& txNew, int nBlockHeight) const; // v6.0: single SB

std::string GetRequiredPaymentsString(int nBlockHeight);
bool FillBlockPayee(CMutableTransaction& txCoinbase, CMutableTransaction& txCoinstake, const int nHeight, bool fProofOfStake) const;

Expand Down
33 changes: 33 additions & 0 deletions src/budget/finalizedbudget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "chainparams.h"
#include "masternodeman.h"
#include "utilmoneystr.h"
#include "validation.h"

CFinalizedBudget::CFinalizedBudget() :
Expand Down Expand Up @@ -390,6 +391,38 @@ bool CFinalizedBudget::GetPayeeAndAmount(int64_t nBlockHeight, CScript& payee, C
return true;
}

bool CFinalizedBudget::AllBudgetsPaid(const CTransaction& tx) const
{
// make a map for faster lookup and deal with duplicate payees
struct cmp {
bool operator()(const CTxOut& a, const CTxOut& b) const
{
return a.scriptPubKey < b.scriptPubKey ||
(a.scriptPubKey == b.scriptPubKey && a.nValue < b.nValue);
}
};
std::map<CTxOut, int, cmp> txouts;
for (const CTxOut& o : tx.vout) {
txouts[o]++;
}

for (const CTxBudgetPayment& payment : vecBudgetPayments) {
auto it = txouts.find(CTxOut(payment.nAmount, payment.payee));
if (it == txouts.end() || it->second == 0) {
// Payment not found
CTxDestination addr;
const std::string& payee = ExtractDestination(payment.payee, addr) ? EncodeDestination(addr) : HexStr(payment.payee);
LogPrint(BCLog::MNBUDGET, "Missing payment of %s for %s (proposal hash: %s)\n",
FormatMoney(payment.nAmount), payee, payment.nProposalHash.ToString());
return false;
}
it->second--;
}

// all budgets are paid by tx
return true;
}

// return broadcast serialization
CDataStream CFinalizedBudget::GetBroadcast() const
{
Expand Down
3 changes: 3 additions & 0 deletions src/budget/finalizedbudget.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class CFinalizedBudget
bool GetBudgetPaymentByBlock(int64_t nBlockHeight, CTxBudgetPayment& payment) const;
bool GetPayeeAndAmount(int64_t nBlockHeight, CScript& payee, CAmount& nAmount) const;

// Check if ALL the budgets are paid by transaction tx
bool AllBudgetsPaid(const CTransaction& tx) const;

// Check finalized budget proposals. Masternodes only (when voting on finalized budgets)
bool CheckProposals(const std::map<uint256, CBudgetProposal>& mapWinningProposals) const;
// Total amount paid out by this budget
Expand Down
2 changes: 1 addition & 1 deletion src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ bool IsBlockPayeeValid(const CBlock& block, const CBlockIndex* pindexPrev)

// Check budget payments during superblocks
if (sporkManager.IsSporkActive(SPORK_13_ENABLE_SUPERBLOCKS) && consensus.IsSuperBlock(nBlockHeight)) {
// !TODO...
return g_budgetman.IsValidSuperBlockTx(coinbase_tx, nBlockHeight);
}

return true;
Expand Down