Add algorithm-aware mining difficulty

This commit is contained in:
Capri 2026-09-17 18:31:43 +00:00
commit 37d4ae0d1b
Signed by: VextaProject
SSH key fingerprint: SHA256:739BZv8LvIDY2tR7lXLy/IQ6MaGTQtYcG9GNvCjmpVM
4 changed files with 92 additions and 3 deletions

View file

@ -137,11 +137,46 @@ double GetDifficulty(const CBlockIndex* blockindex)
return dDiff;
}
double GetAlgoDifficulty(
const CBlockIndex* blockindex,
const Consensus::Params& consensusParams,
int algo)
{
assert(blockindex != nullptr);
if (algo == ALGO_SHA256D) {
return GetDifficulty(blockindex);
}
if (algo != ALGO_RANDOMX) {
return 0.0;
}
bool negative;
bool overflow;
arith_uint256 target;
target.SetCompact(blockindex->nBits, &negative, &overflow);
if (negative || overflow || target == 0) {
return 0.0;
}
const arith_uint256 reference =
UintToArith256(consensusParams.randomXInitialTarget);
if (reference == 0) {
return 0.0;
}
return reference.getdouble() / target.getdouble();
}
static double GetNextDifficulty(const CBlockIndex* tip, const Consensus::Params& consensusParams, int algo)
{
CBlockIndex next;
next.nBits = GetNextWorkRequired(tip, nullptr, consensusParams, algo);
return GetDifficulty(&next);
return GetAlgoDifficulty(&next, consensusParams, algo);
}
static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next)