wallet: fix crash on importdescriptors with a range ending at 2^31-1 #35989

Manually merged
shuv-amp merged 5 commits from wallet-descriptor-range-overflow into master 2026-10-08 23:06:12 +00:00 AGit
shuv-amp commented 2026-08-16 20:15:09 +00:00 (Migrated from github.com)

WalletDescriptor stores an exclusive range end in int32_t. Importing
[2147483647, 2147483647] produces an end of 2^31, which narrows to a negative
value and aborts the node during keypool top-up. An import without a range can
hit the same issue with an oversized -keypool.

Check the exclusive end before constructing the wallet descriptor, covering
both explicit and default ranges. The bound stays wallet-specific: scanning
RPCs still accept INT32_MAX as an inclusive endpoint.

Also calculate the keypool top-up endpoint in int64_t before checking its
bound, and reject reversed ranges before computing high - low in
CheckDescriptorRangeBounds. These fix the overflows reached by
keypoolrefill INT32_MAX after advancing a descriptor, and by [INT64_MAX, 0],
respectively. Regression tests cover all three fixes.

I tested on macOS arm64 with Clang 22 and UBSan passed: 34 targeted unit
cases and the importdescriptors, keypool, deriveaddresses (RPC and CLI),
scantxoutset, and scanblocks functional tests.

`WalletDescriptor` stores an exclusive range end in `int32_t`. Importing `[2147483647, 2147483647]` produces an end of `2^31`, which narrows to a negative value and aborts the node during keypool top-up. An import without a range can hit the same issue with an oversized `-keypool`. Check the exclusive end before constructing the wallet descriptor, covering both explicit and default ranges. The bound stays wallet-specific: scanning RPCs still accept `INT32_MAX` as an inclusive endpoint. Also calculate the keypool top-up endpoint in `int64_t` before checking its bound, and reject reversed ranges before computing `high - low` in `CheckDescriptorRangeBounds`. These fix the overflows reached by `keypoolrefill INT32_MAX` after advancing a descriptor, and by `[INT64_MAX, 0]`, respectively. Regression tests cover all three fixes. I tested on macOS arm64 with Clang 22 and UBSan passed: 34 targeted unit cases and the importdescriptors, keypool, deriveaddresses (RPC and CLI), scantxoutset, and scanblocks functional tests.
DrahtBot commented 2026-08-16 20:15:16 +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 polespinasa, achow101
Concept ACK jeanpablojp, vicjuma
Stale ACK kriss39, molnard

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:

  • #36402 (refactor: wallet_importdescriptors to separate run_test() contents into specific functions by nebula-21)
  • #36245 (wallet: start descriptor top up at range_start instead of index 0 by kriss39)
  • #36236 (wallet, rpc: add verify_balance option to importdescriptors by musaHaruna)
  • #35444 (wallet: make descriptor SPKM mutex non-recursive by w0xlt)

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/35989 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35989.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35989#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 | [polespinasa](https://github.com/bitcoin/bitcoin/pull/35989#pullrequestreview-5429298694), [achow101](https://github.com/bitcoin/bitcoin/pull/35989#issuecomment-6070265975) | | Concept ACK | [jeanpablojp](https://github.com/bitcoin/bitcoin/pull/35989#pullrequestreview-5002207884), [vicjuma](https://github.com/bitcoin/bitcoin/pull/35989#pullrequestreview-5197217777) | | Stale ACK | [kriss39](https://github.com/bitcoin/bitcoin/pull/35989#issuecomment-5663222534), [molnard](https://github.com/bitcoin/bitcoin/pull/35989#issuecomment-5972743151) | 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: * [#36402](https://github.com/bitcoin/bitcoin/pull/36402) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36402.svg"></sub> (refactor: `wallet_importdescriptors` to separate `run_test()` contents into specific functions by nebula-21) * [#36245](https://github.com/bitcoin/bitcoin/pull/36245) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36245.svg"></sub> (wallet: start descriptor top up at range_start instead of index 0 by kriss39) * [#36236](https://github.com/bitcoin/bitcoin/pull/36236) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36236.svg"></sub> (wallet, rpc: add verify_balance option to importdescriptors by musaHaruna) * [#35444](https://github.com/bitcoin/bitcoin/pull/35444) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35444.svg"></sub> (wallet: make descriptor SPKM mutex non-recursive by w0xlt) 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-->
jeanpablojp (Migrated from github.com) reviewed 2026-08-23 10:47:41 +00:00
jeanpablojp (Migrated from github.com) left a comment

