forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 15
/
rpcgovernance.cpp
999 lines (796 loc) · 42 KB
/
rpcgovernance.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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
// Copyright (c) 2014-2017 The Däsh Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//#define ENABLE_biblepay_DEBUG
#include "activemasternode.h"
#include "darksend.h"
#include "governance.h"
#include "governance-vote.h"
#include "governance-classes.h"
#include "init.h"
#include "main.h"
#include "masternode.h"
#include "masternode-sync.h"
#include "masternodeconfig.h"
#include "masternodeman.h"
#include "rpcserver.h"
#include "util.h"
#include "utilmoneystr.h"
#include <boost/lexical_cast.hpp>
#include "podc.h"
UniValue gobject(const UniValue& params, bool fHelp)
{
std::string strCommand;
if (params.size() >= 1)
strCommand = params[0].get_str();
if (fHelp ||
(strCommand != "vote-many" && strCommand != "serialize-trigger" && strCommand != "vote-conf" && strCommand != "serialize" && strCommand != "vote-alias" && strCommand != "prepare" && strCommand != "submit" && strCommand != "count" &&
strCommand != "deserialize" && strCommand != "get" && strCommand != "getvotes" && strCommand != "getcurrentvotes" && strCommand != "list" && strCommand != "diff"))
throw std::runtime_error(
"gobject \"command\"...\n"
"Manage governance objects\n"
"\nAvailable commands:\n"
" prepare - Prepare governance object by signing and creating tx\n"
" submit - Submit governance object to network\n"
" deserialize - Deserialize governance object from hex string to JSON\n"
" count - Count governance objects and votes\n"
" get - Get governance object by hash\n"
" getvotes - Get all votes for a governance object hash (including old votes)\n"
" getcurrentvotes - Get only current (tallying) votes for a governance object hash (does not include old votes)\n"
" list - List governance objects (can be filtered by validity and/or object type)\n"
" diff - List differences since last diff\n"
" vote-alias - Vote on a governance object by masternode alias (using masternode.conf setup)\n"
" vote-conf - Vote on a governance object by masternode configured in biblepay.conf\n"
" vote-many - Vote on a governance object by all masternodes (using masternode.conf setup)\n"
);
if(strCommand == "count")
return governance.ToString();
/*
------ Example Governance Item ------
gobject submit 6e622bb41bad1fb18e7f23ae96770aeb33129e18bd9efe790522488e580a0a03 0 1 1464292854 "beer-reimbursement" 5b5b22636f6e7472616374222c207b2270726f6a6563745f6e616d65223a20225c22626565722d7265696d62757273656d656e745c22222c20227061796d656e745f61646472657373223a20225c225879324c4b4a4a64655178657948726e34744744514238626a6876464564615576375c22222c2022656e645f64617465223a202231343936333030343030222c20226465736372697074696f6e5f75726c223a20225c227777772e646173687768616c652e6f72672f702f626565722d7265696d62757273656d656e745c22222c2022636f6e74726163745f75726c223a20225c22626565722d7265696d62757273656d656e742e636f6d2f3030312e7064665c22222c20227061796d656e745f616d6f756e74223a20223233342e323334323232222c2022676f7665726e616e63655f6f626a6563745f6964223a2037342c202273746172745f64617465223a202231343833323534303030227d5d5d1
*/
// DEBUG : TEST DESERIALIZATION OF GOVERNANCE META DATA
if(strCommand == "deserialize")
{
if (params.size() != 2) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject deserialize <data-hex>'");
}
std::string strHex = params[1].get_str();
std::vector<unsigned char> v = ParseHex(strHex);
std::string s(v.begin(), v.end());
UniValue u(UniValue::VOBJ);
u.read(s);
return u.write().c_str();
}
if (strCommand == "serialize-trigger")
{
/* [["trigger",{"event_block_height":5520,"payment_addresses":"
yaddress1|yaddr2","payment_amounts":"2.00000000|2.00000000",
"proposal_hashes":"pph","type":2}]]
*/
std::string sEventBlockHeight = params[1].get_str();
std::string sPaymentAddresses = params[2].get_str();
std::string sPaymentAmounts = params[3].get_str();
std::string sProposalHashes = params[4].get_str();
std::string sType = params[5].get_str();
std::string sQ = "\"";
std::string sJson = "[[" + sQ + "trigger" + sQ + ",{";
sJson += GJE("event_block_height", sEventBlockHeight, true, false); // Must be an int
sJson += GJE("payment_addresses", sPaymentAddresses, true, true);
sJson += GJE("payment_amounts", sPaymentAmounts, true, true);
sJson += GJE("proposal_hashes", sProposalHashes, true, true);
sJson += GJE("type", sType, false, false); // Must be an Int <- This is the line of code causing us great pain on New Years Day of 2018
sJson += "}]]";
UniValue u(UniValue::VOBJ);
// make into hex
std::vector<unsigned char> vchJson = vector<unsigned char>(sJson.begin(), sJson.end());
std::string sHex = HexStr(vchJson.begin(), vchJson.end());
u.push_back(Pair("Hex", sHex));
return u;
}
if (strCommand == "serialize")
{
/* [["proposal",{
"end_epoch":"1507777400",
"name":"TEST",
"payment_address":"bqd2cL18KD1v8bYJDFERM2LyvE7R6wZ8Rs",
"payment_amount":"5",
"start_epoch":"1507731300",
"type":1,
"url":"https://myproposal.biblepay.org"
}]]
*/
std::string sEnd = params[1].get_str();
std::string sName = params[2].get_str();
std::string sAddress = params[3].get_str();
std::string sAmount = params[4].get_str();
std::string sStart = params[5].get_str();
std::string sType = params[6].get_str();
std::string sURL = params[7].get_str();
std::string sQ = "\"";
std::string sJson = "[[" + sQ + "proposal" + sQ + ",{";
sJson += GJE("end_epoch",sEnd,true,true);
sJson += GJE("name",sName,true,true);
sJson += GJE("payment_address",sAddress,true,true);
sJson += GJE("payment_amount",sAmount,true,true);
sJson += GJE("start_epoch",sStart,true,true);
sJson += GJE("type",sType,true,false);
sJson += GJE("url",sURL,false,true);
sJson += "}]]";
UniValue u(UniValue::VOBJ);
// make into hex
std::vector<unsigned char> vchJson = vector<unsigned char>(sJson.begin(), sJson.end());
std::string sHex = HexStr(vchJson.begin(), vchJson.end());
u.push_back(Pair("Hex", sHex));
/*
std::vector<unsigned char> v = ParseHex(sHex);
std::string sFromHex(v.begin(), v.end());
u.push_back(Pair("Json", sJson));
u.push_back(Pair("JsonFromHex", sFromHex));
*/
return u;
}
// PREPARE THE GOVERNANCE OBJECT BY CREATING A COLLATERAL TRANSACTION
if(strCommand == "prepare")
{
if (params.size() != 5) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject prepare <parent-hash> <revision> <time> <data-hex>'");
}
// ASSEMBLE NEW GOVERNANCE OBJECT FROM USER PARAMETERS
uint256 hashParent;
// -- attach to root node (root node doesn't really exist, but has a hash of zero)
if(params[1].get_str() == "0") {
hashParent = uint256();
} else {
hashParent = ParseHashV(params[1], "fee-txid, parameter 1");
}
std::string strRevision = params[2].get_str();
std::string strTime = params[3].get_str();
int nRevision = boost::lexical_cast<int>(strRevision);
int nTime = boost::lexical_cast<int>(strTime);
std::string strData = params[4].get_str();
// CREATE A NEW COLLATERAL TRANSACTION FOR THIS SPECIFIC OBJECT
CGovernanceObject govobj(hashParent, nRevision, nTime, uint256(), strData);
if((govobj.GetObjectType() == GOVERNANCE_OBJECT_TRIGGER) ||
(govobj.GetObjectType() == GOVERNANCE_OBJECT_WATCHDOG)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Trigger and watchdog objects need not be prepared (however only masternodes can create them)");
}
std::string strError = "";
if(!govobj.IsValidLocally(strError, false))
throw JSONRPCError(RPC_INTERNAL_ERROR, "Governance object is not valid - " + govobj.GetHash().ToString() + " - " + strError);
CWalletTx wtx;
if(!pwalletMain->GetBudgetSystemCollateralTX(wtx, govobj.GetHash(), govobj.GetMinCollateralFee(), false)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Error making collateral transaction for governance object. Please check your wallet balance and make sure your wallet is unlocked.");
}
// -- make our change address
CReserveKey reservekey(pwalletMain);
// -- send the tx to the network
pwalletMain->CommitTransaction(wtx, reservekey, NetMsgType::TX);
DBG( cout << "gobject: prepare "
<< " strData = " << govobj.GetDataAsString()
<< ", hash = " << govobj.GetHash().GetHex()
<< ", txidFee = " << wtx.GetHash().GetHex()
<< endl; );
return wtx.GetHash().ToString();
}
// AFTER COLLATERAL TRANSACTION HAS MATURED USER CAN SUBMIT GOVERNANCE OBJECT TO PROPAGATE NETWORK
if(strCommand == "submit")
{
if ((params.size() < 5) || (params.size() > 6)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject submit <parent-hash> <revision> <time> <data-hex> <fee-txid>'");
}
if(!masternodeSync.IsBlockchainSynced()) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Must wait for client to sync with masternode network. Try again in a minute or so.");
}
CMasternode mn;
bool fMnFound = mnodeman.Get(activeMasternode.vin, mn);
DBG( cout << "gobject: submit activeMasternode.pubKeyMasternode = " << activeMasternode.pubKeyMasternode.GetHash().ToString()
<< ", vin = " << activeMasternode.vin.prevout.ToStringShort()
<< ", params.size() = " << params.size()
<< ", fMnFound = " << fMnFound << endl; );
// ASSEMBLE NEW GOVERNANCE OBJECT FROM USER PARAMETERS
uint256 txidFee;
if(params.size() == 6) {
txidFee = ParseHashV(params[5], "fee-txid, parameter 6");
}
uint256 hashParent;
if(params[1].get_str() == "0") { // attach to root node (root node doesn't really exist, but has a hash of zero)
hashParent = uint256();
} else {
hashParent = ParseHashV(params[1], "parent object hash, parameter 2");
}
// GET THE PARAMETERS FROM USER
std::string strRevision = params[2].get_str();
std::string strTime = params[3].get_str();
int nRevision = boost::lexical_cast<int>(strRevision);
int nTime = boost::lexical_cast<int>(strTime);
std::string strData = params[4].get_str();
CGovernanceObject govobj(hashParent, nRevision, nTime, txidFee, strData);
DBG( cout << "gobject: submit "
<< " strData = " << govobj.GetDataAsString()
<< ", hash = " << govobj.GetHash().GetHex()
<< ", txidFee = " << txidFee.GetHex()
<< endl; );
// Attempt to sign triggers if we are a MN
if((govobj.GetObjectType() == GOVERNANCE_OBJECT_TRIGGER) ||
(govobj.GetObjectType() == GOVERNANCE_OBJECT_WATCHDOG)) {
if(fMnFound) {
govobj.SetMasternodeInfo(mn.vin);
govobj.Sign(activeMasternode.keyMasternode, activeMasternode.pubKeyMasternode);
}
else {
LogPrintf("gobject(submit) -- Object submission rejected because node is not a masternode\n");
throw JSONRPCError(RPC_INVALID_PARAMETER, "Only valid masternodes can submit this type of object");
}
}
else {
if(params.size() != 6) {
LogPrintf("gobject(submit) -- Object submission rejected because fee tx not provided\n");
throw JSONRPCError(RPC_INVALID_PARAMETER, "The fee-txid parameter must be included to submit this type of object");
}
}
std::string strHash = govobj.GetHash().ToString();
std::string strError = "";
if(!govobj.IsValidLocally(strError, true)) {
LogPrintf("gobject(submit) -- Object submission rejected because object is not valid - hash = %s, strError = %s\n", strHash, strError);
throw JSONRPCError(RPC_INTERNAL_ERROR, "Governance object is not valid - " + strHash + " - " + strError);
}
// RELAY THIS OBJECT
// Reject if rate check fails but don't update buffer
if(!governance.MasternodeRateCheck(govobj)) {
LogPrintf("gobject(submit) -- Object submission rejected because of rate check failure - hash = %s\n", strHash);
throw JSONRPCError(RPC_INVALID_PARAMETER, "Object creation rate limit exceeded");
}
// This check should always pass, update buffer
if(!governance.MasternodeRateCheck(govobj, UPDATE_TRUE)) {
LogPrintf("gobject(submit) -- Object submission rejected because of rate check failure (buffer updated) - hash = %s\n", strHash);
throw JSONRPCError(RPC_INVALID_PARAMETER, "Object creation rate limit exceeded");
}
governance.AddSeenGovernanceObject(govobj.GetHash(), SEEN_OBJECT_IS_VALID);
govobj.Relay();
LogPrintf("gobject(submit) -- Adding locally created governance object - %s\n", strHash);
bool fAddToSeen = true;
governance.AddGovernanceObject(govobj, fAddToSeen);
return govobj.GetHash().ToString();
}
if(strCommand == "vote-conf")
{
if(params.size() != 4)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject vote-conf <governance-hash> [funding|valid|delete] [yes|no|abstain]'");
uint256 hash;
std::string strVote;
hash = ParseHashV(params[1], "Object hash");
std::string strVoteSignal = params[2].get_str();
std::string strVoteOutcome = params[3].get_str();
vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
if(eVoteSignal == VOTE_SIGNAL_NONE) {
throw JSONRPCError(RPC_INVALID_PARAMETER,
"Invalid vote signal. Please using one of the following: "
"(funding|valid|delete|endorsed) OR `custom sentinel code` ");
}
vote_outcome_enum_t eVoteOutcome = CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome);
if(eVoteOutcome == VOTE_OUTCOME_NONE) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
}
int nSuccessful = 0;
int nFailed = 0;
UniValue resultsObj(UniValue::VOBJ);
std::vector<unsigned char> vchMasterNodeSignature;
std::string strMasterNodeSignMessage;
UniValue statusObj(UniValue::VOBJ);
UniValue returnObj(UniValue::VOBJ);
CMasternode mn;
bool fMnFound = mnodeman.Get(activeMasternode.vin, mn);
if(!fMnFound) {
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Can't find masternode by collateral output"));
resultsObj.push_back(Pair("biblepay.conf", statusObj));
returnObj.push_back(Pair("overall", strprintf("Voted successfully %d time(s) and failed %d time(s).", nSuccessful, nFailed)));
returnObj.push_back(Pair("detail", resultsObj));
return returnObj;
}
CGovernanceVote vote(mn.vin, hash, eVoteSignal, eVoteOutcome);
if(!vote.Sign(activeMasternode.keyMasternode, activeMasternode.pubKeyMasternode)) {
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Failure to sign."));
resultsObj.push_back(Pair("biblepay.conf", statusObj));
returnObj.push_back(Pair("overall", strprintf("Voted successfully %d time(s) and failed %d time(s).", nSuccessful, nFailed)));
returnObj.push_back(Pair("detail", resultsObj));
return returnObj;
}
CGovernanceException exception;
if(governance.ProcessVoteAndRelay(vote, exception)) {
nSuccessful++;
statusObj.push_back(Pair("result", "success"));
}
else {
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", exception.GetMessage()));
}
resultsObj.push_back(Pair("biblepay.conf", statusObj));
returnObj.push_back(Pair("overall", strprintf("Voted successfully %d time(s) and failed %d time(s).", nSuccessful, nFailed)));
returnObj.push_back(Pair("detail", resultsObj));
return returnObj;
}
if(strCommand == "vote-many")
{
if(params.size() != 4)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject vote-many <governance-hash> [funding|valid|delete] [yes|no|abstain]'");
uint256 hash;
std::string strVote;
hash = ParseHashV(params[1], "Object hash");
std::string strVoteSignal = params[2].get_str();
std::string strVoteOutcome = params[3].get_str();
vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
if(eVoteSignal == VOTE_SIGNAL_NONE) {
throw JSONRPCError(RPC_INVALID_PARAMETER,
"Invalid vote signal. Please using one of the following: "
"(funding|valid|delete|endorsed) OR `custom sentinel code` ");
}
vote_outcome_enum_t eVoteOutcome = CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome);
if(eVoteOutcome == VOTE_OUTCOME_NONE) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
}
int nSuccessful = 0;
int nFailed = 0;
std::vector<CMasternodeConfig::CMasternodeEntry> mnEntries;
mnEntries = masternodeConfig.getEntries();
UniValue resultsObj(UniValue::VOBJ);
BOOST_FOREACH(CMasternodeConfig::CMasternodeEntry mne, masternodeConfig.getEntries()) {
std::string strError;
std::vector<unsigned char> vchMasterNodeSignature;
std::string strMasterNodeSignMessage;
CPubKey pubKeyCollateralAddress;
CKey keyCollateralAddress;
CPubKey pubKeyMasternode;
CKey keyMasternode;
UniValue statusObj(UniValue::VOBJ);
if(!darkSendSigner.GetKeysFromSecret(mne.getPrivKey(), keyMasternode, pubKeyMasternode)){
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Masternode signing error, could not set key correctly"));
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
continue;
}
uint256 nTxHash;
nTxHash.SetHex(mne.getTxHash());
int nOutputIndex = 0;
if(!ParseInt32(mne.getOutputIndex(), &nOutputIndex)) {
continue;
}
CTxIn vin(COutPoint(nTxHash, nOutputIndex));
CMasternode mn;
bool fMnFound = mnodeman.Get(vin, mn);
if(!fMnFound) {
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Can't find masternode by collateral output"));
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
continue;
}
CGovernanceVote vote(mn.vin, hash, eVoteSignal, eVoteOutcome);
if(!vote.Sign(keyMasternode, pubKeyMasternode)){
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Failure to sign."));
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
continue;
}
CGovernanceException exception;
if(governance.ProcessVoteAndRelay(vote, exception)) {
nSuccessful++;
statusObj.push_back(Pair("result", "success"));
}
else {
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", exception.GetMessage()));
}
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
}
UniValue returnObj(UniValue::VOBJ);
returnObj.push_back(Pair("overall", strprintf("Voted successfully %d time(s) and failed %d time(s).", nSuccessful, nFailed)));
returnObj.push_back(Pair("detail", resultsObj));
return returnObj;
}
// MASTERNODES CAN VOTE ON GOVERNANCE OBJECTS ON THE NETWORK FOR VARIOUS SIGNALS AND OUTCOMES
if(strCommand == "vote-alias")
{
if(params.size() != 5)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject vote-alias <governance-hash> [funding|valid|delete] [yes|no|abstain] <alias-name>'");
uint256 hash;
std::string strVote;
// COLLECT NEEDED PARAMETRS FROM USER
hash = ParseHashV(params[1], "Object hash");
std::string strVoteSignal = params[2].get_str();
std::string strVoteOutcome = params[3].get_str();
std::string strAlias = params[4].get_str();
// CONVERT NAMED SIGNAL/ACTION AND CONVERT
vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
if(eVoteSignal == VOTE_SIGNAL_NONE) {
throw JSONRPCError(RPC_INVALID_PARAMETER,
"Invalid vote signal. Please using one of the following: "
"(funding|valid|delete|endorsed) OR `custom sentinel code` ");
}
vote_outcome_enum_t eVoteOutcome = CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome);
if(eVoteOutcome == VOTE_OUTCOME_NONE) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
}
// EXECUTE VOTE FOR EACH MASTERNODE, COUNT SUCCESSES VS FAILURES
int nSuccessful = 0;
int nFailed = 0;
std::vector<CMasternodeConfig::CMasternodeEntry> mnEntries;
mnEntries = masternodeConfig.getEntries();
UniValue resultsObj(UniValue::VOBJ);
BOOST_FOREACH(CMasternodeConfig::CMasternodeEntry mne, masternodeConfig.getEntries())
{
// IF WE HAVE A SPECIFIC NODE REQUESTED TO VOTE, DO THAT
if(strAlias != mne.getAlias()) continue;
// INIT OUR NEEDED VARIABLES TO EXECUTE THE VOTE
std::string strError;
std::vector<unsigned char> vchMasterNodeSignature;
std::string strMasterNodeSignMessage;
CPubKey pubKeyCollateralAddress;
CKey keyCollateralAddress;
CPubKey pubKeyMasternode;
CKey keyMasternode;
// SETUP THE SIGNING KEY FROM MASTERNODE.CONF ENTRY
UniValue statusObj(UniValue::VOBJ);
if(!darkSendSigner.GetKeysFromSecret(mne.getPrivKey(), keyMasternode, pubKeyMasternode)) {
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", strprintf("Invalid masternode key %s.", mne.getPrivKey())));
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
continue;
}
// SEARCH FOR THIS MASTERNODE ON THE NETWORK, THE NODE MUST BE ACTIVE TO VOTE
uint256 nTxHash;
nTxHash.SetHex(mne.getTxHash());
int nOutputIndex = 0;
if(!ParseInt32(mne.getOutputIndex(), &nOutputIndex)) {
continue;
}
CTxIn vin(COutPoint(nTxHash, nOutputIndex));
CMasternode mn;
bool fMnFound = mnodeman.Get(vin, mn);
if(!fMnFound) {
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Masternode must be publically available on network to vote. Masternode not found."));
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
continue;
}
// CREATE NEW GOVERNANCE OBJECT VOTE WITH OUTCOME/SIGNAL
CGovernanceVote vote(vin, hash, eVoteSignal, eVoteOutcome);
if(!vote.Sign(keyMasternode, pubKeyMasternode)) {
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Failure to sign."));
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
continue;
}
// UPDATE LOCAL DATABASE WITH NEW OBJECT SETTINGS
CGovernanceException exception;
if(governance.ProcessVoteAndRelay(vote, exception)) {
nSuccessful++;
statusObj.push_back(Pair("result", "success"));
}
else {
nFailed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", exception.GetMessage()));
}
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
}
// REPORT STATS TO THE USER
UniValue returnObj(UniValue::VOBJ);
returnObj.push_back(Pair("overall", strprintf("Voted successfully %d time(s) and failed %d time(s).", nSuccessful, nFailed)));
returnObj.push_back(Pair("detail", resultsObj));
return returnObj;
}
// USERS CAN QUERY THE SYSTEM FOR A LIST OF VARIOUS GOVERNANCE ITEMS
if(strCommand == "list" || strCommand == "diff")
{
if (params.size() > 3)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject [list|diff] [valid] [type]'");
// GET MAIN PARAMETER FOR THIS MODE, VALID OR ALL?
std::string strShow = "valid";
if (params.size() >= 2) strShow = params[1].get_str();
if (strShow != "valid" && strShow != "all")
return "Invalid mode, should be 'valid' or 'all'";
std::string strType = "all";
if (params.size() == 3) strType = params[2].get_str();
if (strType != "proposals" && strType != "triggers" && strType != "watchdogs" && strType != "all")
return "Invalid type, should be 'proposals', 'triggers', 'watchdogs' or 'all'";
// GET STARTING TIME TO QUERY SYSTEM WITH
int nStartTime = 0; //list
if(strCommand == "diff") nStartTime = governance.GetLastDiffTime();
// SETUP BLOCK INDEX VARIABLE / RESULTS VARIABLE
UniValue objResult(UniValue::VOBJ);
// GET MATCHING GOVERNANCE OBJECTS
LOCK2(cs_main, governance.cs);
std::vector<CGovernanceObject*> objs = governance.GetAllNewerThan(nStartTime);
governance.UpdateLastDiffTime(GetTime());
// CREATE RESULTS FOR USER
BOOST_FOREACH(CGovernanceObject* pGovObj, objs)
{
if(strShow == "valid" && !pGovObj->IsSetCachedValid()) continue;
if(strType == "proposals" && pGovObj->GetObjectType() != GOVERNANCE_OBJECT_PROPOSAL) continue;
if(strType == "triggers" && pGovObj->GetObjectType() != GOVERNANCE_OBJECT_TRIGGER) continue;
if(strType == "watchdogs" && pGovObj->GetObjectType() != GOVERNANCE_OBJECT_WATCHDOG) continue;
UniValue bObj(UniValue::VOBJ);
bObj.push_back(Pair("DataHex", pGovObj->GetDataAsHex()));
bObj.push_back(Pair("DataString", pGovObj->GetDataAsString()));
bObj.push_back(Pair("Hash", pGovObj->GetHash().ToString()));
bObj.push_back(Pair("CollateralHash", pGovObj->GetCollateralHash().ToString()));
bObj.push_back(Pair("ObjectType", pGovObj->GetObjectType()));
bObj.push_back(Pair("CreationTime", pGovObj->GetCreationTime()));
const CTxIn& masternodeVin = pGovObj->GetMasternodeVin();
if(masternodeVin != CTxIn()) {
bObj.push_back(Pair("SigningMasternode", masternodeVin.prevout.ToStringShort()));
}
// REPORT STATUS FOR FUNDING VOTES SPECIFICALLY
bObj.push_back(Pair("AbsoluteYesCount", pGovObj->GetAbsoluteYesCount(VOTE_SIGNAL_FUNDING)));
bObj.push_back(Pair("YesCount", pGovObj->GetYesCount(VOTE_SIGNAL_FUNDING)));
bObj.push_back(Pair("NoCount", pGovObj->GetNoCount(VOTE_SIGNAL_FUNDING)));
bObj.push_back(Pair("AbstainCount", pGovObj->GetAbstainCount(VOTE_SIGNAL_FUNDING)));
// REPORT VALIDITY AND CACHING FLAGS FOR VARIOUS SETTINGS
std::string strError = "";
bObj.push_back(Pair("fBlockchainValidity", pGovObj->IsValidLocally(strError, false)));
bObj.push_back(Pair("IsValidReason", strError.c_str()));
bObj.push_back(Pair("fCachedValid", pGovObj->IsSetCachedValid()));
bObj.push_back(Pair("fCachedFunding", pGovObj->IsSetCachedFunding()));
bObj.push_back(Pair("fCachedDelete", pGovObj->IsSetCachedDelete()));
bObj.push_back(Pair("fCachedEndorsed", pGovObj->IsSetCachedEndorsed()));
objResult.push_back(Pair(pGovObj->GetHash().ToString(), bObj));
}
return objResult;
}
// GET SPECIFIC GOVERNANCE ENTRY
if(strCommand == "get")
{
if (params.size() != 2)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Correct usage is 'gobject get <governance-hash>'");
// COLLECT VARIABLES FROM OUR USER
uint256 hash = ParseHashV(params[1], "GovObj hash");
LOCK2(cs_main, governance.cs);
// FIND THE GOVERNANCE OBJECT THE USER IS LOOKING FOR
CGovernanceObject* pGovObj = governance.FindGovernanceObject(hash);
if(pGovObj == NULL)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown governance object");
// REPORT BASIC OBJECT STATS
UniValue objResult(UniValue::VOBJ);
objResult.push_back(Pair("DataHex", pGovObj->GetDataAsHex()));
objResult.push_back(Pair("DataString", pGovObj->GetDataAsString()));
objResult.push_back(Pair("Hash", pGovObj->GetHash().ToString()));
objResult.push_back(Pair("CollateralHash", pGovObj->GetCollateralHash().ToString()));
objResult.push_back(Pair("ObjectType", pGovObj->GetObjectType()));
objResult.push_back(Pair("CreationTime", pGovObj->GetCreationTime()));
const CTxIn& masternodeVin = pGovObj->GetMasternodeVin();
if(masternodeVin != CTxIn()) {
objResult.push_back(Pair("SigningMasternode", masternodeVin.prevout.ToStringShort()));
}
// SHOW (MUCH MORE) INFORMATION ABOUT VOTES FOR GOVERNANCE OBJECT (THAN LIST/DIFF ABOVE)
// -- FUNDING VOTING RESULTS
UniValue objFundingResult(UniValue::VOBJ);
objFundingResult.push_back(Pair("AbsoluteYesCount", pGovObj->GetAbsoluteYesCount(VOTE_SIGNAL_FUNDING)));
objFundingResult.push_back(Pair("YesCount", pGovObj->GetYesCount(VOTE_SIGNAL_FUNDING)));
objFundingResult.push_back(Pair("NoCount", pGovObj->GetNoCount(VOTE_SIGNAL_FUNDING)));
objFundingResult.push_back(Pair("AbstainCount", pGovObj->GetAbstainCount(VOTE_SIGNAL_FUNDING)));
objResult.push_back(Pair("FundingResult", objFundingResult));
// -- VALIDITY VOTING RESULTS
UniValue objValid(UniValue::VOBJ);
objValid.push_back(Pair("AbsoluteYesCount", pGovObj->GetAbsoluteYesCount(VOTE_SIGNAL_VALID)));
objValid.push_back(Pair("YesCount", pGovObj->GetYesCount(VOTE_SIGNAL_VALID)));
objValid.push_back(Pair("NoCount", pGovObj->GetNoCount(VOTE_SIGNAL_VALID)));
objValid.push_back(Pair("AbstainCount", pGovObj->GetAbstainCount(VOTE_SIGNAL_VALID)));
objResult.push_back(Pair("ValidResult", objValid));
// -- DELETION CRITERION VOTING RESULTS
UniValue objDelete(UniValue::VOBJ);
objDelete.push_back(Pair("AbsoluteYesCount", pGovObj->GetAbsoluteYesCount(VOTE_SIGNAL_DELETE)));
objDelete.push_back(Pair("YesCount", pGovObj->GetYesCount(VOTE_SIGNAL_DELETE)));
objDelete.push_back(Pair("NoCount", pGovObj->GetNoCount(VOTE_SIGNAL_DELETE)));
objDelete.push_back(Pair("AbstainCount", pGovObj->GetAbstainCount(VOTE_SIGNAL_DELETE)));
objResult.push_back(Pair("DeleteResult", objDelete));
// -- ENDORSED VIA MASTERNODE-ELECTED BOARD
UniValue objEndorsed(UniValue::VOBJ);
objEndorsed.push_back(Pair("AbsoluteYesCount", pGovObj->GetAbsoluteYesCount(VOTE_SIGNAL_ENDORSED)));
objEndorsed.push_back(Pair("YesCount", pGovObj->GetYesCount(VOTE_SIGNAL_ENDORSED)));
objEndorsed.push_back(Pair("NoCount", pGovObj->GetNoCount(VOTE_SIGNAL_ENDORSED)));
objEndorsed.push_back(Pair("AbstainCount", pGovObj->GetAbstainCount(VOTE_SIGNAL_ENDORSED)));
objResult.push_back(Pair("EndorsedResult", objEndorsed));
// --
std::string strError = "";
objResult.push_back(Pair("fLocalValidity", pGovObj->IsValidLocally(strError, false)));
objResult.push_back(Pair("IsValidReason", strError.c_str()));
objResult.push_back(Pair("fCachedValid", pGovObj->IsSetCachedValid()));
objResult.push_back(Pair("fCachedFunding", pGovObj->IsSetCachedFunding()));
objResult.push_back(Pair("fCachedDelete", pGovObj->IsSetCachedDelete()));
objResult.push_back(Pair("fCachedEndorsed", pGovObj->IsSetCachedEndorsed()));
return objResult;
}
// GETVOTES FOR SPECIFIC GOVERNANCE OBJECT
if(strCommand == "getvotes")
{
if (params.size() != 2)
throw std::runtime_error(
"Correct usage is 'gobject getvotes <governance-hash>'"
);
// COLLECT PARAMETERS FROM USER
uint256 hash = ParseHashV(params[1], "Governance hash");
// FIND OBJECT USER IS LOOKING FOR
LOCK(governance.cs);
CGovernanceObject* pGovObj = governance.FindGovernanceObject(hash);
if(pGovObj == NULL) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown governance-hash");
}
// REPORT RESULTS TO USER
UniValue bResult(UniValue::VOBJ);
// GET MATCHING VOTES BY HASH, THEN SHOW USERS VOTE INFORMATION
std::vector<CGovernanceVote> vecVotes = governance.GetMatchingVotes(hash);
BOOST_FOREACH(CGovernanceVote vote, vecVotes) {
bResult.push_back(Pair(vote.GetHash().ToString(), vote.ToString()));
}
return bResult;
}
// GETVOTES FOR SPECIFIC GOVERNANCE OBJECT
if(strCommand == "getcurrentvotes")
{
if (params.size() != 2 && params.size() != 4)
throw std::runtime_error(
"Correct usage is 'gobject getcurrentvotes <governance-hash> [txid vout_index]'"
);
// COLLECT PARAMETERS FROM USER
uint256 hash = ParseHashV(params[1], "Governance hash");
CTxIn mnCollateralOutpoint;
if (params.size() == 4) {
uint256 txid = ParseHashV(params[2], "Masternode Collateral hash");
std::string strVout = params[3].get_str();
uint32_t vout = boost::lexical_cast<uint32_t>(strVout);
mnCollateralOutpoint = CTxIn(txid, vout);
}
// FIND OBJECT USER IS LOOKING FOR
LOCK(governance.cs);
CGovernanceObject* pGovObj = governance.FindGovernanceObject(hash);
if(pGovObj == NULL) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown governance-hash");
}
// REPORT RESULTS TO USER
UniValue bResult(UniValue::VOBJ);
// GET MATCHING VOTES BY HASH, THEN SHOW USERS VOTE INFORMATION
std::vector<CGovernanceVote> vecVotes = governance.GetCurrentVotes(hash, mnCollateralOutpoint);
BOOST_FOREACH(CGovernanceVote vote, vecVotes) {
bResult.push_back(Pair(vote.GetHash().ToString(), vote.ToString()));
}
return bResult;
}
return NullUniValue;
}
UniValue voteraw(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 7)
throw std::runtime_error(
"voteraw <masternode-tx-hash> <masternode-tx-index> <governance-hash> <vote-signal> [yes|no|abstain] <time> <vote-sig>\n"
"Compile and relay a governance vote with provided external signature instead of signing vote internally\n"
);
uint256 hashMnTx = ParseHashV(params[0], "mn tx hash");
int nMnTxIndex = params[1].get_int();
CTxIn vin = CTxIn(hashMnTx, nMnTxIndex);
uint256 hashGovObj = ParseHashV(params[2], "Governance hash");
std::string strVoteSignal = params[3].get_str();
std::string strVoteOutcome = params[4].get_str();
vote_signal_enum_t eVoteSignal = CGovernanceVoting::ConvertVoteSignal(strVoteSignal);
if(eVoteSignal == VOTE_SIGNAL_NONE) {
throw JSONRPCError(RPC_INVALID_PARAMETER,
"Invalid vote signal. Please using one of the following: "
"(funding|valid|delete|endorsed) OR `custom sentinel code` ");
}
vote_outcome_enum_t eVoteOutcome = CGovernanceVoting::ConvertVoteOutcome(strVoteOutcome);
if(eVoteOutcome == VOTE_OUTCOME_NONE) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'");
}
int64_t nTime = params[5].get_int64();
std::string strSig = params[6].get_str();
bool fInvalid = false;
std::vector<unsigned char> vchSig = DecodeBase64(strSig.c_str(), &fInvalid);
if (fInvalid) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Malformed base64 encoding");
}
CMasternode mn;
bool fMnFound = mnodeman.Get(vin, mn);
if(!fMnFound) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Failure to find masternode in list : " + vin.prevout.ToStringShort());
}
CGovernanceVote vote(vin, hashGovObj, eVoteSignal, eVoteOutcome);
vote.SetTime(nTime);
vote.SetSignature(vchSig);
if(!vote.IsValid(true)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Failure to verify vote.");
}
CGovernanceException exception;
if(governance.ProcessVoteAndRelay(vote, exception)) {
return "Voted successfully";
}
else {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Error voting : " + exception.GetMessage());
}
}
UniValue getgovernanceinfo(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 0) {
throw std::runtime_error(
"getgovernanceinfo\n"
"Returns an object containing governance parameters.\n"
"\nResult:\n"
"{\n"
" \"governanceminquorum\": xxxxx, (numeric) the absolute minimum number of votes needed to trigger a governance action\n"
" \"masternodewatchdogmaxseconds\": xxxxx, (numeric) sentinel watchdog expiration time in seconds\n"
" \"proposalfee\": xxx.xx, (numeric) the collateral transaction fee which must be paid to create a proposal in " + CURRENCY_UNIT + "\n"
" \"superblockcycle\": xxxxx, (numeric) the number of blocks between superblocks\n"
" \"lastsuperblock\": xxxxx, (numeric) the block number of the last superblock\n"
" \"nextsuperblock\": xxxxx, (numeric) the block number of the next superblock\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("getgovernanceinfo", "")
+ HelpExampleRpc("getgovernanceinfo", "")
);
}
// Compute last/next superblock
int nLastSuperblock, nNextSuperblock;
// Get current block height
int nBlockHeight = 0;
{
LOCK(cs_main);
nBlockHeight = (int)chainActive.Height();
}
// Get chain parameters
int nSuperblockStartBlock = Params().GetConsensus().nSuperblockStartBlock;
int nSuperblockCycle = Params().GetConsensus().nSuperblockCycle;
// Get first superblock
int nFirstSuperblockOffset = (nSuperblockCycle - nSuperblockStartBlock % nSuperblockCycle) % nSuperblockCycle;
int nFirstSuperblock = nSuperblockStartBlock + nFirstSuperblockOffset;
if(nBlockHeight < nFirstSuperblock){
nLastSuperblock = 0;
nNextSuperblock = nFirstSuperblock;
} else {
nLastSuperblock = nBlockHeight - nBlockHeight % nSuperblockCycle;
nNextSuperblock = nLastSuperblock + nSuperblockCycle;
}
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("governanceminquorum", Params().GetConsensus().nGovernanceMinQuorum));
obj.push_back(Pair("masternodewatchdogmaxseconds", MASTERNODE_WATCHDOG_MAX_SECONDS));
CAmount nFee = GetAdjustedTime() > 1533081700 ? GOVERNANCE_PROPOSAL_FEE_TX2 : GOVERNANCE_PROPOSAL_FEE_TX;
obj.push_back(Pair("proposalfee", ValueFromAmount(nFee)));
obj.push_back(Pair("superblockcycle", Params().GetConsensus().nSuperblockCycle));
obj.push_back(Pair("lastsuperblock", nLastSuperblock));
obj.push_back(Pair("nextsuperblock", nNextSuperblock));
double nBudget = CSuperblock::GetPaymentsLimit(nNextSuperblock) / COIN;
obj.push_back(Pair("nextbudget", nBudget));
return obj;
}
UniValue getsuperblockbudget(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 1) {
throw std::runtime_error(
"getsuperblockbudget index\n"
"\nReturns the absolute maximum sum of superblock payments allowed.\n"
"\nArguments:\n"
"1. index (numeric, required) The block index\n"
"\nResult:\n"
"n (numeric) The absolute maximum sum of superblock payments allowed, in " + CURRENCY_UNIT + "\n"
"\nExamples:\n"
+ HelpExampleCli("getsuperblockbudget", "1000")
+ HelpExampleRpc("getsuperblockbudget", "1000")
);
}
int nBlockHeight = params[0].get_int();
if (nBlockHeight < 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
}
CAmount nBudget = CSuperblock::GetPaymentsLimit(nBlockHeight);
std::string strBudget = FormatMoney(nBudget);
return strBudget;
}