mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
refactor: Use NotNull for CreateNewBlock and pimpls #36451
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36451
Loading…
Reference in a new issue
No description provided.
Delete branch "2610-more-not-null-refactor"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Current code uses nullable smart pointers for some stuff that is never null. This is perfectly fine, but a bit confusing:
Clarify this at compile-time by using
NotNull(or a plain reference)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.
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:
src/test/fuzzand treat them as errors by hebasto)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.
🔵 Suggestion · getblocktemplate retains a dead post-creation null check
The new
NotNullUniquePtrreturn contract leavesgetblocktemplate's pre-existing post-assignment null check redundant, retaining an untestable error branch that the other mining RPC changes remove.Location:
src/rpc/mining.cpp:909(head).🔵 Suggestion · Pimpl conversions leave four unused includes
After the pimpl conversions, five headers still
#include <memory>but no longer name anything from it directly:src/addrman.h,src/node/txreconciliation.h,src/txrequest.h,src/test/fuzz/util/check_globals.handsrc/dbwrapper.h. Their pimpl members, andCDBIterator's constructor parameter, are nowutil::NotNullUniquePtr, andutil/not_null.hboth defines that alias and includes<memory>. The earlier advice to keep<memory>indbwrapper.his out of date, because its constructor no longer namesstd::unique_ptr. This is optional include cleanup with no build or runtime effect. IWYU was not run to confirm it would suggest these removals.Location:
src/addrman.h:11(head).🔵 Suggestion · waitNext reconstructs a NotNull guarantee discarded by the manager
BlockTemplateImpl::waitNext(src/node/interfaces.cpp:928) wraps the result ofWaitAndCreateNewBlockinutil::NotNull{std::move(new_template)}, which runsNotNull's asserting constructor again on a template that was already typed non-null.WaitAndCreateNewBlockgets autil::NotNullUniquePtr<CBlockTemplate>fromCreateNewTemplate(src/node/block_template_manager.cpp:168) but returns it as a nullablestd::unique_ptrat lines 171 and 181. The guarantee is lost at the return boundary and rebuilt at runtime. The nullable return is still needed for interrupt (line 140), shutdown (line 144) and timeout (line 187), and the current code is correct. This PR introduced the re-wrap; base passed thestd::unique_ptrstraight through.Location:
src/node/interfaces.cpp:924(head).Review coverage was incomplete.
Reviewed:
fa9158641839· Review report · Previous reportspermalink
Concept ACK
The Windows MSVC bugs are a bit tiring. Can someone remove it? Ref: https://github.com/bitcoin/bitcoin/issues/36153
Concept ACK
Here is an MRE: https://godbolt.org/z/Kadaq5h46.
However, https://en.cppreference.com/cpp/compiler_support/20 lists P1825R0 as implemented in MSVC since v19.24 VS 2019 16.4.
Concept ACK, at least in the context of
CreateNewBlockinfa0e2f600bit adds much useful clarity.In
fa0e2f600brefactor: Return NotNull from CreateNewBlock: this might be worth its own commit, e.g.github.com/Sjors/bitcoin@d5591a9143In
fa0e2f600brefactor: Return NotNull from CreateNewBlock: having to addutil::NotNull{...}is a bit tedious. But if you forget, the error is clear enough:(the current code is fine, I don't think we should make the constructor implicit)
Yes, being explicit is intentional, since the
NotNullctor acts as anAsserton the value.I think it is easier to review knowing that it can not be null, so I'll leave as-is for now, but happy to rebase on your commit, if other reviewers prefer that.
edit: took ralph nit
ACK 8f4c3d9f5eddde27ab316cb2b2c7a96610258d57
@Sjors Wrong thread? Usually my commits start with
fa..., so this seems off.Oops, wrong hash, not wrong thread.
ACK
fa7e949f74Funny that the nullptr return in
CreateNewBlockwas dead since its introduction.ACK
fa7e949f74Checked that
CreateNewBlockcan't return null, that the converted pimpls are always initialized and never reset, and that the only movable one (MuSig2SecNonce) isn't used after a move.Remaining nits are fine in follow-ups.
There are a few noisy
util::NotNull{std::make_unique<T>(...)}usages here (dbwrapper.cpp,net.cpp(×2),txmempool.h), in a follow-up we could add autil::MakeNotNullUnique:nit: not for this PR, but if you touch this line again (and don't mind turning a missing tip into an abort), maybe this would be a bit simpler (and would match the comment above it):
nit: unrelated, but since we're touching
CreateNewBlock, we could also avoid the nakednewhere:There's another one in
github.com/bitcoin/bitcoin@6f1365e34f/src/rpc/mining.cpp (L911)that we could removeSure, could roll into a new commit or new pull
Sure, can be added in a new commit or new pull
I presume the Assume was written intentionally, so I think I'll leave as-is for now.
I can see that it can be removed, but I think it is fine to keep, because the type is still a nullable uniq ptr. The general idea was that the compiler would force you at compile time to remove a check if it didn't make any sense. This one, I can see go both ways, so I'll leave as-is
It was intentional: https://github.com/bitcoin/bitcoin/pull/31283#discussion_r1937078288
Could be reconsidered, but that seems better for a seperate PR.
Approach ACK
Do you want to add the pimpl
m_internalsinvalidationinterface.h? Would also support the comments that you left unresolved from l0rinc being taken here.thx, added a new commit in the beginning
thx, modified the last commit
🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/43239307477/job/131255305827LLM reason (✨ experimental): CI failed because IWYU added a missing
<memory>include tosrc/addrman.h, leaving an uncommitted diff that the check rejects.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.
reACK
fa2187d8f7Since my last review, added non-null smart-pointer factories and simplified block-template allocation.
Happy to re-review if @sedited’s suggestion is also applied.
ACK
fa91586418CreateNewBlockeither returns a template or throws, so those null checks were dead and the wait path still checks for null before wrapping it. Also checked that invalidating a MuSig nonce leaves the pimpl intacreACK
fa91586418Latest push makes
ValidationSignals::m_internalsnon-null - its sole constructor allocates it, and nothing resets or moves it out.reACK
fa91586418Code review ACK
fa91586418. Nice cleanup: makes it obvious thatCreateNewBlockcan't fail and that these pimpls are always set, and removes the dead error paths that went with them.In commit "util: refactor: Add and use MakeNotNullShared and MakeNotNullUnique" (
fa4785828e)Not a blocker, and the ship may have sailed, but I think these names are not great. I'd suggest
UniqueRef<T>andSharedRef<T>(withMakeUniqueRefandMakeSharedRef) instead ofNotNullUniquePtr<T>andNotNullSharedPtr<T>.There is a big semantic difference between
unique_ptrandshared_ptr: they handle lifetimes completely differently. Burying the unique vs shared distinction in the middle of the name makes a truly meaningful distinction look like noise, and makes code using these types harder to read than it needs to be. Knowing a pointer is not null is nice, because it lets you make assumptions and simplify code paths, but it's a less important property that the type name shouldn't lead with. I'd hope not to see more names likeNotNullTransactionReffollowing this pattern."Ref" seems like the right word, because a non-null smart pointer works like a reference that owns or shares what it refers to. WebKit uses the same naming:
UniqueReffor a non-nullunique_ptr, andRefvsRefPtrfor non-null vs nullable refcounted pointers.This is just an opinion from a potential user of these types, so feel free to take it or leave it.
Hmm, I guess this could make sense as a scripted-diff, because:
reference_wrapper(a not null ptr in the std lib)However, stuff like
if (ptr) return util::NotNull{std::move(ptr)};would then appear inconsistent, so I am not sure about this rename?In commit "refactor: Mark pimpls as NotNull" (
fa071a2c45)With the current
NotNullconstructor, PRs like this one that introduceNotNullcan look safer than they are, because changing a member to aNotNulltype addsAssertcalls that don't show up in the diff. It would be better to writeNotNullinitializers so the check is visible, usingMakeNotNullUnique/MakeNotNullSharedorutil::NotNull{...}.For example, with
m_implaNotNullUniquePtr, this aborts ifLoadImpl()returns null, but nothing on the line says so:Forwarding functions do the same:
vec.emplace_back(LoadImpl())into a vector ofNotNull.Most other places don't have this problem, by design, because the
NotNullconstructor isexplicit. Member initializer lists and forwarding functions are the exceptions.This PR has seven initializers like the one above. They all use
make_unique, so they're fine, but checking them means looking outside the diff. In this PR, they could have been made clearer with:diff
Other PRs could follow the same convention.
The compiler could also catch these cases by making the constructor that takes a plain smart pointer private, so a
NotNullcould only be created withMakeNotNullUnique/MakeNotNullSharedor a checking function likeutil::CheckNotNull(expr).ا
اال
Maybe that is a good thing, because a manual NotNull construction (outside of the
Make...()helpers) should be rare and it may be beneficial to make it stand out a bit more.Happy to push such a scripted-diff, or review a pull with it.