refactor: Use CBlockIndex parameters as reference #35229

Open
optout21 wants to merge 8 commits from 2605-block-param-ref into master AGit
optout21 commented 2026-05-07 07:11:44 +00:00 (Migrated from github.com)

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
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 commented 2026-05-07 07:11:55 +00:00 (Migrated from github.com)

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

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35229 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35229.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35229#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [stickies-v](https://github.com/bitcoin/bitcoin/pull/35229#pullrequestreview-4242099645), [musaHaruna](https://github.com/bitcoin/bitcoin/pull/35229#pullrequestreview-4318505356), [w0xlt](https://github.com/bitcoin/bitcoin/pull/35229#issuecomment-4700259476) | | Stale ACK | [andrewtoth](https://github.com/bitcoin/bitcoin/pull/35229#pullrequestreview-6007711417) | 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: * [#37016](https://github.com/bitcoin/bitcoin/pull/37016) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37016.svg"></sub> (rpc: fail scanblocks instead of silently skipping missing filters by shivansh023023) * [#37015](https://github.com/bitcoin/bitcoin/pull/37015) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37015.svg"></sub> (Use O(log n) skiplist construction in CBlockIndex by sipa) * [#36844](https://github.com/bitcoin/bitcoin/pull/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) * [#35837](https://github.com/bitcoin/bitcoin/pull/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](https://github.com/bitcoin/bitcoin/pull/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) * [#34132](https://github.com/bitcoin/bitcoin/pull/34132) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34132.svg"></sub> (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. <!--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>
stickies-v (Migrated from github.com) reviewed 2026-05-07 07:52:42 +00:00
stickies-v (Migrated from github.com) left a comment

Concept ACK

Concept ACK
stickies-v (Migrated from github.com) commented 2026-05-07 07:52:15 +00:00

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

