331 lines
9.5 KiB
C++
331 lines
9.5 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
|
// Copyright (c) 2014-2019 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 <chain.h>
|
|
#include <chainparams.h>
|
|
#include <validation.h>
|
|
|
|
// Include whatever header actually declares LogPrintf in your codebase.
|
|
// In DigiByte, it might be "logging.h" or "util/system.h". Adjust as needed:
|
|
#include <logging.h> // or #include <util/system.h> or #include "util.h"
|
|
|
|
/**
|
|
* CBlockIndex default constructor
|
|
*/
|
|
CBlockIndex::CBlockIndex()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* CBlockIndex constructor that copies from a block header.
|
|
* We can safely call LogPrintf here because we are in a .cpp file that includes logging.
|
|
*/
|
|
CBlockIndex::CBlockIndex(const CBlockHeader& block)
|
|
: nVersion(block.nVersion),
|
|
hashMerkleRoot(block.hashMerkleRoot),
|
|
nTime(block.nTime),
|
|
nBits(block.nBits),
|
|
nNonce(block.nNonce)
|
|
{
|
|
|
|
}
|
|
|
|
int CBlockIndex::GetAlgo() const
|
|
{
|
|
switch (nVersion & BLOCK_VERSION_ALGO) {
|
|
case BLOCK_VERSION_SHA256D:
|
|
return ALGO_SHA256D;
|
|
case BLOCK_VERSION_RANDOMX:
|
|
return ALGO_RANDOMX;
|
|
default:
|
|
return ALGO_UNKNOWN;
|
|
}
|
|
}
|
|
|
|
void CChain::SetTip(CBlockIndex *pindex) {
|
|
if (pindex == nullptr) {
|
|
vChain.clear();
|
|
return;
|
|
}
|
|
vChain.resize(pindex->nHeight + 1);
|
|
while (pindex && vChain[pindex->nHeight] != pindex) {
|
|
vChain[pindex->nHeight] = pindex;
|
|
pindex = pindex->pprev;
|
|
}
|
|
}
|
|
|
|
CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
|
|
int nStep = 1;
|
|
std::vector<uint256> vHave;
|
|
vHave.reserve(32);
|
|
|
|
if (!pindex)
|
|
pindex = Tip();
|
|
while (pindex) {
|
|
vHave.push_back(pindex->GetBlockHash());
|
|
// Stop when we have added the genesis block.
|
|
if (pindex->nHeight == 0)
|
|
break;
|
|
// Exponentially larger steps back, plus the genesis block.
|
|
int nHeight = std::max(pindex->nHeight - nStep, 0);
|
|
if (Contains(pindex)) {
|
|
// Use O(1) CChain index if possible.
|
|
pindex = (*this)[nHeight];
|
|
} else {
|
|
// Otherwise, use O(log n) skiplist.
|
|
pindex = pindex->GetAncestor(nHeight);
|
|
}
|
|
if (vHave.size() > 10)
|
|
nStep *= 2;
|
|
}
|
|
|
|
return CBlockLocator(vHave);
|
|
}
|
|
|
|
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
|
|
if (pindex == nullptr) {
|
|
return nullptr;
|
|
}
|
|
if (pindex->nHeight > Height())
|
|
pindex = pindex->GetAncestor(Height());
|
|
while (pindex && !Contains(pindex))
|
|
pindex = pindex->pprev;
|
|
return pindex;
|
|
}
|
|
|
|
CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime, int height) const
|
|
{
|
|
std::pair<int64_t, int> blockparams = std::make_pair(nTime, height);
|
|
auto lower = std::lower_bound(
|
|
vChain.begin(), vChain.end(), blockparams,
|
|
[](CBlockIndex* pBlock, const std::pair<int64_t, int>& bp) {
|
|
return pBlock->GetBlockTimeMax() < bp.first || pBlock->nHeight < bp.second;
|
|
}
|
|
);
|
|
return (lower == vChain.end() ? nullptr : *lower);
|
|
}
|
|
|
|
/** Turn the lowest '1' bit in the binary representation of a number into '0'. */
|
|
int static inline InvertLowestOne(int n) { return n & (n - 1); }
|
|
|
|
/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
|
|
int static inline GetSkipHeight(int height) {
|
|
if (height < 2)
|
|
return 0;
|
|
|
|
return (height & 1)
|
|
? InvertLowestOne(InvertLowestOne(height - 1)) + 1
|
|
: InvertLowestOne(height);
|
|
}
|
|
|
|
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
|
|
{
|
|
if (height > nHeight || height < 0) {
|
|
return nullptr;
|
|
}
|
|
|
|
const CBlockIndex* pindexWalk = this;
|
|
int heightWalk = nHeight;
|
|
while (heightWalk > height) {
|
|
int heightSkip = GetSkipHeight(heightWalk);
|
|
int heightSkipPrev = GetSkipHeight(heightWalk - 1);
|
|
if (pindexWalk->pskip != nullptr &&
|
|
(heightSkip == height ||
|
|
(heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
|
|
heightSkipPrev >= height)))) {
|
|
// Only follow pskip if pprev->pskip isn't better than pskip->pprev.
|
|
pindexWalk = pindexWalk->pskip;
|
|
heightWalk = heightSkip;
|
|
} else {
|
|
assert(pindexWalk->pprev);
|
|
pindexWalk = pindexWalk->pprev;
|
|
heightWalk--;
|
|
}
|
|
}
|
|
return pindexWalk;
|
|
}
|
|
|
|
CBlockIndex* CBlockIndex::GetAncestor(int height)
|
|
{
|
|
return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
|
|
}
|
|
|
|
void CBlockIndex::BuildSkip()
|
|
{
|
|
if (pprev)
|
|
pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
|
|
}
|
|
|
|
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;
|
|
|
|
// 2**256 / (bnTarget+1)
|
|
return (~bnTarget / (bnTarget + 1)) + 1;
|
|
}
|
|
|
|
arith_uint256 GetBlockProof(const CBlockIndex& block)
|
|
{
|
|
// Raw proof derived directly from this block's nBits.
|
|
return GetBlockProofBase(block);
|
|
}
|
|
|
|
static arith_uint256 ScaleMultiAlgoChainwork(
|
|
const arith_uint256& work,
|
|
uint32_t numerator,
|
|
uint32_t denominator)
|
|
{
|
|
if (work == 0 || numerator == 0 || denominator == 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (numerator == denominator) {
|
|
return work;
|
|
}
|
|
|
|
// Compute floor(work * numerator / denominator) without first
|
|
// multiplying the full 256-bit value. base_uint multiplication silently
|
|
// truncates on overflow, so split work into quotient and remainder:
|
|
//
|
|
// work = quotient * denominator + remainder
|
|
//
|
|
// Then:
|
|
//
|
|
// floor(work * numerator / denominator)
|
|
// = quotient * numerator
|
|
// + floor(remainder * numerator / denominator)
|
|
//
|
|
// remainder is smaller than the 32-bit denominator, so its product with
|
|
// the 32-bit numerator fits comfortably inside 256 bits.
|
|
arith_uint256 quotient = work / denominator;
|
|
arith_uint256 remainder = work - quotient * denominator;
|
|
|
|
const arith_uint256 maxValue = ~arith_uint256(0);
|
|
|
|
if (quotient > maxValue / numerator) {
|
|
return 0;
|
|
}
|
|
|
|
quotient *= numerator;
|
|
|
|
remainder *= numerator;
|
|
remainder /= denominator;
|
|
|
|
if (quotient > maxValue - remainder) {
|
|
return 0;
|
|
}
|
|
|
|
return quotient + remainder;
|
|
}
|
|
|
|
arith_uint256 GetBlockProof(
|
|
const CBlockIndex& block,
|
|
const Consensus::Params& params)
|
|
{
|
|
// Preserve historical SHA256D chainwork exactly before multi-algo
|
|
// activation.
|
|
if (block.nHeight < params.randomXActivationHeight) {
|
|
return GetBlockProofBase(block);
|
|
}
|
|
|
|
// Genesis has no parent state from which per-algo targets can be
|
|
// calculated. This path is only relevant if activation were configured
|
|
// at height zero.
|
|
if (block.pprev == nullptr) {
|
|
return GetBlockProofBase(block);
|
|
}
|
|
|
|
const int blockAlgo = block.GetAlgo();
|
|
if (blockAlgo != ALGO_SHA256D && blockAlgo != ALGO_RANDOMX) {
|
|
return 0;
|
|
}
|
|
|
|
// Normalize heterogeneous PoW families using the geometric mean of the
|
|
// expected targets for all active algorithms, calculated from the same
|
|
// parent state. Competing SHA256D and RandomX children therefore receive
|
|
// the same chainwork increment regardless of their native difficulty
|
|
// scales.
|
|
arith_uint256 averageTarget(1);
|
|
|
|
for (int algo = 0; algo < NUM_ALGOS_IMPL; ++algo) {
|
|
const unsigned int nBits =
|
|
GetNextWorkRequired(block.pprev, nullptr, params, algo);
|
|
|
|
arith_uint256 target;
|
|
bool negative;
|
|
bool overflow;
|
|
target.SetCompact(nBits, &negative, &overflow);
|
|
|
|
if (negative ||
|
|
overflow ||
|
|
target == 0 ||
|
|
target > UintToArith256(params.powLimit)) {
|
|
return 0;
|
|
}
|
|
|
|
const arith_uint256 root = target.ApproxNthRoot(NUM_ALGOS);
|
|
if (root == 0) {
|
|
return 0;
|
|
}
|
|
|
|
averageTarget *= root;
|
|
}
|
|
|
|
if (averageTarget == 0) {
|
|
return 0;
|
|
}
|
|
|
|
arith_uint256 normalizedWork =
|
|
(~averageTarget / (averageTarget + 1)) + 1;
|
|
|
|
// Apply a fixed, branch-independent consensus scale. The default 1/1
|
|
// preserves the normalized-work value exactly.
|
|
return ScaleMultiAlgoChainwork(
|
|
normalizedWork,
|
|
params.multiAlgoChainworkScaleNumerator,
|
|
params.multiAlgoChainworkScaleDenominator);
|
|
}
|
|
|
|
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.nPowTargetSpacing) / GetBlockProof(tip, params);
|
|
if (r.bits() > 63) {
|
|
return sign * std::numeric_limits<int64_t>::max();
|
|
}
|
|
return sign * r.GetLow64();
|
|
}
|
|
|
|
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb)
|
|
{
|
|
if (pa->nHeight > pb->nHeight) {
|
|
pa = pa->GetAncestor(pb->nHeight);
|
|
} else if (pb->nHeight > pa->nHeight) {
|
|
pb = pb->GetAncestor(pa->nHeight);
|
|
}
|
|
|
|
while (pa != pb && pa && pb) {
|
|
pa = pa->pprev;
|
|
pb = pb->pprev;
|
|
}
|
|
|
|
// Eventually all chain branches meet at the genesis block.
|
|
assert(pa == pb);
|
|
return pa;
|
|
}
|