#35229 refactor: Use CBlockIndex parameters as reference
| Reviewability | Reviews | Agreement | Size | ||||
|---|---|---|---|---|---|---|---|
| #35229 refactor: Use CBlockIndex parameters as reference | Ready | 0 | Strong | 72 + 7 tests | |||
|
|
|
|
| |||
Reviewability: ReadyStalePaused Agreement: StrongPositivePositive w/ caveatsNeutralMildDisputedBlockedCrickets Reviews: current code-review ACKs, then (+stale ACKs) and -NACKs; greener = more ACKs. Size: lines added or modified outside tests, then in tests; greener = smaller. | |||||||
Goal
This PR refactors multiple internal functions across chain management, block index handling, block filters, and peer processing to take `CBlockIndex` by reference instead of pointer where null arguments are prohibited. It also switches `CChain`'s internal vector storage from raw `CBlockIndex*` pointers to `std::reference_wrapper<CBlockIndex>`.
Problem: Functions accepting pointer arguments where null is invalid fail to communicate or enforce the non-null invariant through the type system, creating risks of unexpected nullptr dereferences or requiring redundant defensive checks.
Evidence of importance:
- Author describes the change as a 'janitorial refactor, a continuation of #34440' for 'slight improvement in code quality, maintainability and robustness' with 'more explicit representation of the non-null input invariant'.
Risk of a mistake: medium (validation, p2p). Unchecked dereferencing of a pointer passed into the newly converted reference parameters could trigger an assertion failure or crash the node during chain sync or P2P message handling.
Changed since the previous assessment: No functional changes to the scope; appears to be a rebase or minor code cleanup maintaining the same commit structure and affected methods.
Reviewability
Ready
Nothing known that would invalidate a review now.
Waiting on (from the thread read):
- reviewer: musaHaruna and others to review the updated commits following suggestions and rebase 2026-10-27
Agreement
Strong
substantive support, no open objection (musaHaruna, musaHaruna)
Thread notes: The author responded to musaHaruna's remaining suggestions, pushed fixes for them alongside self-review tweaks, and rebased.
Objections, suggestions, and questions:
| Reviewer | Type | Harm, request, or question | Status | Must be | Author replied | Quote |
|---|---|---|---|---|---|---|
| musaHaruna (contributor) | suggestion, maintenance | make the comment concise by removing mention of reserve and stating the core invariant | fixed · fix pushed · author simplified the comment in a push | answered | yes | 2026-05-19: 'Nit: We can make this comment a bit more concise and technically precise.' Status from: 2026-05-20: optout21: 'Simplified the comment, as suggested, to only "Pointers, never nullptr"' |
| musaHaruna (contributor) | suggestion, interface | explicitly pass the default nullptr with an inline comment to match codebase convention | answered · author declined, stating it is unrelated to the PR and optional parameters should not be passed explicitly | answered | yes | 2026-05-19: 'I think we can explicitly pass the default `nullptr` with an inline comment here to match the codebase convention.' Status from: 2026-05-20: optout21: 'This is not related to this PR, plus I don't see why to explicitly pass an optional parameters, so not taking this.' |
| maflcko (member) | objection, correctness | confusing return-value invariant assumption in LastCommonAncestor | fixed · fix pushed · author explained call-site genesis invariant and changed Assume to Assert | changed | yes | 2026-05-19: 'As explained previously, this seems confusing, see https://github.com/bitcoin/bitcoin/pull/34440#discussion_r3085393412' Status from: 2026-05-20: optout21: 'I've double-checked the usages (call sites), and this can't happen -- all chain segments must have the same genesis. The' |
| maflcko (member) | suggestion, approach | use std::reference_wrapper instead of raw pointers and a code comment | answered · fix pushed · author adopted std::reference_wrapper for CChain::vChain and replied | answered | yes | 2026-05-19: '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 com' Status from: 2026-05-26: optout21: 'Did change `CChain::vChain` array to store `std::reference_wrapper<CBlockIndex>` items instead of pointers' |
| musaHaruna (contributor) | suggestion, maintenance | include <functional> directly for std::reference_wrapper | fixed · fix pushed · author applied the suggestion and pushed | answered | yes | 2026-10-23: 'Could we include `<functional>` directly for `std::reference_wrapper`, as recommended by the developers note?' Status from: 2026-10-27: optout21: 'Applied your suggestions.' |
| musaHaruna (contributor) | suggestion, style | clarify commit message for cec59ef to describe adding comments instead of assertion | fixed · fix pushed · author applied the suggestion and pushed | answered | yes | 2026-10-23: '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.' Status from: 2026-10-27: optout21: 'Applied your suggestions.' |
| musaHaruna (contributor) | suggestion, approach | use std::ref(block) directly as resize fill value rather than constructing a local CBlockIndex placeholder | fixed · fix pushed · author confirmed and pushed the change | answered | yes | 2026-10-23: 'Could we use `std::ref(block)` directly as the `resize()` fill value instead of constructing a local `CBlockIndex placeholder`?' Status from: 2026-10-27: optout21: 'Done.' |
Support:
- stickies-v (member) (Concept ACK, 2026-05-07): (no reason given) [not substantive]
- musaHaruna (contributor) (Concept ACK, 2026-05-19) [read]: 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
- w0xlt (contributor) (Concept ACK, 2026-06-14): (no reason given) [not substantive]
- musaHaruna (contributor) (2026-10-23) [read]: Reviewed the reference conversions, their call sites, and the relevant non-null and ancestry assumptions. I think the code looks good.
Participants:
- stickies-v (member, support, 2 statements 2026-05-07 to 2026-05-07): Concept ACK and a naming nit
- maflcko (member, objection, 2 statements 2026-05-19 to 2026-05-19): pointed out confusing invariant assumption in LastCommonAncestor and suggested std::reference_wrapper
- musaHaruna (contributor, support, 5 statements 2026-05-19 to 2026-10-23): Concept ACK and code review with suggestions on includes and resize fill value
- w0xlt (contributor, support, 1 statements 2026-06-14 to 2026-06-14): Concept ACK
- sedited (member, neutral, 1 statements 2026-09-22 to 2026-09-22): pinged reviewers for another review
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/chain.h +10/-6
- src/index/blockfilterindex.cpp +8/-8
- 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
The short text the ranking pass compares across PRs.
This pull request converts `CBlockIndex` pointer arguments to reference parameters across `LastCommonAncestor`, block filter lookups, `BaseIndex::ProcessBlock`, and several `PeerManagerImpl` block download helpers where null is invalid. It also changes `CChain`'s internal vector storage to hold `std::reference_wrapper<CBlockIndex>` instead of raw pointers. The change addresses internal type safety and non-null invariant documentation across consensus-adjacent and P2P code paths. The author presents this purely as a janitorial code-quality refactor continuing from merged PR #34440 with no external payoff.
Data
thread read through 2026-10-27 (21 statements), head 61566b5a3a
labels: Refactoring
Categories
Validation
P4 · cleanup
This janitorial refactor updates CChain storage and function signatures to pass CBlockIndex by reference where non-null invariants hold. As the author states, it is a 'janitorial refactor' providing 'slight improvement in code quality, maintainability and robustness' with no impact on consensus, block connection, or chainstate correctness.
Membership: Directly refactors block-index management classes and functions in src/chain.h and src/chain.cpp, including CChain's internal storage.
Factors: security/stability 0, bug 0, performance 0, user value 0, leverage 0