refactor: Use NotNull for CreateNewBlock and pimpls #36451

Manually merged
maflcko merged 4 commits from 2610-more-not-null-refactor into master 2026-11-16 15:19:54 +00:00 AGit
maflcko commented 2026-10-07 11:35:08 +00:00 (Migrated from github.com)

Current code uses nullable smart pointers for some stuff that is never null. This is perfectly fine, but a bit confusing:

  • When reading the code, it is not clear which pointer is nullable or never null.
  • Pointers that are never null still have confusing null checks (with possible error handling return branches), which are always dead in coverage reports or when applying mutation testing.
  • While some places have the redundant null checks on pointers that are never null, other places do not, which adds to the confusion.

Clarify this at compile-time by using NotNull (or a plain reference)

Current code uses nullable smart pointers for some stuff that is never null. This is perfectly fine, but a bit confusing: * When reading the code, it is not clear which pointer is nullable or never null. * Pointers that are never null still have confusing null checks (with possible error handling return branches), which are always dead in coverage reports or when applying mutation testing. * While some places have the redundant null checks on pointers that are never null, other places do not, which adds to the confusion. Clarify this at compile-time by using `NotNull` (or a plain reference)
DrahtBot commented 2026-10-07 11:35:20 +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
ACK enochaz, l0rinc, ViniciusCestarii, ryanofsky
Concept ACK stickies-v
Approach ACK sedited
Stale ACK Sjors

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:

  • #36808 (iwyu: Fix warnings in src/test/fuzz and treat them as errors by hebasto)
  • #36598 ([Demo] validation: remove AssumeUTXO by l0rinc)
  • #36525 (mining: add getTotalFees(), protect m_block_template with mutex by Sjors)
  • #36356 (mining: move getblocktemplate cache into BlockTemplateManager by w0xlt)
  • #35671 (mining: add TxCollection to bandwidth-efficiently validate external block templates by Sjors)
  • #35591 ([DO NOT MERGE] Erlay: bandwidth-efficient transaction relay protocol (Full implementation) by sr-gi)
  • #34132 (coins,dbwrapper: make point-read and txindex lookup failures fatal by l0rinc)
  • #33922 (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors)
  • #33117 (Interfaces: Expose UTXO Snapshot Loading and Add Progress Notifications by D33r-Gee)
  • #32468 (rpc: generateblock to allow multiple outputs with custom payouts by polespinasa)

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.

<!--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/36451 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36451.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36451#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 | | ---- | --------- | | ACK | [enochaz](https://github.com/bitcoin/bitcoin/pull/36451#pullrequestreview-5891924837), [l0rinc](https://github.com/bitcoin/bitcoin/pull/36451#issuecomment-6855152184), [ViniciusCestarii](https://github.com/bitcoin/bitcoin/pull/36451#issuecomment-6862086074), [ryanofsky](https://github.com/bitcoin/bitcoin/pull/36451#pullrequestreview-5922167024) | | Concept ACK | [stickies-v](https://github.com/bitcoin/bitcoin/pull/36451#issuecomment-6038861130) | | Approach ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/36451#pullrequestreview-5873480073) | | Stale ACK | [Sjors](https://github.com/bitcoin/bitcoin/pull/36451#pullrequestreview-5839039964) | 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: * [#36808](https://github.com/bitcoin/bitcoin/pull/36808) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36808.svg"></sub> (iwyu: Fix warnings in `src/test/fuzz` and treat them as errors by hebasto) * [#36598](https://github.com/bitcoin/bitcoin/pull/36598) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36598.svg"></sub> ([Demo] validation: remove AssumeUTXO by l0rinc) * [#36525](https://github.com/bitcoin/bitcoin/pull/36525) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36525.svg"></sub> (mining: add getTotalFees(), protect m_block_template with mutex by Sjors) * [#36356](https://github.com/bitcoin/bitcoin/pull/36356) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36356.svg"></sub> (mining: move getblocktemplate cache into BlockTemplateManager by w0xlt) * [#35671](https://github.com/bitcoin/bitcoin/pull/35671) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35671.svg"></sub> (mining: add TxCollection to bandwidth-efficiently validate external block templates by Sjors) * [#35591](https://github.com/bitcoin/bitcoin/pull/35591) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35591.svg"></sub> ([DO NOT MERGE] Erlay: bandwidth-efficient transaction relay protocol (Full implementation) by sr-gi) * [#34132](https://github.com/bitcoin/bitcoin/pull/34132) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34132.svg"></sub> (coins,dbwrapper: make point-read and txindex lookup failures fatal by l0rinc) * [#33922](https://github.com/bitcoin/bitcoin/pull/33922) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33922.svg"></sub> (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors) * [#33117](https://github.com/bitcoin/bitcoin/pull/33117) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33117.svg"></sub> (Interfaces: Expose UTXO Snapshot Loading and Add Progress Notifications by D33r-Gee) * [#32468](https://github.com/bitcoin/bitcoin/pull/32468) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/32468.svg"></sub> (rpc: generateblock to allow multiple outputs with custom payouts by polespinasa) 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-->
First-time contributor