Concept ACK

I reproduced the crash both ways on master.

Concept ACK I reproduced the crash both ways on master.
jeanpablojp (Migrated from github.com) commented 2026-08-23 10:47:41 +00:00

Only the overflow case changes behaviour here, and I couldn't find a test for it. A keypoolrefill 2147483647 reaches this addition without going through an import, as long as every active descriptor has already advanced one index. On master, UBSan flags it. Worth a case so it doesn't come back?

Only the overflow case changes behaviour here, and I couldn't find a test for it. A `keypoolrefill 2147483647` reaches this addition without going through an import, as long as every active descriptor has already advanced one index. On master, UBSan flags it. Worth a case so it doesn't come back?
DrahtBot commented 2026-08-24 22:22:23 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
shuv-amp (Migrated from github.com) reviewed 2026-08-25 19:48:25 +00:00
shuv-amp (Migrated from github.com) commented 2026-08-25 19:48:25 +00:00

Added. It only fails under UBSan though: the wrapped sum is negative, so std::max()
discarded it anyway and range_end comes out the same either way.

Added. It only fails under UBSan though: the wrapped sum is negative, so `std::max()` discarded it anyway and `range_end` comes out the same either way.
molnard (Migrated from github.com) reviewed 2026-09-02 18:03:00 +00:00
molnard (Migrated from github.com) left a comment

ACK def4fb0101

I reviewed the code. The changes are straightforward and focused on fixing the reported issues.

Testing

The following results are from local test runs.

Compared master b811aeabad94ef48cd0f0fb1d2fcc456594aeedb with the PR tip:

  • Importing [2147483647,2147483647] terminated master. The PR returned error -8 and remained responsive.
  • Importing without a range under -keypool=3000000000 terminated master. The PR returned error -8 and remained responsive.
  • Calling keypoolrefill 2147483647 after advancing every active descriptor to index 1 terminated master. The PR returned error -4 and remained responsive.

Non-blocking follow-ups

Possbile follow-ups, worth considering separately from this PR:

  • Detecting/recovering invalid descriptor ranges already persisted by the old bug.
  • Centralizing validation of oversized -keypool values. For example: accepted as a wallet configuration option without an upper-bound check (wallet.cpp)
  • Clarifying whether TopUpWithDB should return failure when the requested endpoint is unrepresentable, instead of leaving the range unchanged and returning true (plus writes the unchanged descriptor). This is separate from the tested keypoolrefill RPC, which correctly returns an error.
ACK def4fb0101b73f825db7f20ff6d6dd7170b5d5ca I reviewed the code. The changes are straightforward and focused on fixing the reported issues. ### Testing The following results are from local test runs. Compared master `b811aeabad94ef48cd0f0fb1d2fcc456594aeedb` with the PR tip: - Importing `[2147483647,2147483647]` terminated master. The PR returned error `-8` and remained responsive. - Importing without a range under `-keypool=3000000000` terminated master. The PR returned error `-8` and remained responsive. - Calling `keypoolrefill 2147483647` after advancing every active descriptor to index 1 terminated master. The PR returned error `-4` and remained responsive. ### Non-blocking follow-ups Possbile follow-ups, worth considering separately from this PR: - Detecting/recovering invalid descriptor ranges already persisted by the old bug. - Centralizing validation of oversized `-keypool` values. For example: accepted as a wallet configuration option without an upper-bound check (wallet.cpp) - Clarifying whether `TopUpWithDB` should return failure when the requested endpoint is unrepresentable, instead of leaving the range unchanged and returning `true` (plus writes the unchanged descriptor). This is separate from the tested `keypoolrefill` RPC, which correctly returns an error.
molnard (Migrated from github.com) commented 2026-09-02 17:18:51 +00:00

nit , readability choice:

        new_range_end = std::max(static_cast<int32_t>(target_end), new_range_end);
nit , readability choice: ```suggestion new_range_end = std::max(static_cast<int32_t>(target_end), new_range_end); ```
shuv-amp (Migrated from github.com) reviewed 2026-09-05 06:17:34 +00:00
shuv-amp (Migrated from github.com) commented 2026-09-05 06:17:34 +00:00

I'd keep GetEnd() here, it's explicit about the lower bound.

I'd keep `GetEnd()` here, it's explicit about the lower bound.
shuv-amp commented 2026-09-05 06:47:20 +00:00 (Migrated from github.com)

Detecting/recovering invalid descriptor ranges already persisted by the old bug.

Follow-up, agreed. On the reported import the descriptor write happens inside
AddWalletDescriptor with the assert after it, and activation is back in
ProcessDescriptorImport once that call returns, so the record persists unactivated even with
active=true. On reload Load() runs zero iterations over the inverted range and
TopUpKeyPool skips it for being inactive. The wallet reopens, but none of that descriptor's
scripts are registered.

I thought UpdateWalletDescriptor might put the assert back in play since it resets
m_max_cached_index, but it also assigns the incoming descriptor over m_wallet_descriptor, so
the old range is gone before the top up. Haven't tested recovery on an affected wallet.

Centralizing validation of oversized -keypool values.

There's narrowing before the addition too: TopUpWithDB assigns m_keypool_size to an
unsigned int, so -keypool=4294968296 gives a target_size of 1000. The member keeps the full
value, so the no-range import path still rejects it with -8 here. A bound of INT32_MAX would be
representable and still ask for two billion derivations, so the limit has to be about work, not
just the type.

Clarifying whether TopUpWithDB should return failure when the requested endpoint is unrepresentable, instead of leaving the range unchanged and returning true (plus writes the unchanged descriptor).

I'd keep it as is here and look at the return value with its callers separately. keypoolrefill
discards the bool, the -4 you saw is GetKeyPoolSize() < kpSize, which sums across active spkms
rather than reporting the descriptor that failed. UpdateWithSigningProvider throws
std::runtime_error("Could not top up scriptPubKeys") on false and MarkUnusedAddresses logs
"Topping up keypool failed (locked wallet)", so changing it means touching those too. The
unchanged write is pre-existing, keypoolrefill 1 on a full keypool does the same, though that
only says it isn't new, not that true is the right answer.

> Detecting/recovering invalid descriptor ranges already persisted by the old bug. Follow-up, agreed. On the reported import the descriptor write happens inside `AddWalletDescriptor` with the assert after it, and activation is back in `ProcessDescriptorImport` once that call returns, so the record persists unactivated even with `active=true`. On reload `Load()` runs zero iterations over the inverted range and `TopUpKeyPool` skips it for being inactive. The wallet reopens, but none of that descriptor's scripts are registered. I thought `UpdateWalletDescriptor` might put the assert back in play since it resets `m_max_cached_index`, but it also assigns the incoming descriptor over `m_wallet_descriptor`, so the old range is gone before the top up. Haven't tested recovery on an affected wallet. > Centralizing validation of oversized `-keypool` values. There's narrowing before the addition too: `TopUpWithDB` assigns `m_keypool_size` to an `unsigned int`, so `-keypool=4294968296` gives a `target_size` of 1000. The member keeps the full value, so the no-range import path still rejects it with -8 here. A bound of `INT32_MAX` would be representable and still ask for two billion derivations, so the limit has to be about work, not just the type. > Clarifying whether `TopUpWithDB` should return failure when the requested endpoint is unrepresentable, instead of leaving the range unchanged and returning `true` (plus writes the unchanged descriptor). I'd keep it as is here and look at the return value with its callers separately. `keypoolrefill` discards the bool, the -4 you saw is `GetKeyPoolSize() < kpSize`, which sums across active spkms rather than reporting the descriptor that failed. `UpdateWithSigningProvider` throws `std::runtime_error("Could not top up scriptPubKeys")` on false and `MarkUnusedAddresses` logs "Topping up keypool failed (locked wallet)", so changing it means touching those too. The unchanged write is pre-existing, `keypoolrefill 1` on a full keypool does the same, though that only says it isn't new, not that `true` is the right answer.
molnard commented 2026-09-07 19:10:07 +00:00 (Migrated from github.com)

I agree with these points and think they can be handled in follow-up PRs. They don't block this fix, so this PR can be merged as is.

I agree with these points and think they can be handled in follow-up PRs. They don't block this fix, so this PR can be merged as is.
kriss39 commented 2026-09-14 11:29:13 +00:00 (Migrated from github.com)

tACK def4fb0101

Built and ran both tests. Also tried it by hand: importing [2147483647, 2147483647] into a watch-only wallet is rejected with "End of range is too high", the node stays up and the wallet is still empty after a restart. Without the first two commits the new importdescriptors case takes the node down as expected.

One note on the wallet_keypool.py case: it also passes on master in a non-sanitizer build. The wrapped sum ends up below GetEnd(), so std::max just keeps the old end and keypoolrefill fails with the same -4. So it only catches the overflow under UBSan. Still worth having, maybe with a comment in the test saying that.

Not something this PR needs to solve, but [2147482648, 2147482648] is accepted both with and without this PR (range_end 2147482649 still fits in int32_t), and the top up then derives every index from 0, so the RPC doesn't come back for a very long time. That's the existing O(range_start) cost in TopUpWithDB, #36245 addresses it. The only thing that changes here is that the next_index + target_size addition no longer overflows, so a UBSan build stops aborting on it.

tACK def4fb0101 Built and ran both tests. Also tried it by hand: importing `[2147483647, 2147483647]` into a watch-only wallet is rejected with "End of range is too high", the node stays up and the wallet is still empty after a restart. Without the first two commits the new importdescriptors case takes the node down as expected. One note on the wallet_keypool.py case: it also passes on master in a non-sanitizer build. The wrapped sum ends up below GetEnd(), so std::max just keeps the old end and keypoolrefill fails with the same -4. So it only catches the overflow under UBSan. Still worth having, maybe with a comment in the test saying that. Not something this PR needs to solve, but `[2147482648, 2147482648]` is accepted both with and without this PR (range_end 2147482649 still fits in int32_t), and the top up then derives every index from 0, so the RPC doesn't come back for a very long time. That's the existing O(range_start) cost in TopUpWithDB, #36245 addresses it. The only thing that changes here is that the next_index + target_size addition no longer overflows, so a UBSan build stops aborting on it.
vicjuma (Migrated from github.com) reviewed 2026-09-14 11:45:58 +00:00
vicjuma (Migrated from github.com) left a comment

Concept ACK

Before
Image

After

Image
Concept ACK **Before** <img width="2134" height="1218" alt="Image" src="https://github.com/user-attachments/assets/afd72087-d598-421d-8485-e1950fd317aa" /> **After** <img width="2144" height="902" alt="Image" src="https://github.com/user-attachments/assets/5b87fe92-019f-4396-98b8-2ad6d6dae8b4" />
DrahtBot commented 2026-09-17 02:29:43 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
polespinasa commented 2026-09-17 06:10:52 +00:00 (Migrated from github.com)
There is another (unraelated) overflow that this PR could fix, see https://github.com/bitcoin/bitcoin/pull/34861#discussion_r4021705766
shuv-amp commented 2026-09-19 15:48:43 +00:00 (Migrated from github.com)

Rebased onto d48e76e689, adapting the import guard to wallet/imports.cpp.
Added a separate commit for the range-validation overflow reported by @Rob1Ham
and linked by @polespinasa, including the suggested regression test. The new
test fails before the fix and passes after it under UBSan. The targeted unit
and functional tests passed.

Rebased onto `d48e76e689`, adapting the import guard to `wallet/imports.cpp`. Added a separate commit for the range-validation overflow reported by @Rob1Ham and linked by @polespinasa, including the suggested regression test. The new test fails before the fix and passes after it under UBSan. The targeted unit and functional tests passed.
molnard (Migrated from github.com) reviewed 2026-09-21 21:29:57 +00:00
molnard (Migrated from github.com) left a comment

tACK 457c9e8999

Tested locally. The code hasn't changed since my previous review, and I still consider it good.

tACK 457c9e8999fbee1d37f41eaede681f787e657395 Tested locally. The code hasn't changed since my previous review, and I still consider it good.
achow101 (Migrated from github.com) reviewed 2026-09-25 23:57:27 +00:00
achow101 (Migrated from github.com) commented 2026-09-25 23:48:44 +00:00

In 1889440f82 "wallet: reject unrepresentable descriptor import ranges"

This should be in CheckDescriptorRangeBounds so that all bounds checks are in the same place. This makes other RPCs like deriveaddresses also be covered by the check.

In 1889440f821d6bf8394050c96cc8e06a2f18d9c2 "wallet: reject unrepresentable descriptor import ranges" This should be in `CheckDescriptorRangeBounds` so that all bounds checks are in the same place. This makes other RPCs like `deriveaddresses` also be covered by the check.
achow101 commented 2026-09-26 00:04:21 +00:00 (Migrated from github.com)
ACK 457c9e8999fbee1d37f41eaede681f787e657395
polespinasa commented 2026-09-28 16:50:58 +00:00 (Migrated from github.com)

This is a behaviour change as now descriptors with a previously accepted range will now fail. Probably worth a release note.

This is a behaviour change as now descriptors with a previously accepted range will now fail. Probably worth a release note.
First-time contributor

Base: aef8a04966d7ab6c04edc5d19b733e78205405de
Head: 93c8aec3fbc8c5a2fcc834c4acc1b7d96bffa9a0

No verified findings are available from this partial review.

Review coverage was incomplete. The archaeology review had incomplete evidence. The alternatives experiment had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review did not complete.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `aef8a04966d7ab6c04edc5d19b733e78205405de` Head: `93c8aec3fbc8c5a2fcc834c4acc1b7d96bffa9a0` No verified findings are available from this partial review. Review coverage was incomplete. The archaeology review had incomplete evidence. The alternatives experiment had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review did not complete. [Full review report](<https://review.fish.foo/traces/35989-93c8aec3fbc8c5a2fcc834c4acc1b7d96bffa9a0-477-2.html>)
polespinasa (Migrated from github.com) reviewed 2026-09-28 17:14:55 +00:00
polespinasa (Migrated from github.com) commented 2026-09-28 17:14:39 +00:00

in 867ad9bffb wallet: fix integer overflow in descriptor keypool top up

This can now be a db writte no-op as maybe new_range_end did not change.
Maybe:

$ git diff
diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp
index 5eab5d505e..9e5e6a51de 100644
--- a/src/wallet/scriptpubkeyman.cpp
+++ b/src/wallet/scriptpubkeyman.cpp
@@ -1165,8 +1165,15 @@ bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int siz
         }
         m_max_cached_index++;
     }
-    SetRangeEnd(new_range_end);
-    batch.WriteDescriptor(GetID(), m_wallet_descriptor);
+    if (new_range_end > m_wallet_descriptor.GetEnd()) {
+        SetRangeEnd(new_range_end);
+        if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
+            throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
+        }
+    }
 
     // By this point, the cache size should be the size of the entire range
     assert(m_wallet_descriptor.GetEnd() - 1 == m_max_cached_index);

in 867ad9bffbd3ab26c9899207ddd11380e0c1a1bd wallet: fix integer overflow in descriptor keypool top up This can now be a db writte no-op as maybe `new_range_end` did not change. Maybe: ```diff $ git diff diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 5eab5d505e..9e5e6a51de 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -1165,8 +1165,15 @@ bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int siz } m_max_cached_index++; } - SetRangeEnd(new_range_end); - batch.WriteDescriptor(GetID(), m_wallet_descriptor); + if (new_range_end > m_wallet_descriptor.GetEnd()) { + SetRangeEnd(new_range_end); + if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) { + throw std::runtime_error(std::string(__func__) + ": writing descriptor failed"); + } + } // By this point, the cache size should be the size of the entire range assert(m_wallet_descriptor.GetEnd() - 1 == m_max_cached_index); ```
shuv-amp (Migrated from github.com) reviewed 2026-09-29 19:44:32 +00:00
shuv-amp (Migrated from github.com) commented 2026-09-29 19:44:32 +00:00

I'd keep the write unconditional. It persists the whole record, not just range_end, and two callers rely on it when the end doesn't move.

With the diff, wallet_migration.py fails in test_basic with 8 descriptors instead of 11: CreateFromMigration uses a keypool size of 0, so the end never moves and this is the only write of the migrated descriptors. MarkUnusedAddresses also relies on it to persist next_index, so when the range already covers next_index + keypool, it's lost on restart and getnewaddress can hand out an already paid address again.

Master writes unconditionally too, and in the overflow case it also keeps GetEnd(), so this PR doesn't add a new no-op write. Skipping the write would need those two callers to persist their own changes, and together with the return value check that I'd do separately.

I'd keep the write unconditional. It persists the whole record, not just range_end, and two callers rely on it when the end doesn't move. With the diff, wallet_migration.py fails in test_basic with 8 descriptors instead of 11: CreateFromMigration uses a keypool size of 0, so the end never moves and this is the only write of the migrated descriptors. MarkUnusedAddresses also relies on it to persist next_index, so when the range already covers next_index + keypool, it's lost on restart and getnewaddress can hand out an already paid address again. Master writes unconditionally too, and in the overflow case it also keeps GetEnd(), so this PR doesn't add a new no-op write. Skipping the write would need those two callers to persist their own changes, and together with the return value check that I'd do separately.
shuv-amp commented 2026-09-29 19:49:37 +00:00 (Migrated from github.com)

Added a release note in 2b87954093 as suggested by @polespinasa.

Added a release note in 2b87954093 as suggested by @polespinasa.
molnard commented 2026-10-03 19:32:23 +00:00 (Migrated from github.com)

ACK 2b87954093

Tested locally.

ACK 2b87954093bfc004c7de7dc325df35accc642379 Tested locally.
polespinasa (Migrated from github.com) reviewed 2026-10-06 08:00:48 +00:00
polespinasa (Migrated from github.com) commented 2026-10-06 08:00:03 +00:00

I would not mention deriveaddresses and scantxoutset not mentioning them already means that this change does not affect them.

I would not mention `deriveaddresses` and `scantxoutset` not mentioning them already means that this change does not affect them.
shuv-amp (Migrated from github.com) reviewed 2026-10-06 13:43:45 +00:00
shuv-amp (Migrated from github.com) commented 2026-10-06 13:43:45 +00:00

Removed in 93c8aec3fb.

Removed in 93c8aec3fb.
polespinasa (Migrated from github.com) reviewed 2026-10-06 13:45:29 +00:00
polespinasa (Migrated from github.com) left a comment
ACK 93c8aec3fbc8c5a2fcc834c4acc1b7d96bffa9a0
achow101 commented 2026-10-08 22:25:07 +00:00 (Migrated from github.com)
ACK 93c8aec3fbc8c5a2fcc834c4acc1b7d96bffa9a0
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!35989
No description provided.