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