#35229 refactor: Use CBlockIndex parameters as reference

full analysis

https://github.com/bitcoin/bitcoin/pull/35229 · optout21 · +78/-68 in 12 files, 7 commits · labels: Refactoring

Goal

  • Prevent accidental null dereferences by enforcing non-null invariants at compile time
  • Protects developers from runtime null errors across chain and index logic

This PR converts `const CBlockIndex*` parameters to `const CBlockIndex&` across several internal methods in validation, p2p, and indexes where null pointers are not permissible. It also updates `CChain::vChain` to hold `std::reference_wrapper<CBlockIndex>` elements instead of bare pointers to enforce non-null invariants.

Problem: Functions that expect valid block index pointers currently accept nullable pointer types, which requires implicit assumptions or runtime checks rather than compile-time enforcement of non-nullness.

Category: Indexes (#7 of 7)

P4 · cleanup

  • P4 because method signature changes here have no impact on index correctness or sync speed
  • Provides only minor maintainability value without altering how block filters or indexes run

Changing block filter index method arguments from pointer to reference is a cosmetic type-safety cleanup with no measurable index performance or correctness impact.

Membership: Modifies BaseIndex::ProcessBlock and BlockFilterIndex::LookupFilterRange signatures in src/index/.

Factors: security/stability 0, bug 0, performance 0, user value 0, leverage 0

Category: P2P (#64 of 65)

P4 · cleanup

  • Converting pointer arguments in PeerManagerImpl to references is a minor internal refactor that does not modify peer relay or network behavior.

Converting pointer arguments in PeerManagerImpl to references is a minor internal refactor that does not modify peer relay or network behavior.

Membership: Updates internal block download helper signatures in src/net_processing.cpp.

Factors: security/stability 0, bug 0, performance 0, user value 0, leverage 0

Category: Validation (#47 of 48)

P4 · cleanup

  • P4 because it is a routine internal cleanup with no consensus bug fixes or performance gains
  • The benefit is limited to compile-time safety checks within internal chain state management

Replacing CBlockIndex pointers with references and std::reference_wrapper in CChain is an internal janitorial cleanup that does not alter consensus or chain validation rules.

Membership: Modifies CChain internal container and LastCommonAncestor in src/chain.cpp and src/chain.h.

Factors: security/stability 0, bug 0, performance 0, user value 0, leverage 0

Reviewability: Stale: Author silent

  • Author has been silent for 113 days, exceeding the stale author threshold
  • Reviewing now may be unproductive until the author returns to address feedback

The author has been silent for 113 days since the last push on 2026-05-27, exceeding the 60-day project staleness threshold.

Author status: silent since 2026-05-27

Resolved concerns:

  • maflcko pointed out confusion around LastCommonAncestor return invariants; author changed Assume to Assert and clarified the shared genesis requirement.
  • maflcko suggested using std::reference_wrapper in CChain to enforce non-nullness without comment reliance; author implemented it in CChain::vChain.

Agreement: Strong

  • Concept approved with broad agreement and no objections raised
  • Support for compile-time safety and clearer non-null guarantees (musaHaruna)
  • Concept approval without stated reasons (stickies-v, w0xlt)
  • Suggested using reference wrappers for internal chain storage (maflcko)

Strong concept agreement from multiple reviewers; inline comments on invariants resolved by author pushes.

Multiple contributors gave Concept ACKs, with musaHaruna articulating clear benefits for non-null compile-time guarantees. The technical feedback from maflcko was incorporated into subsequent commits.

  • musaHaruna (2026-05-19): 'Changing parameters from const CBlockIndex* to const CBlockIndex& where nullptr is not a valid state improves clarity and gives stronger compile-time guarantees'
  • stickies-v (2026-05-07): 'Concept ACK'
  • w0xlt (2026-06-14): 'Concept ACK'

Objections:

ReviewerKindHarmStatusBlockingAuthor repliedQuote
maflckoapproachConfusing return-value invariant assumption in LastCommonAncestorresolvednoyes2026-05-19: 'As explained previously, this seems confusing, see #34440#discussion_r3085393412'
Settled: 2026-05-20: 'Indeed, thanks for the reminder! I wanted to have here something to "code-show" the return-is-not-nullptr assumption... changed it to Assert.'

Support:

  • musaHaruna: 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
  • stickies-v: Concept ACK [not substantive]
  • w0xlt: Concept ACK [not substantive]

Participants: stickies-v (support), musaHaruna (support), maflcko (objection), w0xlt (support)

State derived from the lists: substantive support, no open objection (musaHaruna)

Review verdicts (DrahtBot): 0

Files

14 lines under test/bench/ci.

  • src/net_processing.cpp +24/-20
  • src/chain.cpp +14/-11
  • src/index/base.cpp +9/-9
  • src/index/blockfilterindex.cpp +8/-8
  • src/chain.h +9/-6
  • src/test/blockfilter_index_tests.cpp +6/-6
  • src/index/blockfilterindex.h +2/-2
  • src/rpc/blockchain.cpp +2/-1
  • src/validation.cpp +1/-2
  • src/index/base.h +1/-1
  • src/node/interfaces.cpp +1/-1
  • src/test/chain_tests.cpp +1/-1

Card

This PR refactors several CBlockIndex parameter signatures across validation, p2p, and index modules from pointers to references to enforce non-nullness at compile time. It also updates CChain::vChain to hold std::reference_wrapper elements. Multiple reviewers have voiced concept support for the compile-time invariant enforcement, and initial technical feedback was addressed. However, the author has been inactive for over 110 days, making the pull request stale.

Data

dossier JSON · extract JSON · model openrouter/google/gemini-3.8-flash, generated 2026-09-17T21:30, confidence high, input hash 0d3790ccfb3e2819