Repository navigation
Expand file tree
/
Copy pathpow.cpp
More file actions
428 lines (361 loc) · 15.6 KB
/
Copy pathpow.cpp
File metadata and controls
428 lines (361 loc) · 15.6 KB
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
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2022 The Bitcoin Core developers
// Copyright (c) 2014-2026 The DigiByte 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 <logging.h>
#include <arith_uint256.h>
#include <chain.h>
#include <primitives/block.h>
#include <uint256.h>
#include <chainparams.h>
inline unsigned int PowLimit(const Consensus::Params& params)
{
return UintToArith256(params.powLimit).GetCompact();
}
unsigned int InitialDifficulty(const Consensus::Params& params, int algo)
{
const auto& it = params.initialTarget.find(algo);
if (it == params.initialTarget.end())
return PowLimit(params);
return UintToArith256(it->second).GetCompact();
}
unsigned int GetNextWorkRequiredV1(const CBlockIndex* pindexLast, const Consensus::Params& params, int algo)
{
int nHeight = pindexLast->nHeight + 1;
bool fNewDifficultyProtocol = (nHeight >= params.nDiffChangeTarget);
int blockstogoback = 0;
//set default to pre-v2.0 values
int64_t retargetTimespan = params.nTargetTimespan;
//int64_t retargetSpacing = nTargetSpacing;
int64_t retargetInterval = params.nInterval;
//if v2.0 changes are in effect for block num, alter retarget values
if(fNewDifficultyProtocol && !params.fPowAllowMinDifficultyBlocks) {
LogPrintf("GetNextWorkRequired nActualTimespan Limiting\n");
retargetTimespan = params.nTargetTimespanRe;
//retargetSpacing = nTargetSpacingRe;
retargetInterval = params.nIntervalRe;
}
// Only change once per interval
if ((pindexLast->nHeight+1) % retargetInterval != 0)
{
return pindexLast->nBits;
}
// DigiByte: 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
blockstogoback = retargetInterval-1;
if ((pindexLast->nHeight+1) != retargetInterval)
blockstogoback = retargetInterval;
// Go back by what we want to be 14 days 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();
// thanks to RealSolid & WDC for this code
if(fNewDifficultyProtocol && !params.fPowAllowMinDifficultyBlocks) {
if (nActualTimespan < (retargetTimespan - (retargetTimespan/4)) ) nActualTimespan = (retargetTimespan - (retargetTimespan/4));
if (nActualTimespan > (retargetTimespan + (retargetTimespan/2)) ) nActualTimespan = (retargetTimespan + (retargetTimespan/2));
}
else {
if (nActualTimespan < retargetTimespan/4) nActualTimespan = retargetTimespan/4;
if (nActualTimespan > retargetTimespan*4) nActualTimespan = retargetTimespan*4;
}
arith_uint256 bnNew;
arith_uint256 bnBefore;
bnNew.SetCompact(pindexLast->nBits);
bnBefore=bnNew;
bnNew *= nActualTimespan;
bnNew /= retargetTimespan;
if (bnNew > UintToArith256(params.powLimit))
bnNew = UintToArith256(params.powLimit);
// debug print
LogPrintf("nTargetTimespan = %d nActualTimespan = %d\n", retargetTimespan, nActualTimespan);
LogPrintf("Before: %08x %s\n", pindexLast->nBits, ArithToUint256(bnBefore).ToString());
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), ArithToUint256(bnNew).ToString());
return bnNew.GetCompact();
}
unsigned int GetNextWorkRequiredV2(const CBlockIndex* pindexLast, const Consensus::Params& params, int algo)
{
LogPrintf("Height (Before): %s\n", pindexLast->nHeight);
// find previous block with same algo
const CBlockIndex* pindexPrev = GetLastBlockIndexForAlgo(pindexLast, params, algo);
// find first block in averaging interval
// Go back by what we want to be nAveragingInterval blocks
const CBlockIndex* pindexFirst = pindexPrev;
for (int i = 0; pindexFirst && i < params.nAveragingInterval - 1; i++)
{
pindexFirst = pindexFirst->pprev;
pindexFirst = GetLastBlockIndexForAlgo(pindexFirst, params, algo);
}
if (pindexFirst == nullptr)
{
LogPrintf("Use default POW Limit\n");
return InitialDifficulty(params, algo);
}
// Limit adjustment step
int64_t nActualTimespan = pindexPrev->GetBlockTime() - pindexFirst->GetBlockTime();
if (nActualTimespan < params.nMinActualTimespan)
nActualTimespan = params.nMinActualTimespan;
if (nActualTimespan > params.nMaxActualTimespan)
nActualTimespan = params.nMaxActualTimespan;
// Retarget
arith_uint256 bnNew;
bnNew.SetCompact(pindexPrev->nBits);
bnNew *= nActualTimespan;
bnNew /= params.nAveragingTargetTimespan;
if (bnNew > UintToArith256(params.powLimit))
{
bnNew = UintToArith256(params.powLimit);
}
return bnNew.GetCompact();
}
static unsigned int GetNextWorkRequiredV3(const CBlockIndex* pindexLast, const Consensus::Params& params, int algo, const CBlockIndex* pindexPrevAlgo)
{
// 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;
}
if (pindexPrevAlgo == nullptr || pindexFirst == nullptr)
return InitialDifficulty(params, algo); // not enough blocks available
// Limit adjustment step
// Use medians to prevent time-warp attacks
int64_t nActualTimespan = pindexLast->GetMedianTimePast() - pindexFirst->GetMedianTimePast();
nActualTimespan = params.nAveragingTargetTimespan + (nActualTimespan - params.nAveragingTargetTimespan)/6;
if (nActualTimespan < params.nMinActualTimespanV3)
nActualTimespan = params.nMinActualTimespanV3;
if (nActualTimespan > params.nMaxActualTimespanV3)
nActualTimespan = params.nMaxActualTimespanV3;
// Global retarget
arith_uint256 bnNew;
bnNew.SetCompact(pindexPrevAlgo->nBits);
bnNew *= nActualTimespan;
bnNew /= params.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;
}
}
if (nAdjustments < 0)
{
for (int i = 0; i < -nAdjustments; i++)
{
bnNew *= 100 + params.nLocalDifficultyAdjustment;
bnNew /= 100;
}
}
if (bnNew > UintToArith256(params.powLimit))
bnNew = UintToArith256(params.powLimit);
return bnNew.GetCompact();
}
static unsigned int GetNextWorkRequiredV4(const CBlockIndex* pindexLast, const Consensus::Params& params, int algo, const CBlockIndex* pindexPrevAlgo)
{
// 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;
}
if (pindexPrevAlgo == nullptr || pindexFirst == nullptr)
{
return InitialDifficulty(params, algo);
}
// Limit adjustment step
// Use medians to prevent time-warp attacks
int64_t nActualTimespan = pindexLast-> GetMedianTimePast() - pindexFirst->GetMedianTimePast();
nActualTimespan = params.nAveragingTargetTimespanV4 + (nActualTimespan - params.nAveragingTargetTimespanV4)/4;
if (nActualTimespan < params.nMinActualTimespanV4)
nActualTimespan = params.nMinActualTimespanV4;
if (nActualTimespan > params.nMaxActualTimespanV4)
nActualTimespan = params.nMaxActualTimespanV4;
//Global retarget
arith_uint256 bnNew;
bnNew.SetCompact(pindexPrevAlgo->nBits);
bnNew *= nActualTimespan;
bnNew /= params.nAveragingTargetTimespanV4;
//Per-algo retarget
int nAdjustments = pindexPrevAlgo->nHeight + NUM_ALGOS - 1 - pindexLast->nHeight;
if (nAdjustments > 0)
{
for (int i = 0; i < nAdjustments; i++)
{
bnNew *= 100;
bnNew /= (100 + params.nLocalTargetAdjustment);
}
}
else if (nAdjustments < 0)//make it easier
{
for (int i = 0; i < -nAdjustments; i++)
{
bnNew *= (100 + params.nLocalTargetAdjustment);
bnNew /= 100;
if (bnNew > UintToArith256(params.powLimit)) {
bnNew = UintToArith256(params.powLimit);
break;
}
}
}
if (bnNew > UintToArith256(params.powLimit))
{
bnNew = UintToArith256(params.powLimit);
}
return bnNew.GetCompact();
}
static unsigned int GetNextWorkRequiredImpl(const CBlockIndex* pindexLast, const CBlockHeader* pblock, const Consensus::Params& params, int algo, const PreviousAlgoBlocks* previous_algos)
{
// Genesis block
if (pindexLast == nullptr)
return InitialDifficulty(params, algo);
if (params.fPowAllowMinDifficultyBlocks)
{
// Special difficulty rule for regtest:
// Always allow min difficulty blocks if fEasyPow is set
if (params.fEasyPow) {
return PowLimit(params);
}
// Special difficulty rule for testnet:
// If the new block's timestamp is more than 2 minutes
// then allow mining of a min-difficulty block.
if (pblock->nTime > pindexLast->nTime + params.nTargetSpacing*2)
return PowLimit(params);
}
if (pindexLast->nHeight < params.multiAlgoDiffChangeTarget)
return GetNextWorkRequiredV1(pindexLast, params, algo);
else if (pindexLast->nHeight < params.alwaysUpdateDiffChangeTarget){
return GetNextWorkRequiredV2(pindexLast, params, algo);
}
const CBlockIndex* previous_algo = previous_algos && algo >= 0 && algo < NUM_ALGOS_IMPL
? (*previous_algos)[algo]
: GetLastBlockIndexForAlgo(pindexLast, params, algo);
if (pindexLast->nHeight < params.workComputationChangeTarget)
return GetNextWorkRequiredV3(pindexLast, params, algo, previous_algo);
return GetNextWorkRequiredV4(pindexLast, params, algo, previous_algo);
}
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader* pblock, const Consensus::Params& params, int algo)
{
return GetNextWorkRequiredImpl(pindexLast, pblock, params, algo, nullptr);
}
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader* pblock, const Consensus::Params& params, int algo, const PreviousAlgoBlocks& previous_algos)
{
return GetNextWorkRequiredImpl(pindexLast, pblock, params, algo, &previous_algos);
}
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
{
if (params.fPowNoRetargeting)
return pindexLast->nBits;
// Limit adjustment step
int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
if (nActualTimespan < params.nPowTargetTimespan/4)
nActualTimespan = params.nPowTargetTimespan/4;
if (nActualTimespan > params.nPowTargetTimespan*4)
nActualTimespan = params.nPowTargetTimespan*4;
// Retarget
const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
arith_uint256 bnNew;
bnNew.SetCompact(pindexLast->nBits);
bnNew *= nActualTimespan;
bnNew /= params.nPowTargetTimespan;
if (bnNew > bnPowLimit)
bnNew = bnPowLimit;
return bnNew.GetCompact();
}
// Check that on difficulty adjustments, the new difficulty does not increase
// or decrease beyond the permitted limits.
bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t height, uint32_t old_nbits, uint32_t new_nbits)
{
if (params.fPowAllowMinDifficultyBlocks) return true;
// DigiByte v8.22.2 worked perfectly without this Bitcoin Core v26.2 difficulty validation.
// DigiByte uses real-time MultiShield difficulty adjustment on every single block across
// all 4 difficulty eras (V1, V2, V3, V4), which is fundamentally incompatible with
// Bitcoin's 2016-block difficulty validation model.
// Disable this validation entirely for ALL DigiByte networks and rely on the proper
// DigiByte difficulty validation that occurs in the block validation pipeline.
if (params.nPowTargetSpacing == 15) {
// This is DigiByte (ALL networks use 15-second blocks)
// Completely bypass Bitcoin's validation since DigiByte adjusts
// difficulty in real-time on every block in all 4 eras
return true;
}
if (height % params.DifficultyAdjustmentInterval() == 0) {
int64_t smallest_timespan = params.nPowTargetTimespan/4;
int64_t largest_timespan = params.nPowTargetTimespan*4;
const arith_uint256 pow_limit = UintToArith256(params.powLimit);
arith_uint256 observed_new_target;
observed_new_target.SetCompact(new_nbits);
// Calculate the largest difficulty value possible:
arith_uint256 largest_difficulty_target;
largest_difficulty_target.SetCompact(old_nbits);
largest_difficulty_target *= largest_timespan;
largest_difficulty_target /= params.nPowTargetTimespan;
if (largest_difficulty_target > pow_limit) {
largest_difficulty_target = pow_limit;
}
// Round and then compare this new calculated value to what is
// observed.
arith_uint256 maximum_new_target;
maximum_new_target.SetCompact(largest_difficulty_target.GetCompact());
if (maximum_new_target < observed_new_target) return false;
// Calculate the smallest difficulty value possible:
arith_uint256 smallest_difficulty_target;
smallest_difficulty_target.SetCompact(old_nbits);
smallest_difficulty_target *= smallest_timespan;
smallest_difficulty_target /= params.nPowTargetTimespan;
if (smallest_difficulty_target > pow_limit) {
smallest_difficulty_target = pow_limit;
}
// Round and then compare this new calculated value to what is
// observed.
arith_uint256 minimum_new_target;
minimum_new_target.SetCompact(smallest_difficulty_target.GetCompact());
if (minimum_new_target > observed_new_target) return false;
} else if (old_nbits != new_nbits) {
return false;
}
return true;
}
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
bool fNegative;
bool fOverflow;
arith_uint256 bnTarget;
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
// Check range
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
return false;
// Check proof of work matches claimed amount
if (UintToArith256(hash) > bnTarget)
return false;
return true;
}
const CBlockIndex* GetLastBlockIndexForAlgo(const CBlockIndex* pindex, const Consensus::Params& params, int algo)
{
for (; pindex; pindex = pindex->pprev)
{
if (pindex->GetAlgo() != algo)
continue;
// ignore special min-difficulty testnet blocks
if (params.fPowAllowMinDifficultyBlocks &&
pindex->pprev &&
pindex->nTime > pindex->pprev->nTime + params.nTargetSpacing*2)
{
continue;
}
return pindex;
}
return nullptr;
}
uint256 GetPoWAlgoHash(const CBlockHeader& block)
{
return block.GetPoWAlgoHash(Params().GetConsensus());
}