Skip to content

refactor: Use CBlockIndex parameters as reference - #35229

Open
optout21 wants to merge 8 commits into
bitcoin:masterfrom
optout21:2605-block-param-ref
Open

optout21 wants to merge 8 commits into
bitcoin:masterfrom
optout21:2605-block-param-ref

Conversation

@optout21

@optout21 optout21 commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

This is a janitorial refactor, a continuation of #34440. Input method parameters of type const CBlockIndex* are changed to reference const CBlockIndex& in some places where it makes no sense to call with a nullptr.

Motivation: slight improvement in code quality, maintainability and robustness.

  • more explicit representation of the non-null input invariant
  • less chance of nullptr-access error with future code changes
  • more explicit checks at call sites for the non-null input invariant.

The following methods are affected:

  • LastCommonAncestor() in chain.h
  • PeerManagerImpl::FindNextBlocks() (net_processing.cpp)
  • PeerManagerImpl::TryDownloadingHistoricalBlocks() (net_processing.cpp)
  • BlockFilterIndex::LookupFilterRange(), BlockFilterIndex::LookupFilterHashRange() (blockfilterindex.h), and LookupRange() in blockfilterindex.cpp
  • BaseIndex::ProcessBlock() (base.h)

General guidelines:

  • When a method has an input of type const CBlockIndex*, and by contract it cannot be nullptr, change it to const CBlockIndex&. The reference type is an implicit documentation of the nun-nullptr invariant.
  • When calling a method with reference parameter, ensure that it cannot be nullptr (it is often derived from a pointer), by/because:
    • explicit prior check and error handling
    • an assert/Assert/Assume
    • itself being a non-nullptr invariant input
    • it is being an output from a method with no-nullptr output invariant
  • For local variables, use reference types whenever possible, for non-nullptr references
  • For technical reasons pointers are still used even in some non-nullptr cases:
    • containers/variables that can staty unitialized initially
    • containers that need reserve()
    • using reference type in std::set would result in some copy constructor calls
    • std::optional cannot hold a reference type

@DrahtBot

DrahtBot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK stickies-v, musaHaruna, w0xlt
Stale ACK andrewtoth

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #37016 (rpc: fail scanblocks instead of silently skipping missing filters by shivansh023023)
  • #37015 (Use O(log n) skiplist construction in CBlockIndex by sipa)
  • #36844 (iwyu: Fix warnings in more modules in src/ and treat them as errors by hebasto)
  • #35837 (rpc: fail scanblocks when block filter range is unavailable by MicSm)
  • #35522 (refactor: Extract per-message helpers from SendMessages() (move-only) by pablomartin4btc)
  • #34132 (coins,dbwrapper: make database point-read failures fatal by l0rinc)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/net_processing.cpp: Block are placed into block_indexes. → Blocks are placed into block_indexes. [subject–verb agreement]

2026-11-27 12:52:18

@stickies-v stickies-v left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

Comment thread src/net_processing.cpp Outdated
* \param peer Peer which blocks will be downloaded from.
* \param state Pointer to the state of the peer.
* \param pindexWalk Pointer to the starting block to add to vBlocks.
* \param indexWalk The starting block to add to vBlocks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: developer notes prefer camel_case while you're touching this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@musaHaruna musaHaruna left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK.

Refactor makes sense to me. Changing parameters from const CBlockIndex* to const CBlockIndex& where nullptr is not a valid state improves clarity and gives stronger compile-time guarantees around non-nullability by making the invariant explicit.

Comment thread src/chain.h Outdated
@@ -379,7 +382,7 @@ class CDiskBlockIndex : public CBlockIndex
class CChain
{
private:
std::vector<CBlockIndex*> vChain;
std::vector<CBlockIndex*> vChain; // pointer to be able to reserve, but never contains nullptr

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We can make this comment a bit more concise and technically precise.

Right now it mentions reserve, but the ultimate underlying constraint is simply that std::vector fundamentally cannot hold references entirely. We can strip out the mention of the specific method and focus purely on the core safety invariant and the language rule.

Suggested change
std::vector<CBlockIndex*> vChain; // pointer to be able to reserve, but never contains nullptr
std::vector<CBlockIndex*> vChain; // Pointers, never nullptr (std::vector cannot hold references)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the code-typing overhead of .get() is not too much, why not just use std::reference_wrapper, which enforces everything at compile-time without any runtime overhead and does not rely on a code comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@musaHaruna , @maflcko : I was incorrect here, what I meant is that if reference_wrapper's are used, then resize() has a problem (not reserve()!), because it would call the default constructor, and CBlockIndex does not have one. But I realized now that this can be solved (by passing an instance to resize).
So what I did:

