forked from dashpay/dash
/
budgetproposal.cpp
346 lines (296 loc) · 9.34 KB
/
budgetproposal.cpp
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
// Copyright (c) 2014-2015 The Dash developers
// Copyright (c) 2015-2022 The PIVX Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "budget/budgetproposal.h"
#include "chainparams.h"
#include "script/standard.h"
#include "utilstrencodings.h"
CBudgetProposal::CBudgetProposal():
nAllotted(0),
fValid(true),
strInvalid(""),
strProposalName("unknown"),
strURL(""),
nBlockStart(0),
nBlockEnd(0),
address(),
nAmount(0),
nFeeTXHash(UINT256_ZERO),
nTime(0)
{}
CBudgetProposal::CBudgetProposal(const std::string& name,
const std::string& url,
int paycount,
const CScript& payee,
const CAmount& amount,
int blockstart,
const uint256& nfeetxhash):
nAllotted(0),
fValid(true),
strInvalid(""),
strProposalName(name),
strURL(url),
nBlockStart(blockstart),
address(payee),
nAmount(amount),
nFeeTXHash(nfeetxhash),
nTime(0)
{
// calculate the expiration block
nBlockEnd = nBlockStart + (Params().GetConsensus().nBudgetCycleBlocks + 1) * paycount;
}
// initialize from network broadcast message
bool CBudgetProposal::ParseBroadcast(CDataStream& broadcast)
{
*this = CBudgetProposal();
try {
broadcast >> LIMITED_STRING(strProposalName, 20);
broadcast >> LIMITED_STRING(strURL, 64);
broadcast >> nTime;
broadcast >> nBlockStart;
broadcast >> nBlockEnd;
broadcast >> nAmount;
broadcast >> address;
broadcast >> nFeeTXHash;
} catch (std::exception& e) {
return error("Unable to deserialize proposal broadcast: %s", e.what());
}
return true;
}
void CBudgetProposal::SyncVotes(CNode* pfrom, bool fPartial, int& nInvCount) const
{
for (const auto& it: mapVotes) {
const CBudgetVote& vote = it.second;
if (vote.IsValid() && (!fPartial || !vote.IsSynced())) {
pfrom->PushInventory(CInv(MSG_BUDGET_VOTE, vote.GetHash()));
nInvCount++;
}
}
}
bool CBudgetProposal::IsHeavilyDownvoted(int mnCount)
{
if (GetNays() - GetYeas() > 3 * mnCount / 10) {
strInvalid = "Heavily Downvoted";
return true;
}
return false;
}
bool CBudgetProposal::CheckStartEnd()
{
// block start must be a superblock
if (nBlockStart < 0 ||
nBlockStart % Params().GetConsensus().nBudgetCycleBlocks != 0) {
strInvalid = "Invalid nBlockStart";
return false;
}
if (nBlockEnd < nBlockStart) {
strInvalid = "Invalid nBlockEnd (end before start)";
return false;
}
if (GetTotalPaymentCount() > Params().GetConsensus().nMaxProposalPayments) {
strInvalid = "Invalid payment count";
return false;
}
return true;
}
bool CBudgetProposal::CheckAmount(const CAmount& nTotalBudget)
{
// check minimum amount
if (nAmount < PROPOSAL_MIN_AMOUNT) {
strInvalid = "Invalid nAmount (too low)";
return false;
}
// check maximum amount
// can only pay out 10% of the possible coins (min value of coins)
if (nAmount > nTotalBudget) {
strInvalid = "Invalid nAmount (too high)";
return false;
}
return true;
}
bool CBudgetProposal::CheckAddress()
{
// !TODO: There might be an issue with multisig in the coinbase on mainnet
// we will add support for it in a future release.
if (address.IsPayToScriptHash()) {
strInvalid = "Multisig is not currently supported.";
return false;
}
// Check address
CTxDestination dest;
if (!ExtractDestination(address, dest, false)) {
strInvalid = "Invalid script";
return false;
}
if (!IsValidDestination(dest)) {
strInvalid = "Invalid recipient address";
return false;
}
return true;
}
/* TODO: Add this to IsWellFormed() for the next hard-fork
* This will networkly reject malformed proposal names and URLs
*/
bool CBudgetProposal::CheckStrings()
{
if (strProposalName != SanitizeString(strProposalName)) {
strInvalid = "Proposal name contains illegal characters.";
return false;
}
if (strURL != SanitizeString(strURL)) {
strInvalid = "Proposal URL contains illegal characters.";
}
}
bool CBudgetProposal::IsWellFormed(const CAmount& nTotalBudget)
{
return CheckStartEnd() && CheckAmount(nTotalBudget) && CheckAddress();
}
bool CBudgetProposal::updateExpired(int nCurrentHeight)
{
if (IsExpired(nCurrentHeight)) {
strInvalid = "Proposal expired";
return true;
}
return false;
}
bool CBudgetProposal::UpdateValid(int nCurrentHeight, int mnCount)
{
fValid = false;
// Never kill a proposal before the first superblock
if (nCurrentHeight > nBlockStart && IsHeavilyDownvoted(mnCount)) {
return false;
}
if (updateExpired(nCurrentHeight)) {
return false;
}
fValid = true;
strInvalid.clear();
return true;
}
bool CBudgetProposal::IsEstablished() const
{
return nTime < GetAdjustedTime() - Params().GetConsensus().nProposalEstablishmentTime;
}
bool CBudgetProposal::IsPassing(int nBlockStartBudget, int nBlockEndBudget, int mnCount) const
{
if (!fValid)
return false;
if (this->nBlockStart > nBlockStartBudget)
return false;
if (this->nBlockEnd < nBlockEndBudget)
return false;
if (GetYeas() - GetNays() <= mnCount / 10)
return false;
if (!IsEstablished())
return false;
return true;
}
bool CBudgetProposal::IsExpired(int nCurrentHeight) const
{
return nBlockEnd < nCurrentHeight;
}
bool CBudgetProposal::AddOrUpdateVote(const CBudgetVote& vote, std::string& strError)
{
std::string strAction = "New vote inserted:";
const COutPoint& mnId = vote.GetVin().prevout;
const int64_t voteTime = vote.GetTime();
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 CBudgetProposal::GetVotesArray() const
{
UniValue ret(UniValue::VARR);
for (const auto& it: mapVotes) {
ret.push_back(it.second.ToJSON());
}
return ret;
}
void CBudgetProposal::SetSynced(bool synced)
{
for (auto& it: mapVotes) {
CBudgetVote& vote = it.second;
if (synced) {
if (vote.IsValid()) vote.SetSynced(true);
} else {
vote.SetSynced(false);
}
}
}
double CBudgetProposal::GetRatio() const
{
int yeas = GetYeas();
int nays = GetNays();
if (yeas + nays == 0) return 0.0f;
return ((double)(yeas) / (double)(yeas + nays));
}
int CBudgetProposal::GetVoteCount(CBudgetVote::VoteDirection vd) const
{
int ret = 0;
for (const auto& it : mapVotes) {
const CBudgetVote& vote = it.second;
if (vote.GetDirection() == vd && vote.IsValid())
ret++;
}
return ret;
}
int CBudgetProposal::GetBlockStartCycle() const
{
//end block is half way through the next cycle (so the proposal will be removed much after the payment is sent)
return GetBlockCycle(nBlockStart);
}
int CBudgetProposal::GetBlockCycle(int nHeight)
{
return nHeight - nHeight % Params().GetConsensus().nBudgetCycleBlocks;
}
int CBudgetProposal::GetBlockEndCycle() const
{
return nBlockEnd;
}
int CBudgetProposal::GetTotalPaymentCount() const
{
return (GetBlockEndCycle() - GetBlockStartCycle()) / Params().GetConsensus().nBudgetCycleBlocks;
}
int CBudgetProposal::GetRemainingPaymentCount(int nCurrentHeight) const
{
// If the proposal is already finished (passed the end block cycle), the payments value will be negative
int nPayments = (GetBlockEndCycle() - GetBlockCycle(nCurrentHeight)) / Params().GetConsensus().nBudgetCycleBlocks - 1;
// Take the lowest value
return std::min(nPayments, GetTotalPaymentCount());
}
// return broadcast serialization
CDataStream CBudgetProposal::GetBroadcast() const
{
CDataStream broadcast(SER_NETWORK, PROTOCOL_VERSION);
broadcast.reserve(1000);
broadcast << LIMITED_STRING(strProposalName, 20);
broadcast << LIMITED_STRING(strURL, 64);
broadcast << nTime;
broadcast << nBlockStart;
broadcast << nBlockEnd;
broadcast << nAmount;
broadcast << address;
broadcast << nFeeTXHash;
return broadcast;
}
void CBudgetProposal::Relay()
{
CInv inv(MSG_BUDGET_PROPOSAL, GetHash());
g_connman->RelayInv(inv);
}