LCOV - code coverage report
Current view: top level - src/budget - finalizedbudget.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 38 38 100.0 %
Date: 2025-02-23 09:33:43 Functions: 4 5 80.0 %

          Line data    Source code
       1             : // Copyright (c) 2014-2015 The Dash developers
       2             : // Copyright (c) 2015-2021 The PIVX Core developers
       3             : // Distributed under the MIT/X11 software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #ifndef PIVX_BUDGET_FINALIZEDBUDGET_H
       7             : #define PIVX_BUDGET_FINALIZEDBUDGET_H
       8             : 
       9             : #include "budget/budgetproposal.h"
      10             : #include "budget/finalizedbudgetvote.h"
      11             : #include "net.h"
      12             : #include "streams.h"
      13             : 
      14             : class CTxBudgetPayment;
      15             : class CBudgetManager;
      16             : 
      17             : static std::map<uint256, std::pair<uint256,int> > mapPayment_History;   // proposal hash --> (block hash, block height)
      18             : 
      19             : enum class TrxValidationStatus {
      20             :     InValid,         /** Transaction verification failed */
      21             :     Valid,           /** Transaction successfully verified */
      22             :     DoublePayment,   /** Transaction successfully verified, but includes a double-budget-payment */
      23             :     VoteThreshold    /** If not enough masternodes have voted on a finalized budget */
      24             : };
      25             : 
      26             : //
      27             : // Finalized Budget : Contains the suggested proposals to pay on a given block
      28             : //
      29             : 
      30             : class CFinalizedBudget
      31             : {
      32             : private:
      33             :     friend class CBudgetManager;
      34             : 
      35             :     bool fAutoChecked; //If it matches what we see, we'll auto vote for it (masternode only)
      36             :     bool fValid;
      37             :     std::string strInvalid;
      38             : 
      39             :     // Functions used inside IsWellFormed/UpdateValid - setting strInvalid
      40             :     bool updateExpired(int nCurrentHeight);
      41             :     bool CheckStartEnd();
      42             :     bool CheckAmount(const CAmount& nTotalBudget);
      43             :     bool CheckName();
      44             : 
      45             : protected:
      46             :     std::map<COutPoint, CFinalizedBudgetVote> mapVotes;
      47             :     std::string strBudgetName;
      48             :     int nBlockStart;
      49             :     std::vector<CTxBudgetPayment> vecBudgetPayments;
      50             :     uint256 nFeeTXHash;
      51             :     std::string strProposals;
      52             : 
      53             : public:
      54             :     static constexpr unsigned int MAX_PROPOSALS_PER_CYCLE = 100;
      55             : 
      56             :     // Set in CBudgetManager::AddFinalizedBudget via CheckCollateral
      57             :     int64_t nTime;
      58             : 
      59             :     CFinalizedBudget();
      60             :     CFinalizedBudget(const std::string& name, int blockstart, const std::vector<CTxBudgetPayment>& vecBudgetPaymentsIn, const uint256& nfeetxhash);
      61             : 
      62             :     bool AddOrUpdateVote(const CFinalizedBudgetVote& vote, std::string& strError);
      63             :     UniValue GetVotesObject() const;
      64             :     void SetSynced(bool synced);    // sets fSynced on votes (true only if valid)
      65             : 
      66             :     // sync budget votes with a node
      67             :     void SyncVotes(CNode* pfrom, bool fPartial, int& nInvCount) const;
      68             : 
      69             :     // sets fValid and strInvalid, returns fValid
      70             :     bool UpdateValid(int nHeight);
      71             :     // Static checks that should be done only once - sets strInvalid
      72             :     bool IsWellFormed(const CAmount& nTotalBudget);
      73         487 :     bool IsValid() const  { return fValid; }
      74          20 :     void SetStrInvalid(const std::string& _strInvalid) { strInvalid = _strInvalid; }
      75           2 :     std::string IsInvalidReason() const { return strInvalid; }
      76           4 :     std::string IsInvalidLogStr() const { return strprintf("[%s (%s)]: %s", GetName(), GetProposalsStr(), IsInvalidReason()); }
      77             : 
      78           2 :     bool IsAutoChecked() const { return fAutoChecked; }
      79           1 :     void SetAutoChecked(bool _fAutoChecked) { fAutoChecked = _fAutoChecked; }
      80             : 
      81          14 :     void SetProposalsStr(const std::string _strProposals) { strProposals = _strProposals; }
      82             : 
      83         632 :     std::string GetName() const { return strBudgetName; }
      84         282 :     std::string GetProposalsStr() const { return strProposals; }
      85             :     std::vector<uint256> GetProposalsHashes() const;
      86        5106 :     int GetBlockStart() const { return nBlockStart; }
      87        3672 :     int GetBlockEnd() const { return nBlockStart + (int)(vecBudgetPayments.size() - 1); }
      88         150 :     const uint256& GetFeeTXHash() const { return nFeeTXHash;  }
      89             :     int GetVoteCount() const;
      90             :     std::vector<uint256> GetVotesHashes() const;
      91             :     bool IsPaidAlready(const uint256& nProposalHash, const uint256& nBlockHash, int nBlockHeight) const;
      92             :     TrxValidationStatus IsTransactionValid(const CTransaction& txNew, const uint256& nBlockHash, int nBlockHeight) const;
      93             :     bool GetBudgetPaymentByBlock(int64_t nBlockHeight, CTxBudgetPayment& payment) const;
      94             :     bool GetPayeeAndAmount(int64_t nBlockHeight, CScript& payee, CAmount& nAmount) const;
      95             : 
      96             :     // Check finalized budget proposals. Masternodes only (when voting on finalized budgets)
      97             :     bool CheckProposals(const std::map<uint256, CBudgetProposal>& mapWinningProposals) const;
      98             :     // Total amount paid out by this budget
      99             :     CAmount GetTotalPayout() const;
     100             : 
     101         525 :     uint256 GetHash() const
     102             :     {
     103         525 :         CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
     104         525 :         ss << strBudgetName;
     105         525 :         ss << nBlockStart;
     106         525 :         ss << vecBudgetPayments;
     107         525 :         return ss.GetHash();
     108             :     }
     109             : 
     110             :     // Serialization for local DB
     111          20 :     SERIALIZE_METHODS(CFinalizedBudget, obj)
     112             :     {
     113          10 :         READWRITE(LIMITED_STRING(obj.strBudgetName, 20));
     114          10 :         READWRITE(obj.nFeeTXHash);
     115          10 :         READWRITE(obj.nTime);
     116          10 :         READWRITE(obj.nBlockStart);
     117          10 :         READWRITE(obj.vecBudgetPayments);
     118          10 :         READWRITE(obj.fAutoChecked);
     119          10 :         READWRITE(obj.mapVotes);
     120          10 :         READWRITE(obj.strProposals);
     121          10 :     }
     122             : 
     123             :     // Serialization for network messages.
     124             :     bool ParseBroadcast(CDataStream& broadcast);
     125             :     CDataStream GetBroadcast() const;
     126             :     void Relay();
     127             : 
     128             :     // compare finalized budget by votes (sort tie with feeHash)
     129             :     bool operator>(const CFinalizedBudget& other) const;
     130             :     // compare finalized budget pointers
     131           2 :     static bool PtrGreater(CFinalizedBudget* a, CFinalizedBudget* b) { return *a > *b; }
     132             : };
     133             : 
     134             : 
     135             : /*
     136             :  * Budget Payment class
     137             :  */
     138        1214 : class CTxBudgetPayment
     139             : {
     140             : public:
     141             :     uint256 nProposalHash;
     142             :     CScript payee;
     143             :     CAmount nAmount;
     144             : 
     145         620 :     CTxBudgetPayment()
     146        1240 :     {
     147         620 :         payee = CScript();
     148         620 :         nAmount = 0;
     149         620 :         nProposalHash = UINT256_ZERO;
     150             :     }
     151             : 
     152           7 :     CTxBudgetPayment(const uint256& _nProposalHash, const CScript& _payee, const CAmount _nAmount) :
     153             :         nProposalHash(_nProposalHash),
     154             :         payee(_payee),
     155           7 :         nAmount(_nAmount)
     156             :     {}
     157             : 
     158             :     //for saving to the serialized db
     159         605 :     SERIALIZE_METHODS(CTxBudgetPayment, obj) { READWRITE(obj.payee, obj.nAmount, obj.nProposalHash); }
     160             : 
     161             :     // compare payments by proposal hash
     162             :     inline bool operator>(const CTxBudgetPayment& other) const
     163             :     {
     164             :         return UintToArith256(nProposalHash) > UintToArith256(other.nProposalHash);
     165             :     }
     166             : 
     167             : };
     168             : 
     169             : #endif // PIVX_BUDGET_FINALIZEDBUDGET_H

Generated by: LCOV version 1.14