  • Simplified the comment, as suggested, to only "Pointers, never nullptr"
  • I tried using reference_wrapper locally in PeerManagerImpl::FindNextBlocks(). I think the get() is not a big inconvenience, and the zero memory and runtime overhead is important. In there the resize sets them all to a (*nullptr) initially, which kind of defies the purpose, but at least the intent is clear. I put this in a separate commit.

I may reconsider keeping the reference_wrapper commit or not, pending review.
I will also consider using reference_wrapper in the CChain class member vector, which is a riskier change.

Comment thread src/index/base.cpp
@@ -249,7 +249,7 @@ void BaseIndex::Sync()
pindex = pindex_next;


if (!ProcessBlock(pindex)) return; // error logged internally
if (!ProcessBlock(*pindex)) return; // error logged internally

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can explicitly pass the default nullptr with an inline comment here to match the codebase convention.

Suggested change
if (!ProcessBlock(*pindex)) return; // error logged internally
if (!ProcessBlock(*pindex, /*block_data=*/nullptr)) return; // error logged internally

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to this PR, plus I don't see why to explicitly pass an optional parameters, so not taking this.

Comment thread src/chain.cpp Outdated
@@ -173,5 +174,5 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex*
pa = pa->pprev;
pb = pb->pprev;
}
return pa;
return *Assume(pa);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As explained previously, this seems confusing, see #34440 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, thanks for the reminder! I wanted to have here something to "code-show" the return-is-not-nullptr assumption. I've double-checked the usages (call sites), and this can't happen -- all chain segments must have the same genesis. Therefore I've changed it to Assert. Similarly changed one other introduced Assume to Assert in net_processing.cpp.

Comment thread src/chain.h Outdated
@@ -379,7 +382,7 @@ class CDiskBlockIndex : public CBlockIndex
class CChain
{
private:
std::vector<CBlockIndex*> vChain;
std::vector<CBlockIndex*> vChain; // pointer to be able to reserve, but never contains nullptr

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the code-typing overhead of .get() is not too much, why not just use std::reference_wrapper, which enforces everything at compile-time without any runtime overhead and does not rely on a code comment?

@optout21 optout21 left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did changes, following recent reviews. Thanks for the reviews!
What's changed:

  • Change of Assume() to more appropriate Assert() (2 places).
  • Inside PeerManagerImpl::FindNextBlocks(), for the vToFetch vector, use std::reference_wrapper<const CBlockIndex> instead of pointers (new commit!)
  • Some comment adjustments.
  • Some minor renaming of touched variable/parameters.

git range-diff dfe62214070408e699067598309b779c1ed1214d...ed44574efd38d4b7815ab96ae6f68f8b02dee88a

Comment thread src/net_processing.cpp Outdated
* \param peer Peer which blocks will be downloaded from.
* \param state Pointer to the state of the peer.
* \param pindexWalk Pointer to the starting block to add to vBlocks.
* \param indexWalk The starting block to add to vBlocks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread src/index/base.cpp
@@ -249,7 +249,7 @@ void BaseIndex::Sync()
pindex = pindex_next;


if (!ProcessBlock(pindex)) return; // error logged internally
if (!ProcessBlock(*pindex)) return; // error logged internally

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to this PR, plus I don't see why to explicitly pass an optional parameters, so not taking this.

Comment thread src/chain.cpp Outdated
@@ -173,5 +174,5 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex*
pa = pa->pprev;
pb = pb->pprev;
}
return pa;
return *Assume(pa);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, thanks for the reminder! I wanted to have here something to "code-show" the return-is-not-nullptr assumption. I've double-checked the usages (call sites), and this can't happen -- all chain segments must have the same genesis. Therefore I've changed it to Assert. Similarly changed one other introduced Assume to Assert in net_processing.cpp.

