PIVX Core  5.6.99
P2P Digital Currency
finalizedbudget.h
Go to the documentation of this file.
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 
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,
21  Valid,
24 };
25 
26 //
27 // Finalized Budget : Contains the suggested proposals to pay on a given block
28 //
29 
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;
49  std::vector<CTxBudgetPayment> vecBudgetPayments;
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 
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  bool IsValid() const { return fValid; }
74  void SetStrInvalid(const std::string& _strInvalid) { strInvalid = _strInvalid; }
75  std::string IsInvalidReason() const { return strInvalid; }
76  std::string IsInvalidLogStr() const { return strprintf("[%s (%s)]: %s", GetName(), GetProposalsStr(), IsInvalidReason()); }
77 
78  bool IsAutoChecked() const { return fAutoChecked; }
79  void SetAutoChecked(bool _fAutoChecked) { fAutoChecked = _fAutoChecked; }
80 
81  void SetProposalsStr(const std::string _strProposals) { strProposals = _strProposals; }
82 
83  std::string GetName() const { return strBudgetName; }
84  std::string GetProposalsStr() const { return strProposals; }
85  std::vector<uint256> GetProposalsHashes() const;
86  int GetBlockStart() const { return nBlockStart; }
87  int GetBlockEnd() const { return nBlockStart + (int)(vecBudgetPayments.size() - 1); }
88  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  uint256 GetHash() const
102  {
103  CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
104  ss << strBudgetName;
105  ss << nBlockStart;
106  ss << vecBudgetPayments;
107  return ss.GetHash();
108  }
109 
110  // Serialization for local DB
112  {
113  READWRITE(LIMITED_STRING(obj.strBudgetName, 20));
114  READWRITE(obj.nFeeTXHash);
115  READWRITE(obj.nTime);
116  READWRITE(obj.nBlockStart);
117  READWRITE(obj.vecBudgetPayments);
118  READWRITE(obj.fAutoChecked);
119  READWRITE(obj.mapVotes);
120  READWRITE(obj.strProposals);
121  }
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  static bool PtrGreater(CFinalizedBudget* a, CFinalizedBudget* b) { return *a > *b; }
132 };
133 
134 
135 /*
136  * Budget Payment class
137  */
139 {
140 public:
144 
146  {
147  payee = CScript();
148  nAmount = 0;
150  }
151 
152  CTxBudgetPayment(const uint256& _nProposalHash, const CScript& _payee, const CAmount _nAmount) :
153  nProposalHash(_nProposalHash),
154  payee(_payee),
155  nAmount(_nAmount)
156  {}
157 
158  //for saving to the serialized db
159  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  {
165  }
166 
167 };
168 
169 #endif // PIVX_BUDGET_FINALIZEDBUDGET_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
arith_uint256 UintToArith256(const uint256 &a)
bool GetPayeeAndAmount(int64_t nBlockHeight, CScript &payee, CAmount &nAmount) const
std::string IsInvalidLogStr() const
static constexpr unsigned int MAX_PROPOSALS_PER_CYCLE
void SetAutoChecked(bool _fAutoChecked)
static bool PtrGreater(CFinalizedBudget *a, CFinalizedBudget *b)
std::string GetProposalsStr() const
std::string IsInvalidReason() const
bool GetBudgetPaymentByBlock(int64_t nBlockHeight, CTxBudgetPayment &payment) const
bool UpdateValid(int nHeight)
bool IsValid() const
std::string GetName() const
std::string strInvalid
SERIALIZE_METHODS(CFinalizedBudget, obj)
void SetProposalsStr(const std::string _strProposals)
std::vector< CTxBudgetPayment > vecBudgetPayments
void SetSynced(bool synced)
const uint256 & GetFeeTXHash() const
bool IsPaidAlready(const uint256 &nProposalHash, const uint256 &nBlockHash, int nBlockHeight) const
bool CheckAmount(const CAmount &nTotalBudget)
bool updateExpired(int nCurrentHeight)
bool AddOrUpdateVote(const CFinalizedBudgetVote &vote, std::string &strError)
TrxValidationStatus IsTransactionValid(const CTransaction &txNew, const uint256 &nBlockHash, int nBlockHeight) const
bool ParseBroadcast(CDataStream &broadcast)
void SyncVotes(CNode *pfrom, bool fPartial, int &nInvCount) const
bool CheckProposals(const std::map< uint256, CBudgetProposal > &mapWinningProposals) const
std::map< COutPoint, CFinalizedBudgetVote > mapVotes
bool operator>(const CFinalizedBudget &other) const
void SetStrInvalid(const std::string &_strInvalid)
bool IsWellFormed(const CAmount &nTotalBudget)
int GetBlockEnd() const
std::string strProposals
bool IsAutoChecked() const
std::vector< uint256 > GetVotesHashes() const
std::string strBudgetName
CDataStream GetBroadcast() const
CAmount GetTotalPayout() const
std::vector< uint256 > GetProposalsHashes() const
uint256 GetHash() const
int GetVoteCount() const
UniValue GetVotesObject() const
int GetBlockStart() const
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:216
uint256 GetHash()
Definition: hash.h:236
Information about a peer.
Definition: net.h:669
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
bool operator>(const CTxBudgetPayment &other) const
CTxBudgetPayment(const uint256 &_nProposalHash, const CScript &_payee, const CAmount _nAmount)
SERIALIZE_METHODS(CTxBudgetPayment, obj)
256-bit opaque blob.
Definition: uint256.h:138
TrxValidationStatus
@ Valid
Transaction verification failed.
@ DoublePayment
Transaction successfully verified.
@ VoteThreshold
Transaction successfully verified, but includes a double-budget-payment.
const char * name
Definition: rest.cpp:37
@ SER_GETHASH
Definition: serialize.h:176
#define LIMITED_STRING(obj, n)
Definition: serialize.h:515
#define READWRITE(...)
Definition: serialize.h:183
#define strprintf
Definition: tinyformat.h:1056
const uint256 UINT256_ZERO
constant uint256 instances
Definition: uint256.h:175