Skip to content

Commit 415b81e

Browse files
authored
Refactor some pow functions (#3198)
* Refactor GetNextWorkRequired * Move special diff rule out of DGW
1 parent b2fed38 commit 415b81e

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

src/pow.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ unsigned int static KimotoGravityWell(const CBlockIndex* pindexLast, const Conse
7979
return bnNew.GetCompact();
8080
}
8181

82-
unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) {
82+
unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const Consensus::Params& params) {
8383
/* current difficulty formula, dash - DarkGravity v3, written by Evan Duffield - evan@dash.org */
8484
const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
8585
int64_t nPastBlocks = 24;
@@ -89,21 +89,6 @@ unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const CBlockH
8989
return bnPowLimit.GetCompact();
9090
}
9191

92-
if (params.fPowAllowMinDifficultyBlocks) {
93-
// recent block is more than 2 hours old
94-
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + 2 * 60 * 60) {
95-
return bnPowLimit.GetCompact();
96-
}
97-
// recent block is more than 10 minutes old
98-
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing * 4) {
99-
arith_uint256 bnNew = arith_uint256().SetCompact(pindexLast->nBits) * 10;
100-
if (bnNew > bnPowLimit) {
101-
bnNew = bnPowLimit;
102-
}
103-
return bnNew.GetCompact();
104-
}
105-
}
106-
10792
const CBlockIndex *pindex = pindexLast;
10893
arith_uint256 bnPastTargetAvg;
10994

@@ -182,22 +167,41 @@ unsigned int GetNextWorkRequiredBTC(const CBlockIndex* pindexLast, const CBlockH
182167

183168
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
184169
{
170+
assert(pindexLast != nullptr);
171+
assert(pblock != nullptr);
172+
const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
173+
185174
// this is only active on devnets
186175
if (pindexLast->nHeight < params.nMinimumDifficultyBlocks) {
187-
unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
188-
return nProofOfWorkLimit;
176+
return bnPowLimit.GetCompact();
189177
}
190178

191-
// Most recent algo first
192-
if (pindexLast->nHeight + 1 >= params.nPowDGWHeight) {
193-
return DarkGravityWave(pindexLast, pblock, params);
179+
if (pindexLast->nHeight + 1 < params.nPowKGWHeight) {
180+
return GetNextWorkRequiredBTC(pindexLast, pblock, params);
194181
}
195-
else if (pindexLast->nHeight + 1 >= params.nPowKGWHeight) {
196-
return KimotoGravityWell(pindexLast, params);
182+
183+
// Note: GetNextWorkRequiredBTC has it's own special difficulty rule,
184+
// so we only apply this to post-BTC algos.
185+
if (params.fPowAllowMinDifficultyBlocks) {
186+
// recent block is more than 2 hours old
187+
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + 2 * 60 * 60) {
188+
return bnPowLimit.GetCompact();
189+
}
190+
// recent block is more than 10 minutes old
191+
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing * 4) {
192+
arith_uint256 bnNew = arith_uint256().SetCompact(pindexLast->nBits) * 10;
193+
if (bnNew > bnPowLimit) {
194+
return bnPowLimit.GetCompact();
195+
}
196+
return bnNew.GetCompact();
197+
}
197198
}
198-
else {
199-
return GetNextWorkRequiredBTC(pindexLast, pblock, params);
199+
200+
if (pindexLast->nHeight + 1 < params.nPowDGWHeight) {
201+
return KimotoGravityWell(pindexLast, params);
200202
}
203+
204+
return DarkGravityWave(pindexLast, params);
201205
}
202206

203207
// for DIFF_BTC only!

0 commit comments

Comments
 (0)