Comment thread src/chain.h Outdated
@@ -379,7 +382,7 @@ class CDiskBlockIndex : public CBlockIndex
class CChain
{
private:
std::vector<CBlockIndex*> vChain;
std::vector<CBlockIndex*> vChain; // pointer to be able to reserve, but never contains nullptr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@musaHaruna , @maflcko : I was incorrect here, what I meant is that if reference_wrapper's are used, then resize() has a problem (not reserve()!), because it would call the default constructor, and CBlockIndex does not have one. But I realized now that this can be solved (by passing an instance to resize).
So what I did:

  • Simplified the comment, as suggested, to only "Pointers, never nullptr"
  • I tried using reference_wrapper locally in PeerManagerImpl::FindNextBlocks(). I think the get() is not a big inconvenience, and the zero memory and runtime overhead is important. In there the resize sets them all to a (*nullptr) initially, which kind of defies the purpose, but at least the intent is clear. I put this in a separate commit.

I may reconsider keeping the reference_wrapper commit or not, pending review.
I will also consider using reference_wrapper in the CChain class member vector, which is a riskier change.

@optout21
optout21 force-pushed the 2605-block-param-ref branch 2 times, most recently from ed44574 to 52d0091 Compare May 20, 2026 11:29
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/26158964766/job/76945600531
LLM reason (✨ experimental): CI failed due to a compiler error from Clang -Werror: null pointer dereference to a reference in net_processing.cpp (line 1485).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@optout21
optout21 force-pushed the 2605-block-param-ref branch 2 times, most recently from a9a2eb8 to 82fdd03 Compare May 20, 2026 11:33
@optout21

optout21 commented May 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Pushed compile fix: initializing the reference_wrapper with std::ref(*(CBlockIndex*)nullptr) gave a build error on MacOS (but not locally). Now it's filled with the index_walk input parameter as a placeholder.

git range-diff dfe62214070408e699067598309b779c1ed1214d...82fdd038cfe379c0dbdfda37e3504f97f8e21564
git range-diff ed44574efd38d4b7815ab96ae6f68f8b02dee88a...82fdd038cfe379c0dbdfda37e3504f97f8e21564
git range-diff dfe62214070408e699067598309b779c1ed1214d...ed44574efd38d4b7815ab96ae6f68f8b02dee88a

@optout21
optout21 marked this pull request as draft May 26, 2026 09:09
@optout21
optout21 force-pushed the 2605-block-param-ref branch from 82fdd03 to c6160bc Compare May 26, 2026 09:18
@optout21

optout21 commented May 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Performed changes:

  • Reverted last addition of storing reference_wrapper's inside PeerManagerImpl::FindNextBlocks(). That's an internal, local usage, so there is not much benefit of this change there, hence I reverted it.
  • Did change CChain::vChain array to store std::reference_wrapper<CBlockIndex> items instead of pointers (new commit). This is to better show and ensure that entries are never null. The interfaces returning pointers are not changed.

git range-diff 82fdd038cfe379c0dbdfda37e3504f97f8e21564...a1ed86a7e66c4f13dc58fab71982eba3340602e0

@optout21
optout21 marked this pull request as ready for review May 26, 2026 10:11
@optout21
optout21 force-pushed the 2605-block-param-ref branch from c6160bc to a1ed86a Compare May 26, 2026 10:14

@musaHaruna musaHaruna left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the reference conversions, their call sites, and the relevant non-null and ancestry assumptions. I think the code looks good.

A couple of small suggestions:

