-
Notifications
You must be signed in to change notification settings - Fork 15
/
masternode-payments.cpp
1010 lines (806 loc) · 36.1 KB
/
masternode-payments.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-2020 The PIVX developers
// Copyright (c) 2021-2022 The DECENOMY Core Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "masternode-payments.h"
#include "addrman.h"
#include "chainparams.h"
#include "fs.h"
#include "masternode-sync.h"
#include "masternodeman.h"
#include "netmessagemaker.h"
#include "spork.h"
#include "sync.h"
#include "util.h"
#include "utilmoneystr.h"
#include "core_io.h"
/** Object for who's going to get paid on which blocks */
CMasternodePayments masternodePayments;
RecursiveMutex cs_vecPayments;
RecursiveMutex cs_mapMasternodeBlocks;
RecursiveMutex cs_mapMasternodePayeeVotes;
//
// CMasternodePaymentDB
//
CMasternodePaymentDB::CMasternodePaymentDB()
{
pathDB = GetDataDir() / "mnpayments.dat";
strMagicMessage = "MasternodePayments";
}
bool CMasternodePaymentDB::Write(const CMasternodePayments& objToSave)
{
int64_t nStart = GetTimeMillis();
// serialize, checksum data up to that point, then append checksum
CDataStream ssObj(SER_DISK, CLIENT_VERSION);
ssObj << strMagicMessage; // masternode cache file specific magic message
ssObj << FLATDATA(Params().MessageStart()); // network specific magic number
ssObj << objToSave;
uint256 hash = Hash(ssObj.begin(), ssObj.end());
ssObj << hash;
// open output file, and associate with CAutoFile
FILE* file = fsbridge::fopen(pathDB, "wb");
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull())
return error("%s : Failed to open file %s", __func__, pathDB.string());
// Write and commit header, data
try {
fileout << ssObj;
} catch (const std::exception& e) {
return error("%s : Serialize or I/O error - %s", __func__, e.what());
}
fileout.fclose();
LogPrint(BCLog::MASTERNODE,"Written info to mnpayments.dat %dms\n", GetTimeMillis() - nStart);
return true;
}
CMasternodePaymentDB::ReadResult CMasternodePaymentDB::Read(CMasternodePayments& objToLoad, bool fDryRun)
{
int64_t nStart = GetTimeMillis();
// open input file, and associate with CAutoFile
FILE* file = fsbridge::fopen(pathDB, "rb");
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (filein.IsNull()) {
error("%s : Failed to open file %s", __func__, pathDB.string());
return FileError;
}
// use file size to size memory buffer
int fileSize = fs::file_size(pathDB);
int dataSize = fileSize - sizeof(uint256);
// Don't try to resize to a negative number if file is small
if (dataSize < 0)
dataSize = 0;
std::vector<unsigned char> vchData;
vchData.resize(dataSize);
uint256 hashIn;
// read data and checksum from file
try {
filein.read((char*)&vchData[0], dataSize);
filein >> hashIn;
} catch (const std::exception& e) {
error("%s : Deserialize or I/O error - %s", __func__, e.what());
return HashReadError;
}
filein.fclose();
CDataStream ssObj(vchData, SER_DISK, CLIENT_VERSION);
// verify stored checksum matches input data
uint256 hashTmp = Hash(ssObj.begin(), ssObj.end());
if (hashIn != hashTmp) {
error("%s : Checksum mismatch, data corrupted", __func__);
return IncorrectHash;
}
unsigned char pchMsgTmp[4];
std::string strMagicMessageTmp;
try {
// de-serialize file header (masternode cache file specific magic message) and ..
ssObj >> strMagicMessageTmp;
// ... verify the message matches predefined one
if (strMagicMessage != strMagicMessageTmp) {
error("%s : Invalid masternode payement cache magic message", __func__);
return IncorrectMagicMessage;
}
// de-serialize file header (network specific magic number) and ..
ssObj >> FLATDATA(pchMsgTmp);
// ... verify the network matches ours
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) {
error("%s : Invalid network magic number", __func__);
return IncorrectMagicNumber;
}
// de-serialize data into CMasternodePayments object
ssObj >> objToLoad;
} catch (const std::exception& e) {
objToLoad.Clear();
error("%s : Deserialize or I/O error - %s", __func__, e.what());
return IncorrectFormat;
}
LogPrint(BCLog::MASTERNODE,"Loaded info from mnpayments.dat %dms\n", GetTimeMillis() - nStart);
LogPrint(BCLog::MASTERNODE," %s\n", objToLoad.ToString());
if (!fDryRun) {
LogPrint(BCLog::MASTERNODE,"Masternode payments manager - cleaning....\n");
objToLoad.CleanPaymentList();
LogPrint(BCLog::MASTERNODE,"Masternode payments manager - result:\n");
LogPrint(BCLog::MASTERNODE," %s\n", objToLoad.ToString());
}
return Ok;
}
uint256 CMasternodePaymentWinner::GetHash() const
{
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << std::vector<unsigned char>(payee.begin(), payee.end());
ss << nBlockHeight;
ss << vinMasternode.prevout;
return ss.GetHash();
}
std::string CMasternodePaymentWinner::GetStrMessage() const
{
if (Params().GetConsensus().NetworkUpgradeActive(chainActive.Tip()->nHeight, Consensus::UPGRADE_TIME_PROTOCOL_V2)) {
return vinMasternode.prevout.ToStringShort() + std::to_string(nBlockHeight) + HexStr(payee);
} else {
return vinMasternode.prevout.ToStringShort() + std::to_string(nBlockHeight) + ScriptToAsmStr(payee);
}
}
bool CMasternodePaymentWinner::IsValid(CNode* pnode, std::string& strError)
{
CMasternode* pmn = mnodeman.Find(vinMasternode);
if (!pmn) {
strError = strprintf("Unknown Masternode %s", vinMasternode.prevout.ToStringShort());
LogPrint(BCLog::MASTERNODE,"CMasternodePaymentWinner::IsValid - %s\n", strError);
mnodeman.AskForMN(pnode, vinMasternode);
return false;
}
if (sporkManager.IsSporkActive(SPORK_102_FORCE_ENABLED_MASTERNODE)) {
if (pmn->Status() != "ENABLED") {
strError = strprintf("Masternode is not in ENABLED state - Status(): %d", pmn->Status());
LogPrint(BCLog::MASTERNODE, "CMasternodePaymentWinner::IsValid - Force masternode requirement to have ENABLED status instead of ACTIVE - %s\n", strError);
return false;
}
}
int n = mnodeman.GetMasternodeRank(vinMasternode, nBlockHeight - 100);
if (n > MNPAYMENTS_SIGNATURES_TOTAL) {
//It's common to have masternodes mistakenly think they are in the top 10
// We don't want to print all of these messages, or punish them unless they're way off
if (n > MNPAYMENTS_SIGNATURES_TOTAL * 2) {
strError = strprintf("Masternode not in the top %d (%d)", MNPAYMENTS_SIGNATURES_TOTAL * 2, n);
LogPrint(BCLog::MASTERNODE,"CMasternodePaymentWinner::IsValid - %s\n", strError);
//if (masternodeSync.IsSynced()) Misbehaving(pnode->GetId(), 20);
}
return false;
}
return true;
}
void CMasternodePaymentWinner::Relay()
{
CInv inv(MSG_MASTERNODE_WINNER, GetHash());
g_connman->RelayInv(inv);
}
void DumpMasternodePayments()
{
int64_t nStart = GetTimeMillis();
CMasternodePaymentDB paymentdb;
CMasternodePayments tempPayments;
LogPrint(BCLog::MASTERNODE,"Verifying mnpayments.dat format...\n");
CMasternodePaymentDB::ReadResult readResult = paymentdb.Read(tempPayments, true);
// there was an error and it was not an error on file opening => do not proceed
if (readResult == CMasternodePaymentDB::FileError)
LogPrint(BCLog::MASTERNODE,"Missing budgets file - mnpayments.dat, will try to recreate\n");
else if (readResult != CMasternodePaymentDB::Ok) {
LogPrint(BCLog::MASTERNODE,"Error reading mnpayments.dat: ");
if (readResult == CMasternodePaymentDB::IncorrectFormat)
LogPrint(BCLog::MASTERNODE,"magic is ok but data has invalid format, will try to recreate\n");
else {
LogPrint(BCLog::MASTERNODE,"file format is unknown or invalid, please fix it manually\n");
return;
}
}
LogPrint(BCLog::MASTERNODE,"Writting info to mnpayments.dat...\n");
paymentdb.Write(masternodePayments);
LogPrint(BCLog::MASTERNODE,"Budget dump finished %dms\n", GetTimeMillis() - nStart);
}
bool IsBlockValueValid(int nHeight, CAmount nExpectedValue, CAmount nMinted)
{
// No superblock, regular check
return nMinted <= nExpectedValue;
}
bool IsBlockPayeeValid(const CBlock& block, int nBlockHeight)
{
const Consensus::Params& consensus = Params().GetConsensus();
if (!masternodeSync.IsSynced()) { //there is no budget data to use to check anything -- find the longest chain
LogPrint(BCLog::MASTERNODE, "Client not synced, skipping block payee checks\n");
return true;
}
const bool isPoSActive = Params().GetConsensus().NetworkUpgradeActive(nBlockHeight, Consensus::UPGRADE_POS);
const CTransaction& txNew = (isPoSActive ? block.vtx[1] : block.vtx[0]);
// if it's the mint block then verify if exists an output with the right amount and address
if (consensus.nMintHeight == nBlockHeight) {
LogPrint(BCLog::MASTERNODE, "masternode", "IsBlockPayeeValid: Check mint reward\n");
CAmount amount = consensus.nMintValue;
CScript payee = GetScriptForDestination(DecodeDestination(consensus.sMintAddress));
LogPrint(BCLog::MASTERNODE, "IsBlockPayeeValid, expected mint amount is %lld, coins %f\n", amount, (float)amount / COIN);
bool fMintFound = false;
for(CTxOut out : txNew.vout) {
if (payee == out.scriptPubKey && amount == out.nValue) {
fMintFound = true;
}
}
if(!fMintFound) return false;
}
//check for masternode payee
if (masternodePayments.IsTransactionValid(txNew, nBlockHeight))
return true;
LogPrint(BCLog::MASTERNODE,"Invalid mn payment detected %s\n", txNew.ToString().c_str());
// fails if spork 8 is enabled and
// spork 113 is disabled or current time is outside the reconsider window
if (sporkManager.IsSporkActive(SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT)) {
return false;
} else {
LogPrint(BCLog::MASTERNODE,"Masternode payment enforcement is disabled, accepting block\n");
return true;
}
}
void FillBlockPayee(CMutableTransaction& txNew, const CBlockIndex* pindexPrev, bool fProofOfStake)
{
masternodePayments.FillBlockPayee(txNew, pindexPrev, fProofOfStake);
}
std::string GetRequiredPaymentsString(int nBlockHeight)
{
return masternodePayments.GetRequiredPaymentsString(nBlockHeight);
}
void CMasternodePayments::FillBlockPayee(CMutableTransaction& txNew, const CBlockIndex* pindexPrev, bool fProofOfStake)
{
if (!pindexPrev) return;
bool hasPayment = true;
CScript payee;
int nHeight = pindexPrev->nHeight + 1;
const Consensus::Params& consensus = Params().GetConsensus();
//spork
if (!masternodePayments.GetBlockPayee(nHeight, payee)) {
//no masternode detected
CMasternode* winningNode = mnodeman.GetCurrentMasterNode(1);
if (winningNode) {
payee = GetScriptForDestination(winningNode->pubKeyCollateralAddress.GetID());
} else {
LogPrint(BCLog::MASTERNODE,"CreateNewBlock: Failed to detect masternode to pay\n");
hasPayment = false;
}
}
CAmount masternodePayment = CMasternode::GetMasternodePayment(nHeight);
CAmount blockValue = CMasternode::GetBlockValue(nHeight);
CAmount nMintValue = 0;
CScript mintPayee;
// Mint
if (consensus.nMintHeight == nHeight) {
nMintValue = consensus.nMintValue;
mintPayee = GetScriptForDestination(DecodeDestination(consensus.sMintAddress));
}
//subtract mn payment from the stake reward plus the mint value
CAmount reductionAmount = masternodePayment + nMintValue;
if (hasPayment) {
if (fProofOfStake) {
/**For Proof Of Stake vout[0] must be null
* Stake reward can be split into many different outputs, so we must
* use vout.size() to align with several different cases.
* An additional output is appended as the masternode payment
*/
unsigned int i = txNew.vout.size();
txNew.vout.resize(i + 1);
txNew.vout[i].scriptPubKey = payee;
txNew.vout[i].nValue = masternodePayment;
//subtract mn payment from the stake reward
if (i == 2) {
// Majority of cases; do it quick and move on
txNew.vout[i - 1].nValue -= masternodePayment;
} else if (i > 2) {
// special case, stake is split between (i-1) outputs
unsigned int outputs = i-1;
CAmount mnPaymentSplit = masternodePayment / outputs;
CAmount mnPaymentRemainder = masternodePayment - (mnPaymentSplit * outputs);
for (unsigned int j=1; j<=outputs; j++) {
txNew.vout[j].nValue -= mnPaymentSplit;
}
// in case it's not an even division, take the last bit of dust from the last one
txNew.vout[outputs].nValue -= mnPaymentRemainder;
}
} else {
txNew.vout.resize(2);
txNew.vout[1].scriptPubKey = payee;
txNew.vout[1].nValue = masternodePayment;
txNew.vout[0].nValue = blockValue - reductionAmount;
}
CTxDestination address1;
ExtractDestination(payee, address1);
LogPrint(BCLog::MASTERNODE,"Masternode payment of %s to %s\n", FormatMoney(masternodePayment).c_str(), EncodeDestination(address1).c_str());
} else {
if(nMintValue > 0) {
// removes the mint value if there is no masternode to pay
if (fProofOfStake) {
unsigned int i = txNew.vout.size();
if (i == 2) {
// Majority of cases; do it quick and move on
txNew.vout[i - 1].nValue -= nMintValue;
} else if (i > 2) {
// special case, stake is split between (i-1) outputs
unsigned int outputs = i-1;
CAmount mnPaymentSplit = nMintValue / outputs;
CAmount mnPaymentRemainder = nMintValue - (mnPaymentSplit * outputs);
for (unsigned int j=1; j<=outputs; j++) {
txNew.vout[j].nValue -= mnPaymentSplit;
}
// in case it's not an even division, take the last bit of dust from the last one
txNew.vout[outputs].nValue -= mnPaymentRemainder;
}
} else {
txNew.vout[0].nValue = blockValue - nMintValue;
}
}
}
// Adds mint value
if(nMintValue > 0) {
unsigned int i = txNew.vout.size();
txNew.vout.resize(i + 1);
txNew.vout[i].scriptPubKey = mintPayee;
txNew.vout[i].nValue = nMintValue;
}
}
void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
if (sporkManager.IsSporkActive(SPORK_114_MN_PAYMENT_V2)) return; // voting is disabled
if (!masternodeSync.IsBlockchainSynced()) return;
if (fLiteMode) return; //disable all Masternode related functionality
if (strCommand == NetMsgType::GETMNWINNERS) { //Masternode Payments Request Sync
if (fLiteMode) return; //disable all Masternode related functionality
int nCountNeeded;
vRecv >> nCountNeeded;
if (Params().NetworkID() == CBaseChainParams::MAIN) {
if (pfrom->HasFulfilledRequest(NetMsgType::GETMNWINNERS)) {
LogPrintf("CMasternodePayments::ProcessMessageMasternodePayments() : mnget - peer already asked me for the list\n");
return;
}
}
pfrom->FulfilledRequest(NetMsgType::GETMNWINNERS);
masternodePayments.Sync(pfrom, nCountNeeded);
LogPrint(BCLog::MASTERNODE, "mnget - Sent Masternode winners to peer %i\n", pfrom->GetId());
} else if (strCommand == NetMsgType::MNWINNER) { //Masternode Payments Declare Winner
//this is required in litemodef
CMasternodePaymentWinner winner;
vRecv >> winner;
if (pfrom->nVersion < ActiveProtocol()) return;
int nHeight;
{
TRY_LOCK(cs_main, locked);
if (!locked || chainActive.Tip() == NULL) return;
nHeight = chainActive.Tip()->nHeight;
}
if (masternodePayments.mapMasternodePayeeVotes.count(winner.GetHash())) {
LogPrint(BCLog::MASTERNODE, "mnw - Already seen - %s bestHeight %d\n", winner.GetHash().ToString().c_str(), nHeight);
masternodeSync.AddedMasternodeWinner(winner.GetHash());
return;
}
int nFirstBlock = nHeight - (mnodeman.CountEnabled() * 1.25);
if (winner.nBlockHeight < nFirstBlock || winner.nBlockHeight > nHeight + 20) {
LogPrint(BCLog::MASTERNODE, "mnw - winner out of range - FirstBlock %d Height %d bestHeight %d\n", nFirstBlock, winner.nBlockHeight, nHeight);
return;
}
// reject old signature version
if (Params().GetConsensus().NetworkUpgradeActive(chainActive.Tip()->nHeight, Consensus::UPGRADE_TIME_PROTOCOL_V2) &&
winner.nMessVersion != MessageVersion::MESS_VER_HASH) {
LogPrint(BCLog::MASTERNODE, "mnw - rejecting old message version %d\n", winner.nMessVersion);
return;
}
std::string strError = "";
if (!winner.IsValid(pfrom, strError)) {
// if(strError != "") LogPrint(BCLog::MASTERNODE,"mnw - invalid message - %s\n", strError);
return;
}
if (!masternodePayments.CanVote(winner.vinMasternode.prevout, winner.nBlockHeight)) {
// LogPrint(BCLog::MASTERNODE,"mnw - masternode already voted - %s\n", winner.vinMasternode.prevout.ToStringShort());
return;
}
if (!winner.CheckSignature()) {
if (masternodeSync.IsSynced()) {
LogPrintf("CMasternodePayments::ProcessMessageMasternodePayments() : mnw - invalid signature\n");
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20);
}
// it could just be a non-synced masternode
mnodeman.AskForMN(pfrom, winner.vinMasternode);
return;
}
if (winner.nBlockHeight >= nHeight &&
sporkManager.IsSporkActive(SPORK_109_FORCE_ENABLED_VOTED_MASTERNODE)
) {
CMasternode* pmn = mnodeman.Find(winner.payee);
if (!pmn) {
LogPrint(BCLog::MASTERNODE, "mnw - winner payee is not a masternode");
return;
}
if (!pmn->IsEnabled()) {
LogPrint(BCLog::MASTERNODE, "mnw - winner payee is a masternode but is not ENABLED");
return;
}
}
CTxDestination address1;
ExtractDestination(winner.payee, address1);
LogPrint(BCLog::MASTERNODE, "mnw - winning vote - Addr %s Height %d bestHeight %d - %s\n", EncodeDestination(address1), winner.nBlockHeight, nHeight, winner.vinMasternode.prevout.ToStringShort());
if (masternodePayments.AddWinningMasternode(winner)) {
winner.Relay();
masternodeSync.AddedMasternodeWinner(winner.GetHash());
}
}
}
bool CMasternodePayments::GetBlockPayeeV1(int nBlockHeight, CScript& payee)
{
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::GetBlockPayeeV1() nHeight %d. \n", nBlockHeight);
LOCK(cs_mapMasternodeBlocks);
if (mapMasternodeBlocks.count(nBlockHeight)) {
return mapMasternodeBlocks[nBlockHeight].GetPayee(payee);
}
return false;
}
bool CMasternodePayments::GetBlockPayeeV2(int nBlockHeight, CScript& payee)
{
LogPrint(BCLog::MASTERNODE, "%s : nHeight %d. \n", __func__, nBlockHeight);
// pay to the oldest MN that still had no payment but its input is old enough and it was active long enough
auto pmn = mnodeman.GetNextMasternodeInQueueForPayment(nBlockHeight);
if (pmn) {
LogPrint(BCLog::MASTERNODE,"%s : Found by GetNextMasternodeInQueueForPayment \n", __func__);
payee = GetScriptForDestination(pmn->pubKeyCollateralAddress.GetID());
CTxDestination address1;
ExtractDestination(payee, address1);
LogPrint(BCLog::MASTERNODE,"%s : Winner payee %s nHeight %d. \n", __func__, EncodeDestination(address1).c_str(), nBlockHeight);
return true;
}
LogPrint(BCLog::MASTERNODE,"%s : Failed to find masternode to pay\n", __func__);
return false;
}
bool CMasternodePayments::GetBlockPayee(int nBlockHeight, CScript& payee)
{
return
!sporkManager.IsSporkActive(SPORK_114_MN_PAYMENT_V2) ?
GetBlockPayeeV1(nBlockHeight, payee) :
GetBlockPayeeV2(nBlockHeight, payee);
}
// Is this masternode scheduled to get paid soon?
// -- Only look ahead up to 8 blocks to allow for propagation of the latest 2 winners
bool CMasternodePayments::IsScheduled(CMasternode& mn, int nNotBlockHeight)
{
if (sporkManager.IsSporkActive(SPORK_114_MN_PAYMENT_V2)) return false; // voting is disabled
int nHeight;
{
TRY_LOCK(cs_main, locked);
if (!locked || chainActive.Tip() == NULL) return false;
nHeight = chainActive.Tip()->nHeight;
}
CScript mnpayee;
mnpayee = GetScriptForDestination(mn.pubKeyCollateralAddress.GetID());
CScript payee;
for (int64_t h = nHeight; h <= nHeight + 8; h++) {
if (h == nNotBlockHeight) continue;
CMasternodeBlockPayees mnbp;
{
LOCK(cs_mapMasternodeBlocks);
if (mapMasternodeBlocks.count(h)) {
mnbp = mapMasternodeBlocks[h];
}
}
if (mnbp.nBlockHeight > 0 && mnbp.GetPayee(payee)) {
if (mnpayee == payee) {
return true;
}
}
}
return false;
}
bool CMasternodePayments::AddWinningMasternode(CMasternodePaymentWinner& winnerIn)
{
uint256 blockHash;
if (!GetBlockHash(blockHash, winnerIn.nBlockHeight - 100)) {
return false;
}
{
LOCK(cs_mapMasternodePayeeVotes);
if (mapMasternodePayeeVotes.count(winnerIn.GetHash())) {
return false;
}
mapMasternodePayeeVotes[winnerIn.GetHash()] = winnerIn;
}
{
LOCK(cs_mapMasternodeBlocks);
if (!mapMasternodeBlocks.count(winnerIn.nBlockHeight)) {
CMasternodeBlockPayees blockPayees(winnerIn.nBlockHeight);
mapMasternodeBlocks[winnerIn.nBlockHeight] = blockPayees;
}
mapMasternodeBlocks[winnerIn.nBlockHeight].AddPayee(winnerIn.payee, 1);
}
return true;
}
bool CMasternodeBlockPayees::IsTransactionValidV1(const CTransaction& txNew, int nBlockHeight)
{
//require at least 6 signatures
int nMaxSignatures = 0;
for (CMasternodePayee& payee : vecPayments)
if (payee.nVotes >= nMaxSignatures && payee.nVotes >= MNPAYMENTS_SIGNATURES_REQUIRED)
nMaxSignatures = payee.nVotes;
LogPrint(BCLog::MASTERNODE, "%s - nMaxSignatures: %d\n", __func__, nMaxSignatures);
// if we don't have at least 6 signatures on a payee, approve whichever is the longest chain
if (nMaxSignatures < MNPAYMENTS_SIGNATURES_REQUIRED) {
return true;
}
std::string strPayeesPossible = "";
CAmount requiredMasternodePayment = CMasternode::GetMasternodePayment(nBlockHeight);
for (CMasternodePayee& payee : vecPayments) {
bool found = false;
for (CTxOut out : txNew.vout) {
if (payee.scriptPubKey == out.scriptPubKey) {
if(out.nValue == requiredMasternodePayment)
found = true;
else
LogPrintf("%s : Masternode payment value (%s) different from required value (%s).\n",
__func__, FormatMoney(out.nValue).c_str(), FormatMoney(requiredMasternodePayment).c_str());
}
}
if (payee.nVotes >= MNPAYMENTS_SIGNATURES_REQUIRED) {
if (found) {
CMasternode* pmn = mnodeman.Find(payee.scriptPubKey);
bool result = false;
if(sporkManager.IsSporkActive(SPORK_110_FORCE_ENABLED_MASTERNODE_PAYMENT)) {
result = pmn && pmn->IsEnabled(); // it is a existing masternode and it is enabled then it is OK
} else {
result = true;
}
CTxDestination addr;
ExtractDestination(payee.scriptPubKey, addr);
LogPrint(BCLog::MASTERNODE, "%s - payee.scriptPubKey: %s\n", __func__, EncodeDestination(addr));
return result;
}
CTxDestination address1;
ExtractDestination(payee.scriptPubKey, address1);
if (strPayeesPossible != "")
strPayeesPossible += ",";
strPayeesPossible += EncodeDestination(address1);
}
}
LogPrint(BCLog::MASTERNODE,"%s - Missing required payment of %s to %s\n", __func__, FormatMoney(requiredMasternodePayment).c_str(), strPayeesPossible.c_str());
return false;
}
bool CMasternodeBlockPayees::IsTransactionValidV2(const CTransaction& txNew, int nBlockHeight)
{
// if there is no MNs, then there is no enough data to perform verification
if (mnodeman.CountEnabled() == 0) {
LogPrint(BCLog::MASTERNODE, "%s - mnodeman.CountEnabled() == 0\n", __func__);
return true;
}
// if the masternode list is not synced, then there is no enough data to perform verification
if (!masternodeSync.IsSynced()) {
LogPrint(BCLog::MASTERNODE, "%s - !masternodeSync.IsSynced()\n", __func__);
return true;
}
auto requiredMasternodePayment = CMasternode::GetMasternodePayment(nBlockHeight);
auto found = false;
CScript paidPayee;
for (CTxOut out : txNew.vout) {
if (out.nValue == requiredMasternodePayment) {
found = true;
paidPayee = out.scriptPubKey;
}
}
if (found) {
if(LogAcceptCategory(BCLog::MASTERNODE)) {
auto mn = mnodeman.Find(paidPayee);
if(mn) {
CScript payee = GetScriptForDestination(mn->pubKeyCollateralAddress.GetID());
CTxDestination addr;
ExtractDestination(payee, addr);
LogPrint(BCLog::MASTERNODE, "%s - Paid masternode %s\n", __func__, EncodeDestination(addr));
LogPrint(BCLog::MASTERNODE, "%s - Last paid at ~%d blocks\n", __func__, mn->SecondsSincePayment(chainActive[nBlockHeight - 1]) / Params().GetConsensus().nTargetSpacing);
}
}
// fetch the paid masternode from our masternode list
auto pmn = mnodeman.Find(paidPayee);
// check if the masternode really exists and is enabled
if (!pmn || !pmn->IsEnabled()) return false;
// get the masternodes choosen on this decision
auto eligible = mnodeman.GetNextMasternodeInQueueEligible(nBlockHeight);
auto nmn = eligible.first;
auto result = false;
if(LogAcceptCategory(BCLog::MASTERNODE)) {
CScript payee = GetScriptForDestination(nmn->pubKeyCollateralAddress.GetID());
CTxDestination addr;
ExtractDestination(payee, addr);
LogPrint(BCLog::MASTERNODE, "%s - Elected masternode %s\n", __func__, EncodeDestination(addr));
for (auto& txin : eligible.second) {
auto pmn2 = mnodeman.Find(txin);
CScript payee2 = GetScriptForDestination(pmn2->pubKeyCollateralAddress.GetID());
CTxDestination addr2;
ExtractDestination(payee2, addr2);
LogPrint(
BCLog::MASTERNODE,
"%s - Eligible masternode %s - Last paid at ~%d blocks (%d)(%d)\n",
__func__,
EncodeDestination(addr2),
pmn2->SecondsSincePayment(chainActive[nBlockHeight - 1]) / Params().GetConsensus().nTargetSpacing,
pmn2->GetLastPaid(chainActive[nBlockHeight - 1]),
pmn2->sigTime
);
}
LogPrint(
BCLog::MASTERNODE,
"%s - Elected masternodes count %d\n",
__func__,
eligible.second.size()
);
}
if (pmn->GetVin() == nmn->GetVin()) { // if they match, then the paid masternode is OK
result = true;
} else { // else, iterate on the eligible list and see if there is another possibility of a valid masternode to pay
for (auto& txin : eligible.second) {
if (pmn->GetVin() == txin) {
result = true; // there is a plausible masternode to pay, therefore return true
}
}
if(!result) {
CTxDestination addr;
ExtractDestination(paidPayee, addr);
LogPrint(BCLog::MASTERNODE, "%s - Paid masternode %s is not eligible\n", __func__, EncodeDestination(addr));
}
}
return result;
} else {
LogPrint(BCLog::MASTERNODE, "%s - Missing required payment of %s\n", __func__, FormatMoney(requiredMasternodePayment));
return false;
}
return false;
}
bool CMasternodeBlockPayees::IsTransactionValid(const CTransaction& txNew, int nBlockHeight)
{
auto pVersion = sporkManager.IsSporkActive(SPORK_114_MN_PAYMENT_V2) ? 2 : 1;
auto result = pVersion == 1 ?
IsTransactionValidV1(txNew, nBlockHeight) :
IsTransactionValidV2(txNew, nBlockHeight);
LogPrint(BCLog::MASTERNODE, "%s - IsTransactionValidV%d: %d\n", __func__, pVersion, result);
return result;
}
std::string CMasternodeBlockPayees::GetRequiredPaymentsString()
{
LOCK(cs_vecPayments);
std::string ret = "Unknown";
for (CMasternodePayee& payee : vecPayments) {
CTxDestination address1;
ExtractDestination(payee.scriptPubKey, address1);
if (ret != "Unknown") {
ret += ", ";
}
ret = EncodeDestination(address1) + ":" + std::to_string(payee.nVotes);
}
return ret;
}
std::string CMasternodePayments::GetRequiredPaymentsString(int nBlockHeight)
{
LOCK(cs_mapMasternodeBlocks);
if (mapMasternodeBlocks.count(nBlockHeight)) {
return mapMasternodeBlocks[nBlockHeight].GetRequiredPaymentsString();
}
return "Unknown";
}
bool CMasternodePayments::IsTransactionValid(const CTransaction& txNew, int nBlockHeight)
{
CMasternodeBlockPayees mnbp;
{
LOCK(cs_mapMasternodeBlocks);
if (mapMasternodeBlocks.count(nBlockHeight)) {
mnbp = mapMasternodeBlocks[nBlockHeight];
}
}
if (mnbp.nBlockHeight > 0) {
return mnbp.IsTransactionValid(txNew, nBlockHeight);
}
if (sporkManager.IsSporkActive(SPORK_112_MASTERNODE_LAST_PAID_V2)) { // if voting is disabled, try again
return mnbp.IsTransactionValid(txNew, nBlockHeight);
}
return true;
}
void CMasternodePayments::CleanPaymentList()
{
int nHeight;
{
TRY_LOCK(cs_main, locked);
if (!locked || chainActive.Tip() == NULL) return;
nHeight = chainActive.Tip()->nHeight;
}
LOCK2(cs_mapMasternodePayeeVotes, cs_mapMasternodeBlocks);
//keep up to five cycles for historical sake
int nLimit = std::max(int(mnodeman.size() * (sporkManager.IsSporkActive(SPORK_112_MASTERNODE_LAST_PAID_V2) ? 2 : 1.25)), 1000);
std::map<uint256, CMasternodePaymentWinner>::iterator it = mapMasternodePayeeVotes.begin();
while (it != mapMasternodePayeeVotes.end()) {
CMasternodePaymentWinner winner = (*it).second;
if (nHeight - winner.nBlockHeight > nLimit) {
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::CleanPaymentList - Removing old Masternode payment - block %d\n", winner.nBlockHeight);
masternodeSync.mapSeenSyncMNW.erase((*it).first);
mapMasternodePayeeVotes.erase(it++);
mapMasternodeBlocks.erase(winner.nBlockHeight);
} else {
++it;
}
}
}
void CMasternodePayments::ProcessBlock(int nBlockHeight)
{
if (!fMasterNode) return;
if (sporkManager.IsSporkActive(SPORK_114_MN_PAYMENT_V2)) return; // voting is disabled
auto nHeight = nLastBlockHeight;
for (auto& activeMasternode : amnodeman.GetActiveMasternodes()) {
if (activeMasternode.vin == nullopt) {
LogPrint(BCLog::MASTERNODE, "%s: Active Masternode not initialized.", __func__);
continue;
}
//reference node - hybrid mode
int n = mnodeman.GetMasternodeRank(*(activeMasternode.vin), nBlockHeight - 100);
if (n == -1 || n == INT_MAX) {
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::ProcessBlock - Unknown Masternode\n");
continue;
}
if (n > MNPAYMENTS_SIGNATURES_TOTAL) {
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::ProcessBlock - Masternode not in the top %d (%d)\n", MNPAYMENTS_SIGNATURES_TOTAL, n);
continue;
}
if (nBlockHeight <= nLastBlockHeight) continue;
CMasternodePaymentWinner newWinner(*(activeMasternode.vin));
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::ProcessBlock() Start nHeight %d - vin %s. \n", nBlockHeight, activeMasternode.vin->prevout.ToStringShort());
// pay to the oldest MN that still had no payment but its input is old enough and it was active long enough
CMasternode* pmn = mnodeman.GetNextMasternodeInQueueForPayment(nBlockHeight);
if (pmn != NULL) {
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::ProcessBlock() Found by FindOldestNotInVec \n");
newWinner.nBlockHeight = nBlockHeight;
CScript payee = GetScriptForDestination(pmn->pubKeyCollateralAddress.GetID());
newWinner.AddPayee(payee);
CTxDestination address1;
ExtractDestination(payee, address1);
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::ProcessBlock() Winner payee %s nHeight %d. \n", EncodeDestination(address1).c_str(), newWinner.nBlockHeight);
} else {
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::ProcessBlock() Failed to find masternode to pay\n");
}
std::string errorMessage;
CPubKey pubKeyMasternode;
CKey keyMasternode;
if (!CMessageSigner::GetKeysFromSecret(activeMasternode.strMasterNodePrivKey, keyMasternode, pubKeyMasternode)) {
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::ProcessBlock() - Error upon calling GetKeysFromSecret.\n");
continue;
}
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::ProcessBlock() - Signing Winner\n");
if (newWinner.Sign(keyMasternode, pubKeyMasternode)) {
LogPrint(BCLog::MASTERNODE, "CMasternodePayments::ProcessBlock() - AddWinningMasternode\n");
if (AddWinningMasternode(newWinner)) {
newWinner.Relay();
nHeight = nBlockHeight;
}
}
}
nLastBlockHeight = nHeight;
}
void CMasternodePayments::Sync(CNode* node, int nCountNeeded)
{
int nHeight;
{
TRY_LOCK(cs_main, locked);
if (!locked || chainActive.Tip() == NULL) return;
nHeight = chainActive.Tip()->nHeight;
}
LOCK(cs_mapMasternodePayeeVotes);
int nCount = (mnodeman.CountEnabled() * 1.25);
if (nCountNeeded > nCount) nCountNeeded = nCount;
int nInvCount = 0;
std::map<uint256, CMasternodePaymentWinner>::iterator it = mapMasternodePayeeVotes.begin();
while (it != mapMasternodePayeeVotes.end()) {
CMasternodePaymentWinner winner = (*it).second;
if (winner.nBlockHeight >= nHeight - nCountNeeded && winner.nBlockHeight <= nHeight + 20) {
node->PushInventory(CInv(MSG_MASTERNODE_WINNER, winner.GetHash()));
nInvCount++;
}
++it;
}
g_connman->PushMessage(node, CNetMsgMaker(node->GetSendVersion()).Make(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_MNW, nInvCount));