Change pindexMostWork parameter of ActivateBestChainStep() to reference
What changed, and why it matters
This is a small internal code cleanup in Bitcoin Core. It changes one function parameter from a pointer (which could theoretically be null) to a reference (which cannot be null), because the caller always passes a valid object anyway. There is no change to how the program behaves, no bug fixed, and no security issue introduced.
No action needed. This is a non-security refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors Chainstate::ActivateBestChainStep() in src/validation.cpp/h. The CBlockIndex* pindexMostWork parameter is changed to CBlockIndex& index_most_work, and all internal uses are updated from pointer dereferences to direct member access or address-of where needed. The single call site in ActivateBestChain() already dereferences a non-null pindexMostWork before calling, so the change only enforces at the type level what was already true. No functional behavior changes.
Changed components
src/validation.cppsrc/validation.hInspect captured patch +10 / −10
diff --git a/src/validation.cpp b/src/validation.cpp
index d57997ce..3ed267b0 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<const CBlock>& pblock, bool& fInvalidFound, std::vector<ConnectedBlock>& connected_blocks)
+bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex& index_most_work, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, std::vector<ConnectedBlock>& 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<CBlockIndex*> 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<const CBlock>(), connected_blocks, disconnectpool)) {
+ if (!ConnectTip(state, pindexConnect, pindexConnect == &index_most_work ? pblock : std::shared_ptr<const CBlock>(), 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 cee0dd72..8816b070 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -851,7 +851,7 @@ public:
std::pair<int, int> GetPruneRange(int last_height_can_prune) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
protected:
- bool ActivateBestChainStep(BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, std::vector<ConnectedBlock>& connected_blocks) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
+ bool ActivateBestChainStep(BlockValidationState& state, CBlockIndex& index_most_work, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, std::vector<ConnectedBlock>& connected_blocks) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
bool ConnectTip(
BlockValidationState& state,
CBlockIndex* pindexNew,
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.