🔵 Suggestion · getblocktemplate retains a dead post-creation null check

The new NotNullUniquePtr return contract leaves getblocktemplate'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.h and src/dbwrapper.h. Their pimpl members, and CDBIterator's constructor parameter, are now util::NotNullUniquePtr, and util/not_null.h both defines that alias and includes <memory>. The earlier advice to keep <memory> in dbwrapper.h is out of date, because its constructor no longer names std::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 of WaitAndCreateNewBlock in util::NotNull{std::move(new_template)}, which runs NotNull's asserting constructor again on a template that was already typed non-null. WaitAndCreateNewBlock gets a util::NotNullUniquePtr<CBlockTemplate> from CreateNewTemplate (src/node/block_template_manager.cpp:168) but returns it as a nullable std::unique_ptr at 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 the std::unique_ptr straight through.

Location: src/node/interfaces.cpp:924 (head).

Review coverage was incomplete.


Reviewed: fa9158641839 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:464886e574fc96b3587e582688c29b6bd7a73bba755cfc1d2f031cc648c7ec97:a99dfe27a5d9c587aaf93be1a4842812699d6122d1d9babba7210869336cc02c --> <!-- ralph-base:9183edd25e60aa410bc3e4728989a1d75c309a09 --> <!-- ralph-head:fa9158641839e6dbc3cab63b07929998247b1f97 --> <!-- ralph-case:8fb92baa316edb07365804ad694fab9d261d1c26531cdce019ca0b244bf424b7 --> **🔵 Suggestion · getblocktemplate retains a dead post-creation null check** The new `NotNullUniquePtr` return contract leaves `getblocktemplate`'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`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/fa9158641839e6dbc3cab63b07929998247b1f97/src/rpc/mining.cpp#L909>) (head). **🔵 Suggestion · Pimpl conversions leave four unused <memory> 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.h` and `src/dbwrapper.h`. Their pimpl members, and `CDBIterator`'s constructor parameter, are now `util::NotNullUniquePtr`, and `util/not_null.h` both defines that alias and includes `<memory>`. The earlier advice to keep `<memory>` in `dbwrapper.h` is out of date, because its constructor no longer names `std::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`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/fa9158641839e6dbc3cab63b07929998247b1f97/src/addrman.h#L11>) (head). **🔵 Suggestion · waitNext reconstructs a NotNull guarantee discarded by the manager** `BlockTemplateImpl::waitNext` (`src/node/interfaces.cpp:928`) wraps the result of `WaitAndCreateNewBlock` in `util::NotNull{std::move(new_template)}`, which runs `NotNull`'s asserting constructor again on a template that was already typed non-null. `WaitAndCreateNewBlock` gets a `util::NotNullUniquePtr<CBlockTemplate>` from `CreateNewTemplate` (`src/node/block_template_manager.cpp:168`) but returns it as a nullable `std::unique_ptr` at 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 the `std::unique_ptr` straight through. Location: [`src/node/interfaces.cpp:924`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/fa9158641839e6dbc3cab63b07929998247b1f97/src/node/interfaces.cpp#L924>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`fa9158641839`](<https://git.fish.foo/bitcoin/bitcoin/commit/fa9158641839e6dbc3cab63b07929998247b1f97>) · [Review report](<https://review.fish.foo/report/36451-fa9158641839e6dbc3cab63b07929998247b1f97-2634-9.html>) · [Previous reports](<https://review.fish.foo/traces/36451/>) [permalink](<https://review.fish.foo/report/36451-fa9158641839e6dbc3cab63b07929998247b1f97-2634-9.html>)
sedited commented 2026-10-07 11:54:08 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
maflcko commented 2026-10-07 12:06:44 +00:00 (Migrated from github.com)
The Windows MSVC bugs are a bit tiring. Can someone remove it? Ref: https://github.com/bitcoin/bitcoin/issues/36153
stickies-v commented 2026-10-07 13:22:29 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
hebasto commented 2026-10-14 07:55:57 +00:00 (Migrated from github.com)
> The Windows MSVC bugs are a bit tiring. 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.
Sjors (Migrated from github.com) reviewed 2026-10-20 04:01:42 +00:00
Sjors (Migrated from github.com) left a comment

