forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathfinalizedbudget.cpp
More file actions
459 lines (398 loc) · 16.4 KB
/
Copy pathfinalizedbudget.cpp
File metadata and controls
459 lines (398 loc) · 16.4 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// 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.
#include "budget/finalizedbudget.h"
#include "masternodeman.h"
#include "utilmoneystr.h"
#include "validation.h"
CFinalizedBudget::CFinalizedBudget() :
fAutoChecked(false),
fValid(true),
strInvalid(),
mapVotes(),
strBudgetName(""),
nBlockStart(0),
vecBudgetPayments(),
nFeeTXHash(UINT256_ZERO),
strProposals(""),
nTime(0)
{ }
CFinalizedBudget::CFinalizedBudget(const std::string& name,
int blockstart,
const std::vector<CTxBudgetPayment>& vecBudgetPaymentsIn,
const uint256& nfeetxhash):
fAutoChecked(false),
fValid(true),
strInvalid(),
mapVotes(),
strBudgetName(name),
nBlockStart(blockstart),
vecBudgetPayments(vecBudgetPaymentsIn),
nFeeTXHash(nfeetxhash),
strProposals(""),
nTime(0)
{ }
bool CFinalizedBudget::ParseBroadcast(CDataStream& broadcast)
{
*this = CFinalizedBudget();
try {
broadcast >> LIMITED_STRING(strBudgetName, 20);
broadcast >> nBlockStart;
broadcast >> vecBudgetPayments;
broadcast >> nFeeTXHash;
} catch (std::exception& e) {
return error("Unable to deserialize finalized budget broadcast: %s", e.what());
}
return true;
}
bool CFinalizedBudget::AddOrUpdateVote(const CFinalizedBudgetVote& vote, std::string& strError)
{
const COutPoint& mnId = vote.GetVin().prevout;
const int64_t voteTime = vote.GetTime();
std::string strAction = "New vote inserted:";
if (mapVotes.count(mnId)) {
const int64_t oldTime = mapVotes[mnId].GetTime();
if (oldTime > voteTime) {
strError = strprintf("new vote older than existing vote - %s\n", vote.GetHash().ToString());
LogPrint(BCLog::MNBUDGET, "%s: %s\n", __func__, strError);
return false;
}
if (voteTime - oldTime < BUDGET_VOTE_UPDATE_MIN) {
strError = strprintf("time between votes is too soon - %s - %lli sec < %lli sec\n",
vote.GetHash().ToString(), voteTime - oldTime, BUDGET_VOTE_UPDATE_MIN);
LogPrint(BCLog::MNBUDGET, "%s: %s\n", __func__, strError);
return false;
}
strAction = "Existing vote updated:";
}
mapVotes[mnId] = vote;
LogPrint(BCLog::MNBUDGET, "%s: %s %s\n", __func__, strAction.c_str(), vote.GetHash().ToString().c_str());
return true;
}
UniValue CFinalizedBudget::GetVotesObject() const
{
UniValue ret(UniValue::VOBJ);
for (const auto& it: mapVotes) {
const CFinalizedBudgetVote& vote = it.second;
ret.pushKV(vote.GetVin().prevout.ToStringShort(), vote.ToJSON());
}
return ret;
}
void CFinalizedBudget::SetSynced(bool synced)
{
for (auto& it: mapVotes) {
CFinalizedBudgetVote& vote = it.second;
if (synced) {
if (vote.IsValid()) vote.SetSynced(true);
} else {
vote.SetSynced(false);
}
}
}
bool CFinalizedBudget::CheckProposals(const std::map<uint256, CBudgetProposal>& mapWinningProposals) const
{
if (mapWinningProposals.empty()) {
LogPrint(BCLog::MNBUDGET,"%s: No Budget-Proposals found, aborting\n", __func__);
return false;
}
if (mapWinningProposals.size() != vecBudgetPayments.size()) {
LogPrint(BCLog::MNBUDGET,"%s: Budget-Proposal length (%ld) doesn't match Budget-Payment length (%ld).\n",
__func__, mapWinningProposals.size(), vecBudgetPayments.size());
return false;
}
for (unsigned int i = 0; i < vecBudgetPayments.size(); i++) {
LogPrint(BCLog::MNBUDGET,"%s: Budget-Payments - nProp %d %s\n", __func__, i, vecBudgetPayments[i].nProposalHash.ToString());
LogPrint(BCLog::MNBUDGET,"%s: Budget-Payments - Payee %d %s\n", __func__, i, HexStr(vecBudgetPayments[i].payee));
LogPrint(BCLog::MNBUDGET,"%s: Budget-Payments - nAmount %d %lli\n", __func__, i, vecBudgetPayments[i].nAmount);
}
for (const auto& it: mapWinningProposals) {
LogPrint(BCLog::MNBUDGET,"%s: Budget-Proposals - nProp %s\n", __func__, (it.first).ToString());
LogPrint(BCLog::MNBUDGET,"%s: Budget-Proposals - Payee %s\n", __func__, HexStr((it.second).GetPayee()));
LogPrint(BCLog::MNBUDGET,"%s: Budget-Proposals - nAmount %lli\n", __func__, (it.second).GetAmount());
}
for (const CTxBudgetPayment& p : vecBudgetPayments) {
const auto& it = mapWinningProposals.find(p.nProposalHash);
if (it == mapWinningProposals.end()) {
LogPrint(BCLog::MNBUDGET,"%s: Proposal %s not found\n", __func__, p.nProposalHash.ToString());
return false;
}
const CBudgetProposal& prop = it->second;
if (p.payee != prop.GetPayee()) {
LogPrint(BCLog::MNBUDGET,"%s: payee doesn't match %s != %s\n", __func__, HexStr(p.payee), HexStr(prop.GetPayee()));
return false;
}
if (p.nAmount != prop.GetAmount()) {
LogPrint(BCLog::MNBUDGET,"%s: payee amount doesn't match %lli != %lli\n", __func__, p.nAmount, prop.GetAmount());
return false;
}
}
LogPrint(BCLog::MNBUDGET,"%s: Finalized Budget Matches! Submitting Vote.\n", __func__);
return true;
}
CAmount CFinalizedBudget::GetTotalPayout() const
{
CAmount ret = 0;
for (auto & vecBudgetPayment : vecBudgetPayments) {
ret += vecBudgetPayment.nAmount;
}
return ret;
}
std::vector<uint256> CFinalizedBudget::GetProposalsHashes() const
{
std::vector<uint256> vHashes;
for (const CTxBudgetPayment& budgetPayment : vecBudgetPayments) {
vHashes.push_back(budgetPayment.nProposalHash);
}
return vHashes;
}
void CFinalizedBudget::SyncVotes(CNode* pfrom, bool fPartial, int& nInvCount) const
{
for (const auto& it: mapVotes) {
const CFinalizedBudgetVote& vote = it.second;
if (vote.IsValid() && (!fPartial || !vote.IsSynced())) {
pfrom->PushInventory(CInv(MSG_BUDGET_FINALIZED_VOTE, vote.GetHash()));
nInvCount++;
}
}
}
bool CFinalizedBudget::CheckStartEnd()
{
if (nBlockStart == 0) {
strInvalid = "Invalid BlockStart == 0";
return false;
}
// Must be the correct block for payment to happen (once a month)
if (!Params().GetConsensus().IsSuperBlock(nBlockStart)) {
strInvalid = "Invalid BlockStart";
return false;
}
// The following 2 checks check the same (basically if vecBudgetPayments.size() > 100)
if (GetBlockEnd() - nBlockStart + 1 > (int) MAX_PROPOSALS_PER_CYCLE) {
strInvalid = "Invalid BlockEnd";
return false;
}
if ((int)vecBudgetPayments.size() > (int) MAX_PROPOSALS_PER_CYCLE) {
strInvalid = "Invalid budget payments count (too many)";
return false;
}
return true;
}
bool CFinalizedBudget::CheckAmount(const CAmount& nTotalBudget)
{
// Can only pay out 10% of the possible coins (min value of coins)
if (GetTotalPayout() > nTotalBudget) {
strInvalid = "Invalid Payout (more than max)";
return false;
}
return true;
}
bool CFinalizedBudget::CheckName()
{
if (strBudgetName == "") {
strInvalid = "Invalid Budget Name";
return false;
}
return true;
}
bool CFinalizedBudget::updateExpired(int nCurrentHeight)
{
// Remove finalized budgets 2 * MAX_PROPOSALS_PER_CYCLE blocks after their end
const int nBlockEnd = GetBlockEnd();
if (nCurrentHeight >= nBlockEnd + 2 * (int) MAX_PROPOSALS_PER_CYCLE) {
strInvalid = strprintf("(ends at block %ld) too old and obsolete (current %ld)", nBlockEnd, nCurrentHeight);
return true;
}
return false;
}
bool CFinalizedBudget::IsWellFormed(const CAmount& nTotalBudget)
{
return CheckStartEnd() && CheckAmount(nTotalBudget) && CheckName();
}
bool CFinalizedBudget::UpdateValid(int nCurrentHeight)
{
fValid = false;
if (updateExpired(nCurrentHeight)) {
return false;
}
fValid = true;
strInvalid.clear();
return true;
}
int CFinalizedBudget::GetVoteCount() const
{
int ret = 0;
for (const auto& it : mapVotes) {
if (it.second.IsValid()) {
ret++;
}
}
return ret;
}
std::vector<uint256> CFinalizedBudget::GetVotesHashes() const
{
std::vector<uint256> vRet;
for (const auto& it: mapVotes) {
vRet.push_back(it.second.GetHash());
}
return vRet;
}
bool CFinalizedBudget::IsPaidAlready(const uint256& nProposalHash, const uint256& nBlockHash, int nBlockHeight) const
{
// Remove budget-payments from former/future payment cycles
int nPaidBlockHeight = 0;
uint256 nOldProposalHash;
for(auto it = mapPayment_History.begin(); it != mapPayment_History.end(); /* No incrementation needed */ ) {
nPaidBlockHeight = (*it).second.second;
if((nPaidBlockHeight < GetBlockStart()) || (nPaidBlockHeight > GetBlockEnd())) {
nOldProposalHash = (*it).first;
LogPrint(BCLog::MNBUDGET, "%s: Budget Proposal %s, Block %d from old cycle deleted\n",
__func__, nOldProposalHash.ToString().c_str(), nPaidBlockHeight);
it = mapPayment_History.erase(it);
} else {
++it;
}
}
// Now that we only have payments from the current payment cycle check if this budget was paid already
if(mapPayment_History.count(nProposalHash) == 0) {
// New proposal payment, insert into map for checks with later blocks from this cycle
mapPayment_History.emplace(std::piecewise_construct,
std::forward_as_tuple(nProposalHash),
std::forward_as_tuple(nBlockHash, nBlockHeight));
LogPrint(BCLog::MNBUDGET, "%s: Budget Proposal %s, Block %d (%s) added to payment history (size=%d)\n",
__func__, nProposalHash.ToString(), nBlockHeight, nBlockHash.ToString(), mapPayment_History.size());
return false;
}
// This budget payment was already checked/paid
const uint256& nPaidBlockHash = mapPayment_History.at(nProposalHash).first;
// If we are checking a different block, and the paid one is on chain
// -> reject transaction so it gets paid to a masternode instead
if (nBlockHash != nPaidBlockHash) {
LOCK(cs_main);
CBlockIndex* pindex = LookupBlockIndex(nPaidBlockHash);
return pindex && chainActive.Contains(pindex);
}
// Re-checking same block. Not a double payment.
return false;
}
TrxValidationStatus CFinalizedBudget::IsTransactionValid(const CTransaction& txNew, const uint256& nBlockHash, int nBlockHeight) const
{
const int nBlockEnd = GetBlockEnd();
if (nBlockHeight > nBlockEnd) {
LogPrint(BCLog::MNBUDGET,"%s: Invalid block - height: %d end: %d\n", __func__, nBlockHeight, nBlockEnd);
return TrxValidationStatus::InValid;
}
if (nBlockHeight < nBlockStart) {
LogPrint(BCLog::MNBUDGET,"%s: Invalid block - height: %d start: %d\n", __func__, nBlockHeight, nBlockStart);
return TrxValidationStatus::InValid;
}
const int nCurrentBudgetPayment = nBlockHeight - nBlockStart;
if (nCurrentBudgetPayment > (int)vecBudgetPayments.size() - 1) {
LogPrint(BCLog::MNBUDGET,"%s: Invalid last block - current budget payment: %d of %d\n",
__func__, nCurrentBudgetPayment + 1, (int)vecBudgetPayments.size());
return TrxValidationStatus::InValid;
}
// Check if this proposal was paid already. If so, pay a masternode instead
if(IsPaidAlready(vecBudgetPayments[nCurrentBudgetPayment].nProposalHash, nBlockHash, nBlockHeight)) {
LogPrint(BCLog::MNBUDGET,"%s: Double Budget Payment of %d for proposal %d detected. Paying a masternode instead.\n",
__func__, vecBudgetPayments[nCurrentBudgetPayment].nAmount, vecBudgetPayments[nCurrentBudgetPayment].nProposalHash.GetHex());
// No matter what we've found before, stop all checks here. In future releases there might be more than one budget payment
// per block, so even if the first one was not paid yet this one disables all budget payments for this block.
return TrxValidationStatus::DoublePayment;
}
// Search the payment
const CScript& scriptExpected = vecBudgetPayments[nCurrentBudgetPayment].payee;
const CAmount& amountExpected = vecBudgetPayments[nCurrentBudgetPayment].nAmount;
// Budget payment is usually the last output of coinstake txes, iterate backwords
for (auto out = txNew.vout.rbegin(); out != txNew.vout.rend(); ++out) {
LogPrint(BCLog::MNBUDGET,"%s: nCurrentBudgetPayment=%d, payee=%s == out.scriptPubKey=%s, amount=%ld == out.nValue=%ld\n",
__func__, nCurrentBudgetPayment, HexStr(scriptExpected), HexStr(out->scriptPubKey), amountExpected, out->nValue);
if (scriptExpected == out->scriptPubKey && amountExpected == out->nValue) {
// payment found
LogPrint(BCLog::MNBUDGET,"%s: Found valid Budget Payment of %d for proposal %d\n",
__func__, amountExpected, vecBudgetPayments[nCurrentBudgetPayment].nProposalHash.GetHex());
return TrxValidationStatus::Valid;
}
}
// payment not found
CTxDestination address1;
ExtractDestination(scriptExpected, address1);
LogPrint(BCLog::MNBUDGET,"%s: Missing required payment - %s: %d c: %d\n",
__func__, EncodeDestination(address1), amountExpected, nCurrentBudgetPayment);
return TrxValidationStatus::InValid;
}
bool CFinalizedBudget::GetBudgetPaymentByBlock(int64_t nBlockHeight, CTxBudgetPayment& payment) const
{
int i = nBlockHeight - GetBlockStart();
if (i < 0) return false;
if (i > (int)vecBudgetPayments.size() - 1) return false;
payment = vecBudgetPayments[i];
return true;
}
bool CFinalizedBudget::GetPayeeAndAmount(int64_t nBlockHeight, CScript& payee, CAmount& nAmount) const
{
int i = nBlockHeight - GetBlockStart();
if (i < 0) return false;
if (i > (int)vecBudgetPayments.size() - 1) return false;
payee = vecBudgetPayments[i].payee;
nAmount = vecBudgetPayments[i].nAmount;
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;
}
void CFinalizedBudget::PayAllBudgets(CMutableTransaction& tx) const
{
for (const CTxBudgetPayment& payment : vecBudgetPayments) {
tx.vout.emplace_back(payment.nAmount, payment.payee);
}
}
// return broadcast serialization
CDataStream CFinalizedBudget::GetBroadcast() const
{
CDataStream broadcast(SER_NETWORK, PROTOCOL_VERSION);
broadcast.reserve(1000);
broadcast << LIMITED_STRING(strBudgetName, 20);
broadcast << nBlockStart;
broadcast << vecBudgetPayments;
broadcast << nFeeTXHash;
return broadcast;
}
bool CFinalizedBudget::operator>(const CFinalizedBudget& other) const
{
const int count = GetVoteCount();
const int otherCount = other.GetVoteCount();
if (count == otherCount) return UintToArith256(GetFeeTXHash()) > UintToArith256(other.GetFeeTXHash());
return count > otherCount;
}
void CFinalizedBudget::Relay()
{
CInv inv(MSG_BUDGET_FINALIZED, GetHash());
g_connman->RelayInv(inv);
}