nit: developer notes prefer camel_case while you're touching this
musaHaruna (Migrated from github.com) reviewed 2026-05-19 11:36:09 +00:00
musaHaruna (Migrated from github.com) left a comment

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.

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.
musaHaruna (Migrated from github.com) reviewed 2026-05-19 11:37:33 +00:00
musaHaruna (Migrated from github.com) commented 2026-05-19 11:37:34 +00:00

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)
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. ```suggestion std::vector<CBlockIndex*> vChain; // Pointers, never nullptr (std::vector cannot hold references)
musaHaruna (Migrated from github.com) reviewed 2026-05-19 11:44:24 +00:00
musaHaruna (Migrated from github.com) commented 2026-05-19 11:44:24 +00:00

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
I think we can explicitly pass the default `nullptr` with an inline comment here to match the codebase convention. ```suggestion if (!ProcessBlock(*pindex, /*block_data=*/nullptr)) return; // error logged internally
maflcko (Migrated from github.com) reviewed 2026-05-19 11:50:45 +00:00
maflcko (Migrated from github.com) commented 2026-05-19 11:49:38 +00:00
As explained previously, this seems confusing, see https://github.com/bitcoin/bitcoin/pull/34440#discussion_r3085393412
maflcko (Migrated from github.com) commented 2026-05-19 11:50:39 +00:00

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?

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 (Migrated from github.com) reviewed 2026-05-20 11:15:03 +00:00
optout21 (Migrated from github.com) left a comment

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

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!](https://github.com/bitcoin/bitcoin/pull/35229/changes/063e705c5c3945477869bea035b181440bc4ac2f)) - Some comment adjustments. - Some minor renaming of touched variable/parameters. `git range-diff dfe62214070408e699067598309b779c1ed1214d...ed44574efd38d4b7815ab96ae6f68f8b02dee88a`
optout21 (Migrated from github.com) commented 2026-05-20 10:27:08 +00:00

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.

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`.
optout21 (Migrated from github.com) commented 2026-05-20 11:11:58 +00:00

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

@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 (Migrated from github.com) commented 2026-05-20 10:18:07 +00:00

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

This is not related to this PR, plus I don't see why to explicitly pass an optional parameters, so not taking this.
optout21 (Migrated from github.com) commented 2026-05-20 10:16:46 +00:00

Done.

Done.
DrahtBot commented 2026-05-20 11:30:47 +00:00 (Migrated from github.com)

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

<!--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>
optout21 commented 2026-05-20 11:37:27 +00:00 (Migrated from github.com)

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
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 commented 2026-05-26 09:18:48 +00:00 (Migrated from github.com)

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

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](https://github.com/bitcoin/bitcoin/pull/35229/changes/a1ed86a7e66c4f13dc58fab71982eba3340602e0)). 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 commented 2026-05-27 06:42:27 +00:00 (Migrated from github.com)

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

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
w0xlt commented 2026-06-14 00:47:18 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
sedited commented 2026-09-22 07:59:28 +00:00 (Migrated from github.com)

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

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

Review failed because required verification did not complete.

Earlier concerns neither republished nor resolved: 1 minor finding · 2 suggestions. See the Run archive linked from the Review report for their assessments.

Review coverage was incomplete.


Reviewed: bad93f723486 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:806938d40e279b6d701b2e78128bb1a09b55193a0b697245a1dce6241d3081c1:3607c760b63c01fb7f1f349adfc3fd0614b148a7df02e42f2f0edc2d448925a1 --> <!-- ralph-base:0629982adad837abb5521f07aa7f269abdc610f6 --> <!-- ralph-head:bad93f723486b2424cfa34d86cfb864989aee3d6 --> <!-- ralph-case:69641aba511ebb78041b4eb1ea84ef07cb7ad9214fee737b2f31a405eec4599e --> Review failed because required verification did not complete. Earlier concerns neither republished nor resolved: 1 minor finding · 2 suggestions. See the Run archive linked from the Review report for their assessments. Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`bad93f723486`](<https://git.fish.foo/bitcoin/bitcoin/commit/bad93f723486b2424cfa34d86cfb864989aee3d6>) · [Review report](<https://review.fish.foo/report/35229-bad93f723486b2424cfa34d86cfb864989aee3d6-3255-6.html>) · [Previous reports](<https://review.fish.foo/traces/35229/>) [permalink](<https://review.fish.foo/report/35229-bad93f723486b2424cfa34d86cfb864989aee3d6-3255-6.html>)
musaHaruna (Migrated from github.com) reviewed 2026-10-23 19:04:19 +00:00
musaHaruna (Migrated from github.com) left a comment

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.
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](https://github.com/bitcoin/bitcoin/pull/35229/changes/a1ed86a7e66c4f13dc58fab71982eba3340602e0#diff-05137bf4d07f31a6cc237b1dd772e0b38bc2a60610a7ca86827e98fc126e8407) `chain.h`, Could we include `<functional>` directly for `std::reference_wrapper`, as recommended by the [developers note](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#source-code-organization)? 2. In [cec59ef](https://github.com/bitcoin/bitcoin/pull/35229/changes/cec59ef3b6c3ff661835fdffd256c6dcbeda9c6a) 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.
musaHaruna (Migrated from github.com) reviewed 2026-10-23 19:04:56 +00:00
musaHaruna (Migrated from github.com) commented 2026-10-23 19:04:56 +00:00
    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.

```suggestion 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 (Migrated from github.com) reviewed 2026-10-27 13:39:58 +00:00
optout21 (Migrated from github.com) left a comment

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.
(git range-diff 8f3a7096b44aa2203ecdad6800d8c498c87b7d1c...61566b5a3a20e8b8f68d7cd1640b755131e9bd82)

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. (`git range-diff 8f3a7096b44aa2203ecdad6800d8c498c87b7d1c...61566b5a3a20e8b8f68d7cd1640b755131e9bd82`)
optout21 (Migrated from github.com) commented 2026-10-27 13:38:41 +00:00

Done.

Done.
andrewtoth (Migrated from github.com) reviewed 2026-11-03 22:56:29 +00:00
andrewtoth (Migrated from github.com) commented 2026-11-03 22:51:41 +00:00

These parameters should be annotated with LIFETIMEBOUND

const CBlockIndex& LastCommonAncestor(const CBlockIndex& a LIFETIMEBOUND, const CBlockIndex& b LIFETIMEBOUND);
These parameters should be annotated with `LIFETIMEBOUND` ```suggestion const CBlockIndex& LastCommonAncestor(const CBlockIndex& a LIFETIMEBOUND, const CBlockIndex& b LIFETIMEBOUND); ```
andrewtoth (Migrated from github.com) commented 2026-11-03 22:52:45 +00:00

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

Would it make sense to rename this to `m_chain` if we are touching it here?
andrewtoth (Migrated from github.com) commented 2026-11-03 22:53:21 +00:00

Why not rename vBlocks here to block_indexes?

Why not rename `vBlocks` here to ` block_indexes`?
andrewtoth (Migrated from github.com) commented 2026-11-03 22:56:24 +00:00

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

Why not rename all these to modern snake_case from hungarianCamelCase? Since we're touching it?
optout21 (Migrated from github.com) reviewed 2026-11-05 10:50:35 +00:00
optout21 (Migrated from github.com) left a comment

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

git range-diff 61566b5a3a20e8b8f68d7cd1640b755131e9bd82...f3a095ad3c8cc4ac319d37e233df2fb5285d0902

Applied suggestions from @andrewtoth, thanks! (`LIFETIMEBOUND`, renames). `git range-diff 61566b5a3a20e8b8f68d7cd1640b755131e9bd82...f3a095ad3c8cc4ac319d37e233df2fb5285d0902`
optout21 (Migrated from github.com) commented 2026-11-05 10:45:48 +00:00

Done

Done
optout21 (Migrated from github.com) commented 2026-11-05 10:46:42 +00:00

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

It makes sense, as its type is changed, most of its occurrences are touched. Done.
optout21 (Migrated from github.com) commented 2026-11-05 10:47:13 +00:00

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

Done as a small housekeeping task (same commit, added co-author).
optout21 (Migrated from github.com) commented 2026-11-05 10:49:40 +00:00

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

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).
bartoli commented 2026-11-06 10:04:10 +00:00 (Migrated from github.com)

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

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 (Migrated from github.com) approved these changes 2026-11-21 19:36:59 +00:00
andrewtoth (Migrated from github.com) left a comment
ACK f3a095ad3c8cc4ac319d37e233df2fb5285d0902
stickies-v (Migrated from github.com) reviewed 2026-11-23 20:27:48 +00:00
stickies-v (Migrated from github.com) commented 2026-11-23 17:52:27 +00:00

