MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / LwmaCalculateNextWorkRequired

Function LwmaCalculateNextWorkRequired

src/pow.cpp:73–122  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

71}
72
73unsigned int LwmaCalculateNextWorkRequired(const CBlockIndex* pindexLast, const Consensus::Params& params)
74{
75 if (params.fPowNoRetargeting) {
76 return pindexLast->nBits;
77 }
78
79 const int height = pindexLast->nHeight + 1;
80 const int64_t T = params.nPowTargetSpacing;
81 const int N = params.nZawyLwmaAveragingWindow;
82 const int k = params.ZawyLwmaAdjustedWeight(height);
83 const int dnorm = params.ZawyLwmaMinDenominator(height);
84 const bool limit_st = params.bZawyLwmaSolvetimeLimitation;
85 assert(height > N);
86
87 arith_uint256 sum_target;
88 int t = 0, j = 0;
89
90 // Loop through N most recent blocks.
91 for (int i = height - N; i < height; i++) {
92 const CBlockIndex* block = pindexLast->GetAncestor(i);
93 const CBlockIndex* block_Prev = block->GetAncestor(i - 1);
94 int64_t solvetime = block->GetBlockTime() - block_Prev->GetBlockTime();
95
96 if (limit_st && solvetime > 6 * T) {
97 solvetime = 6 * T;
98 }
99
100 j++;
101 t += solvetime * j; // Weighted solvetime sum.
102
103 // Target sum divided by a factor, (k N^2).
104 // The factor is a part of the final equation. However we divide sum_target here to avoid
105 // potential overflow.
106 arith_uint256 target;
107 target.SetCompact(block->nBits);
108 sum_target += target / (k * N * N);
109 }
110 // Keep t reasonable in case strange solvetimes occurred.
111 if (t < N * k / dnorm) {
112 t = N * k / dnorm;
113 }
114
115 const arith_uint256 pow_limit = UintToArith256(params.PowLimit(true));
116 arith_uint256 next_target = t * sum_target;
117 if (next_target > pow_limit) {
118 next_target = pow_limit;
119 }
120
121 return next_target.GetCompact();
122}
123
124unsigned int DigishieldGetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock,
125 const Consensus::Params& params)

Callers 2

LwmaGetNextWorkRequiredFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 6

UintToArith256Function · 0.85
GetAncestorMethod · 0.80
GetCompactMethod · 0.80
GetBlockTimeMethod · 0.45

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68