refactor: Change CChain methods to use references, add tests#34440
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline 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. |
|
🚧 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. |
|
Added new commit to change |
|
Added new commit to change |
1f8f7d4 Change BlockRequestAllowed() to take ref (optout) Pull request description: As [suggested here](#34416 (comment)), a minor refactor of `PeerManagerImpl::BlockRequestAllowed()` to take reference parameter (instead of pointer). The motivation is to make the code safer, by minimizing the risk of null-dereference, and to be more consistent. The change is local to the `PeerManagerImpl::BlockRequestAllowed()` class. Related to #34440. ACKs for top commit: maflcko: review ACK 1f8f7d4 🎐 l0rinc: tested ACK 1f8f7d4 stickies-v: ACK 1f8f7d4 Tree-SHA512: 9c2de2d3e7d067e018db7ec54c79f512ccc1da54574d4fb362f6697ee6e235783779d7094cf20856cd34e08a1dbc74609d8351fe7b2287cd8ec0c836ef07be19
d7f296b to
32457f4
Compare
|
Rebased, following #34464 ; one commit got annihilated. |
Add basic unit tests to the `CChain` class, filling a gap. Co-authored-by: l0rinc <pap.lorinc@gmail.com>
Add (lengthier) unit tests for `CChain::FindFork()`. Co-authored-by: l0rinc <pap.lorinc@gmail.com>
The `CChain::Contains()` method dereferences its input without checking, potentially resulting in nullptr-dereference if invoked with `nullptr`. To avoid this possibility, its input is changed to a reference instead. Call sites are adapted accoringly, extra nullptr-check is added as needed.
To minimize chance of erroneous nullptr dereference, `CChain::Next()` is changed to take a reference instead of a pointer. Call sites have been adapted. Notably, NextSyncBlock() now checks the FindFork() result before calling into Next(), because the fork lookup may return null.
The internal null-guard in FindFork() was removed in favor of adding any missing guards at call sites.
ActivateBestChainStep() is always called with non-nullptr pindexMostWork parameter, change the type of the parameter from pointer to reference to enforce this. Also rename the parameter (prefix p doesn't make sense any more).
|
Rebased to master, conflict due to #33477, one less change in
|
hodlinator
left a comment
There was a problem hiding this comment.
re-ACK 7c75244
Reasoning: #34440 (review)
Just smaller diff for "Change CChain::Next() to take reference" since previous ACK.
|
reACK 7c75244 Rebased locally, got the same result, ran tests locally with rebase, they all passed (checked the failing p2p_orphan_handling.py test separately for db56bcd). |
|
CI failure could not be reproduced locally, the root cause is likely unrelated, likely same as #34731. Retrying. |
|
re-review ACK 7c75244 🌅 Show signatureSignature: |
|
ACK 7c75244 |
I wonder if there should be a tracking issue for those, to keep track. No rush, but some of them seem interesting to look into in the long term. |
I haven't consider this topic large/important/systematic enough to warrant a tracking issue or even a plain issue. The mentioned follow-ups are on my list :) |
Refactor
CChainmethods (Contains(),Next(),FindFork()) to use references instead of pointers, to minimize the risk of accidentalnullptrdereference (memory access violation). Also add missing unit tests to theCChainclass.The
CChain::Contains()method (insrc/chain.h) dereferences its input without checking. TheNext()method also calls into this with anullptrif invoked withnullptr. While most call sites have indirect guarantee that the input is notnullptr, it's not easy to establish this to all call sites with high confidence. These methods are publicly available. There is no known high-level use case to trigger this error, but the fix is easy, and makes the code safer.Changes:
CChainclass methodsCChain::FindFork()CChain::Contains()to take referenceCChain::Next()to take referenceCChain::FindFork()to take referencepindexMostWorkparameter ofActivateBestChainStep()to reference* pindex-->& index)Alternative. A simpler change is to stick with pointers, with extra checks where needed, see #34416 .
This change is remotely related to and indirectly triggered by #32875 .
Further ideas, not considered in this PR:
InvalidateBlock()andPreciousBlock()to take references.CChaininternals to store references instead of pointersFindFork,FindEarliestAtLeast,FindForkInGlobalIndex,blockman.AddToBlockIndex, etc.