In commit d41e5ffd98:

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

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.
stickies-v (Migrated from github.com) commented 2026-11-23 16:21:17 +00:00

nit: might m_entries or m_indexes be a better name?

nit: might `m_entries` or `m_indexes` be a better name?
stickies-v (Migrated from github.com) commented 2026-11-23 20:16:47 +00:00

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

git diff on f3a095ad3c
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. */

nit: `std::vector` offers more idiomatic methods here, while you're touching this: <details> <summary>git diff on f3a095ad3c</summary> ```diff 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>
stickies-v (Migrated from github.com) commented 2026-11-23 20:07:47 +00:00

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

This sounds like a precondition on the parameter, but I think you meant to say that only non-null entries are appended?
stickies-v (Migrated from github.com) commented 2026-11-23 20:27:14 +00:00

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?

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`?
stickies-v (Migrated from github.com) commented 2026-11-23 20:10:56 +00:00

In commit f3a095ad3c:

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.

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 (Migrated from github.com) reviewed 2026-11-27 12:52:04 +00:00
optout21 (Migrated from github.com) left a comment

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

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`
optout21 (Migrated from github.com) commented 2026-11-27 12:33:28 +00:00

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.

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.
optout21 (Migrated from github.com) commented 2026-11-27 12:31:37 +00:00

Taken.

Taken.
optout21 (Migrated from github.com) commented 2026-11-27 12:37:58 +00:00

Good point, taken

Good point, taken
optout21 (Migrated from github.com) commented 2026-11-27 12:33:54 +00:00

Indeed, I was wrong here.

Indeed, I was wrong here.
optout21 (Migrated from github.com) commented 2026-11-27 12:38:15 +00:00

Done

Done
optout21 (Migrated from github.com) commented 2026-11-27 12:37:36 +00:00

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.

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.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35229
No description provided.