refactor: Use CBlockIndex parameters as reference #35229

pull optout21 wants to merge 8 commits into bitcoin:master from optout21:2605-block-param-ref changing 12 files +103 −90
  1. optout21 commented at 7:11 AM on May 7, 2026: 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
  2. DrahtBot added the label Refactoring on May 7, 2026
  3. DrahtBot commented at 7:11 AM on May 7, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    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 <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36844 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36844.svg"></sub> (iwyu: Fix warnings in more modules in src/ and treat them as errors by hebasto)
    • #36808 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36808.svg"></sub> (iwyu: Fix warnings in src/test/fuzz and treat them as errors by hebasto)
    • #36598 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36598.svg"></sub> ([Demo] validation: remove AssumeUTXO by l0rinc)
    • #35837 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35837.svg"></sub> (rpc: fail scanblocks when block filter range is unavailable by MicSm)
    • #35522 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35522.svg"></sub> (refactor: Extract per-message helpers from SendMessages() (move-only) by pablomartin4btc)

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

    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]

    <sup>2026-11-27 12:52:18</sup>

  4. optout21 force-pushed on May 7, 2026
  5. DrahtBot added the label CI failed on May 7, 2026
  6. in src/net_processing.cpp:928 in dfe6221407
     926 |      * \param vBlocks      Vector of blocks to download which will be appended to.
     927 | +    *                     Must not contain nullptr.
     928 |      * \param peer         Peer which blocks will be downloaded from.
     929 |      * \param state        Pointer to the state of the peer.
     930 | -    * \param pindexWalk   Pointer to the starting block to add to vBlocks.
     931 | +    * \param indexWalk    The starting block to add to vBlocks.
    


    stickies-v commented at 7:52 AM on May 7, 2026:

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


    optout21 commented at 10:16 AM on May 20, 2026:

    Done.

  7. stickies-v commented at 7:52 AM on May 7, 2026: contributor

    Concept ACK

  8. DrahtBot removed the label CI failed on May 7, 2026
  9. optout21 marked this as ready for review on May 9, 2026
  10. musaHaruna commented at 11:36 AM on May 19, 2026: contributor

    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.

  11. in src/chain.h:385 in dfe6221407
     381 | @@ -379,7 +382,7 @@ class CDiskBlockIndex : public CBlockIndex
     382 |  class CChain
     383 |  {
     384 |  private:
     385 | -    std::vector<CBlockIndex*> vChain;
     386 | +    std::vector<CBlockIndex*> vChain;  // pointer to be able to reserve, but never contains nullptr
    


    musaHaruna commented at 11:37 AM on May 19, 2026:

    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.

        std::vector<CBlockIndex*> vChain;  // Pointers, never nullptr (std::vector cannot hold references)
    

    maflcko commented at 11:50 AM on May 19, 2026:

    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 commented at 11:11 AM on May 20, 2026:

    @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.

  12. in src/index/base.cpp:252 in dfe6221407 outdated
     248 | @@ -249,7 +249,7 @@ void BaseIndex::Sync()
     249 |              pindex = pindex_next;
     250 |  
     251 |  
     252 | -            if (!ProcessBlock(pindex)) return; // error logged internally
     253 | +            if (!ProcessBlock(*pindex)) return; // error logged internally
    


    musaHaruna commented at 11:44 AM on May 19, 2026:

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

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

    optout21 commented at 10:18 AM on May 20, 2026:

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

  13. in src/chain.cpp:177 in dfe6221407
     173 | @@ -173,5 +174,5 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex*
     174 |          pa = pa->pprev;
     175 |          pb = pb->pprev;
     176 |      }
     177 | -    return pa;
     178 | +    return *Assume(pa);
    


    maflcko commented at 11:49 AM on May 19, 2026:

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


    optout21 commented at 10:27 AM on May 20, 2026:

    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.

  14. optout21 commented at 11:15 AM on May 20, 2026: contributor

    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

  15. optout21 force-pushed on May 20, 2026
  16. optout21 force-pushed on May 20, 2026
  17. DrahtBot added the label CI failed on May 20, 2026
  18. DrahtBot commented at 11:30 AM on May 20, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/26158964766/job/76945600531</sub> <sub>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).</sub>

    <details><summary>Hints</summary>

    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.

    </details>

  19. optout21 force-pushed on May 20, 2026
  20. optout21 force-pushed on May 20, 2026
  21. optout21 commented at 11:37 AM on May 20, 2026: contributor

    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
    
  22. DrahtBot removed the label CI failed on May 20, 2026
  23. optout21 marked this as a draft on May 26, 2026
  24. optout21 force-pushed on May 26, 2026
  25. optout21 commented at 9:18 AM on May 26, 2026: contributor

    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

  26. optout21 marked this as ready for review on May 26, 2026
  27. optout21 force-pushed on May 26, 2026
  28. DrahtBot added the label CI failed on May 26, 2026
  29. optout21 closed this on May 26, 2026

  30. optout21 reopened this on May 26, 2026

  31. DrahtBot removed the label CI failed on May 26, 2026
  32. optout21 commented at 6:42 AM on May 27, 2026: contributor

    There was an unrelated CI failure, but on the second try all CI builds passed. The failure was in "Windows, ucrt, test cross-built":

    Error: fatal: unable to access 'https://github.com/bitcoin/bitcoin/': The requested URL returned error: 403
    

    seemingly an intermittent infrastructure issue, most likely unrelated to this PR.

    https://github.com/bitcoin/bitcoin/actions/runs/26446276736/job/77856772060?pr=35229

  33. w0xlt commented at 12:47 AM on June 14, 2026: contributor

    Concept ACK

  34. sedited requested review from stickies-v on Aug 10, 2026
  35. sedited requested review from musaHaruna on Aug 10, 2026
  36. sedited commented at 7:59 AM on September 22, 2026: contributor

    Pinging concept ackers @musaHaruna @stickies-v @w0xlt for another review.

  37. musaHaruna commented at 7:04 PM on October 23, 2026: contributor

    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.
  38. in src/chain.cpp:19 in a1ed86a7e6
      14 | @@ -15,10 +15,12 @@ std::string CBlockIndex::ToString() const
      15 |  
      16 |  void CChain::SetTip(CBlockIndex& block)
      17 |  {
      18 | -    CBlockIndex* pindex = &block;
      19 | -    vChain.resize(pindex->nHeight + 1);
      20 | -    while (pindex && vChain[pindex->nHeight] != pindex) {
      21 | -        vChain[pindex->nHeight] = pindex;
      22 | +    CBlockIndex placeholder;
      23 | +    vChain.resize(block.nHeight + 1, std::ref(placeholder));
    


    musaHaruna commented at 7:04 PM on October 23, 2026:
        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.


    optout21 commented at 1:38 PM on October 27, 2026:

    Done.

  39. optout21 commented at 1:39 PM on October 27, 2026: contributor

    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)

  40. optout21 force-pushed on Oct 27, 2026
  41. in src/chain.h:317 in 61566b5a3a
     314 | -const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb);
     315 | +/**
     316 | + * Find the forking point between two chain tips.
     317 | + * The two chains must share a common ancestor (at least the same genesis).
     318 | + */
     319 | +const CBlockIndex& LastCommonAncestor(const CBlockIndex& a, const CBlockIndex& b);
    


    andrewtoth commented at 10:51 PM on November 3, 2026:

    These parameters should be annotated with LIFETIMEBOUND

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

    optout21 commented at 10:45 AM on November 5, 2026:

    Done

  42. in src/chain.h:386 in 61566b5a3a
     382 | @@ -379,7 +383,7 @@ class CDiskBlockIndex : public CBlockIndex
     383 |  class CChain
     384 |  {
     385 |  private:
     386 | -    std::vector<CBlockIndex*> vChain;
     387 | +    std::vector<std::reference_wrapper<CBlockIndex>> vChain;
    


    andrewtoth commented at 10:52 PM on November 3, 2026:

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


    optout21 commented at 10:46 AM on November 5, 2026:

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

  43. in src/net_processing.cpp:1012 in 61566b5a3a
    1009 | -    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);
    1010 | +    /**
    1011 | +     * Request blocks for the background chainstate, if one is in use.
    1012 | +     * vBlocks must not contain nullptr.
    1013 | +     */
    1014 | +    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);
    


    andrewtoth commented at 10:53 PM on November 3, 2026:

    Why not rename vBlocks here to block_indexes?


    optout21 commented at 10:47 AM on November 5, 2026:

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

  44. in src/net_processing.cpp:1577 in 61566b5a3a
    1570 | @@ -1568,15 +1571,16 @@ void PeerManagerImpl::TryDownloadingHistoricalBlocks(const Peer& peer, unsigned
    1571 |          return;
    1572 |      }
    1573 |  
    1574 | -    FindNextBlocks(vBlocks, peer, state, from_tip, count, std::min<int>(from_tip->nHeight + BLOCK_DOWNLOAD_WINDOW, target_block->nHeight));
    1575 | +    FindNextBlocks(vBlocks, peer, state, from_tip, count, std::min<int>(from_tip.nHeight + BLOCK_DOWNLOAD_WINDOW, target_block.nHeight));
    1576 |  }
    1577 |  
    1578 | -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)
    1579 | +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)
    


    andrewtoth commented at 10:56 PM on November 3, 2026:

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


    optout21 commented at 10:49 AM on November 5, 2026:

    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).

  45. optout21 commented at 10:50 AM on November 5, 2026: contributor

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

    git range-diff 61566b5a3a20e8b8f68d7cd1640b755131e9bd82...f3a095ad3c8cc4ac319d37e233df2fb5285d0902

  46. optout21 force-pushed on Nov 5, 2026
  47. bartoli commented at 10:04 AM on November 6, 2026: 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

  48. andrewtoth approved
  49. andrewtoth commented at 7:36 PM on November 21, 2026: contributor

    ACK f3a095ad3c8cc4ac319d37e233df2fb5285d0902

  50. DrahtBot requested review from musaHaruna on Nov 21, 2026
  51. in src/chain.h:387 in f3a095ad3c
     383 | @@ -379,7 +384,7 @@ class CDiskBlockIndex : public CBlockIndex
     384 |  class CChain
     385 |  {
     386 |  private:
     387 | -    std::vector<CBlockIndex*> vChain;
     388 | +    std::vector<std::reference_wrapper<CBlockIndex>> m_chain;
    


    stickies-v commented at 4:21 PM on November 23, 2026:

    nit: might m_entries or m_indexes be a better name?


    optout21 commented at 12:31 PM on November 27, 2026:

    Taken.

  52. in src/chain.h:386 in d41e5ffd98
     382 | @@ -379,7 +383,7 @@ class CDiskBlockIndex : public CBlockIndex
     383 |  class CChain
     384 |  {
     385 |  private:
     386 | -    std::vector<CBlockIndex*> vChain;
     387 | +    std::vector<CBlockIndex*> vChain;  // Pointers, never nullptr
    


    stickies-v commented at 5:52 PM on November 23, 2026:

    In commit d41e5ffd985bae48337f01022e7f2841decc2091:

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


    optout21 commented at 12:33 PM on November 27, 2026:

    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.

  53. in src/net_processing.cpp:1010 in f3a095ad3c
    1007 |  
    1008 | -    /** Request blocks for the background chainstate, if one is in use. */
    1009 | -    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);
    1010 | +    /**
    1011 | +     * Request blocks for the background chainstate, if one is in use.
    1012 | +     * block_indexes must not contain nullptr.
    


    stickies-v commented at 8:07 PM on November 23, 2026:

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


    optout21 commented at 12:33 PM on November 27, 2026:

    Indeed, I was wrong here.

  54. in src/net_processing.cpp:1024 in f3a095ad3c
    1023 | -    * \param count        Maximum number of blocks to allow in vBlocks. No more
    1024 | +    * \param index_walk   The starting block to add to blocks.
    1025 | +    * \param count        Maximum number of blocks to allow in blocks. No more
    1026 |      *                     blocks will be added if it reaches this size.
    1027 | -    * \param nWindowEnd   Maximum height of blocks to allow in vBlocks. No
    1028 | +    * \param window_end   Maximum height of blocks to allow in blocks. No
    


    stickies-v commented at 8:10 PM on November 23, 2026:

    In commit f3a095ad3c8cc4ac319d37e233df2fb5285d0902:

    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.


    optout21 commented at 12:37 PM on November 27, 2026:

    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.

  55. in src/chain.h:403 in f3a095ad3c
     400 |  
     401 |      /** Returns the index entry for the tip of this chain, or nullptr if none. */
     402 |      CBlockIndex* Tip() const
     403 |      {
     404 | -        return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
     405 | +        return m_chain.size() > 0 ? &(m_chain[m_chain.size() - 1].get()) : nullptr;
    


    stickies-v commented at 8:16 PM on November 23, 2026:

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

    <details> <summary>git diff on f3a095ad3c</summary>

    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. */
    
    

    </details>


    optout21 commented at 12:37 PM on November 27, 2026:

    Good point, taken

  56. in src/net_processing.cpp:1021 in f3a095ad3c
    1021 | +    *                     Must not contain nullptr.
    1022 |      * \param peer         Peer which blocks will be downloaded from.
    1023 |      * \param state        Pointer to the state of the peer.
    1024 | -    * \param pindexWalk   Pointer to the starting block to add to vBlocks.
    1025 | -    * \param count        Maximum number of blocks to allow in vBlocks. No more
    1026 | +    * \param index_walk   The starting block to add to blocks.
    


    stickies-v commented at 8:27 PM on November 23, 2026:

    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?


    optout21 commented at 12:38 PM on November 27, 2026:

    Done

  57. DrahtBot requested review from stickies-v on Nov 23, 2026
  58. refactor: In LastCommonAncestor, change CBlockIndex input to reference 1da6e9e566
  59. refactor: In FindNextBlocks, change CBlockIndex input to reference 5b4d84eb78
  60. doc: Code-document that vBlocks never contains null (net_processing) 09cc7b793a
  61. refactor: In TryDownloadingHistoricalBlocks, change CBlockIndex input to reference
    Also rename a parameter (vBlocks -> block_indexes), for consistency (housekeeping).
    
    Co-authored-by: Andrew Toth <andrewstoth@gmail.com>
    1c506401fe
  62. refactor: In LookupX methods, change CBlockIndex input to reference
    Methods affected: BlockFilterIndex::LookupFilterRange, BlockFilterIndex::LookupFilterHashRange,
    and LookupRange, in blockfilterindex.
    11f06ae665
  63. refactor: In ProcessBlock change CBlockIndex input to reference ca26753beb
  64. refactor: Store reference_wrapper items within CChain class
    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.
    88732319a4
  65. refactor: Rename parameters of FindNextBlocks() to snake_case
    Change to more descriptive names, using snake_case
    (drop type prefixes and camel case).
    
    Co-authored-by: Andrew Toth <andrewstoth@gmail.com>
    bad93f7234
  66. optout21 force-pushed on Nov 27, 2026
  67. optout21 commented at 12:52 PM on November 27, 2026: contributor

    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


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-01 23:52 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me