-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgovernance-classes.cpp
832 lines (678 loc) · 29.8 KB
/
governance-classes.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
// Copyright (c) 2014-2017 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//#define ENABLE_DASH_DEBUG
#include "core_io.h"
#include "governance-classes.h"
#include "init.h"
#include "validation.h"
#include "utilstrencodings.h"
#include <boost/algorithm/string.hpp>
#include <univalue.h>
// DECLARE GLOBAL VARIABLES FOR GOVERNANCE CLASSES
CGovernanceTriggerManager triggerman;
// SPLIT UP STRING BY DELIMITER
// http://www.boost.org/doc/libs/1_58_0/doc/html/boost/algorithm/split_idp202406848.html
std::vector<std::string> SplitBy(const std::string& strCommand, const std::string& strDelimit)
{
std::vector<std::string> vParts;
boost::split(vParts, strCommand, boost::is_any_of(strDelimit));
for(int q=0; q<(int)vParts.size(); q++) {
if(strDelimit.find(vParts[q]) != std::string::npos) {
vParts.erase(vParts.begin()+q);
--q;
}
}
return vParts;
}
CAmount ParsePaymentAmount(const std::string& strAmount)
{
DBG( std::cout << "ParsePaymentAmount Start: strAmount = " << strAmount << std::endl; );
CAmount nAmount = 0;
if (strAmount.empty()) {
std::ostringstream ostr;
ostr << "ParsePaymentAmount: Amount is empty";
throw std::runtime_error(ostr.str());
}
if(strAmount.size() > 20) {
// String is much too long, the functions below impose stricter
// requirements
std::ostringstream ostr;
ostr << "ParsePaymentAmount: Amount string too long";
throw std::runtime_error(ostr.str());
}
// Make sure the string makes sense as an amount
// Note: No spaces allowed
// Also note: No scientific notation
size_t pos = strAmount.find_first_not_of("0123456789.");
if (pos != std::string::npos) {
std::ostringstream ostr;
ostr << "ParsePaymentAmount: Amount string contains invalid character";
throw std::runtime_error(ostr.str());
}
pos = strAmount.find(".");
if (pos == 0) {
// JSON doesn't allow values to start with a decimal point
std::ostringstream ostr;
ostr << "ParsePaymentAmount: Invalid amount string, leading decimal point not allowed";
throw std::runtime_error(ostr.str());
}
// Make sure there's no more than 1 decimal point
if ((pos != std::string::npos) && (strAmount.find(".", pos+1) != std::string::npos)) {
std::ostringstream ostr;
ostr << "ParsePaymentAmount: Invalid amount string, too many decimal points";
throw std::runtime_error(ostr.str());
}
// Note this code is taken from AmountFromValue in rpcserver.cpp
// which is used for parsing the amounts in createrawtransaction.
if (!ParseFixedPoint(strAmount, 8, &nAmount)) {
nAmount = 0;
std::ostringstream ostr;
ostr << "ParsePaymentAmount: ParseFixedPoint failed for string: " << strAmount;
throw std::runtime_error(ostr.str());
}
if (!MoneyRange(nAmount)) {
nAmount = 0;
std::ostringstream ostr;
ostr << "ParsePaymentAmount: Invalid amount string, value outside of valid money range";
throw std::runtime_error(ostr.str());
}
DBG( std::cout << "ParsePaymentAmount Returning true nAmount = " << nAmount << std::endl; );
return nAmount;
}
/**
* Add Governance Object
*/
bool CGovernanceTriggerManager::AddNewTrigger(uint256 nHash)
{
DBG( std::cout << "CGovernanceTriggerManager::AddNewTrigger: Start" << std::endl; );
AssertLockHeld(governance.cs);
// IF WE ALREADY HAVE THIS HASH, RETURN
if(mapTrigger.count(nHash)) {
DBG(
std::cout << "CGovernanceTriggerManager::AddNewTrigger: Already have hash"
<< ", nHash = " << nHash.GetHex()
<< ", count = " << mapTrigger.count(nHash)
<< ", mapTrigger.size() = " << mapTrigger.size()
<< std::endl; );
return false;
}
CSuperblock_sptr pSuperblock;
try {
CSuperblock_sptr pSuperblockTmp(new CSuperblock(nHash));
pSuperblock = pSuperblockTmp;
}
catch(std::exception& e) {
DBG( std::cout << "CGovernanceTriggerManager::AddNewTrigger Error creating superblock"
<< ", e.what() = " << e.what()
<< std::endl; );
LogPrintf("CGovernanceTriggerManager::AddNewTrigger -- Error creating superblock: %s\n", e.what());
return false;
}
catch(...) {
LogPrintf("CGovernanceTriggerManager::AddNewTrigger: Unknown Error creating superblock\n");
DBG( std::cout << "CGovernanceTriggerManager::AddNewTrigger Error creating superblock catchall" << std::endl; );
return false;
}
pSuperblock->SetStatus(SEEN_OBJECT_IS_VALID);
DBG( std::cout << "CGovernanceTriggerManager::AddNewTrigger: Inserting trigger" << std::endl; );
mapTrigger.insert(std::make_pair(nHash, pSuperblock));
DBG( std::cout << "CGovernanceTriggerManager::AddNewTrigger: End" << std::endl; );
return true;
}
/**
*
* Clean And Remove
*
*/
void CGovernanceTriggerManager::CleanAndRemove()
{
LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Start\n");
DBG( std::cout << "CGovernanceTriggerManager::CleanAndRemove: Start" << std::endl; );
AssertLockHeld(governance.cs);
// Remove triggers that are invalid or expired
DBG( std::cout << "CGovernanceTriggerManager::CleanAndRemove: mapTrigger.size() = " << mapTrigger.size() << std::endl; );
LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- mapTrigger.size() = %d\n", mapTrigger.size());
trigger_m_it it = mapTrigger.begin();
while(it != mapTrigger.end()) {
bool remove = false;
CGovernanceObject* pObj = nullptr;
CSuperblock_sptr& pSuperblock = it->second;
if(!pSuperblock) {
DBG( std::cout << "CGovernanceTriggerManager::CleanAndRemove: NULL superblock marked for removal" << std::endl; );
LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- NULL superblock marked for removal\n");
remove = true;
} else {
pObj = governance.FindGovernanceObject(it->first);
if(!pObj || pObj->GetObjectType() != GOVERNANCE_OBJECT_TRIGGER) {
DBG( std::cout << "CGovernanceTriggerManager::CleanAndRemove: Unknown or non-trigger superblock" << std::endl; );
LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Unknown or non-trigger superblock\n");
pSuperblock->SetStatus(SEEN_OBJECT_ERROR_INVALID);
}
DBG( std::cout << "CGovernanceTriggerManager::CleanAndRemove: superblock status = " << pSuperblock->GetStatus() << std::endl; );
LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- superblock status = %d\n", pSuperblock->GetStatus());
switch(pSuperblock->GetStatus()) {
case SEEN_OBJECT_ERROR_INVALID:
case SEEN_OBJECT_UNKNOWN:
LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Unknown or invalid trigger found\n");
remove = true;
break;
case SEEN_OBJECT_IS_VALID:
case SEEN_OBJECT_EXECUTED:
remove = pSuperblock->IsExpired();
break;
default:
break;
}
}
LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- %smarked for removal\n", remove ? "" : "NOT ");
if(remove) {
DBG(
std::string strDataAsPlainString = "NULL";
if(pObj) {
strDataAsPlainString = pObj->GetDataAsPlainString();
}
std::cout << "CGovernanceTriggerManager::CleanAndRemove: Removing object: "
<< strDataAsPlainString
<< std::endl;
);
LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Removing trigger object\n");
// mark corresponding object for deletion
if (pObj) {
pObj->fCachedDelete = true;
if (pObj->nDeletionTime == 0) {
pObj->nDeletionTime = GetAdjustedTime();
}
}
// delete the trigger
mapTrigger.erase(it++);
}
else {
++it;
}
}
DBG( std::cout << "CGovernanceTriggerManager::CleanAndRemove: End" << std::endl; );
}
/**
* Get Active Triggers
*
* - Look through triggers and scan for active ones
* - Return the triggers in a list
*/
std::vector<CSuperblock_sptr> CGovernanceTriggerManager::GetActiveTriggers()
{
AssertLockHeld(governance.cs);
std::vector<CSuperblock_sptr> vecResults;
DBG( std::cout << "GetActiveTriggers: mapTrigger.size() = " << mapTrigger.size() << std::endl; );
// LOOK AT THESE OBJECTS AND COMPILE A VALID LIST OF TRIGGERS
trigger_m_it it = mapTrigger.begin();
while(it != mapTrigger.end()) {
CGovernanceObject* pObj = governance.FindGovernanceObject((*it).first);
if(pObj) {
DBG( std::cout << "GetActiveTriggers: pObj->GetDataAsPlainString() = " << pObj->GetDataAsPlainString() << std::endl; );
vecResults.push_back(it->second);
}
++it;
}
DBG( std::cout << "GetActiveTriggers: vecResults.size() = " << vecResults.size() << std::endl; );
return vecResults;
}
/**
* Is Superblock Triggered
*
* - Does this block have a non-executed and actived trigger?
*/
bool CSuperblockManager::IsSuperblockTriggered(int nBlockHeight)
{
LogPrint("gobject", "CSuperblockManager::IsSuperblockTriggered -- Start nBlockHeight = %d\n", nBlockHeight);
if (!CSuperblock::IsValidBlockHeight(nBlockHeight)) {
return false;
}
LOCK(governance.cs);
// GET ALL ACTIVE TRIGGERS
std::vector<CSuperblock_sptr> vecTriggers = triggerman.GetActiveTriggers();
LogPrint("gobject", "CSuperblockManager::IsSuperblockTriggered -- vecTriggers.size() = %d\n", vecTriggers.size());
DBG( std::cout << "IsSuperblockTriggered Number triggers = " << vecTriggers.size() << std::endl; );
for (const auto& pSuperblock : vecTriggers)
{
if(!pSuperblock) {
LogPrintf("CSuperblockManager::IsSuperblockTriggered -- Non-superblock found, continuing\n");
DBG( std::cout << "IsSuperblockTriggered Not a superblock, continuing " << std::endl; );
continue;
}
CGovernanceObject* pObj = pSuperblock->GetGovernanceObject();
if(!pObj) {
LogPrintf("CSuperblockManager::IsSuperblockTriggered -- pObj == NULL, continuing\n");
DBG( std::cout << "IsSuperblockTriggered pObj is NULL, continuing" << std::endl; );
continue;
}
LogPrint("gobject", "CSuperblockManager::IsSuperblockTriggered -- data = %s\n", pObj->GetDataAsPlainString());
// note : 12.1 - is epoch calculation correct?
if(nBlockHeight != pSuperblock->GetBlockHeight()) {
LogPrint("gobject", "CSuperblockManager::IsSuperblockTriggered -- block height doesn't match nBlockHeight = %d, blockStart = %d, continuing\n",
nBlockHeight,
pSuperblock->GetBlockHeight());
DBG( std::cout << "IsSuperblockTriggered Not the target block, continuing"
<< ", nBlockHeight = " << nBlockHeight
<< ", superblock->GetBlockHeight() = " << pSuperblock->GetBlockHeight()
<< std::endl; );
continue;
}
// MAKE SURE THIS TRIGGER IS ACTIVE VIA FUNDING CACHE FLAG
pObj->UpdateSentinelVariables();
if(pObj->IsSetCachedFunding()) {
LogPrint("gobject", "CSuperblockManager::IsSuperblockTriggered -- fCacheFunding = true, returning true\n");
DBG( std::cout << "IsSuperblockTriggered returning true" << std::endl; );
return true;
}
else {
LogPrint("gobject", "CSuperblockManager::IsSuperblockTriggered -- fCacheFunding = false, continuing\n");
DBG( std::cout << "IsSuperblockTriggered No fCachedFunding, continuing" << std::endl; );
}
}
return false;
}
bool CSuperblockManager::GetBestSuperblock(CSuperblock_sptr& pSuperblockRet, int nBlockHeight)
{
if(!CSuperblock::IsValidBlockHeight(nBlockHeight)) {
return false;
}
AssertLockHeld(governance.cs);
std::vector<CSuperblock_sptr> vecTriggers = triggerman.GetActiveTriggers();
int nYesCount = 0;
for (const auto& pSuperblock : vecTriggers) {
if(!pSuperblock) {
DBG( std::cout << "GetBestSuperblock Not a superblock, continuing" << std::endl; );
continue;
}
CGovernanceObject* pObj = pSuperblock->GetGovernanceObject();
if(!pObj) {
DBG( std::cout << "GetBestSuperblock pObj is NULL, continuing" << std::endl; );
continue;
}
if(nBlockHeight != pSuperblock->GetBlockHeight()) {
DBG( std::cout << "GetBestSuperblock Not the target block, continuing" << std::endl; );
continue;
}
// DO WE HAVE A NEW WINNER?
int nTempYesCount = pObj->GetAbsoluteYesCount(VOTE_SIGNAL_FUNDING);
DBG( std::cout << "GetBestSuperblock nTempYesCount = " << nTempYesCount << std::endl; );
if(nTempYesCount > nYesCount) {
nYesCount = nTempYesCount;
pSuperblockRet = pSuperblock;
DBG( std::cout << "GetBestSuperblock Valid superblock found, pSuperblock set" << std::endl; );
}
}
return nYesCount > 0;
}
/**
* Create Superblock Payments
*
* - Create the correct payment structure for a given superblock
*/
void CSuperblockManager::CreateSuperblock(CMutableTransaction& txNewRet, int nBlockHeight, std::vector<CTxOut>& voutSuperblockRet)
{
DBG( std::cout << "CSuperblockManager::CreateSuperblock Start" << std::endl; );
LOCK(governance.cs);
// GET THE BEST SUPERBLOCK FOR THIS BLOCK HEIGHT
CSuperblock_sptr pSuperblock;
if(!CSuperblockManager::GetBestSuperblock(pSuperblock, nBlockHeight)) {
LogPrint("gobject", "CSuperblockManager::CreateSuperblock -- Can't find superblock for height %d\n", nBlockHeight);
DBG( std::cout << "CSuperblockManager::CreateSuperblock Failed to get superblock for height, returning" << std::endl; );
return;
}
// make sure it's empty, just in case
voutSuperblockRet.clear();
// CONFIGURE SUPERBLOCK OUTPUTS
// Superblock payments are appended to the end of the coinbase vout vector
DBG( std::cout << "CSuperblockManager::CreateSuperblock Number payments: " << pSuperblock->CountPayments() << std::endl; );
// TODO: How many payments can we add before things blow up?
// Consider at least following limits:
// - max coinbase tx size
// - max "budget" available
for(int i = 0; i < pSuperblock->CountPayments(); i++) {
CGovernancePayment payment;
DBG( std::cout << "CSuperblockManager::CreateSuperblock i = " << i << std::endl; );
if(pSuperblock->GetPayment(i, payment)) {
DBG( std::cout << "CSuperblockManager::CreateSuperblock Payment found " << std::endl; );
// SET COINBASE OUTPUT TO SUPERBLOCK SETTING
CTxOut txout = CTxOut(payment.nAmount, payment.script);
txNewRet.vout.push_back(txout);
voutSuperblockRet.push_back(txout);
// PRINT NICE LOG OUTPUT FOR SUPERBLOCK PAYMENT
CTxDestination address1;
ExtractDestination(payment.script, address1);
CBitcoinAddress address2(address1);
// TODO: PRINT NICE N.N DASH OUTPUT
DBG( std::cout << "CSuperblockManager::CreateSuperblock Before LogPrintf call, nAmount = " << payment.nAmount << std::endl; );
LogPrintf("NEW Superblock : output %d (addr %s, amount %d)\n", i, address2.ToString(), payment.nAmount);
DBG( std::cout << "CSuperblockManager::CreateSuperblock After LogPrintf call " << std::endl; );
} else {
DBG( std::cout << "CSuperblockManager::CreateSuperblock Payment not found " << std::endl; );
}
}
DBG( std::cout << "CSuperblockManager::CreateSuperblock End" << std::endl; );
}
bool CSuperblockManager::IsValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward)
{
// GET BEST SUPERBLOCK, SHOULD MATCH
LOCK(governance.cs);
CSuperblock_sptr pSuperblock;
if(CSuperblockManager::GetBestSuperblock(pSuperblock, nBlockHeight)) {
return pSuperblock->IsValid(txNew, nBlockHeight, blockReward);
}
return false;
}
void CSuperblockManager::ExecuteBestSuperblock(int nBlockHeight)
{
LOCK(governance.cs);
CSuperblock_sptr pSuperblock;
if(GetBestSuperblock(pSuperblock, nBlockHeight)) {
// All checks are done in CSuperblock::IsValid via IsBlockValueValid and IsBlockPayeeValid,
// tip wouldn't be updated if anything was wrong. Mark this trigger as executed.
pSuperblock->SetExecuted();
}
}
CSuperblock::
CSuperblock()
: nGovObjHash(),
nBlockHeight(0),
nStatus(SEEN_OBJECT_UNKNOWN),
vecPayments()
{}
CSuperblock::
CSuperblock(uint256& nHash)
: nGovObjHash(nHash),
nBlockHeight(0),
nStatus(SEEN_OBJECT_UNKNOWN),
vecPayments()
{
DBG( std::cout << "CSuperblock Constructor Start" << std::endl; );
CGovernanceObject* pGovObj = GetGovernanceObject();
if(!pGovObj) {
DBG( std::cout << "CSuperblock Constructor pGovObjIn is NULL, returning" << std::endl; );
throw std::runtime_error("CSuperblock: Failed to find Governance Object");
}
DBG( std::cout << "CSuperblock Constructor pGovObj : "
<< pGovObj->GetDataAsPlainString()
<< ", nObjectType = " << pGovObj->GetObjectType()
<< std::endl; );
if (pGovObj->GetObjectType() != GOVERNANCE_OBJECT_TRIGGER) {
DBG( std::cout << "CSuperblock Constructor pGovObj not a trigger, returning" << std::endl; );
throw std::runtime_error("CSuperblock: Governance Object not a trigger");
}
UniValue obj = pGovObj->GetJSONObject();
// FIRST WE GET THE START HEIGHT, THE BLOCK HEIGHT AT WHICH THE PAYMENT SHALL OCCUR
nBlockHeight = obj["event_block_height"].get_int();
// NEXT WE GET THE PAYMENT INFORMATION AND RECONSTRUCT THE PAYMENT VECTOR
std::string strAddresses = obj["payment_addresses"].get_str();
std::string strAmounts = obj["payment_amounts"].get_str();
ParsePaymentSchedule(strAddresses, strAmounts);
LogPrint("gobject", "CSuperblock -- nBlockHeight = %d, strAddresses = %s, strAmounts = %s, vecPayments.size() = %d\n",
nBlockHeight, strAddresses, strAmounts, vecPayments.size());
DBG( std::cout << "CSuperblock Constructor End" << std::endl; );
}
/**
* Is Valid Superblock Height
*
* - See if a block at this height can be a superblock
*/
bool CSuperblock::IsValidBlockHeight(int nBlockHeight)
{
// SUPERBLOCKS CAN HAPPEN ONLY after hardfork and only ONCE PER CYCLE
return nBlockHeight >= Params().GetConsensus().nSuperblockStartBlock &&
((nBlockHeight % Params().GetConsensus().nSuperblockCycle) == 0);
}
void CSuperblock::GetNearestSuperblocksHeights(int nBlockHeight, int& nLastSuperblockRet, int& nNextSuperblockRet)
{
const Consensus::Params& consensusParams = Params().GetConsensus();
int nSuperblockStartBlock = consensusParams.nSuperblockStartBlock;
int nSuperblockCycle = consensusParams.nSuperblockCycle;
// Get first superblock
int nFirstSuperblockOffset = (nSuperblockCycle - nSuperblockStartBlock % nSuperblockCycle) % nSuperblockCycle;
int nFirstSuperblock = nSuperblockStartBlock + nFirstSuperblockOffset;
if(nBlockHeight < nFirstSuperblock) {
nLastSuperblockRet = 0;
nNextSuperblockRet = nFirstSuperblock;
} else {
nLastSuperblockRet = nBlockHeight - nBlockHeight % nSuperblockCycle;
nNextSuperblockRet = nLastSuperblockRet + nSuperblockCycle;
}
}
CAmount CSuperblock::GetPaymentsLimit(int nBlockHeight)
{
const Consensus::Params& consensusParams = Params().GetConsensus();
if(!IsValidBlockHeight(nBlockHeight)) {
return 0;
}
// min subsidy for high diff networks and vice versa
int nBits = consensusParams.fPowAllowMinDifficultyBlocks ? UintToArith256(consensusParams.powLimit).GetCompact() : 1;
// some part of all blocks issued during the cycle goes to superblock, see GetBlockSubsidy
CAmount nSuperblockPartOfSubsidy = GetBlockSubsidy(nBits, nBlockHeight - 1, consensusParams, true);
CAmount nPaymentsLimit = nSuperblockPartOfSubsidy * consensusParams.nSuperblockCycle;
LogPrint("gobject", "CSuperblock::GetPaymentsLimit -- Valid superblock height %d, payments max %lld\n", nBlockHeight, nPaymentsLimit);
return nPaymentsLimit;
}
void CSuperblock::ParsePaymentSchedule(const std::string& strPaymentAddresses, const std::string& strPaymentAmounts)
{
// SPLIT UP ADDR/AMOUNT STRINGS AND PUT IN VECTORS
std::vector<std::string> vecParsed1;
std::vector<std::string> vecParsed2;
vecParsed1 = SplitBy(strPaymentAddresses, "|");
vecParsed2 = SplitBy(strPaymentAmounts, "|");
// IF THESE DONT MATCH, SOMETHING IS WRONG
if (vecParsed1.size() != vecParsed2.size()) {
std::ostringstream ostr;
ostr << "CSuperblock::ParsePaymentSchedule -- Mismatched payments and amounts";
LogPrintf("%s\n", ostr.str());
throw std::runtime_error(ostr.str());
}
if (vecParsed1.size() == 0) {
std::ostringstream ostr;
ostr << "CSuperblock::ParsePaymentSchedule -- Error no payments";
LogPrintf("%s\n", ostr.str());
throw std::runtime_error(ostr.str());
}
// LOOP THROUGH THE ADDRESSES/AMOUNTS AND CREATE PAYMENTS
/*
ADDRESSES = [ADDR1|2|3|4|5|6]
AMOUNTS = [AMOUNT1|2|3|4|5|6]
*/
DBG( std::cout << "CSuperblock::ParsePaymentSchedule vecParsed1.size() = " << vecParsed1.size() << std::endl; );
for (int i = 0; i < (int)vecParsed1.size(); i++) {
CBitcoinAddress address(vecParsed1[i]);
if (!address.IsValid()) {
std::ostringstream ostr;
ostr << "CSuperblock::ParsePaymentSchedule -- Invalid Dash Address : " << vecParsed1[i];
LogPrintf("%s\n", ostr.str());
throw std::runtime_error(ostr.str());
}
/*
TODO
- There might be an issue with multisig in the coinbase on mainnet, we will add support for it in a future release.
- Post 12.3+ (test multisig coinbase transaction)
*/
if(address.IsScript()) {
std::ostringstream ostr;
ostr << "CSuperblock::ParsePaymentSchedule -- Script addresses are not supported yet : " << vecParsed1[i];
LogPrintf("%s\n", ostr.str());
throw std::runtime_error(ostr.str());
}
DBG( std::cout << "CSuperblock::ParsePaymentSchedule i = " << i
<< ", vecParsed2[i] = " << vecParsed2[i]
<< std::endl; );
CAmount nAmount = ParsePaymentAmount(vecParsed2[i]);
DBG( std::cout << "CSuperblock::ParsePaymentSchedule: "
<< "amount string = " << vecParsed2[i]
<< ", nAmount = " << nAmount
<< std::endl; );
CGovernancePayment payment(address, nAmount);
if(payment.IsValid()) {
vecPayments.push_back(payment);
}
else {
vecPayments.clear();
std::ostringstream ostr;
ostr << "CSuperblock::ParsePaymentSchedule -- Invalid payment found: address = " << address.ToString()
<< ", amount = " << nAmount;
LogPrintf("%s\n", ostr.str());
throw std::runtime_error(ostr.str());
}
}
}
bool CSuperblock::GetPayment(int nPaymentIndex, CGovernancePayment& paymentRet)
{
if((nPaymentIndex<0) || (nPaymentIndex >= (int)vecPayments.size())) {
return false;
}
paymentRet = vecPayments[nPaymentIndex];
return true;
}
CAmount CSuperblock::GetPaymentsTotalAmount()
{
CAmount nPaymentsTotalAmount = 0;
int nPayments = CountPayments();
for(int i = 0; i < nPayments; i++) {
nPaymentsTotalAmount += vecPayments[i].nAmount;
}
return nPaymentsTotalAmount;
}
/**
* Is Transaction Valid
*
* - Does this transaction match the superblock?
*/
bool CSuperblock::IsValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward)
{
// TODO : LOCK(cs);
// No reason for a lock here now since this method only accesses data
// internal to *this and since CSuperblock's are accessed only through
// shared pointers there's no way our object can get deleted while this
// code is running.
if(!IsValidBlockHeight(nBlockHeight)) {
LogPrintf("CSuperblock::IsValid -- ERROR: Block invalid, incorrect block height\n");
return false;
}
std::string strPayeesPossible = "";
// CONFIGURE SUPERBLOCK OUTPUTS
int nOutputs = txNew.vout.size();
int nPayments = CountPayments();
int nMinerPayments = nOutputs - nPayments;
LogPrint("gobject", "CSuperblock::IsValid nOutputs = %d, nPayments = %d, GetDataAsHexString = %s\n",
nOutputs, nPayments, GetGovernanceObject()->GetDataAsHexString());
// We require an exact match (including order) between the expected
// superblock payments and the payments actually in the block.
if(nMinerPayments < 0) {
// This means the block cannot have all the superblock payments
// so it is not valid.
// TODO: could that be that we just hit coinbase size limit?
LogPrintf("CSuperblock::IsValid -- ERROR: Block invalid, too few superblock payments\n");
return false;
}
// payments should not exceed limit
CAmount nPaymentsTotalAmount = GetPaymentsTotalAmount();
CAmount nPaymentsLimit = GetPaymentsLimit(nBlockHeight);
if(nPaymentsTotalAmount > nPaymentsLimit) {
LogPrintf("CSuperblock::IsValid -- ERROR: Block invalid, payments limit exceeded: payments %lld, limit %lld\n", nPaymentsTotalAmount, nPaymentsLimit);
return false;
}
// miner should not get more than he would usually get
CAmount nBlockValue = txNew.GetValueOut();
if(nBlockValue > blockReward + nPaymentsTotalAmount) {
LogPrintf("CSuperblock::IsValid -- ERROR: Block invalid, block value limit exceeded: block %lld, limit %lld\n", nBlockValue, blockReward + nPaymentsTotalAmount);
return false;
}
int nVoutIndex = 0;
for(int i = 0; i < nPayments; i++) {
CGovernancePayment payment;
if(!GetPayment(i, payment)) {
// This shouldn't happen so log a warning
LogPrintf("CSuperblock::IsValid -- WARNING: Failed to find payment: %d of %d total payments\n", i, nPayments);
continue;
}
bool fPaymentMatch = false;
for (int j = nVoutIndex; j < nOutputs; j++) {
// Find superblock payment
fPaymentMatch = ((payment.script == txNew.vout[j].scriptPubKey) &&
(payment.nAmount == txNew.vout[j].nValue));
if (fPaymentMatch) {
nVoutIndex = j;
break;
}
}
if(!fPaymentMatch) {
// Superblock payment not found!
CTxDestination address1;
ExtractDestination(payment.script, address1);
CBitcoinAddress address2(address1);
LogPrintf("CSuperblock::IsValid -- ERROR: Block invalid: %d payment %d to %s not found\n", i, payment.nAmount, address2.ToString());
return false;
}
}
return true;
}
bool CSuperblock::IsExpired()
{
bool fExpired{false};
int nExpirationBlocks{0};
// Executed triggers are kept for another superblock cycle (approximately 1 month),
// other valid triggers are kept for ~1 day only, everything else is pruned after ~1h.
switch (nStatus) {
case SEEN_OBJECT_EXECUTED:
nExpirationBlocks = Params().GetConsensus().nSuperblockCycle;
break;
case SEEN_OBJECT_IS_VALID:
nExpirationBlocks = 576;
break;
default:
nExpirationBlocks = 24;
break;
}
int nExpirationBlock = nBlockHeight + nExpirationBlocks;
LogPrint("gobject", "CSuperblock::IsExpired -- nBlockHeight = %d, nExpirationBlock = %d\n", nBlockHeight, nExpirationBlock);
if(governance.GetCachedBlockHeight() > nExpirationBlock) {
LogPrint("gobject", "CSuperblock::IsExpired -- Outdated trigger found\n");
fExpired = true;
CGovernanceObject* pgovobj = GetGovernanceObject();
if(pgovobj) {
LogPrint("gobject", "CSuperblock::IsExpired -- Expiring outdated object: %s\n", pgovobj->GetHash().ToString());
pgovobj->fExpired = true;
pgovobj->nDeletionTime = GetAdjustedTime();
}
}
return fExpired;
}
/**
* Get Required Payment String
*
* - Get a string representing the payments required for a given superblock
*/
std::string CSuperblockManager::GetRequiredPaymentsString(int nBlockHeight)
{
LOCK(governance.cs);
std::string ret = "Unknown";
// GET BEST SUPERBLOCK
CSuperblock_sptr pSuperblock;
if(!GetBestSuperblock(pSuperblock, nBlockHeight)) {
LogPrint("gobject", "CSuperblockManager::GetRequiredPaymentsString -- Can't find superblock for height %d\n", nBlockHeight);
return "error";
}
// LOOP THROUGH SUPERBLOCK PAYMENTS, CONFIGURE OUTPUT STRING
for(int i = 0; i < pSuperblock->CountPayments(); i++) {
CGovernancePayment payment;
if(pSuperblock->GetPayment(i, payment)) {
// PRINT NICE LOG OUTPUT FOR SUPERBLOCK PAYMENT
CTxDestination address1;
ExtractDestination(payment.script, address1);
CBitcoinAddress address2(address1);
// RETURN NICE OUTPUT FOR CONSOLE
if(ret != "Unknown") {
ret += ", " + address2.ToString();
}
else {
ret = address2.ToString();
}
}
}
return ret;
}