Concept ACK, at least in the context of CreateNewBlock in fa0e2f600b it adds much useful clarity.

Concept ACK, at least in the context of `CreateNewBlock` in fa0e2f600bf901f7c8d4e1574b4d3ad1e80a5cff it adds much useful clarity.
Sjors (Migrated from github.com) commented 2026-10-20 03:42:45 +00:00

In fa0e2f600b refactor: Return NotNull from CreateNewBlock: this might be worth its own commit, e.g. github.com/Sjors/bitcoin@d5591a9143

In fa0e2f600bf901f7c8d4e1574b4d3ad1e80a5cff _refactor: Return NotNull from CreateNewBlock_: this might be worth its own commit, e.g. https://github.com/Sjors/bitcoin/commit/d5591a9143cae9829fb1c8a375b88b4297ff7070
Sjors (Migrated from github.com) commented 2026-10-20 03:51:46 +00:00

In fa0e2f600b refactor: Return NotNull from CreateNewBlock: having to add util::NotNull{...} is a bit tedious. But if you forget, the error is clear enough:

note: explicit constructor is not a candidate
   49 |     constexpr explicit NotNull(U&& u) noexcept(...)
      |                        ^

(the current code is fine, I don't think we should make the constructor implicit)

In fa0e2f600bf901f7c8d4e1574b4d3ad1e80a5cff _refactor: Return NotNull from CreateNewBlock_: having to add `util::NotNull{...}` is a bit tedious. But if you forget, the error is clear enough: ``` note: explicit constructor is not a candidate 49 | constexpr explicit NotNull(U&& u) noexcept(...) | ^ ``` (the current code is fine, I don't think we should make the constructor implicit)
maflcko (Migrated from github.com) reviewed 2026-10-20 06:25:37 +00:00
maflcko (Migrated from github.com) commented 2026-10-20 06:25:37 +00:00

Yes, being explicit is intentional, since the NotNull ctor acts as an Assert on the value.

Yes, being explicit is intentional, since the `NotNull` ctor acts as an `Assert` on the value.
maflcko (Migrated from github.com) reviewed 2026-10-20 06:26:20 +00:00
maflcko (Migrated from github.com) commented 2026-10-20 06:26:20 +00:00

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.

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.
maflcko commented 2026-11-06 15:23:55 +00:00 (Migrated from github.com)

edit: took ralph nit

edit: took ralph nit
Sjors (Migrated from github.com) approved these changes 2026-11-10 08:44:33 +00:00
Sjors (Migrated from github.com) left a comment

ACK 8f4c3d9f5eddde27ab316cb2b2c7a96610258d57

ACK 8f4c3d9f5eddde27ab316cb2b2c7a96610258d57
maflcko commented 2026-11-10 08:48:13 +00:00 (Migrated from github.com)

@Sjors Wrong thread? Usually my commits start with fa..., so this seems off.

@Sjors Wrong thread? Usually my commits start with `fa...`, so this seems off.
Sjors commented 2026-11-10 11:59:43 +00:00 (Migrated from github.com)

Oops, wrong hash, not wrong thread.

Oops, wrong hash, not wrong thread.
ViniciusCestarii (Migrated from github.com) reviewed 2026-11-10 18:38:27 +00:00
ViniciusCestarii (Migrated from github.com) left a comment

ACK fa7e949f74

Funny that the nullptr return in CreateNewBlock was dead since its introduction.

ACK fa7e949f74168bf02a6b7abcb1687141c8c58fd0 Funny that the nullptr return in `CreateNewBlock` was dead since its introduction.
l0rinc (Migrated from github.com) approved these changes 2026-11-11 07:52:31 +00:00
l0rinc (Migrated from github.com) left a comment

ACK fa7e949f74

Checked that CreateNewBlock can'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.

ACK fa7e949f74168bf02a6b7abcb1687141c8c58fd0 Checked that `CreateNewBlock` can'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.
l0rinc (Migrated from github.com) commented 2026-11-11 07:36:05 +00:00

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 a util::MakeNotNullUnique:

/// Like std::make_unique, but returns a NotNullUniquePtr.
template <typename T, typename... Args>
NotNullUniquePtr<T> MakeNotNullUnique(Args&&... args)
{
    return NotNull{std::make_unique<T>(std::forward<Args>(args)...)};
}
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 a `util::MakeNotNullUnique`: ```C++ /// Like std::make_unique, but returns a NotNullUniquePtr. template <typename T, typename... Args> NotNullUniquePtr<T> MakeNotNullUnique(Args&&... args) { return NotNull{std::make_unique<T>(std::forward<Args>(args)...)}; } ```
l0rinc (Migrated from github.com) commented 2026-11-11 07:18:03 +00:00

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

                tip_changed = *Assert(tip_block) != block_template.block.hashPrevBlock;
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): ```suggestion tip_changed = *Assert(tip_block) != block_template.block.hashPrevBlock; ```
l0rinc (Migrated from github.com) commented 2026-11-11 07:48:19 +00:00

nit: unrelated, but since we're touching CreateNewBlock, we could also avoid the naked new here:

-    pblocktemplate.reset(new CBlockTemplate());
+    pblocktemplate = std::make_unique<CBlockTemplate>();
nit: unrelated, but since we're touching `CreateNewBlock`, we could also avoid the naked `new` here: ```diff - pblocktemplate.reset(new CBlockTemplate()); + pblocktemplate = std::make_unique<CBlockTemplate>(); ```
l0rinc (Migrated from github.com) commented 2026-11-11 07:06:00 +00:00

fa7e949 refactor: Return NotNull from CreateNewBlock:

There's another one in github.com/bitcoin/bitcoin@6f1365e34f/src/rpc/mining.cpp (L911) that we could remove

> [fa7e949](https://github.com/bitcoin/bitcoin/pull/36451/changes/fa7e949f74168bf02a6b7abcb1687141c8c58fd0) _refactor: Return NotNull from CreateNewBlock_: There's another one in https://github.com/bitcoin/bitcoin/blob/6f1365e34f518fdc9fb963cd246abeb47a34ce8b/src/rpc/mining.cpp#L911 that we could remove
maflcko (Migrated from github.com) reviewed 2026-11-11 08:19:34 +00:00
maflcko (Migrated from github.com) commented 2026-11-11 08:19:34 +00:00

Sure, could roll into a new commit or new pull

Sure, could roll into a new commit or new pull
maflcko (Migrated from github.com) reviewed 2026-11-11 08:19:37 +00:00
maflcko (Migrated from github.com) commented 2026-11-11 08:19:37 +00:00

Sure, can be added in a new commit or new pull

Sure, can be added in a new commit or new pull
maflcko (Migrated from github.com) reviewed 2026-11-11 08:19:39 +00:00
maflcko (Migrated from github.com) commented 2026-11-11 08:19:39 +00:00

I presume the Assume was written intentionally, so I think I'll leave as-is for now.

I presume the Assume was written intentionally, so I think I'll leave as-is for now.
maflcko (Migrated from github.com) reviewed 2026-11-11 08:19:42 +00:00
maflcko (Migrated from github.com) commented 2026-11-11 08:19:42 +00:00

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

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
Sjors (Migrated from github.com) reviewed 2026-11-11 10:59:02 +00:00
Sjors (Migrated from github.com) commented 2026-11-11 10:59:02 +00:00
It was intentional: https://github.com/bitcoin/bitcoin/pull/31283#discussion_r1937078288 Could be reconsidered, but that seems better for a seperate PR.
sedited (Migrated from github.com) reviewed 2026-11-12 11:20:45 +00:00
sedited (Migrated from github.com) left a comment

Approach ACK

Do you want to add the pimpl m_internals in validationinterface.h? Would also support the comments that you left unresolved from l0rinc being taken here.

Approach ACK Do you want to add the pimpl `m_internals` in `validationinterface.h`? Would also support the comments that you left unresolved from l0rinc being taken here.
maflcko (Migrated from github.com) reviewed 2026-11-12 14:36:45 +00:00
maflcko (Migrated from github.com) commented 2026-11-12 14:36:45 +00:00

thx, added a new commit in the beginning

thx, added a new commit in the beginning
maflcko (Migrated from github.com) reviewed 2026-11-12 14:38:34 +00:00
maflcko (Migrated from github.com) commented 2026-11-12 14:38:34 +00:00

thx, modified the last commit

thx, modified the last commit
DrahtBot commented 2026-11-12 14:56:24 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/43239307477/job/131255305827
LLM reason (✨ experimental): CI failed because IWYU added a missing <memory> include to src/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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `iwyu`: https://github.com/bitcoin/bitcoin/actions/runs/43239307477/job/131255305827</sub> <sub>LLM reason (✨ experimental): CI failed because IWYU added a missing `<memory>` include to `src/addrman.h`, leaving an uncommitted diff that the check rejects.</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>
l0rinc commented 2026-11-13 10:41:48 +00:00 (Migrated from github.com)

reACK fa2187d8f7

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

reACK fa2187d8f7e5faf436ae8362e47af53866c8832b Since my last review, added [non-null smart-pointer factories](https://github.com/bitcoin/bitcoin/pull/36451#discussion_r4535185808) and [simplified block-template allocation](https://github.com/bitcoin/bitcoin/pull/36451#discussion_r4535284314). Happy to re-review if [@sedited’s suggestion](https://github.com/bitcoin/bitcoin/pull/36451#pullrequestreview-5873480073) is also applied.
enirox001 (Migrated from github.com) reviewed 2026-11-13 13:36:33 +00:00
enirox001 (Migrated from github.com) left a comment

ACK fa91586418

CreateNewBlock either 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 intac

ACK fa9158641839 `CreateNewBlock` either 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 intac
l0rinc commented 2026-11-13 14:02:03 +00:00 (Migrated from github.com)

reACK fa91586418

Latest push makes ValidationSignals::m_internals non-null - its sole constructor allocates it, and nothing resets or moves it out.

reACK fa9158641839e6dbc3cab63b07929998247b1f97 Latest push makes `ValidationSignals::m_internals` non-null - its sole constructor allocates it, and nothing resets or moves it out.
ViniciusCestarii commented 2026-11-13 18:54:05 +00:00 (Migrated from github.com)

reACK fa91586418

reACK fa9158641839e6dbc3cab63b07929998247b1f97
ryanofsky (Migrated from github.com) approved these changes 2026-11-16 15:04:18 +00:00
ryanofsky (Migrated from github.com) left a comment

Code review ACK fa91586418. Nice cleanup: makes it obvious that CreateNewBlock can't fail and that these pimpls are always set, and removes the dead error paths that went with them.

Code review ACK fa9158641839e6dbc3cab63b07929998247b1f97. Nice cleanup: makes it obvious that `CreateNewBlock` can't fail and that these pimpls are always set, and removes the dead error paths that went with them.
Lines 127-132
ryanofsky (Migrated from github.com) commented 2026-11-16 15:02:47 +00:00

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> and SharedRef<T> (with MakeUniqueRef and MakeSharedRef) instead of NotNullUniquePtr<T> and NotNullSharedPtr<T>.

There is a big semantic difference between unique_ptr and shared_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 like NotNullTransactionRef following 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: UniqueRef for a non-null unique_ptr, and Ref vs RefPtr for 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.

In commit "util: refactor: Add and use MakeNotNullShared and MakeNotNullUnique" (fa4785828e804f44726102070fb1adeacbdce6e5) Not a blocker, and the ship may have sailed, but I think these names are not great. I'd suggest `UniqueRef<T>` and `SharedRef<T>` (with `MakeUniqueRef` and `MakeSharedRef`) instead of `NotNullUniquePtr<T>` and `NotNullSharedPtr<T>`. There is a big semantic difference between `unique_ptr` and `shared_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 like `NotNullTransactionRef` following 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: [`UniqueRef`](https://github.com/WebKit/WebKit/blob/0af6a2c4c2867de5fae2532192677c534ddd1b31/Source/WTF/wtf/UniqueRef.h#L71) for a non-null `unique_ptr`, and `Ref` vs `RefPtr` for 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.
maflcko (Migrated from github.com) reviewed 2026-11-16 15:52:53 +00:00
Lines 127-132
maflcko (Migrated from github.com) commented 2026-11-16 15:52:53 +00:00

Hmm, I guess this could make sense as a scripted-diff, because:

  • The NotNull docs put this in the same bucket as reference_wrapper (a not null ptr in the std lib)
  • The "not null" naming seems fine on a low level, but for high level code the inversion in the type name may be confusing

However, stuff like if (ptr) return util::NotNull{std::move(ptr)}; would then appear inconsistent, so I am not sure about this rename?

Hmm, I guess this could make sense as a scripted-diff, because: * The NotNull docs put this in the same bucket as `reference_wrapper` (a not null ptr in the std lib) * The "not null" naming seems fine on a low level, but for high level code the inversion in the type name may be confusing However, stuff like `if (ptr) return util::NotNull{std::move(ptr)};` would then appear inconsistent, so I am not sure about this rename?
ryanofsky (Migrated from github.com) reviewed 2026-11-16 16:23:19 +00:00
ryanofsky (Migrated from github.com) commented 2026-11-16 15:35:15 +00:00

In commit "refactor: Mark pimpls as NotNull" (fa071a2c45)

With the current NotNull constructor, PRs like this one that introduce NotNull can look safer than they are, because changing a member to a NotNull type adds Assert calls that don't show up in the diff. It would be better to write NotNull initializers so the check is visible, using MakeNotNullUnique/MakeNotNullShared or util::NotNull{...}.

For example, with m_impl a NotNullUniquePtr, this aborts if LoadImpl() returns null, but nothing on the line says so:

Foo::Foo() : m_impl{LoadImpl()} {}

Forwarding functions do the same: vec.emplace_back(LoadImpl()) into a vector of NotNull.

Most other places don't have this problem, by design, because the NotNull constructor is explicit. 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
diff --git a/src/addrman.cpp b/src/addrman.cpp
index f830acdbe3f..14f53936e3e 100644
--- a/src/addrman.cpp
+++ b/src/addrman.cpp
@@ -1259,7 +1259,7 @@ std::optional<AddressPosition> AddrManImpl::FindAddressEntry(const CAddress& add
 }

 AddrMan::AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio)
-    : m_impl(std::make_unique<AddrManImpl>(netgroupman, deterministic, consistency_check_ratio)) {}
+    : m_impl(util::MakeNotNullUnique<AddrManImpl>(netgroupman, deterministic, consistency_check_ratio)) {}

 AddrMan::~AddrMan() = default;

diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp
index 77c57964a9f..ee68cefa579 100644
--- a/src/dbwrapper.cpp
+++ b/src/dbwrapper.cpp
@@ -183,7 +183,7 @@ struct CDBBatch::WriteBatchImpl {

 CDBBatch::CDBBatch(const CDBWrapper& _parent)
     : parent{_parent},
-      m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()}
+      m_impl_batch{util::MakeNotNullUnique<CDBBatch::WriteBatchImpl>()}
 {
     m_key_scratch.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
     m_value_scratch.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
diff --git a/src/musig.cpp b/src/musig.cpp
index 38a49fa2f37..d344e6e34da 100644
--- a/src/musig.cpp
+++ b/src/musig.cpp
@@ -100,7 +100,7 @@ public:
     bool IsValid() { return m_nonce != nullptr; }
 };

-MuSig2SecNonce::MuSig2SecNonce() : m_impl{std::make_unique<MuSig2SecNonceImpl>()} {}
+MuSig2SecNonce::MuSig2SecNonce() : m_impl{util::MakeNotNullUnique<MuSig2SecNonceImpl>()} {}

 MuSig2SecNonce::MuSig2SecNonce(MuSig2SecNonce&&) noexcept = default;
 MuSig2SecNonce& MuSig2SecNonce::operator=(MuSig2SecNonce&&) noexcept = default;
diff --git a/src/node/txdownloadman_impl.cpp b/src/node/txdownloadman_impl.cpp
index 7443367b6bf..4b8f0a54543 100644
--- a/src/node/txdownloadman_impl.cpp
+++ b/src/node/txdownloadman_impl.cpp
@@ -15,7 +15,7 @@
 namespace node {
 // TxDownloadManager wrappers
 TxDownloadManager::TxDownloadManager(const TxDownloadOptions& options) :
-    m_impl{std::make_unique<TxDownloadManagerImpl>(options)}
+    m_impl{util::MakeNotNullUnique<TxDownloadManagerImpl>(options)}
 {}
 TxDownloadManager::~TxDownloadManager() = default;

diff --git a/src/node/txreconciliation.cpp b/src/node/txreconciliation.cpp
index 9a1f640f5a4..fb6e2417ec3 100644
--- a/src/node/txreconciliation.cpp
+++ b/src/node/txreconciliation.cpp
@@ -144,7 +144,7 @@ public:
     }
 };

-TxReconciliationTracker::TxReconciliationTracker(uint32_t recon_version) : m_impl{std::make_unique<TxReconciliationTracker::Impl>(recon_version)} {}
+TxReconciliationTracker::TxReconciliationTracker(uint32_t recon_version) : m_impl{util::MakeNotNullUnique<TxReconciliationTracker::Impl>(recon_version)} {}

 TxReconciliationTracker::~TxReconciliationTracker() = default;

diff --git a/src/test/fuzz/util/check_globals.cpp b/src/test/fuzz/util/check_globals.cpp
index 431519dc269..11a2a34dd62 100644
--- a/src/test/fuzz/util/check_globals.cpp
+++ b/src/test/fuzz/util/check_globals.cpp
@@ -56,5 +56,5 @@ struct CheckGlobalsImpl {
     }
 };

