forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidators.cpp
More file actions
345 lines (288 loc) · 8.91 KB
/
validators.cpp
File metadata and controls
345 lines (288 loc) · 8.91 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
// Copyright (c) 2014-2025 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <governance/common.h>
#include <governance/validators.h>
#include <key_io.h>
#include <timedata.h>
#include <tinyformat.h>
#include <util/strencodings.h>
#include <util/underlying.h>
#include <algorithm>
const size_t MAX_DATA_SIZE = 512;
const size_t MAX_NAME_SIZE = 40;
CProposalValidator::CProposalValidator(const std::string& strHexData, bool fAllowScript) :
objJSON(UniValue::VOBJ),
fJSONValid(false),
fAllowScript(fAllowScript),
strErrorMessages()
{
if (!strHexData.empty()) {
ParseStrHexData(strHexData);
}
}
void CProposalValidator::ParseStrHexData(const std::string& strHexData)
{
std::vector<unsigned char> v = ParseHex(strHexData);
if (v.size() > MAX_DATA_SIZE) {
strErrorMessages = strprintf("data exceeds %lu characters;", MAX_DATA_SIZE);
return;
}
ParseJSONData(std::string(v.begin(), v.end()));
}
bool CProposalValidator::Validate(bool fCheckExpiration)
{
if (!fJSONValid) {
strErrorMessages += "JSON parsing error;";
return false;
}
if (!ValidateType()) {
strErrorMessages += "Invalid type;";
return false;
}
if (!ValidateName()) {
strErrorMessages += "Invalid name;";
return false;
}
if (!ValidateStartEndEpoch(fCheckExpiration)) {
strErrorMessages += "Invalid start:end range;";
return false;
}
if (!ValidatePaymentAmount()) {
strErrorMessages += "Invalid payment amount;";
return false;
}
if (!ValidatePaymentAddress()) {
strErrorMessages += "Invalid payment address;";
return false;
}
if (!ValidateURL()) {
strErrorMessages += "Invalid URL;";
return false;
}
return true;
}
bool CProposalValidator::ValidateType()
{
int64_t nType;
if (!GetDataValue("type", nType)) {
strErrorMessages += "type field not found;";
return false;
}
if (nType != ToUnderlying(GovernanceObject::PROPOSAL)) {
strErrorMessages += strprintf("type is not %d;", ToUnderlying(GovernanceObject::PROPOSAL));
return false;
}
return true;
}
bool CProposalValidator::ValidateName()
{
std::string strName;
if (!GetDataValue("name", strName)) {
strErrorMessages += "name field not found;";
return false;
}
if (strName.size() > MAX_NAME_SIZE) {
strErrorMessages += strprintf("name exceeds %lu characters;", MAX_NAME_SIZE);
return false;
}
if (strName.empty()) {
strErrorMessages += "name cannot be empty;";
return false;
}
static constexpr std::string_view strAllowedChars{"-_abcdefghijklmnopqrstuvwxyz0123456789"};
strName = ToLower(strName);
if (strName.find_first_not_of(strAllowedChars) != std::string::npos) {
strErrorMessages += "name contains invalid characters;";
return false;
}
return true;
}
bool CProposalValidator::ValidateStartEndEpoch(bool fCheckExpiration)
{
int64_t nStartEpoch = 0;
int64_t nEndEpoch = 0;
if (!GetDataValue("start_epoch", nStartEpoch)) {
strErrorMessages += "start_epoch field not found;";
return false;
}
if (!GetDataValue("end_epoch", nEndEpoch)) {
strErrorMessages += "end_epoch field not found;";
return false;
}
if (nEndEpoch <= nStartEpoch) {
strErrorMessages += "end_epoch <= start_epoch;";
return false;
}
if (fCheckExpiration && nEndEpoch <= GetAdjustedTime()) {
strErrorMessages += "expired;";
return false;
}
return true;
}
bool CProposalValidator::ValidatePaymentAmount()
{
double dValue = 0.0;
if (!GetDataValue("payment_amount", dValue)) {
strErrorMessages += "payment_amount field not found;";
return false;
}
if (dValue <= 0.0) {
strErrorMessages += "payment_amount is negative;";
return false;
}
// TODO: Should check for an amount which exceeds the budget but this is
// currently difficult because start and end epochs are defined in terms of
// clock time instead of block height.
return true;
}
bool CProposalValidator::ValidatePaymentAddress()
{
std::string strPaymentAddress;
if (!GetDataValue("payment_address", strPaymentAddress)) {
strErrorMessages += "payment_address field not found;";
return false;
}
if (std::find_if(strPaymentAddress.begin(), strPaymentAddress.end(), IsSpace) != strPaymentAddress.end()) {
strErrorMessages += "payment_address can't have whitespaces;";
return false;
}
CTxDestination dest = DecodeDestination(strPaymentAddress);
if (!IsValidDestination(dest)) {
strErrorMessages += "payment_address is invalid;";
return false;
}
const ScriptHash *scriptID = std::get_if<ScriptHash>(&dest);
if (!fAllowScript && scriptID) {
strErrorMessages += "script addresses are not supported;";
return false;
}
return true;
}
bool CProposalValidator::ValidateURL()
{
std::string strURL;
if (!GetDataValue("url", strURL)) {
strErrorMessages += "url field not found;";
return false;
}
if (std::find_if(strURL.begin(), strURL.end(), IsSpace) != strURL.end()) {
strErrorMessages += "url can't have whitespaces;";
return false;
}
if (strURL.size() < 4U) {
strErrorMessages += "url too short;";
return false;
}
if (!CheckURL(strURL)) {
strErrorMessages += "url invalid;";
return false;
}
return true;
}
void CProposalValidator::ParseJSONData(const std::string& strJSONData)
{
fJSONValid = false;
if (strJSONData.empty()) {
return;
}
try {
UniValue obj(UniValue::VOBJ);
obj.read(strJSONData);
if (obj.isObject()) {
objJSON = obj;
} else {
throw std::runtime_error("Proposal must be a JSON object");
}
fJSONValid = true;
} catch (std::exception& e) {
strErrorMessages += std::string(e.what()) + std::string(";");
} catch (...) {
strErrorMessages += "Unknown exception;";
}
}
bool CProposalValidator::GetDataValue(const std::string& strKey, std::string& strValueRet)
{
bool fOK = false;
try {
strValueRet = objJSON[strKey].get_str();
fOK = true;
} catch (std::exception& e) {
strErrorMessages += std::string(e.what()) + std::string(";");
} catch (...) {
strErrorMessages += "Unknown exception;";
}
return fOK;
}
bool CProposalValidator::GetDataValue(const std::string& strKey, int64_t& nValueRet)
{
bool fOK = false;
try {
const UniValue uValue = objJSON[strKey];
switch (uValue.getType()) {
case UniValue::VNUM:
nValueRet = uValue.getInt<int64_t>();
fOK = true;
break;
default:
break;
}
} catch (std::exception& e) {
strErrorMessages += std::string(e.what()) + std::string(";");
} catch (...) {
strErrorMessages += "Unknown exception;";
}
return fOK;
}
bool CProposalValidator::GetDataValue(const std::string& strKey, double& dValueRet)
{
bool fOK = false;
try {
const UniValue uValue = objJSON[strKey];
switch (uValue.getType()) {
case UniValue::VNUM:
dValueRet = uValue.get_real();
fOK = true;
break;
default:
break;
}
} catch (std::exception& e) {
strErrorMessages += std::string(e.what()) + std::string(";");
} catch (...) {
strErrorMessages += "Unknown exception;";
}
return fOK;
}
/*
The purpose of this function is to replicate the behavior of the
Python urlparse function used by sentinel (urlparse.py). This function
should return false whenever urlparse raises an exception and true
otherwise.
*/
bool CProposalValidator::CheckURL(const std::string& strURLIn)
{
std::string strRest(strURLIn);
std::string::size_type nPos = strRest.find(':');
if (nPos != std::string::npos) {
if (nPos < strRest.size()) {
strRest = strRest.substr(nPos + 1);
} else {
strRest = "";
}
}
// Process netloc
if ((strRest.size() > 2) && (strRest.substr(0, 2) == "//")) {
static constexpr std::string_view strNetlocDelimiters{"/?#"};
strRest = strRest.substr(2);
std::string::size_type nPos2 = strRest.find_first_of(strNetlocDelimiters);
std::string strNetloc = strRest.substr(0, nPos2);
if ((strNetloc.find('[') != std::string::npos) && (strNetloc.find(']') == std::string::npos)) {
return false;
}
if ((strNetloc.find(']') != std::string::npos) && (strNetloc.find('[') == std::string::npos)) {
return false;
}
}
return true;
}