-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpcmasternode-budget.cpp
1055 lines (857 loc) · 44.4 KB
/
rpcmasternode-budget.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
1000
// Copyright (c) 2014-2015 The Dash Developers
// Copyright (c) 2015-2017 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 "activemasternode.h"
#include "db.h"
#include "init.h"
#include "main.h"
#include "masternode-budget.h"
#include "masternode-payments.h"
#include "masternodeconfig.h"
#include "masternodeman.h"
#include "masternode-helpers.h"
#include "rpcserver.h"
#include "utilmoneystr.h"
#include <univalue.h>
#include <fstream>
using namespace std;
void budgetToJSON(CBudgetProposal* pbudgetProposal, UniValue& bObj)
{
CTxDestination address1;
ExtractDestination(pbudgetProposal->GetPayee(), address1);
CBitcoinAddress address2(address1);
bObj.push_back(Pair("Name", pbudgetProposal->GetName()));
bObj.push_back(Pair("URL", pbudgetProposal->GetURL()));
bObj.push_back(Pair("Hash", pbudgetProposal->GetHash().ToString()));
bObj.push_back(Pair("FeeHash", pbudgetProposal->nFeeTXHash.ToString()));
bObj.push_back(Pair("BlockStart", (int64_t)pbudgetProposal->GetBlockStart()));
bObj.push_back(Pair("BlockEnd", (int64_t)pbudgetProposal->GetBlockEnd()));
bObj.push_back(Pair("TotalPaymentCount", (int64_t)pbudgetProposal->GetTotalPaymentCount()));
bObj.push_back(Pair("RemainingPaymentCount", (int64_t)pbudgetProposal->GetRemainingPaymentCount()));
bObj.push_back(Pair("PaymentAddress", address2.ToString()));
bObj.push_back(Pair("Ratio", pbudgetProposal->GetRatio()));
bObj.push_back(Pair("Yeas", (int64_t)pbudgetProposal->GetYeas()));
bObj.push_back(Pair("Nays", (int64_t)pbudgetProposal->GetNays()));
bObj.push_back(Pair("Abstains", (int64_t)pbudgetProposal->GetAbstains()));
bObj.push_back(Pair("TotalPayment", ValueFromAmount(pbudgetProposal->GetAmount() * pbudgetProposal->GetTotalPaymentCount())));
bObj.push_back(Pair("MonthlyPayment", ValueFromAmount(pbudgetProposal->GetAmount())));
bObj.push_back(Pair("IsEstablished", pbudgetProposal->IsEstablished()));
std::string strError = "";
bObj.push_back(Pair("IsValid", pbudgetProposal->IsValid(strError)));
bObj.push_back(Pair("IsValidReason", strError.c_str()));
bObj.push_back(Pair("fValid", pbudgetProposal->fValid));
}
// This command is retained for backwards compatibility, but is deprecated.
// Future removal of this command is planned to keep things clean.
UniValue mnbudget(const UniValue& params, bool fHelp)
{
string strCommand;
if (params.size() >= 1)
strCommand = params[0].get_str();
if (fHelp ||
(strCommand != "vote-alias" && strCommand != "vote-many" && strCommand != "prepare" && strCommand != "submit" && strCommand != "vote" && strCommand != "getvotes" && strCommand != "getinfo" && strCommand != "show" && strCommand != "projection" && strCommand != "check" && strCommand != "nextblock"))
throw runtime_error(
"mnbudget \"command\"... ( \"passphrase\" )\n"
"\nVote or show current budgets\n"
"This command is deprecated, please see individual command documentation for future reference\n\n"
"\nAvailable commands:\n"
" prepare - Prepare proposal for network by signing and creating tx\n"
" submit - Submit proposal for network\n"
" vote-many - Vote on a Vulcoin initiative\n"
" vote-alias - Vote on a Vulcoin initiative\n"
" vote - Vote on a Vulcoin initiative/budget\n"
" getvotes - Show current masternode budgets\n"
" getinfo - Show current masternode budgets\n"
" show - Show all budgets\n"
" projection - Show the projection of which proposals will be paid the next cycle\n"
" check - Scan proposals and remove invalid\n"
" nextblock - Get next superblock for budget system\n");
if (strCommand == "nextblock") {
UniValue newParams(UniValue::VARR);
// forward params but skip command
for (unsigned int i = 1; i < params.size(); i++) {
newParams.push_back(params[i]);
}
return getnextsuperblock(newParams, fHelp);
}
if (strCommand == "prepare") {
UniValue newParams(UniValue::VARR);
// forward params but skip command
for (unsigned int i = 1; i < params.size(); i++) {
newParams.push_back(params[i]);
}
return preparebudget(newParams, fHelp);
}
if (strCommand == "submit") {
UniValue newParams(UniValue::VARR);
// forward params but skip command
for (unsigned int i = 1; i < params.size(); i++) {
newParams.push_back(params[i]);
}
return submitbudget(newParams, fHelp);
}
if (strCommand == "vote" || strCommand == "vote-many" || strCommand == "vote-alias") {
if (strCommand == "vote-alias")
throw runtime_error(
"vote-alias is not supported with this command\n"
"Please use mnbudgetvote instead.\n"
);
return mnbudgetvote(params, fHelp);
}
if (strCommand == "projection") {
UniValue newParams(UniValue::VARR);
// forward params but skip command
for (unsigned int i = 1; i < params.size(); i++) {
newParams.push_back(params[i]);
}
return getbudgetprojection(newParams, fHelp);
}
if (strCommand == "show" || strCommand == "getinfo") {
UniValue newParams(UniValue::VARR);
// forward params but skip command
for (unsigned int i = 1; i < params.size(); i++) {
newParams.push_back(params[i]);
}
return getbudgetinfo(newParams, fHelp);
}
if (strCommand == "getvotes") {
UniValue newParams(UniValue::VARR);
// forward params but skip command
for (unsigned int i = 1; i < params.size(); i++) {
newParams.push_back(params[i]);
}
return getbudgetvotes(newParams, fHelp);
}
if (strCommand == "check") {
UniValue newParams(UniValue::VARR);
// forward params but skip command
for (unsigned int i = 1; i < params.size(); i++) {
newParams.push_back(params[i]);
}
return checkbudgets(newParams, fHelp);
}
return NullUniValue;
}
UniValue preparebudget(const UniValue& params, bool fHelp)
{
int nBlockMin = 0;
CBlockIndex* pindexPrev = chainActive.Tip();
if (fHelp || params.size() != 6)
throw runtime_error(
"preparebudget \"proposal-name\" \"url\" payment-count block-start \"vlc-address\" monthy-payment\n"
"\nPrepare proposal for network by signing and creating tx\n"
"\nArguments:\n"
"1. \"proposal-name\": (string, required) Desired proposal name (20 character limit)\n"
"2. \"url\": (string, required) URL of proposal details (64 character limit)\n"
"3. payment-count: (numeric, required) Total number of monthly payments\n"
"4. block-start: (numeric, required) Starting super block height\n"
"5. \"vlc-address\": (string, required) VLC address to send payments to\n"
"6. monthly-payment: (numeric, required) Monthly payment amount\n"
"\nResult:\n"
"\"xxxx\" (string) proposal fee hash (if successful) or error message (if failed)\n"
"\nExamples:\n" +
HelpExampleCli("preparebudget", "\"test-proposal\" \"https://vlc.org/t/test-proposal\" 2 820800 \"D9oc6C3dttUbv8zd7zGNq1qKBGf4ZQ1XEE\" 500") +
HelpExampleRpc("preparebudget", "\"test-proposal\" \"https://vlc.org/t/test-proposal\" 2 820800 \"D9oc6C3dttUbv8zd7zGNq1qKBGf4ZQ1XEE\" 500"));
LOCK2(cs_main, pwalletMain->cs_wallet);
EnsureWalletIsUnlocked();
std::string strProposalName = SanitizeString(params[0].get_str());
if (strProposalName.size() > 20)
throw runtime_error("Invalid proposal name, limit of 20 characters.");
std::string strURL = SanitizeString(params[1].get_str());
if (strURL.size() > 64)
throw runtime_error("Invalid url, limit of 64 characters.");
int nPaymentCount = params[2].get_int();
if (nPaymentCount < 1)
throw runtime_error("Invalid payment count, must be more than zero.");
// Start must be in the next budget cycle
if (pindexPrev != nullptr) nBlockMin = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
int nBlockStart = params[3].get_int();
if (nBlockStart % GetBudgetPaymentCycleBlocks() != 0) {
int nNext = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
throw runtime_error(strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nNext));
}
int nBlockEnd = nBlockStart + GetBudgetPaymentCycleBlocks() * nPaymentCount; // End must be AFTER current cycle
if (nBlockStart < nBlockMin)
throw runtime_error("Invalid block start, must be more than current height.");
if (nBlockEnd < pindexPrev->nHeight)
throw runtime_error("Invalid ending block, starting block + (payment_cycle*payments) must be more than current height.");
CBitcoinAddress address(params[4].get_str());
if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Vulcoin address");
// Parse VLC address
CScript scriptPubKey = GetScriptForDestination(address.Get());
CAmount nAmount = AmountFromValue(params[5]);
//*************************************************************************
// create transaction 15 minutes into the future, to allow for confirmation time
CBudgetProposalBroadcast budgetProposalBroadcast(strProposalName, strURL, nPaymentCount, scriptPubKey, nAmount, nBlockStart, 0);
std::string strError = "";
if (!budgetProposalBroadcast.IsValid(strError, false))
throw runtime_error("Proposal is not valid - " + budgetProposalBroadcast.GetHash().ToString() + " - " + strError);
bool useIX = false; //true;
// if (params.size() > 7) {
// if(params[7].get_str() != "false" && params[7].get_str() != "true")
// return "Invalid use_ix, must be true or false";
// useIX = params[7].get_str() == "true" ? true : false;
// }
CWalletTx wtx;
if (!pwalletMain->GetBudgetSystemCollateralTX(wtx, budgetProposalBroadcast.GetHash(), useIX)) {
throw runtime_error("Error making collateral transaction for proposal. Please check your wallet balance.");
}
// make our change address
CReserveKey reservekey(pwalletMain);
//send the tx to the network
pwalletMain->CommitTransaction(wtx, reservekey, useIX ? "ix" : "tx");
return wtx.GetHash().ToString();
}
UniValue submitbudget(const UniValue& params, bool fHelp)
{
int nBlockMin = 0;
CBlockIndex* pindexPrev = chainActive.Tip();
if (fHelp || params.size() != 7)
throw runtime_error(
"submitbudget \"proposal-name\" \"url\" payment-count block-start \"vlc-address\" monthy-payment \"fee-tx\"\n"
"\nSubmit proposal to the network\n"
"\nArguments:\n"
"1. \"proposal-name\": (string, required) Desired proposal name (20 character limit)\n"
"2. \"url\": (string, required) URL of proposal details (64 character limit)\n"
"3. payment-count: (numeric, required) Total number of monthly payments\n"
"4. block-start: (numeric, required) Starting super block height\n"
"5. \"vlc-address\": (string, required) VLC address to send payments to\n"
"6. monthly-payment: (numeric, required) Monthly payment amount\n"
"7. \"fee-tx\": (string, required) Transaction hash from preparebudget command\n"
"\nResult:\n"
"\"xxxx\" (string) proposal hash (if successful) or error message (if failed)\n"
"\nExamples:\n" +
HelpExampleCli("submitbudget", "\"test-proposal\" \"https://vlc.org/t/test-proposal\" 2 820800 \"D9oc6C3dttUbv8zd7zGNq1qKBGf4ZQ1XEE\" 500") +
HelpExampleRpc("submitbudget", "\"test-proposal\" \"https://vlc.org/t/test-proposal\" 2 820800 \"D9oc6C3dttUbv8zd7zGNq1qKBGf4ZQ1XEE\" 500"));
// Check these inputs the same way we check the vote commands:
// **********************************************************
std::string strProposalName = SanitizeString(params[0].get_str());
if (strProposalName.size() > 20)
throw runtime_error("Invalid proposal name, limit of 20 characters.");
std::string strURL = SanitizeString(params[1].get_str());
if (strURL.size() > 64)
throw runtime_error("Invalid url, limit of 64 characters.");
int nPaymentCount = params[2].get_int();
if (nPaymentCount < 1)
throw runtime_error("Invalid payment count, must be more than zero.");
// Start must be in the next budget cycle
if (pindexPrev != nullptr) nBlockMin = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
int nBlockStart = params[3].get_int();
if (nBlockStart % GetBudgetPaymentCycleBlocks() != 0) {
int nNext = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
throw runtime_error(strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nNext));
}
int nBlockEnd = nBlockStart + (GetBudgetPaymentCycleBlocks() * nPaymentCount); // End must be AFTER current cycle
if (nBlockStart < nBlockMin)
throw runtime_error("Invalid block start, must be more than current height.");
if (nBlockEnd < pindexPrev->nHeight)
throw runtime_error("Invalid ending block, starting block + (payment_cycle*payments) must be more than current height.");
CBitcoinAddress address(params[4].get_str());
if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Vulcoin address");
// Parse VLC address
CScript scriptPubKey = GetScriptForDestination(address.Get());
CAmount nAmount = AmountFromValue(params[5]);
uint256 hash = ParseHashV(params[6], "parameter 1");
//create the proposal incase we're the first to make it
CBudgetProposalBroadcast budgetProposalBroadcast(strProposalName, strURL, nPaymentCount, scriptPubKey, nAmount, nBlockStart, hash);
std::string strError = "";
int nConf = 0;
if (!IsBudgetCollateralValid(hash, budgetProposalBroadcast.GetHash(), strError, budgetProposalBroadcast.nTime, nConf)) {
throw runtime_error("Proposal FeeTX is not valid - " + hash.ToString() + " - " + strError);
}
if (!masternodeSync.IsBlockchainSynced()) {
throw runtime_error("Must wait for client to sync with masternode network. Try again in a minute or so.");
}
// if(!budgetProposalBroadcast.IsValid(strError)){
// return "Proposal is not valid - " + budgetProposalBroadcast.GetHash().ToString() + " - " + strError;
// }
budget.mapSeenMasternodeBudgetProposals.insert(make_pair(budgetProposalBroadcast.GetHash(), budgetProposalBroadcast));
budgetProposalBroadcast.Relay();
if(budget.AddProposal(budgetProposalBroadcast)) {
return budgetProposalBroadcast.GetHash().ToString();
}
throw runtime_error("Invalid proposal, see debug.log for details.");
}
UniValue mnbudgetvote(const UniValue& params, bool fHelp)
{
std::string strCommand;
if (params.size() >= 1) {
strCommand = params[0].get_str();
// Backwards compatibility with legacy `mnbudget` command
if (strCommand == "vote") strCommand = "local";
if (strCommand == "vote-many") strCommand = "many";
if (strCommand == "vote-alias") strCommand = "alias";
}
if (fHelp || (params.size() == 3 && (strCommand != "local" && strCommand != "many")) || (params.size() == 4 && strCommand != "alias") ||
params.size() > 4 || params.size() < 3)
throw runtime_error(
"mnbudgetvote \"local|many|alias\" \"votehash\" \"yes|no\" ( \"alias\" )\n"
"\nVote on a budget proposal\n"
"\nArguments:\n"
"1. \"mode\" (string, required) The voting mode. 'local' for voting directly from a masternode, 'many' for voting with a MN controller and casting the same vote for each MN, 'alias' for voting with a MN controller and casting a vote for a single MN\n"
"2. \"votehash\" (string, required) The vote hash for the proposal\n"
"3. \"votecast\" (string, required) Your vote. 'yes' to vote for the proposal, 'no' to vote against\n"
"4. \"alias\" (string, required for 'alias' mode) The MN alias to cast a vote for.\n"
"\nResult:\n"
"{\n"
" \"overall\": \"xxxx\", (string) The overall status message for the vote cast\n"
" \"detail\": [\n"
" {\n"
" \"node\": \"xxxx\", (string) 'local' or the MN alias\n"
" \"result\": \"xxxx\", (string) Either 'Success' or 'Failed'\n"
" \"error\": \"xxxx\", (string) Error message, if vote failed\n"
" }\n"
" ,...\n"
" ]\n"
"}\n"
"\nExamples:\n" +
HelpExampleCli("mnbudgetvote", "\"local\" \"ed2f83cedee59a91406f5f47ec4d60bf5a7f9ee6293913c82976bd2d3a658041\" \"yes\"") +
HelpExampleRpc("mnbudgetvote", "\"local\" \"ed2f83cedee59a91406f5f47ec4d60bf5a7f9ee6293913c82976bd2d3a658041\" \"yes\""));
uint256 hash = ParseHashV(params[1], "parameter 1");
std::string strVote = params[2].get_str();
if (strVote != "yes" && strVote != "no") return "You can only vote 'yes' or 'no'";
int nVote = VOTE_ABSTAIN;
if (strVote == "yes") nVote = VOTE_YES;
if (strVote == "no") nVote = VOTE_NO;
int success = 0;
int failed = 0;
UniValue resultsObj(UniValue::VARR);
if (strCommand == "local") {
CPubKey pubKeyMasternode;
CKey keyMasternode;
std::string errorMessage;
UniValue statusObj(UniValue::VOBJ);
while (true) {
if (!masternodeSigner.SetKey(strMasterNodePrivKey, errorMessage, keyMasternode, pubKeyMasternode)) {
failed++;
statusObj.push_back(Pair("node", "local"));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Masternode signing error, could not set key correctly: " + errorMessage));
resultsObj.push_back(statusObj);
break;
}
CMasternode* pmn = mnodeman.Find(activeMasternode.vin);
if (pmn == nullptr) {
failed++;
statusObj.push_back(Pair("node", "local"));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Failure to find masternode in list : " + activeMasternode.vin.ToString()));
resultsObj.push_back(statusObj);
break;
}
CBudgetVote vote(activeMasternode.vin, hash, nVote);
if (!vote.Sign(keyMasternode, pubKeyMasternode)) {
failed++;
statusObj.push_back(Pair("node", "local"));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Failure to sign."));
resultsObj.push_back(statusObj);
break;
}
std::string strError = "";
if (budget.UpdateProposal(vote, nullptr, strError)) {
success++;
budget.mapSeenMasternodeBudgetVotes.insert(make_pair(vote.GetHash(), vote));
vote.Relay();
statusObj.push_back(Pair("node", "local"));
statusObj.push_back(Pair("result", "success"));
statusObj.push_back(Pair("error", ""));
} else {
failed++;
statusObj.push_back(Pair("node", "local"));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Error voting : " + strError));
}
resultsObj.push_back(statusObj);
break;
}
UniValue returnObj(UniValue::VOBJ);
returnObj.push_back(Pair("overall", strprintf("Voted successfully %d time(s) and failed %d time(s).", success, failed)));
returnObj.push_back(Pair("detail", resultsObj));
return returnObj;
}
if (strCommand == "many") {
BOOST_FOREACH (CMasternodeConfig::CMasternodeEntry mne, masternodeConfig.getEntries()) {
std::string errorMessage;
std::vector<unsigned char> vchMasterNodeSignature;
std::string strMasterNodeSignMessage;
CPubKey pubKeyCollateralAddress;
CKey keyCollateralAddress;
CPubKey pubKeyMasternode;
CKey keyMasternode;
UniValue statusObj(UniValue::VOBJ);
if (!masternodeSigner.SetKey(mne.getPrivKey(), errorMessage, keyMasternode, pubKeyMasternode)) {
failed++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Masternode signing error, could not set key correctly: " + errorMessage));
resultsObj.push_back(statusObj);
continue;
}
CMasternode* pmn = mnodeman.Find(pubKeyMasternode);
if (pmn == nullptr) {
failed++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Can't find masternode by pubkey"));
resultsObj.push_back(statusObj);
continue;
}
CBudgetVote vote(pmn->vin, hash, nVote);
if (!vote.Sign(keyMasternode, pubKeyMasternode)) {
failed++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Failure to sign."));
resultsObj.push_back(statusObj);
continue;
}
std::string strError = "";
if (budget.UpdateProposal(vote, nullptr, strError)) {
budget.mapSeenMasternodeBudgetVotes.insert(make_pair(vote.GetHash(), vote));
vote.Relay();
success++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "success"));
statusObj.push_back(Pair("error", ""));
} else {
failed++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", strError.c_str()));
}
resultsObj.push_back(statusObj);
}
UniValue returnObj(UniValue::VOBJ);
returnObj.push_back(Pair("overall", strprintf("Voted successfully %d time(s) and failed %d time(s).", success, failed)));
returnObj.push_back(Pair("detail", resultsObj));
return returnObj;
}
if (strCommand == "alias") {
std::string strAlias = params[3].get_str();
std::vector<CMasternodeConfig::CMasternodeEntry> mnEntries;
mnEntries = masternodeConfig.getEntries();
BOOST_FOREACH(CMasternodeConfig::CMasternodeEntry mne, masternodeConfig.getEntries()) {
if( strAlias != mne.getAlias()) continue;
std::string errorMessage;
std::vector<unsigned char> vchMasterNodeSignature;
std::string strMasterNodeSignMessage;
CPubKey pubKeyCollateralAddress;
CKey keyCollateralAddress;
CPubKey pubKeyMasternode;
CKey keyMasternode;
UniValue statusObj(UniValue::VOBJ);
if(!masternodeSigner.SetKey(mne.getPrivKey(), errorMessage, keyMasternode, pubKeyMasternode)){
failed++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Masternode signing error, could not set key correctly: " + errorMessage));
resultsObj.push_back(statusObj);
continue;
}
CMasternode* pmn = mnodeman.Find(pubKeyMasternode);
if(pmn == nullptr)
{
failed++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Can't find masternode by pubkey"));
resultsObj.push_back(statusObj);
continue;
}
CBudgetVote vote(pmn->vin, hash, nVote);
if(!vote.Sign(keyMasternode, pubKeyMasternode)){
failed++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", "Failure to sign."));
resultsObj.push_back(statusObj);
continue;
}
std::string strError = "";
if(budget.UpdateProposal(vote, nullptr, strError)) {
budget.mapSeenMasternodeBudgetVotes.insert(make_pair(vote.GetHash(), vote));
vote.Relay();
success++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "success"));
statusObj.push_back(Pair("error", ""));
} else {
failed++;
statusObj.push_back(Pair("node", mne.getAlias()));
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("error", strError.c_str()));
}
resultsObj.push_back(statusObj);
}
UniValue returnObj(UniValue::VOBJ);
returnObj.push_back(Pair("overall", strprintf("Voted successfully %d time(s) and failed %d time(s).", success, failed)));
returnObj.push_back(Pair("detail", resultsObj));
return returnObj;
}
return NullUniValue;
}
UniValue getbudgetvotes(const UniValue& params, bool fHelp)
{
if (params.size() != 1)
throw runtime_error(
"getbudgetvotes \"proposal-name\"\n"
"\nPrint vote information for a budget proposal\n"
"\nArguments:\n"
"1. \"proposal-name\": (string, required) Name of the proposal\n"
"\nResult:\n"
"[\n"
" {\n"
" \"mnId\": \"xxxx\", (string) Hash of the masternode's collateral transaction\n"
" \"nHash\": \"xxxx\", (string) Hash of the vote\n"
" \"Vote\": \"YES|NO\", (string) Vote cast ('YES' or 'NO')\n"
" \"nTime\": xxxx, (numeric) Time in seconds since epoch the vote was cast\n"
" \"fValid\": true|false, (boolean) 'true' if the vote is valid, 'false' otherwise\n"
" }\n"
" ,...\n"
"]\n"
"\nExamples:\n" +
HelpExampleCli("getbudgetvotes", "\"test-proposal\"") + HelpExampleRpc("getbudgetvotes", "\"test-proposal\""));
std::string strProposalName = SanitizeString(params[0].get_str());
UniValue ret(UniValue::VARR);
CBudgetProposal* pbudgetProposal = budget.FindProposal(strProposalName);
if (pbudgetProposal == nullptr) throw runtime_error("Unknown proposal name");
std::map<uint256, CBudgetVote>::iterator it = pbudgetProposal->mapVotes.begin();
while (it != pbudgetProposal->mapVotes.end()) {
UniValue bObj(UniValue::VOBJ);
bObj.push_back(Pair("mnId", (*it).second.vin.prevout.hash.ToString()));
bObj.push_back(Pair("nHash", (*it).first.ToString().c_str()));
bObj.push_back(Pair("Vote", (*it).second.GetVoteString()));
bObj.push_back(Pair("nTime", (int64_t)(*it).second.nTime));
bObj.push_back(Pair("fValid", (*it).second.fValid));
ret.push_back(bObj);
it++;
}
return ret;
}
UniValue getnextsuperblock(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
"getnextsuperblock\n"
"\nPrint the next super block height\n"
"\nResult:\n"
"n (numeric) Block height of the next super block\n"
"\nExamples:\n" +
HelpExampleCli("getnextsuperblock", "") + HelpExampleRpc("getnextsuperblock", ""));
CBlockIndex* pindexPrev = chainActive.Tip();
if (!pindexPrev) return "unknown";
int nNext = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
return nNext;
}
UniValue getbudgetprojection(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
"getbudgetprojection\n"
"\nShow the projection of which proposals will be paid the next cycle\n"
"\nResult:\n"
"[\n"
" {\n"
" \"Name\": \"xxxx\", (string) Proposal Name\n"
" \"URL\": \"xxxx\", (string) Proposal URL\n"
" \"Hash\": \"xxxx\", (string) Proposal vote hash\n"
" \"FeeHash\": \"xxxx\", (string) Proposal fee hash\n"
" \"BlockStart\": n, (numeric) Proposal starting block\n"
" \"BlockEnd\": n, (numeric) Proposal ending block\n"
" \"TotalPaymentCount\": n, (numeric) Number of payments\n"
" \"RemainingPaymentCount\": n, (numeric) Number of remaining payments\n"
" \"PaymentAddress\": \"xxxx\", (string) VLC address of payment\n"
" \"Ratio\": x.xxx, (numeric) Ratio of yeas vs nays\n"
" \"Yeas\": n, (numeric) Number of yea votes\n"
" \"Nays\": n, (numeric) Number of nay votes\n"
" \"Abstains\": n, (numeric) Number of abstains\n"
" \"TotalPayment\": xxx.xxx, (numeric) Total payment amount\n"
" \"MonthlyPayment\": xxx.xxx, (numeric) Monthly payment amount\n"
" \"IsEstablished\": true|false, (boolean) Established (true) or (false)\n"
" \"IsValid\": true|false, (boolean) Valid (true) or Invalid (false)\n"
" \"IsValidReason\": \"xxxx\", (string) Error message, if any\n"
" \"fValid\": true|false, (boolean) Valid (true) or Invalid (false)\n"
" \"Alloted\": xxx.xxx, (numeric) Amount alloted in current period\n"
" \"TotalBudgetAlloted\": xxx.xxx (numeric) Total alloted\n"
" }\n"
" ,...\n"
"]\n"
"\nExamples:\n" +
HelpExampleCli("getbudgetprojection", "") + HelpExampleRpc("getbudgetprojection", ""));
UniValue ret(UniValue::VARR);
UniValue resultObj(UniValue::VOBJ);
CAmount nTotalAllotted = 0;
std::vector<CBudgetProposal*> winningProps = budget.GetBudget();
BOOST_FOREACH (CBudgetProposal* pbudgetProposal, winningProps) {
nTotalAllotted += pbudgetProposal->GetAllotted();
CTxDestination address1;
ExtractDestination(pbudgetProposal->GetPayee(), address1);
CBitcoinAddress address2(address1);
UniValue bObj(UniValue::VOBJ);
budgetToJSON(pbudgetProposal, bObj);
bObj.push_back(Pair("Alloted", ValueFromAmount(pbudgetProposal->GetAllotted())));
bObj.push_back(Pair("TotalBudgetAlloted", ValueFromAmount(nTotalAllotted)));
ret.push_back(bObj);
}
return ret;
}
UniValue getbudgetinfo(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() > 1)
throw runtime_error(
"getbudgetinfo ( \"proposal\" )\n"
"\nShow current masternode budgets\n"
"\nArguments:\n"
"1. \"proposal\" (string, optional) Proposal name\n"
"\nResult:\n"
"[\n"
" {\n"
" \"Name\": \"xxxx\", (string) Proposal Name\n"
" \"URL\": \"xxxx\", (string) Proposal URL\n"
" \"Hash\": \"xxxx\", (string) Proposal vote hash\n"
" \"FeeHash\": \"xxxx\", (string) Proposal fee hash\n"
" \"BlockStart\": n, (numeric) Proposal starting block\n"
" \"BlockEnd\": n, (numeric) Proposal ending block\n"
" \"TotalPaymentCount\": n, (numeric) Number of payments\n"
" \"RemainingPaymentCount\": n, (numeric) Number of remaining payments\n"
" \"PaymentAddress\": \"xxxx\", (string) VLC address of payment\n"
" \"Ratio\": x.xxx, (numeric) Ratio of yeas vs nays\n"
" \"Yeas\": n, (numeric) Number of yea votes\n"
" \"Nays\": n, (numeric) Number of nay votes\n"
" \"Abstains\": n, (numeric) Number of abstains\n"
" \"TotalPayment\": xxx.xxx, (numeric) Total payment amount\n"
" \"MonthlyPayment\": xxx.xxx, (numeric) Monthly payment amount\n"
" \"IsEstablished\": true|false, (boolean) Established (true) or (false)\n"
" \"IsValid\": true|false, (boolean) Valid (true) or Invalid (false)\n"
" \"IsValidReason\": \"xxxx\", (string) Error message, if any\n"
" \"fValid\": true|false, (boolean) Valid (true) or Invalid (false)\n"
" }\n"
" ,...\n"
"]\n"
"\nExamples:\n" +
HelpExampleCli("getbudgetprojection", "") + HelpExampleRpc("getbudgetprojection", ""));
UniValue ret(UniValue::VARR);
std::string strShow = "valid";
if (params.size() == 1) {
std::string strProposalName = SanitizeString(params[0].get_str());
CBudgetProposal* pbudgetProposal = budget.FindProposal(strProposalName);
if (pbudgetProposal == nullptr) throw runtime_error("Unknown proposal name");
UniValue bObj(UniValue::VOBJ);
budgetToJSON(pbudgetProposal, bObj);
ret.push_back(bObj);
return ret;
}
std::vector<CBudgetProposal*> winningProps = budget.GetAllProposals();
BOOST_FOREACH (CBudgetProposal* pbudgetProposal, winningProps) {
if (strShow == "valid" && !pbudgetProposal->fValid) continue;
UniValue bObj(UniValue::VOBJ);
budgetToJSON(pbudgetProposal, bObj);
ret.push_back(bObj);
}
return ret;
}
UniValue mnbudgetrawvote(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 6)
throw runtime_error(
"mnbudgetrawvote \"masternode-tx-hash\" masternode-tx-index \"proposal-hash\" yes|no time \"vote-sig\"\n"
"\nCompile and relay a proposal vote with provided external signature instead of signing vote internally\n"
"\nArguments:\n"
"1. \"masternode-tx-hash\" (string, required) Transaction hash for the masternode\n"
"2. masternode-tx-index (numeric, required) Output index for the masternode\n"
"3. \"proposal-hash\" (string, required) Proposal vote hash\n"
"4. yes|no (boolean, required) Vote to cast\n"
"5. time (numeric, required) Time since epoch in seconds\n"
"6. \"vote-sig\" (string, required) External signature\n"
"\nResult:\n"
"\"status\" (string) Vote status or error message\n"
"\nExamples:\n" +
HelpExampleCli("mnbudgetrawvote", "") + HelpExampleRpc("mnbudgetrawvote", ""));
uint256 hashMnTx = ParseHashV(params[0], "mn tx hash");
int nMnTxIndex = params[1].get_int();
CTxIn vin = CTxIn(hashMnTx, nMnTxIndex);
uint256 hashProposal = ParseHashV(params[2], "Proposal hash");
std::string strVote = params[3].get_str();
if (strVote != "yes" && strVote != "no") return "You can only vote 'yes' or 'no'";
int nVote = VOTE_ABSTAIN;
if (strVote == "yes") nVote = VOTE_YES;
if (strVote == "no") nVote = VOTE_NO;
int64_t nTime = params[4].get_int64();
std::string strSig = params[5].get_str();
bool fInvalid = false;
vector<unsigned char> vchSig = DecodeBase64(strSig.c_str(), &fInvalid);
if (fInvalid)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Malformed base64 encoding");
CMasternode* pmn = mnodeman.Find(vin);
if (pmn == nullptr) {
return "Failure to find masternode in list : " + vin.ToString();
}
CBudgetVote vote(vin, hashProposal, nVote);
vote.nTime = nTime;
vote.vchSig = vchSig;
if (!vote.SignatureValid(true)) {
return "Failure to verify signature.";
}
std::string strError = "";
if (budget.UpdateProposal(vote, nullptr, strError)) {
budget.mapSeenMasternodeBudgetVotes.insert(make_pair(vote.GetHash(), vote));
vote.Relay();
return "Voted successfully";
} else {
return "Error voting : " + strError;
}
}
UniValue mnfinalbudget(const UniValue& params, bool fHelp)
{
string strCommand;
if (params.size() >= 1)
strCommand = params[0].get_str();
if (fHelp ||
(strCommand != "suggest" && strCommand != "vote-many" && strCommand != "vote" && strCommand != "show" && strCommand != "getvotes"))
throw runtime_error(
"mnfinalbudget \"command\"... ( \"passphrase\" )\n"
"\nVote or show current budgets\n"
"\nAvailable commands:\n"
" vote-many - Vote on a finalized budget\n"
" vote - Vote on a finalized budget\n"
" show - Show existing finalized budgets\n"
" getvotes - Get vote information for each finalized budget\n");
if (strCommand == "vote-many") {
if (params.size() != 2)
throw runtime_error("Correct usage is 'mnfinalbudget vote-many BUDGET_HASH'");
std::string strHash = params[1].get_str();
uint256 hash(strHash);
int success = 0;
int failed = 0;
UniValue resultsObj(UniValue::VOBJ);
BOOST_FOREACH (CMasternodeConfig::CMasternodeEntry mne, masternodeConfig.getEntries()) {
std::string errorMessage;
std::vector<unsigned char> vchMasterNodeSignature;
std::string strMasterNodeSignMessage;
CPubKey pubKeyCollateralAddress;
CKey keyCollateralAddress;
CPubKey pubKeyMasternode;
CKey keyMasternode;
UniValue statusObj(UniValue::VOBJ);
if (!masternodeSigner.SetKey(mne.getPrivKey(), errorMessage, keyMasternode, pubKeyMasternode)) {
failed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Masternode signing error, could not set key correctly: " + errorMessage));
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
continue;
}
CMasternode* pmn = mnodeman.Find(pubKeyMasternode);
if (pmn == nullptr) {
failed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Can't find masternode by pubkey"));
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
continue;
}
CFinalizedBudgetVote vote(pmn->vin, hash);
if (!vote.Sign(keyMasternode, pubKeyMasternode)) {
failed++;
statusObj.push_back(Pair("result", "failed"));
statusObj.push_back(Pair("errorMessage", "Failure to sign."));
resultsObj.push_back(Pair(mne.getAlias(), statusObj));
continue;
}
std::string strError = "";
if (budget.UpdateFinalizedBudget(vote, nullptr, strError)) {
budget.mapSeenFinalizedBudgetVotes.insert(make_pair(vote.GetHash(), vote));
vote.Relay();
success++;
statusObj.push_back(Pair("result", "success"));
} else {
failed++;
statusObj.push_back(Pair("result", strError.c_str()));
}
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).", success, failed)));
returnObj.push_back(Pair("detail", resultsObj));
return returnObj;
}
if (strCommand == "vote") {
if (params.size() != 2)
throw runtime_error("Correct usage is 'mnfinalbudget vote BUDGET_HASH'");
std::string strHash = params[1].get_str();
uint256 hash(strHash);
CPubKey pubKeyMasternode;
CKey keyMasternode;
std::string errorMessage;
if (!masternodeSigner.SetKey(strMasterNodePrivKey, errorMessage, keyMasternode, pubKeyMasternode))
return "Error upon calling SetKey";
CMasternode* pmn = mnodeman.Find(activeMasternode.vin);
if (pmn == nullptr) {
return "Failure to find masternode in list : " + activeMasternode.vin.ToString();
}
CFinalizedBudgetVote vote(activeMasternode.vin, hash);
if (!vote.Sign(keyMasternode, pubKeyMasternode)) {
return "Failure to sign.";
}
std::string strError = "";
if (budget.UpdateFinalizedBudget(vote, nullptr, strError)) {
budget.mapSeenFinalizedBudgetVotes.insert(make_pair(vote.GetHash(), vote));
vote.Relay();
return "success";
} else {
return "Error voting : " + strError;
}
}
if (strCommand == "show") {
UniValue resultObj(UniValue::VOBJ);
std::vector<CFinalizedBudget*> winningFbs = budget.GetFinalizedBudgets();
BOOST_FOREACH (CFinalizedBudget* finalizedBudget, winningFbs) {
UniValue bObj(UniValue::VOBJ);
bObj.push_back(Pair("FeeTX", finalizedBudget->nFeeTXHash.ToString()));
bObj.push_back(Pair("Hash", finalizedBudget->GetHash().ToString()));
bObj.push_back(Pair("BlockStart", (int64_t)finalizedBudget->GetBlockStart()));
bObj.push_back(Pair("BlockEnd", (int64_t)finalizedBudget->GetBlockEnd()));
bObj.push_back(Pair("Proposals", finalizedBudget->GetProposals()));
bObj.push_back(Pair("VoteCount", (int64_t)finalizedBudget->GetVoteCount()));
bObj.push_back(Pair("Status", finalizedBudget->GetStatus()));