-CheckGlobals::CheckGlobals() : m_impl(std::make_unique<CheckGlobalsImpl>()) {}
+CheckGlobals::CheckGlobals() : m_impl(util::MakeNotNullUnique<CheckGlobalsImpl>()) {}
 CheckGlobals::~CheckGlobals() = default;
diff --git a/src/txrequest.cpp b/src/txrequest.cpp
index 53136a8e710..0fb25f592e2 100644
--- a/src/txrequest.cpp
+++ b/src/txrequest.cpp
@@ -709,7 +709,7 @@ public:
 };

 TxRequestTracker::TxRequestTracker(bool deterministic) :
-    m_impl{std::make_unique<TxRequestTracker::Impl>(deterministic)} {}
+    m_impl{util::MakeNotNullUnique<TxRequestTracker::Impl>(deterministic)} {}

 TxRequestTracker::~TxRequestTracker() = default;

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 NotNull could only be created with MakeNotNullUnique/MakeNotNullShared or a checking function like util::CheckNotNull(expr).

In commit "refactor: Mark pimpls as NotNull" (fa071a2c451d2cab12e181711b2dd64d61f3ea18) With the current `NotNull` constructor, PRs like this one that introduce `NotNull` can look safer than they are, because changing a member to a `NotNull` type adds `Assert` calls that don't show up in the diff. It would be better to write `NotNull` initializers so the check is visible, using `MakeNotNullUnique`/`MakeNotNullShared` or `util::NotNull{...}`. For example, with `m_impl` a `NotNullUniquePtr`, this aborts if `LoadImpl()` returns null, but nothing on the line says so: ```cpp Foo::Foo() : m_impl{LoadImpl()} {} ``` Forwarding functions do the same: `vec.emplace_back(LoadImpl())` into a vector of `NotNull`. Most other places don't have this problem, [by design](https://github.com/bitcoin/bitcoin/pull/34844#discussion_r3422695837), because the `NotNull` constructor is `explicit`. 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: <details><summary>diff</summary> ```diff diff --git a/src/addrman.cpp b/src/addrman.cpp index f830acdbe3f..14f53936e3e 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -1259,7 +1259,7 @@ std::optional<AddressPosition> AddrManImpl::FindAddressEntry(const CAddress& add } AddrMan::AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio) - : m_impl(std::make_unique<AddrManImpl>(netgroupman, deterministic, consistency_check_ratio)) {} + : m_impl(util::MakeNotNullUnique<AddrManImpl>(netgroupman, deterministic, consistency_check_ratio)) {} AddrMan::~AddrMan() = default; diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 77c57964a9f..ee68cefa579 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -183,7 +183,7 @@ struct CDBBatch::WriteBatchImpl { CDBBatch::CDBBatch(const CDBWrapper& _parent) : parent{_parent}, - m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()} + m_impl_batch{util::MakeNotNullUnique<CDBBatch::WriteBatchImpl>()} { m_key_scratch.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); m_value_scratch.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE); diff --git a/src/musig.cpp b/src/musig.cpp index 38a49fa2f37..d344e6e34da 100644 --- a/src/musig.cpp +++ b/src/musig.cpp @@ -100,7 +100,7 @@ public: bool IsValid() { return m_nonce != nullptr; } }; -MuSig2SecNonce::MuSig2SecNonce() : m_impl{std::make_unique<MuSig2SecNonceImpl>()} {} +MuSig2SecNonce::MuSig2SecNonce() : m_impl{util::MakeNotNullUnique<MuSig2SecNonceImpl>()} {} MuSig2SecNonce::MuSig2SecNonce(MuSig2SecNonce&&) noexcept = default; MuSig2SecNonce& MuSig2SecNonce::operator=(MuSig2SecNonce&&) noexcept = default; diff --git a/src/node/txdownloadman_impl.cpp b/src/node/txdownloadman_impl.cpp index 7443367b6bf..4b8f0a54543 100644 --- a/src/node/txdownloadman_impl.cpp +++ b/src/node/txdownloadman_impl.cpp @@ -15,7 +15,7 @@ namespace node { // TxDownloadManager wrappers TxDownloadManager::TxDownloadManager(const TxDownloadOptions& options) : - m_impl{std::make_unique<TxDownloadManagerImpl>(options)} + m_impl{util::MakeNotNullUnique<TxDownloadManagerImpl>(options)} {} TxDownloadManager::~TxDownloadManager() = default; diff --git a/src/node/txreconciliation.cpp b/src/node/txreconciliation.cpp index 9a1f640f5a4..fb6e2417ec3 100644 --- a/src/node/txreconciliation.cpp +++ b/src/node/txreconciliation.cpp @@ -144,7 +144,7 @@ public: } }; -TxReconciliationTracker::TxReconciliationTracker(uint32_t recon_version) : m_impl{std::make_unique<TxReconciliationTracker::Impl>(recon_version)} {} +TxReconciliationTracker::TxReconciliationTracker(uint32_t recon_version) : m_impl{util::MakeNotNullUnique<TxReconciliationTracker::Impl>(recon_version)} {} TxReconciliationTracker::~TxReconciliationTracker() = default; diff --git a/src/test/fuzz/util/check_globals.cpp b/src/test/fuzz/util/check_globals.cpp index 431519dc269..11a2a34dd62 100644 --- a/src/test/fuzz/util/check_globals.cpp +++ b/src/test/fuzz/util/check_globals.cpp @@ -56,5 +56,5 @@ struct CheckGlobalsImpl { } }; -CheckGlobals::CheckGlobals() : m_impl(std::make_unique<CheckGlobalsImpl>()) {} +CheckGlobals::CheckGlobals() : m_impl(util::MakeNotNullUnique<CheckGlobalsImpl>()) {} CheckGlobals::~CheckGlobals() = default; diff --git a/src/txrequest.cpp b/src/txrequest.cpp index 53136a8e710..0fb25f592e2 100644 --- a/src/txrequest.cpp +++ b/src/txrequest.cpp @@ -709,7 +709,7 @@ public: }; TxRequestTracker::TxRequestTracker(bool deterministic) : - m_impl{std::make_unique<TxRequestTracker::Impl>(deterministic)} {} + m_impl{util::MakeNotNullUnique<TxRequestTracker::Impl>(deterministic)} {} TxRequestTracker::~TxRequestTracker() = default; ``` </details> 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 `NotNull` could only be created with `MakeNotNullUnique`/`MakeNotNullShared` or a checking function like `util::CheckNotNull(expr)`.
wp5mvk46vz-hue (Migrated from github.com) reviewed 2026-11-16 17:23:54 +00:00
wp5mvk46vz-hue (Migrated from github.com) left a comment

ا

ا
wp5mvk46vz-hue commented 2026-11-16 17:31:09 +00:00 (Migrated from github.com)

اال

اال
maflcko (Migrated from github.com) reviewed 2026-11-17 20:15:06 +00:00
Lines 127-132
maflcko (Migrated from github.com) commented 2026-11-17 20:15:06 +00:00

However, stuff like if (ptr) return util::NotNull{std::move(ptr)}; would then appear inconsistent, so I am not sure about this rename?

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.

> However, stuff like `if (ptr) return util::NotNull{std::move(ptr)};` would then appear inconsistent, so I am not sure about this rename? 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. <!-- https://github.com/bitcoin/bitcoin/pull/34844#issuecomment-6037421299
Sign in to join this conversation.
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!36451
No description provided.