  1. In commit 91e1110 chain.h, Could we include <functional> directly for std::reference_wrapper, as recommended by the developers note?
  2. In cec59ef The commit message mentions adding an assertion, but the diff only adds comments. It would be more accurate to describe this as documenting the non-null invariant.

Comment thread src/chain.cpp Outdated
while (pindex && vChain[pindex->nHeight] != pindex) {
vChain[pindex->nHeight] = pindex;
CBlockIndex placeholder;
vChain.resize(block.nHeight + 1, std::ref(placeholder));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vChain.resize(block.nHeight + 1, std::ref(placeholder));
vChain.resize(block.nHeight + 1, std::ref(block));

Could we use std::ref(block) directly as the resize() fill value instead of constructing a local CBlockIndex placeholder? This would simplify the implementation by avoiding an unnecessary temporary object while preserving the behaviour for valid ancestry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@optout21 optout21 left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review, @musaHaruna! Applied your suggestions.
git range-diff a1ed86a7e66c4f13dc58fab71982eba3340602e0...1b5108638b15aaa890657fe6d6d59e38e341b6f5

Additionally, applied some minor changes (self-review):
git range-diff 1b5108638b15aaa890657fe6d6d59e38e341b6f5...8f3a7096b44aa2203ecdad6800d8c498c87b7d1c

Also rebased it (just to refresh it, since it's touched, clean rebase).
(git range-diff 8f3a7096b44aa2203ecdad6800d8c498c87b7d1c...61566b5a3a20e8b8f68d7cd1640b755131e9bd82)

Comment thread src/chain.cpp Outdated
while (pindex && vChain[pindex->nHeight] != pindex) {
vChain[pindex->nHeight] = pindex;
CBlockIndex placeholder;
vChain.resize(block.nHeight + 1, std::ref(placeholder));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@optout21
optout21 force-pushed the 2605-block-param-ref branch from a1ed86a to 61566b5 Compare October 27, 2026 13:40
Comment thread src/chain.h Outdated
* Find the forking point between two chain tips.
* The two chains must share a common ancestor (at least the same genesis).
*/
const CBlockIndex& LastCommonAncestor(const CBlockIndex& a, const CBlockIndex& b);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These parameters should be annotated with LIFETIMEBOUND

Suggested change
const CBlockIndex& LastCommonAncestor(const CBlockIndex& a, const CBlockIndex& b);
const CBlockIndex& LastCommonAncestor(const CBlockIndex& a LIFETIMEBOUND, const CBlockIndex& b LIFETIMEBOUND);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread src/chain.h Outdated
{
private:
std::vector<CBlockIndex*> vChain;
std::vector<std::reference_wrapper<CBlockIndex>> vChain;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to rename this to m_chain if we are touching it here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense, as its type is changed, most of its occurrences are touched. Done.

Comment thread src/net_processing.cpp Outdated
* Request blocks for the background chainstate, if one is in use.
* vBlocks must not contain nullptr.
*/
void TryDownloadingHistoricalBlocks(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, const CBlockIndex& from_tip, const CBlockIndex& target_block) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not rename vBlocks here to block_indexes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done as a small housekeeping task (same commit, added co-author).

Comment thread src/net_processing.cpp Outdated
}

void PeerManagerImpl::FindNextBlocks(std::vector<const CBlockIndex*>& vBlocks, const Peer& peer, CNodeState *state, const CBlockIndex *pindexWalk, unsigned int count, int nWindowEnd, const CChain* activeChain, NodeId* nodeStaller)
void PeerManagerImpl::FindNextBlocks(std::vector<const CBlockIndex*>& vBlocks, const Peer& peer, CNodeState *state, const CBlockIndex& index_walk, unsigned int count, int nWindowEnd, const CChain* activeChain, NodeId* nodeStaller)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not rename all these to modern snake_case from hungarianCamelCase? Since we're touching it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, though it's a bit larger, unrelated change. I've done it in a new commit (the last commit, with co-author).

@optout21 optout21 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied suggestions from @andrewtoth, thanks! (LIFETIMEBOUND, renames).

git range-diff 61566b5a3a20e8b8f68d7cd1640b755131e9bd82...f3a095ad3c8cc4ac319d37e233df2fb5285d0902

Comment thread src/chain.h Outdated
* Find the forking point between two chain tips.
* The two chains must share a common ancestor (at least the same genesis).
*/
const CBlockIndex& LastCommonAncestor(const CBlockIndex& a, const CBlockIndex& b);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread src/chain.h Outdated
{
private:
std::vector<CBlockIndex*> vChain;
std::vector<std::reference_wrapper<CBlockIndex>> vChain;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense, as its type is changed, most of its occurrences are touched. Done.

Comment thread src/net_processing.cpp Outdated
* Request blocks for the background chainstate, if one is in use.
* vBlocks must not contain nullptr.
*/
void TryDownloadingHistoricalBlocks(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, const CBlockIndex& from_tip, const CBlockIndex& target_block) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done as a small housekeeping task (same commit, added co-author).

Comment thread src/net_processing.cpp Outdated
}

void PeerManagerImpl::FindNextBlocks(std::vector<const CBlockIndex*>& vBlocks, const Peer& peer, CNodeState *state, const CBlockIndex *pindexWalk, unsigned int count, int nWindowEnd, const CChain* activeChain, NodeId* nodeStaller)
void PeerManagerImpl::FindNextBlocks(std::vector<const CBlockIndex*>& vBlocks, const Peer& peer, CNodeState *state, const CBlockIndex& index_walk, unsigned int count, int nWindowEnd, const CChain* activeChain, NodeId* nodeStaller)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, though it's a bit larger, unrelated change. I've done it in a new commit (the last commit, with co-author).

@optout21
optout21 force-pushed the 2605-block-param-ref branch from 61566b5 to f3a095a Compare November 5, 2026 10:52
@bartoli

bartoli commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

nit: PeerHasHeader() of net_processing.cpp might benefit from a change too:
pindex is always 'asserted' before calls of this method, except for when the method is given index->pprev in the same source file. So either index->pprev can be nullptr, and inside the method there should be an assert on the CBlockIndex pointer before dereferencing it, or we know that pprev cannot be null (or we assert it before the call), and that method could take a reference

@andrewtoth andrewtoth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f3a095a

@DrahtBot
DrahtBot requested a review from musaHaruna November 21, 2026 19:37
Comment thread src/chain.h Outdated
{
private:
std::vector<CBlockIndex*> vChain;
std::vector<std::reference_wrapper<CBlockIndex>> m_chain;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: might m_entries or m_indexes be a better name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken.

Comment thread src/chain.h Outdated
{
private:
std::vector<CBlockIndex*> vChain;
std::vector<CBlockIndex*> vChain; // Pointers, never nullptr

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit d41e5ff:

nit: seems unrelated this (d41e5ff) commit? And it's also not entirely correct at this point, entries can be nullptr during SetTip's resize.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is meant to explicitly document an implicit, pre-existing assumption. This assumption makes it easier to switch to references.
You are right that commit-wise this is not the ideal spot. I've moved it to the "doc" commit.

Comment thread src/net_processing.cpp Outdated
void TryDownloadingHistoricalBlocks(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, const CBlockIndex* from_tip, const CBlockIndex* target_block) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/**
* Request blocks for the background chainstate, if one is in use.
* block_indexes must not contain nullptr.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a precondition on the parameter, but I think you meant to say that only non-null entries are appended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I was wrong here.

Comment thread src/net_processing.cpp Outdated
* \param count Maximum number of blocks to allow in blocks. No more
* blocks will be added if it reaches this size.
* \param nWindowEnd Maximum height of blocks to allow in vBlocks. No
* \param window_end Maximum height of blocks to allow in blocks. No

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit f3a095a:

This commit feels pretty low-value to me. It's fine to modernize names as we touch them, or if names are not good, but not sure that applies here. If you prefer doing it in this PR regardless, I think we should use helpful names like max_block_count and max_height instead of count and window_end.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting doing adjacent cosmetic changes is a slippery slope. I did a mechanical casing-only change to the non-touched parameters (in response to a review comment). But your point is also valid. I've decided to change the mentioned parameter names to more descriptive ones. This is the last such change; I will simply drop this commit if other review commits suggest that or suggest further improvements.

Comment thread src/chain.h Outdated
CBlockIndex* Tip() const
{
return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
return m_chain.size() > 0 ? &(m_chain[m_chain.size() - 1].get()) : nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: std::vector offers more idiomatic methods here, while you're touching this:

git diff on f3a095a
diff --git a/src/chain.h b/src/chain.h
index a4f96c4ce8..88ccb0f545 100644
--- a/src/chain.h
+++ b/src/chain.h
@@ -394,13 +394,13 @@ public:
     /** Returns the index entry for the genesis block of this chain, or nullptr if none. */
     CBlockIndex* Genesis() const
     {
-        return m_chain.size() > 0 ? &(m_chain[0].get()) : nullptr;
+        return m_chain.empty() ? nullptr : &m_chain.front().get();
     }
 
     /** Returns the index entry for the tip of this chain, or nullptr if none. */
     CBlockIndex* Tip() const
     {
-        return m_chain.size() > 0 ? &(m_chain[m_chain.size() - 1].get()) : nullptr;
+        return m_chain.empty() ? nullptr : &m_chain.back().get();
     }
 
     /** Returns the index entry at a particular height in this chain, or nullptr if no such height exists. */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, taken

Comment thread src/net_processing.cpp Outdated
* \param state Pointer to the state of the peer.
* \param pindexWalk Pointer to the starting block to add to vBlocks.
* \param count Maximum number of blocks to allow in vBlocks. No more
* \param index_walk The starting block to add to blocks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The index walking is an implementation detail and should not leak into the fn signature imo. I also interpret the docstring as index_walk to be included in blocks, but I don't think that's the case. Suggest improving the docstring and renaming the parameter to e.g. after_block?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@DrahtBot
DrahtBot requested a review from stickies-v November 23, 2026 20:27
optout21 and others added 8 commits November 27, 2026 12:35
… to reference

Also rename a parameter (vBlocks -> block_indexes), for consistency (housekeeping).

Co-authored-by: Andrew Toth <[email protected]>
Methods affected: BlockFilterIndex::LookupFilterRange, BlockFilterIndex::LookupFilterHashRange,
and LookupRange, in blockfilterindex.
In the `vChain` internal array of `CChain` class, store items as
`std::reference_wrapper<CBlockIndex>`, instead of `CBlockIndex*` pointers.
This gives a better guarantee that the entries are not null.
The APIs returning pointers are not changed.
The member is renamed to `m_entries`.
Note that the entries are not const references, due to the need to update
during validation.
Genesis() and Tip() implementation has been simplified, as an adjacent change.
Change to more descriptive names, using snake_case
(drop type prefixes and camel case).

Co-authored-by: Andrew Toth <[email protected]>
@optout21
optout21 force-pushed the 2605-block-param-ref branch from f3a095a to bad93f7 Compare November 27, 2026 12:52

@optout21 optout21 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied several smaller changes, following review from @stickies-v -- thanks!
The last, adjacent rename commit has grown. I've decided to keep it for now, but if there are doubts about its place here, or further suggestions to fix "one more thing", I will drop it.

git range-diff f3a095a...bad93f7

Comment thread src/chain.h Outdated
{
private:
std::vector<CBlockIndex*> vChain;
std::vector<std::reference_wrapper<CBlockIndex>> m_chain;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken.

Comment thread src/chain.h Outdated
{
private:
std::vector<CBlockIndex*> vChain;
std::vector<CBlockIndex*> vChain; // Pointers, never nullptr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is meant to explicitly document an implicit, pre-existing assumption. This assumption makes it easier to switch to references.
You are right that commit-wise this is not the ideal spot. I've moved it to the "doc" commit.

Comment thread src/net_processing.cpp Outdated
void TryDownloadingHistoricalBlocks(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, const CBlockIndex* from_tip, const CBlockIndex* target_block) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/**
* Request blocks for the background chainstate, if one is in use.
* block_indexes must not contain nullptr.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I was wrong here.

Comment thread src/net_processing.cpp Outdated
* \param count Maximum number of blocks to allow in blocks. No more
* blocks will be added if it reaches this size.
* \param nWindowEnd Maximum height of blocks to allow in vBlocks. No
* \param window_end Maximum height of blocks to allow in blocks. No

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting doing adjacent cosmetic changes is a slippery slope. I did a mechanical casing-only change to the non-touched parameters (in response to a review comment). But your point is also valid. I've decided to change the mentioned parameter names to more descriptive ones. This is the last such change; I will simply drop this commit if other review commits suggest that or suggest further improvements.

Comment thread src/chain.h Outdated
CBlockIndex* Tip() const
{
return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
return m_chain.size() > 0 ? &(m_chain[m_chain.size() - 1].get()) : nullptr;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, taken

Comment thread src/net_processing.cpp Outdated
* \param state Pointer to the state of the peer.
* \param pindexWalk Pointer to the starting block to add to vBlocks.
* \param count Maximum number of blocks to allow in vBlocks. No more
* \param index_walk The starting block to add to blocks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants