Restore per-algo block index tracking

This commit is contained in:
Capri 2026-09-12 20:32:57 +00:00
commit 2210ac3451
Signed by: VextaProject
SSH key fingerprint: SHA256:739BZv8LvIDY2tR7lXLy/IQ6MaGTQtYcG9GNvCjmpVM
3 changed files with 40 additions and 5 deletions

View file

@ -33,6 +33,18 @@ CBlockIndex::CBlockIndex(const CBlockHeader& block)
} }
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) { void CChain::SetTip(CBlockIndex *pindex) {
if (pindex == nullptr) { if (pindex == nullptr) {
vChain.clear(); vChain.clear();

View file

@ -201,6 +201,9 @@ public:
//! (memory only) Maximum nTime in the chain up to and including this block. //! (memory only) Maximum nTime in the chain up to and including this block.
unsigned int nTimeMax{0}; unsigned int nTimeMax{0};
//! (memory only) Most recent block index for each mining algorithm.
CBlockIndex* lastAlgoBlocks[NUM_ALGOS_IMPL]{};
/** /**
* Default constructor (no header): * Default constructor (no header):
*/ */
@ -264,6 +267,8 @@ public:
bool HaveTxsDownloaded() const { return nChainTx != 0; } bool HaveTxsDownloaded() const { return nChainTx != 0; }
int GetAlgo() const;
int64_t GetBlockTime() const int64_t GetBlockTime() const
{ {
return (int64_t)nTime; return (int64_t)nTime;

View file

@ -3036,9 +3036,18 @@ CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block)
pindexNew->nHeight = pindexNew->pprev->nHeight + 1; pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
pindexNew->BuildSkip(); pindexNew->BuildSkip();
} }
// Keep track of the most recent block for each mining algorithm.
// Use memcpy to copy the entire array at once.
if (pindexNew->pprev) { if (pindexNew->pprev) {
memcpy(
pindexNew->lastAlgoBlocks,
pindexNew->pprev->lastAlgoBlocks,
sizeof(pindexNew->lastAlgoBlocks)
);
}
const int algo = pindexNew->GetAlgo();
if (algo >= 0 && algo < NUM_ALGOS_IMPL) {
pindexNew->lastAlgoBlocks[algo] = pindexNew;
} }
pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime); pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
@ -3822,11 +3831,20 @@ bool BlockManager::LoadBlockIndex(
} }
if (ShutdownRequested()) return false; if (ShutdownRequested()) return false;
CBlockIndex* pindex = item.second; CBlockIndex* pindex = item.second;
// Rebuild the most recent block pointer for each mining algorithm.
// Use memcpy to copy the entire array at once.
if (pindex->pprev) { if (pindex->pprev) {
memcpy(
pindex->lastAlgoBlocks,
pindex->pprev->lastAlgoBlocks,
sizeof(pindex->lastAlgoBlocks)
);
} }
const int algo = pindex->GetAlgo();
if (algo >= 0 && algo < NUM_ALGOS_IMPL) {
pindex->lastAlgoBlocks[algo] = pindex;
}
nHeight = pindex-> nHeight; nHeight = pindex-> nHeight;
pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex); pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime); pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);