Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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:
2026-11-27 12:52:18 |
d52fe77 to
dfe6221
Compare
| * \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. |
There was a problem hiding this comment.
nit: developer notes prefer camel_case while you're touching this
musaHaruna
left a comment
There was a problem hiding this 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.
| @@ -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 | |||
There was a problem hiding this comment.
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; // pointer to be able to reserve, but never contains nullptr | |
| std::vector<CBlockIndex*> vChain; // Pointers, never nullptr (std::vector cannot hold references) |
There was a problem hiding this 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?
There was a problem hiding this comment.
@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_wrapperlocally inPeerManagerImpl::FindNextBlocks(). I think theget()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.
| @@ -249,7 +249,7 @@ void BaseIndex::Sync() | |||
| pindex = pindex_next; | |||
|
|
|||
|
|
|||
| if (!ProcessBlock(pindex)) return; // error logged internally | |||
| if (!ProcessBlock(*pindex)) return; // error logged internally | |||
There was a problem hiding this comment.
I think we can explicitly pass the default nullptr with an inline comment here to match the codebase convention.
| if (!ProcessBlock(*pindex)) return; // error logged internally | |
| if (!ProcessBlock(*pindex, /*block_data=*/nullptr)) return; // error logged internally |
There was a problem hiding this comment.
This is not related to this PR, plus I don't see why to explicitly pass an optional parameters, so not taking this.
| @@ -173,5 +174,5 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* | |||
| pa = pa->pprev; | |||
| pb = pb->pprev; | |||
| } | |||
| return pa; | |||
| return *Assume(pa); | |||
There was a problem hiding this comment.
As explained previously, this seems confusing, see #34440 (comment)
There was a problem hiding this comment.
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.
| @@ -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 | |||
There was a problem hiding this 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?
There was a problem hiding this 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 thevToFetchvector, usestd::reference_wrapper<const CBlockIndex>instead of pointers (new commit!) - Some comment adjustments.
- Some minor renaming of touched variable/parameters.
git range-diff dfe62214070408e699067598309b779c1ed1214d...ed44574efd38d4b7815ab96ae6f68f8b02dee88a
| * \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. |
| @@ -249,7 +249,7 @@ void BaseIndex::Sync() | |||
| pindex = pindex_next; | |||
|
|
|||
|
|
|||
| if (!ProcessBlock(pindex)) return; // error logged internally | |||
| if (!ProcessBlock(*pindex)) return; // error logged internally | |||
There was a problem hiding this comment.
This is not related to this PR, plus I don't see why to explicitly pass an optional parameters, so not taking this.
| @@ -173,5 +174,5 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* | |||
| pa = pa->pprev; | |||
| pb = pb->pprev; | |||
| } | |||
| return pa; | |||
| return *Assume(pa); | |||
There was a problem hiding this comment.
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.
| @@ -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 | |||
There was a problem hiding this comment.
@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_wrapperlocally inPeerManagerImpl::FindNextBlocks(). I think theget()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.
ed44574 to
52d0091
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
a9a2eb8 to
82fdd03
Compare
|
Pushed compile fix: initializing the |
82fdd03 to
c6160bc
Compare
|
Performed changes:
|
c6160bc to
a1ed86a
Compare
musaHaruna
left a comment
There was a problem hiding this 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:
- In commit 91e1110
chain.h, Could we include<functional>directly forstd::reference_wrapper, as recommended by the developers note? - 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.
| while (pindex && vChain[pindex->nHeight] != pindex) { | ||
| vChain[pindex->nHeight] = pindex; | ||
| CBlockIndex placeholder; | ||
| vChain.resize(block.nHeight + 1, std::ref(placeholder)); |
There was a problem hiding this comment.
| 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.
There was a problem hiding this 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 (just to refresh it, since it's touched, clean rebase).
(git range-diff 8f3a7096b44aa2203ecdad6800d8c498c87b7d1c...61566b5a3a20e8b8f68d7cd1640b755131e9bd82)
| while (pindex && vChain[pindex->nHeight] != pindex) { | ||
| vChain[pindex->nHeight] = pindex; | ||
| CBlockIndex placeholder; | ||
| vChain.resize(block.nHeight + 1, std::ref(placeholder)); |
a1ed86a to
61566b5
Compare
| * 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); |
There was a problem hiding this comment.
These parameters should be annotated with LIFETIMEBOUND
| const CBlockIndex& LastCommonAncestor(const CBlockIndex& a, const CBlockIndex& b); | |
| const CBlockIndex& LastCommonAncestor(const CBlockIndex& a LIFETIMEBOUND, const CBlockIndex& b LIFETIMEBOUND); |
| { | ||
| private: | ||
| std::vector<CBlockIndex*> vChain; | ||
| std::vector<std::reference_wrapper<CBlockIndex>> vChain; |
There was a problem hiding this comment.
Would it make sense to rename this to m_chain if we are touching it here?
There was a problem hiding this comment.
It makes sense, as its type is changed, most of its occurrences are touched. Done.
| * 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); |
There was a problem hiding this comment.
Why not rename vBlocks here to block_indexes?
There was a problem hiding this comment.
Done as a small housekeeping task (same commit, added co-author).
| } | ||
|
|
||
| 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) |
There was a problem hiding this comment.
Why not rename all these to modern snake_case from hungarianCamelCase? Since we're touching it?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Applied suggestions from @andrewtoth, thanks! (LIFETIMEBOUND, renames).
git range-diff 61566b5a3a20e8b8f68d7cd1640b755131e9bd82...f3a095ad3c8cc4ac319d37e233df2fb5285d0902
| * 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); |
| { | ||
| private: | ||
| std::vector<CBlockIndex*> vChain; | ||
| std::vector<std::reference_wrapper<CBlockIndex>> vChain; |
There was a problem hiding this comment.
It makes sense, as its type is changed, most of its occurrences are touched. Done.
| * 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); |
There was a problem hiding this comment.
Done as a small housekeeping task (same commit, added co-author).
| } | ||
|
|
||
| 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) |
There was a problem hiding this comment.
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).
61566b5 to
f3a095a
Compare
|
nit: PeerHasHeader() of net_processing.cpp might benefit from a change too: |
| { | ||
| private: | ||
| std::vector<CBlockIndex*> vChain; | ||
| std::vector<std::reference_wrapper<CBlockIndex>> m_chain; |
There was a problem hiding this comment.
nit: might m_entries or m_indexes be a better name?
| { | ||
| private: | ||
| std::vector<CBlockIndex*> vChain; | ||
| std::vector<CBlockIndex*> vChain; // Pointers, never nullptr |
There was a problem hiding this comment.
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.
| 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. |
There was a problem hiding this comment.
This sounds like a precondition on the parameter, but I think you meant to say that only non-null entries are appended?
There was a problem hiding this comment.
Indeed, I was wrong here.
| * \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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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; |
There was a problem hiding this comment.
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. */
| * \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. |
There was a problem hiding this comment.
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?
… 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]>
f3a095a to
bad93f7
Compare
optout21
left a comment
There was a problem hiding this 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
| { | ||
| private: | ||
| std::vector<CBlockIndex*> vChain; | ||
| std::vector<std::reference_wrapper<CBlockIndex>> m_chain; |
| { | ||
| private: | ||
| std::vector<CBlockIndex*> vChain; | ||
| std::vector<CBlockIndex*> vChain; // Pointers, never nullptr |
There was a problem hiding this comment.
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.
| 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. |
There was a problem hiding this comment.
Indeed, I was wrong here.
| * \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 |
There was a problem hiding this comment.
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.
| 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; |
| * \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. |

This is a janitorial refactor, a continuation of #34440. Input method parameters of type
const CBlockIndex*are changed to referenceconst CBlockIndex&in some places where it makes no sense to call with a nullptr.Motivation: slight improvement in code quality, maintainability and robustness.
The following methods are affected:
LastCommonAncestor()in chain.hPeerManagerImpl::FindNextBlocks()(net_processing.cpp)PeerManagerImpl::TryDownloadingHistoricalBlocks()(net_processing.cpp)BlockFilterIndex::LookupFilterRange(),BlockFilterIndex::LookupFilterHashRange()(blockfilterindex.h), andLookupRange()in blockfilterindex.cppBaseIndex::ProcessBlock()(base.h)General guidelines:
const CBlockIndex*, and by contract it cannot be nullptr, change it toconst CBlockIndex&. The reference type is an implicit documentation of the nun-nullptr invariant.reserve()std::setwould result in some copy constructor callsstd::optionalcannot hold a reference type