forked from myriadcoin/myriadcoin
-
Notifications
You must be signed in to change notification settings - Fork 9
/
pow.cpp
792 lines (695 loc) · 27.2 KB
/
pow.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
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "pow.h"
#include "arith_uint256.h"
#include "chain.h"
#include "chainparams.h"
#include "primitives/block.h"
#include "uint256.h"
#include "util.h"
#include "bignum.h"
#include "main.h"
static const int64_t nTargetTimespan = 32 * 250; // Argentum: every 250 blocks
static const int64_t nTargetSpacing = 32; // Argentum: 32 sec
static const int64_t nInterval = nTargetTimespan / nTargetSpacing;
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params, int algo)
{
if (pindexLast->nHeight >= params.nMultiAlgoFork)
{
return StabilX(pindexLast, pblock, params, algo);
}
else if (pindexLast->nHeight >= params.nDGW3StartBlock)
{
return DarkGravityWave3(pindexLast, pblock, params, algo);
}
return GetNextWorkRequired_Legacy(pindexLast, pblock, params, algo);
}
unsigned int GetNextWorkRequired_Legacy(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params, int algo)
{
const arith_uint256 nProofOfWorkLimit = UintToArith256(params.powLimit);
// argentum difficulty adjustment protocol switch
int nHeight = pindexLast->nHeight + 1;
// Genesis block
if (pindexLast == NULL)
{
if(fDebug)
{
LogPrintf("pindexLast is null. returning nProofOfWorkLimit\n");
}
return nProofOfWorkLimit.GetCompact();
}
// Only change once per interval
if ((pindexLast->nHeight+1) % nInterval != 0)
{
/* // Special difficulty rule for testnet:
// if (TestNet())
//{
// If the new block's timestamp is more than 2* 10 minutes
// then allow mining of a min-difficulty block.
// if (pblock->nTime > pindexLast->nTime + nTargetSpacing*2)
// return nProofOfWorkLimit.GetCompact();
// else
{
// Return the last non-special-min-difficulty-rules-block
const CBlockIndex* pindex = pindexLast;
while (pindex->pprev && pindex->nHeight % nInterval != 0 && pindex->nBits == nProofOfWorkLimit)
pindex = pindex->pprev;
return pindex->nBits;
}
//}*/
return pindexLast->nBits;
}
// argentum: This fixes an issue where a 51% attack can change difficulty at will.
// Go back the full period unless it's the first retarget after genesis. Code courtesy of Art Forz
int blockstogoback = nInterval-1;
if ((pindexLast->nHeight+1) != nInterval)
blockstogoback = nInterval;
// Go back by what we want to be the last intervals worth of blocks
const CBlockIndex* pindexFirst = pindexLast;
for (int i = 0; pindexFirst && i < blockstogoback; i++)
pindexFirst = pindexFirst->pprev;
assert(pindexFirst);
// Limit adjustment step
int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
if(nHeight >= 111500){
if (nActualTimespan < ((nTargetTimespan*55)/73))
nActualTimespan = ((nTargetTimespan*55)/73);
if (nActualTimespan > ((nTargetTimespan*75)/60))
nActualTimespan = ((nTargetTimespan*75)/60);
}
else if(nHeight >= 79000){
if (nActualTimespan < nTargetTimespan/2)
nActualTimespan = nTargetTimespan/2;
if (nActualTimespan > nTargetTimespan*2)
nActualTimespan = nTargetTimespan*2;
}
else if(nHeight > 10000){
if (nActualTimespan < nTargetTimespan/4)
nActualTimespan = nTargetTimespan/4;
if (nActualTimespan > nTargetTimespan*4)
nActualTimespan = nTargetTimespan*4;
}
else if(nHeight > 5000){
if (nActualTimespan < nTargetTimespan/8)
nActualTimespan = nTargetTimespan/8;
if (nActualTimespan > nTargetTimespan*4)
nActualTimespan = nTargetTimespan*4;
}
else{
if (nActualTimespan < nTargetTimespan/16)
nActualTimespan = nTargetTimespan/16;
if (nActualTimespan > nTargetTimespan*4)
nActualTimespan = nTargetTimespan*4;
}
// Retarget
arith_uint256 bnNew;
arith_uint256 bnOld;
bnNew.SetCompact(pindexLast->nBits);
bnOld = bnNew;
bnNew *= nActualTimespan;
bnNew /= nTargetTimespan;
if (bnNew > nProofOfWorkLimit)
bnNew = nProofOfWorkLimit;
return bnNew.GetCompact();
}
unsigned int DarkGravityWave3(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params, int algo)
{
/* current difficulty formula, darkcoin - DarkGravity v3, written by Evan Duffield - evan@darkcoin.io */
const arith_uint256 nProofOfWorkLimit = UintToArith256(params.powLimit);
const CBlockIndex *BlockLastSolved = pindexLast;
const CBlockIndex *BlockReading = pindexLast;
const CBlockHeader *BlockCreating = pblock;
BlockCreating = BlockCreating;
int64_t nActualTimespan = 0;
int64_t LastBlockTime = 0;
int64_t PastBlocksMin = 24;
int64_t PastBlocksMax = 24;
int64_t CountBlocks = 0;
arith_uint256 PastDifficultyAverage;
arith_uint256 PastDifficultyAveragePrev;
if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || BlockLastSolved->nHeight < PastBlocksMin) {
return nProofOfWorkLimit.GetCompact();
}
for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
if (PastBlocksMax > 0 && i > PastBlocksMax) { break; }
CountBlocks++;
if(CountBlocks <= PastBlocksMin) {
if (CountBlocks == 1) { PastDifficultyAverage.SetCompact(BlockReading->nBits); }
else { PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks)+(arith_uint256().SetCompact(BlockReading->nBits))) / (CountBlocks+1); }
PastDifficultyAveragePrev = PastDifficultyAverage;
}
if(LastBlockTime > 0){
int64_t Diff = (LastBlockTime - BlockReading->GetBlockTime());
nActualTimespan += Diff;
}
LastBlockTime = BlockReading->GetBlockTime();
if (BlockReading->pprev == NULL) { assert(BlockReading); break; }
BlockReading = BlockReading->pprev;
}
arith_uint256 bnNew(PastDifficultyAverage);
int64_t nTargetTimespan = CountBlocks*nTargetSpacing;
if (nActualTimespan < nTargetTimespan/3)
nActualTimespan = nTargetTimespan/3;
if (nActualTimespan > nTargetTimespan*3)
nActualTimespan = nTargetTimespan*3;
// Retarget
bnNew *= nActualTimespan;
bnNew /= nTargetTimespan;
if (bnNew > nProofOfWorkLimit){
bnNew = nProofOfWorkLimit;
}
return bnNew.GetCompact();
}
unsigned int StabilX(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params, int algo)
{
const arith_uint256 nProofOfWorkLimit = UintToArith256(params.powLimit);
int64_t nTargetSpacingPerAlgo = params.nPowTargetSpacingV2 * NUM_ALGOS; // 90 Seconds (NUM_ALGOS * 45 seconds)
int64_t nAveragingTargetTimespan = params.nAveragingInterval * nTargetSpacingPerAlgo; // 10 * 90 = 900s, 15 minutes
int64_t nMinActualTimespan = nAveragingTargetTimespan * (100 - params.nMaxAdjustUp) / 100;
int64_t nMaxActualTimespan = nAveragingTargetTimespan * (100 + params.nMaxAdjustDown) / 100;
// Genesis block
if (pindexLast == NULL)
return nProofOfWorkLimit.GetCompact();
const CBlockIndex* pindexPrev = GetLastBlockIndexForAlgo(pindexLast, algo);
// find first block in averaging interval
// Go back by what we want to be nAveragingInterval blocks per algo
const CBlockIndex* pindexFirst = pindexLast;
for (int i = 0; pindexFirst && i < NUM_ALGOS*params.nAveragingInterval; i++)
{
pindexFirst = pindexFirst->pprev;
}
const CBlockIndex* pindexPrevAlgo = GetLastBlockIndexForAlgo(pindexLast, algo);
if (pindexPrevAlgo == NULL || pindexFirst == NULL)
{
if (fDebug)
{
LogPrintf("StabilX(Algo=%d): not enough blocks available, using default POW limit\n");
}
return nProofOfWorkLimit.GetCompact(); // not enough blocks available
}
// Limit adjustment step
// Use medians to prevent time-warp attacks
int64_t nActualTimespan = pindexLast->GetMedianTimePast() - pindexFirst->GetMedianTimePast();
nActualTimespan = nAveragingTargetTimespan + (nActualTimespan - nAveragingTargetTimespan)/6;
if (fDebug)
{
LogPrintf("StabilX(Algo=%d): nActualTimespan = %d before bounds (%d - %d)\n", algo, nActualTimespan, nMinActualTimespan, nMaxActualTimespan);
}
if (nActualTimespan < nMinActualTimespan)
nActualTimespan = nMinActualTimespan;
if (nActualTimespan > nMaxActualTimespan)
nActualTimespan = nMaxActualTimespan;
if (fDebug)
{
LogPrintf("StabilX(Algo=%d): nActualTimespan = %d after bounds (%d - %d)\n", algo, nActualTimespan, nMinActualTimespan, nMaxActualTimespan);
}
// Global retarget
arith_uint256 bnNew;
arith_uint256 bnOld;
bnNew.SetCompact(pindexPrevAlgo->nBits);
bnOld = bnNew;
bnNew *= nActualTimespan;
bnNew /= nAveragingTargetTimespan;
// Per-algo retarget
int nAdjustments = pindexPrevAlgo->nHeight - pindexLast->nHeight + NUM_ALGOS - 1;
if (nAdjustments > 0)
{
for (int i = 0; i < nAdjustments; i++)
{
bnNew *= 100;
bnNew /= (100 + params.nLocalDifficultyAdjustment);
}
}
else if (nAdjustments < 0)
{
for (int i = 0; i < -nAdjustments; i++)
{
bnNew *= (100 + params.nLocalDifficultyAdjustment);
bnNew /= 100;
}
}
if (bnNew > nProofOfWorkLimit)
{
if (fDebug)
{
LogPrintf("StabilX(Algo=%d): Adjusted target large than limit, so is now POW limit\n", algo);
}
bnNew = nProofOfWorkLimit;
}
/// debug print
if (fDebug)
{
LogPrintf("StabilX(Algo=%d): RETARGET\n", algo);
LogPrintf("StabilX(Algo=%d): nTargetTimespan = %d, nActualTimespan = %d\n", algo, nAveragingTargetTimespan, nActualTimespan);
LogPrintf("StabilX(Algo=%d): Before: %08x %s\n", algo, pindexPrev->nBits, pindexPrev->nBits, bnOld.ToString());
LogPrintf("StabilX(Algo=%d): After: %08x %s\n", algo, bnNew.GetCompact(), bnNew.ToString());
}
return bnNew.GetCompact();
}
/* unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params, int algo)
{
const arith_uint256 nProofOfWorkLimit = UintToArith256(params.powLimit);
// Genesis block
if (pindexLast == NULL)
{
if(fDebug)
{
LogPrintf("pindexLast is null. returning nProofOfWorkLimit\n");
}
return nProofOfWorkLimit.GetCompact();
}
// find previous block with same algo
const CBlockIndex* pindexPrev = GetLastBlockIndexForAlgo(pindexLast, algo);
// Genesis block
if (pindexPrev == NULL)
{
if(fDebug)
{
LogPrintf("pindexPrev is null. returning nProofOfWorkLimit\n");
}
return nProofOfWorkLimit.GetCompact();
}
const CBlockIndex* pindexFirst = NULL;
if( (pindexLast->nHeight >= params.nBlockTimeWarpPreventStart1) && (pindexLast->nHeight < params.nBlockTimeWarpPreventStart2) )
{
// find first block in averaging interval
// Go back by what we want to be nAveragingInterval blocks
pindexFirst = pindexPrev;
for (int i = 0; pindexFirst && i < params.nAveragingInterval - 1; i++)
{
pindexFirst = pindexFirst->pprev;
pindexFirst = GetLastBlockIndexForAlgo(pindexFirst, algo);
}
if (pindexFirst == NULL)
return nProofOfWorkLimit.GetCompact(); // not nAveragingInterval blocks of this algo available
// check block before first block for time warp
const CBlockIndex* pindexFirstPrev = pindexFirst->pprev;
if (pindexFirstPrev == NULL)
return nProofOfWorkLimit.GetCompact();
pindexFirstPrev = GetLastBlockIndexForAlgo(pindexFirstPrev, algo);
if (pindexFirstPrev == NULL)
return nProofOfWorkLimit.GetCompact();
// take previous block if block times are out of order
if (pindexFirstPrev->GetBlockTime() > pindexFirst->GetBlockTime())
{
LogPrintf(" First blocks out of order times, swapping: %d %d\n", pindexFirstPrev->GetBlockTime(), pindexFirst->GetBlockTime());
pindexFirst = pindexFirstPrev;
}
}
else if ( (pindexLast->nHeight >= params.nBlockTimeWarpPreventStart2) && (pindexLast->nHeight < params.nBlockTimeWarpPreventStart3) )
{
// find first block in averaging interval
// Go back by what we want to be nAveragingInterval blocks
pindexFirst = pindexPrev;
for (int i = 0; pindexFirst && i < params.nAveragingInterval - 1; i++)
{
pindexFirst = pindexFirst->pprev;
pindexFirst = GetLastBlockIndexForAlgo(pindexFirst, algo);
}
if (pindexFirst == NULL)
return nProofOfWorkLimit.GetCompact(); // not nAveragingInterval blocks of this algo available
const CBlockIndex* pindexFirstPrev;
for ( ;; )
{
// check blocks before first block for time warp
pindexFirstPrev = pindexFirst->pprev;
if (pindexFirstPrev == NULL)
return nProofOfWorkLimit.GetCompact();
pindexFirstPrev = GetLastBlockIndexForAlgo(pindexFirstPrev, algo);
if (pindexFirstPrev == NULL)
return nProofOfWorkLimit.GetCompact();
// take previous block if block times are out of order
if (pindexFirstPrev->GetBlockTime() > pindexFirst->GetBlockTime())
{
LogPrintf(" First blocks out of order times, swapping: %d %d\n", pindexFirstPrev->GetBlockTime(), pindexFirst->GetBlockTime());
pindexFirst = pindexFirstPrev;
}
else
break;
}
}
else
{
// find first block in averaging interval
// Go back by what we want to be nAveragingInterval blocks
pindexFirst = pindexPrev;
for (int i = 0; pindexFirst && i < params.nAveragingInterval - 1; i++)
{
pindexFirst = pindexFirst->pprev;
pindexFirst = GetLastBlockIndexForAlgo(pindexFirst, algo);
if (pindexFirst == NULL)
{
if(fDebug)
{
LogPrintf("pindexFirst is null. returning nProofOfWorkLimit\n");
}
return nProofOfWorkLimit.GetCompact();
}
}
}
int64_t nActualTimespan;
if (pindexLast->nHeight >= params.nBlockTimeWarpPreventStart3)
{
nActualTimespan = pindexPrev->GetMedianTimePast() - pindexFirst->GetMedianTimePast();
if(fDebug)
{
LogPrintf(" nActualTimespan = %d before bounds %d %d\n", nActualTimespan, pindexPrev->GetMedianTimePast(), pindexFirst->GetMedianTimePast());
}
}
else
{
nActualTimespan = pindexPrev->GetBlockTime() - pindexFirst->GetBlockTime();
if(fDebug)
{
LogPrintf(" nActualTimespan = %d before bounds %d %d\n", nActualTimespan, pindexPrev->GetBlockTime(), pindexFirst->GetBlockTime());
}
}
// Time warp mitigation: Don't adjust difficulty if time is negative
if ( (pindexLast->nHeight >= params.nBlockTimeWarpPreventStart1) && (pindexLast->nHeight < params.nBlockTimeWarpPreventStart2) )
{
if (nActualTimespan < 0)
{
if(fDebug)
{
LogPrintf(" nActualTimespan negative %d\n", nActualTimespan);
LogPrintf(" Keeping: %08x \n", pindexPrev->nBits);
}
return pindexPrev->nBits;
}
}
if (pindexLast->nHeight >= params.Phase2Timespan_Start)
{
return CalculateNextWorkRequiredV2(pindexPrev, pindexFirst, params, algo, nActualTimespan);
}
else
{
return StabilX(pindexPrev, pindexFirst, params, algo, nActualTimespan, pindexLast->nHeight);
}
} */
/*
unsigned int CalculateNextWorkRequiredV2(const CBlockIndex* pindexPrev, const CBlockIndex* pindexFirst, const Consensus::Params& params, int algo, int64_t nActualTimespan)
{
const arith_uint256 nProofOfWorkLimit = UintToArith256(params.powLimit);
int64_t nTargetSpacingPerAlgo = params.nPowTargetSpacingV2 * NUM_ALGOS; // 60 * 5 = 300s per algo
int64_t nAveragingTargetTimespan = params.nAveragingInterval * nTargetSpacingPerAlgo; // 10 * 300 = 3000s, 50 minutes
int64_t nMinActualTimespan = nAveragingTargetTimespan * (100 - params.nMaxAdjustUpV2) / 100;
int64_t nMaxActualTimespan = nAveragingTargetTimespan * (100 + params.nMaxAdjustDown) / 100;
if (nActualTimespan < nMinActualTimespan)
nActualTimespan = nMinActualTimespan;
if (nActualTimespan > nMaxActualTimespan)
nActualTimespan = nMaxActualTimespan;
if(fDebug)
{
LogPrintf(" nActualTimespan = %d after bounds %d %d\n", nActualTimespan, nMinActualTimespan, nMaxActualTimespan);
}
arith_uint256 bnNew;
arith_uint256 bnOld;
bnNew.SetCompact(pindexPrev->nBits);
bnOld = bnNew;
bnNew *= nActualTimespan;
bnNew /= nAveragingTargetTimespan;
if (bnNew > nProofOfWorkLimit)
bnNew = nProofOfWorkLimit;
/// debug print
if(fDebug)
{
LogPrintf("CalculateNextWorkRequiredV2(Algo=%d): RETARGET\n", algo);
LogPrintf("CalculateNextWorkRequiredV2(Algo=%d): nTargetTimespan = %d nActualTimespan = %d\n", algo, nAveragingTargetTimespan, nActualTimespan);
LogPrintf("CalculateNextWorkRequiredV2(Algo=%d): Before: %08x %s\n", algo, pindexPrev->nBits, bnOld.ToString());
LogPrintf("CalculateNextWorkRequiredV2(Algo=%d): After: %08x %s\n", algo, bnNew.GetCompact(), bnNew.ToString());
}
return bnNew.GetCompact();
}
*/
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
bool fNegative;
bool fOverflow;
arith_uint256 bnTarget;
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
LOCK(cs_main);
int nHeight = chainActive.Height();
// Check range
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
return error("CheckProofOfWork(): nBits below minimum work");
// Check proof of work matches claimed amount
if (nHeight > params.nCoinbaseMaturityV2Start){
if (UintToArith256(hash) > bnTarget)
return error("CheckProofOfWork(): hash doesn't match nBits");}
/*if (UintToArith256(hash) > bnTarget)
return error("CheckProofOfWork(): hash doesn't match nBits");*/
return true;
}
arith_uint256 GetBlockProofBase(const CBlockIndex& block)
{
arith_uint256 bnTarget;
bool fNegative;
bool fOverflow;
bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
if (fNegative || fOverflow || bnTarget == 0)
return 0;
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
// as it's too large for a arith_uint256. However, as 2**256 is at least as large
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
// or ~bnTarget / (nTarget+1) + 1.
return (~bnTarget / (bnTarget + 1)) + 1;
}
/*int GetAlgoWorkFactor(int algo)
{
const CChainParams& chainparams = Params();
if (chainActive.Height() < chainparams.GetConsensus().nMultiAlgoFork)
{
return 1;
}
switch (algo)
{
case ALGO_SHA256D:
return 1;
// work factor = absolute work ratio * optimisation factor
case ALGO_SCRYPT:
return 1024 * 4;
default:
return 1;
}
}*/
arith_uint256 GetPrevWorkForAlgo(const CBlockIndex& block, int algo)
{
const CBlockIndex* pindex = █
while (pindex != NULL)
{
if (pindex->GetAlgo() == algo)
{
return GetBlockProofBase(*pindex);
}
pindex = pindex->pprev;
}
return UintToArith256(Params().GetConsensus().powLimit);
}
/*arith_uint256 GetPrevWorkForAlgoWithDecay(const CBlockIndex& block, int algo)
{
int nDistance = 0;
arith_uint256 nWork;
const CBlockIndex* pindex = █
while (pindex != NULL)
{
if (nDistance > 32)
{
return UintToArith256(Params().GetConsensus().powLimit);
}
if (pindex->GetAlgo() == algo)
{
arith_uint256 nWork = GetBlockProofBase(*pindex);
nWork *= (32 - nDistance);
nWork /= 32;
if (nWork < UintToArith256(Params().GetConsensus().powLimit))
nWork = UintToArith256(Params().GetConsensus().powLimit);
return nWork;
}
pindex = pindex->pprev;
nDistance++;
}
return UintToArith256(Params().GetConsensus().powLimit);
}*/
/*arith_uint256 GetPrevWorkForAlgoWithDecay2(const CBlockIndex& block, int algo)
{
int nDistance = 0;
arith_uint256 nWork;
const CBlockIndex* pindex = █
while (pindex != NULL)
{
if (nDistance > 32)
{
return arith_uint256(0);
}
if (pindex->GetAlgo() == algo)
{
arith_uint256 nWork = GetBlockProofBase(*pindex);
nWork *= (32 - nDistance);
nWork /= 32;
return nWork;
}
pindex = pindex->pprev;
nDistance++;
}
return arith_uint256(0);
} */
arith_uint256 GetPrevWorkForAlgoWithDecay3(const CBlockIndex& block, int algo)
{
int nDistance = 0;
arith_uint256 nWork;
const CBlockIndex* pindex = █
while (pindex != NULL)
{
if (nDistance > 100)
{
return arith_uint256(0);
}
if (pindex->GetAlgo() == algo)
{
arith_uint256 nWork = GetBlockProofBase(*pindex);
nWork *= (100 - nDistance);
nWork /= 100;
return nWork;
}
pindex = pindex->pprev;
nDistance++;
}
return arith_uint256(0);
}
arith_uint256 GetGeometricMeanPrevWork(const CBlockIndex& block)
{
//arith_uint256 bnRes;
arith_uint256 nBlockWork = GetBlockProofBase(block);
CBigNum bnBlockWork = CBigNum(ArithToUint256(nBlockWork));
int nAlgo = block.GetAlgo();
for (int algo = 0; algo < NUM_ALGOS_IMPL; algo++)
{
if (algo != nAlgo)
{
arith_uint256 nBlockWorkAlt = GetPrevWorkForAlgoWithDecay3(block, algo);
CBigNum bnBlockWorkAlt = CBigNum(ArithToUint256(nBlockWorkAlt));
if (bnBlockWorkAlt != 0)
bnBlockWork *= bnBlockWorkAlt;
}
}
// Compute the geometric mean
CBigNum bnRes = bnBlockWork.nthRoot(NUM_ALGOS);
//return bnRes;
return UintToArith256(bnRes.getuint256());
}
arith_uint256 GetBlockProof(const CBlockIndex& block)
{
const CChainParams& chainparams = Params();
arith_uint256 bnTarget;
int nHeight = block.nHeight;
int nAlgo = block.GetAlgo();
if (nHeight >= chainparams.GetConsensus().nGeoAvgWork_Start)
{
bnTarget = GetGeometricMeanPrevWork(block);
}
else if (nHeight >= chainparams.GetConsensus().nMultiAlgoFork)
{
arith_uint256 nBlockWork = GetBlockProofBase(block);
for (int algo = 0; algo < NUM_ALGOS; algo++)
{
if (algo != nAlgo)
{
nBlockWork += GetPrevWorkForAlgo(block, algo);
}
}
bnTarget = nBlockWork / NUM_ALGOS;
}
else
{
bnTarget = GetBlockProofBase(block);
}
return bnTarget;
}
bool CheckAuxPowProofOfWork(const CBlockHeader& block, const Consensus::Params& params)
{
int algo = block.GetAlgo();
/* Except for legacy blocks with full version 1, ensure that
the chain ID is correct. Legacy blocks are not allowed since
the merge-mining start, which is checked in AcceptBlockHeader
where the height is known. */
LOCK(cs_main);
int nHeight = chainActive.Height();
if (nHeight >= params.nStartAuxPow){
if (!block.nVersion.IsLegacy() && params.fStrictChainId && block.nVersion.GetChainId() != params.nAuxpowChainId)
return error("%s : block does not have our chain ID"
" (got %d, expected %d, full nVersion %d)",
__func__,
block.nVersion.GetChainId(),
params.nAuxpowChainId,
block.nVersion.GetFullVersion());
/* If there is no auxpow, just check the block hash. */
}
if (!block.auxpow) {
if (block.nVersion.IsAuxpow())
return error("%s : no auxpow on block with auxpow version",
__func__);
if (!CheckProofOfWork(block.GetPoWHash(algo), block.nBits, params))
return error("%s : non-AUX proof of work failed", __func__);
return true;
}
/* We have auxpow. Check it. */
if (!block.nVersion.IsAuxpow())
return error("%s : auxpow on block with non-auxpow version", __func__);
if (!block.auxpow->check(block.GetHash(), block.nVersion.GetChainId(), params))
return error("%s : AUX POW is not valid", __func__);
if(fDebug)
{
bool fNegative;
bool fOverflow;
arith_uint256 bnTarget;
bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
LogPrintf("DEBUG: proof-of-work submitted \n parent-PoWhash: %s\n target: %s bits: %08x \n",
block.auxpow->getParentBlockPoWHash(algo).ToString().c_str(),
bnTarget.ToString().c_str(),
bnTarget.GetCompact());
}
if (!(algo == ALGO_SHA256D || algo == ALGO_SCRYPT) )
{
return error("%s : AUX POW is not allowed on this algo", __func__);
}
if (!CheckProofOfWork(block.auxpow->getParentBlockPoWHash(algo), block.nBits, params))
{
return error("%s : AUX proof of work failed", __func__);
}
return true;
}
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
{
arith_uint256 r;
int sign = 1;
if (to.nChainWork > from.nChainWork) {
r = to.nChainWork - from.nChainWork;
} else {
r = from.nChainWork - to.nChainWork;
sign = -1;
}
r = r * arith_uint256(params.nPowTargetSpacingV2) / GetBlockProof(tip);
if (r.bits() > 63) {
return sign * std::numeric_limits<int64_t>::max();
}
return sign * r.GetLow64();
}
const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, int algo)
{
while (pindex && pindex->pprev && (pindex->GetAlgo() != algo))
pindex = pindex->pprev;
return pindex;
}
const CBlockIndex* GetLastBlockIndexForAlgo(const CBlockIndex* pindex, int algo)
{
for (;;)
{
if (!pindex)
return NULL;
if (pindex->GetAlgo() == algo)
return pindex;
pindex = pindex->pprev;
}
}