diff --git a/src/validation.cpp b/src/validation.cpp index d57997ced5e..3ed267b0eb1 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3172,18 +3172,18 @@ void Chainstate::PruneBlockIndexCandidates() { } /** - * Try to make some progress towards making pindexMostWork the active block. - * pblock is either nullptr or a pointer to a CBlock corresponding to pindexMostWork. + * Try to make some progress towards making index_most_work the active block. + * pblock is either nullptr or a pointer to a CBlock corresponding to index_most_work. * * @returns true unless a system error occurred */ -bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr& pblock, bool& fInvalidFound, std::vector& connected_blocks) +bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex& index_most_work, const std::shared_ptr& pblock, bool& fInvalidFound, std::vector& connected_blocks) { AssertLockHeld(cs_main); if (m_mempool) AssertLockHeld(m_mempool->cs); const CBlockIndex* pindexOldTip = m_chain.Tip(); - const CBlockIndex* pindexFork = pindexMostWork ? m_chain.FindFork(*pindexMostWork) : nullptr; + const CBlockIndex* pindexFork = m_chain.FindFork(index_most_work); // Disconnect active blocks which are no longer in the best chain. bool fBlocksDisconnected = false; @@ -3207,13 +3207,13 @@ bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex* std::vector vpindexToConnect; bool fContinue = true; int nHeight = pindexFork ? pindexFork->nHeight : -1; - while (fContinue && nHeight != pindexMostWork->nHeight) { + while (fContinue && nHeight != index_most_work.nHeight) { // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need // a few blocks along the way. - int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight); + int nTargetHeight = std::min(nHeight + 32, index_most_work.nHeight); vpindexToConnect.clear(); vpindexToConnect.reserve(nTargetHeight - nHeight); - CBlockIndex* pindexIter = pindexMostWork->GetAncestor(nTargetHeight); + CBlockIndex* pindexIter = index_most_work.GetAncestor(nTargetHeight); while (pindexIter && pindexIter->nHeight != nHeight) { vpindexToConnect.push_back(pindexIter); pindexIter = pindexIter->pprev; @@ -3222,7 +3222,7 @@ bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex* // Connect new blocks. for (CBlockIndex* pindexConnect : vpindexToConnect | std::views::reverse) { - if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr(), connected_blocks, disconnectpool)) { + if (!ConnectTip(state, pindexConnect, pindexConnect == &index_most_work ? pblock : std::shared_ptr(), connected_blocks, disconnectpool)) { if (state.IsInvalid()) { // The block violates a consensus rule. if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) { @@ -3372,7 +3372,7 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr< // in case snapshot validation is completed during ActivateBestChainStep, the // result of GetRole() changes from BACKGROUND to NORMAL. const ChainstateRole chainstate_role{this->GetRole()}; - if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connected_blocks)) { + if (!ActivateBestChainStep(state, *pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connected_blocks)) { // A system error occurred return false; } diff --git a/src/validation.h b/src/validation.h index cee0dd728b3..8816b070023 100644 --- a/src/validation.h +++ b/src/validation.h @@ -851,7 +851,7 @@ public: std::pair GetPruneRange(int last_height_can_prune) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main); protected: - bool ActivateBestChainStep(BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr& pblock, bool& fInvalidFound, std::vector& connected_blocks) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs); + bool ActivateBestChainStep(BlockValidationState& state, CBlockIndex& index_most_work, const std::shared_ptr& pblock, bool& fInvalidFound, std::vector& connected_blocks) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs); bool ConnectTip( BlockValidationState& state, CBlockIndex* pindexNew,