wallet: start descriptor top up at range_start instead of index 0 #36245

Open
kriss39 wants to merge 1 commit from fix-topup-cache-from-range-start into master AGit
kriss39 commented 2026-09-14 08:34:00 +00:00 (Migrated from github.com)

DescriptorScriptPubKeyMan::TopUpWithDB() expands the descriptor from m_max_cached_index + 1, which is 0 for a freshly imported descriptor regardless of its range_start. Importing a descriptor with "range": [N, M] therefore derives and watches every index in [0, M), so the cost of an import is O(M) rather than O(M - N) and the wallet treats scriptPubKeys below N as its own. Load() only expands [range_start, range_end), so those scriptPubKeys stop being IsMine the next time the wallet is opened, and a transaction picked up for one of them during the import rescan is no longer credited to the wallet.

Since ParseDescriptorRange() only bounds the size of the range and not its start, "range": [2146483648, 2147483647] is accepted and expands 2^31 indexes into m_map_script_pub_keys before the RPC returns.

Start the top up loop at range_start and record the index that was actually cached instead of counting from -1, so that Load() and TopUpWithDB() agree on m_max_cached_index for descriptors with a non-zero range_start. Before this change Load() left m_max_cached_index at range_end - range_start - 1 for such descriptors, which made the first top up after opening the wallet re-expand the stored range.

Importing wpkh(xpub/0/*) with "range": [600000, 600009] into a blank watch-only wallet goes from 19.5s to 0.09s on this machine.

DescriptorScriptPubKeyMan::TopUpWithDB() expands the descriptor from m_max_cached_index + 1, which is 0 for a freshly imported descriptor regardless of its range_start. Importing a descriptor with "range": [N, M] therefore derives and watches every index in [0, M), so the cost of an import is O(M) rather than O(M - N) and the wallet treats scriptPubKeys below N as its own. Load() only expands [range_start, range_end), so those scriptPubKeys stop being IsMine the next time the wallet is opened, and a transaction picked up for one of them during the import rescan is no longer credited to the wallet. Since ParseDescriptorRange() only bounds the size of the range and not its start, "range": [2146483648, 2147483647] is accepted and expands 2^31 indexes into m_map_script_pub_keys before the RPC returns. Start the top up loop at range_start and record the index that was actually cached instead of counting from -1, so that Load() and TopUpWithDB() agree on m_max_cached_index for descriptors with a non-zero range_start. Before this change Load() left m_max_cached_index at range_end - range_start - 1 for such descriptors, which made the first top up after opening the wallet re-expand the stored range. Importing wpkh(xpub/0/*) with "range": [600000, 600009] into a blank watch-only wallet goes from 19.5s to 0.09s on this machine.
DrahtBot commented 2026-09-14 08:34:08 +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 cprkrn
Stale ACK fametrano

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

No conflicts as of last run.

<!--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/36245 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36245.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36245#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 | [cprkrn](https://github.com/bitcoin/bitcoin/pull/36245#issuecomment-7174526167) | | Stale ACK | [fametrano](https://github.com/bitcoin/bitcoin/pull/36245#pullrequestreview-5760084072) | 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 No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Earlier concerns remain unresolved; see the Run archive linked from the Review report.

🟠 Minor · The upgrade changes below-range_start ownership without recovery guidance

Active, derivable descriptors with range_start > range_end - range_start can lose below-start IsMine recognition after upgrade; no affected production wallet was identified.

Location: src/wallet/scriptpubkeyman.cpp:1152 (head).

🔵 Suggestion · Reload assertions miss a regression in Load()'s cached index

The functional test's load-time top-up repairs an incorrect m_max_cached_index before check() runs, so reverting only the Load() assignment leaves the new assertions unchanged.

Location: test/functional/wallet_importdescriptors.py:226 (head).

🔵 Suggestion · Lower-start re-import coverage does not check recovered ownership

The existing range-lowering test checks keypoolsize, not whether newly included scripts become IsMine; the new funded fixture can directly cover that recovery path.

Location: test/functional/wallet_importdescriptors.py:193 (head).

🔵 Suggestion · The description's example range is rejected before expansion

Source inspection shows that importdescriptors rejects the description's [2146483648, 2147483647]; the commit message's [2146483647, 2147483646] passes the bounds and demonstrates the base expansion.

Location: src/wallet/scriptpubkeyman.cpp:1152 (head).

Review coverage was incomplete.


Reviewed: 012dd0d6e40e · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:3705f06c6a27c475c77f2533c4d016ac03c829c839b7b538bba5e4768ccf54f1:1ed10c4979af6fc548d6b8d4534d505e02e84ac4b0aa4f8633555b2ba75119e8 --> <!-- ralph-base:a690300ee0d523de0b91e59b00a16fe47297cb85 --> <!-- ralph-head:012dd0d6e40ef5e63a875abfb4ec9d7b0906ea1c --> <!-- ralph-case:ff66862616880b99f759f6da2ba6e56bd5b595693e02ce63ae52e09674ed931b --> Earlier concerns remain unresolved; see the Run archive linked from the Review report. **🟠 Minor · The upgrade changes below-`range_start` ownership without recovery guidance** Active, derivable descriptors with `range_start > range_end - range_start` can lose below-start `IsMine` recognition after upgrade; no affected production wallet was identified. Location: [`src/wallet/scriptpubkeyman.cpp:1152`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/012dd0d6e40ef5e63a875abfb4ec9d7b0906ea1c/src/wallet/scriptpubkeyman.cpp#L1152>) (head). **🔵 Suggestion · Reload assertions miss a regression in `Load()`'s cached index** The functional test's load-time top-up repairs an incorrect `m_max_cached_index` before `check()` runs, so reverting only the `Load()` assignment leaves the new assertions unchanged. Location: [`test/functional/wallet_importdescriptors.py:226`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/012dd0d6e40ef5e63a875abfb4ec9d7b0906ea1c/test/functional/wallet_importdescriptors.py#L226>) (head). **🔵 Suggestion · Lower-start re-import coverage does not check recovered ownership** The existing range-lowering test checks `keypoolsize`, not whether newly included scripts become `IsMine`; the new funded fixture can directly cover that recovery path. Location: [`test/functional/wallet_importdescriptors.py:193`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/012dd0d6e40ef5e63a875abfb4ec9d7b0906ea1c/test/functional/wallet_importdescriptors.py#L193>) (head). **🔵 Suggestion · The description's example range is rejected before expansion** Source inspection shows that `importdescriptors` rejects the description's `[2146483648, 2147483647]`; the commit message's `[2146483647, 2147483646]` passes the bounds and demonstrates the base expansion. Location: [`src/wallet/scriptpubkeyman.cpp:1152`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/012dd0d6e40ef5e63a875abfb4ec9d7b0906ea1c/src/wallet/scriptpubkeyman.cpp#L1152>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`012dd0d6e40e`](<https://git.fish.foo/bitcoin/bitcoin/commit/012dd0d6e40ef5e63a875abfb4ec9d7b0906ea1c>) · [Review report](<https://review.fish.foo/report/36245-012dd0d6e40ef5e63a875abfb4ec9d7b0906ea1c-2896-3.html>) · [Previous reports](<https://review.fish.foo/traces/36245/>) [permalink](<https://review.fish.foo/report/36245-012dd0d6e40ef5e63a875abfb4ec9d7b0906ea1c-2896-3.html>)
DrahtBot commented 2026-10-09 01:25:12 +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).
fametrano (Migrated from github.com) reviewed 2026-11-03 22:00:09 +00:00
fametrano (Migrated from github.com) left a comment

tACK 20a828b321

I built this commit and its parent 4bacf21a13 and, on regtest, imported wpkh(tpub/0/*) with "range": [1000, 1009] and timestamp 0 into a blank watch-only wallet, after paying the addresses at index 0, 999 and 1000. The addresses for indexes 0, 999, 1000, 1009, 1010, 1999 and 2000 come from bip32-descriptors, a BIP32 and descriptor library I maintain, so the boundary is checked without relying on deriveaddresses; Core agreed on all seven and on the checksum.

On the parent, indexes 0 and 999 are mine right after the import, with 6 BTC trusted and three transactions; after unloadwallet/loadwallet both turn false and the wallet shows 2 BTC and one transaction. With this PR it shows 2 BTC and one transaction before and after the reload, index 1000 being the first owned. A wallet written by the parent and opened by this commit also shows 2 BTC, so existing wallets end up where a reload already puts them today. Importing [600000, 600009] with timestamp "now" took 21.0 s on the parent and 0.03 s here, in line with the description. The new functional test fails on the parent at the below-start check and passes here.

Ralph's report takes the timing as the author's own and suggests a cache-end unit test; this adds the timing reproduced, the range boundary checked against a second derivation, and the upgrade path from a wallet the old code wrote. The script and its output: https://gist.github.com/fametrano/7071a6299ad44193c059564f3fbaa00c/74e2eb2449f7f7161524f3f8869e4a3fd7078555

tACK 20a828b3219844c1f1b7624915254bac0c4de8f9 I built this commit and its parent 4bacf21a13 and, on regtest, imported `wpkh(tpub/0/*)` with `"range": [1000, 1009]` and timestamp 0 into a blank watch-only wallet, after paying the addresses at index 0, 999 and 1000. The addresses for indexes 0, 999, 1000, 1009, 1010, 1999 and 2000 come from bip32-descriptors, a BIP32 and descriptor library I maintain, so the boundary is checked without relying on `deriveaddresses`; Core agreed on all seven and on the checksum. On the parent, indexes 0 and 999 are mine right after the import, with 6 BTC trusted and three transactions; after `unloadwallet`/`loadwallet` both turn false and the wallet shows 2 BTC and one transaction. With this PR it shows 2 BTC and one transaction before and after the reload, index 1000 being the first owned. A wallet written by the parent and opened by this commit also shows 2 BTC, so existing wallets end up where a reload already puts them today. Importing `[600000, 600009]` with timestamp "now" took 21.0 s on the parent and 0.03 s here, in line with the description. The new functional test fails on the parent at the below-start check and passes here. Ralph's report takes the timing as the author's own and suggests a cache-end unit test; this adds the timing reproduced, the range boundary checked against a second derivation, and the upgrade path from a wallet the old code wrote. The script and its output: https://gist.github.com/fametrano/7071a6299ad44193c059564f3fbaa00c/74e2eb2449f7f7161524f3f8869e4a3fd7078555
cprkrn commented 2026-11-20 19:32:17 +00:00 (Migrated from github.com)

tACK 20a828b321
Read the diff, the PR's new test fails without the fix + passes with it on my Mac. I also re ran the functional tests with some AI tooling, but checked results myself.

The balance check at 217 also passes without the fix once the wallet is reloaded, so line 215 is what catches the bug. Here are some extra tests written by Grok.

The example range [2146483648,2147483647] in the commit message has been rejected since #35989. I'd suggest [2146483647,2147483646] instead, which still shows the bug.

nit: the comment at walletutil.h:88 says the range start never changes, but a re-import can lower it.

tACK 20a828b3219844c1f1b7624915254bac0c4de8f9 Read the diff, the PR's new test fails without the fix + passes with it on my Mac. I also re ran the functional tests with some AI tooling, but checked results myself. The balance check at 217 also passes without the fix once the wallet is reloaded, so line 215 is what catches the bug. Here are some extra [tests](https://gist.github.com/cprkrn/66bea5dce5d5c1d845f86c8610d93948) written by Grok. The example range `[2146483648,2147483647]` in the commit message has been rejected since #35989. I'd suggest `[2146483647,2147483646]` instead, which still shows the bug. nit: the comment at `walletutil.h:88` says the range start never changes, but a re-import can lower it.
kriss39 commented 2026-11-20 19:54:26 +00:00 (Migrated from github.com)

Thanks for testing. Updated the example range in the commit message to [2146483647, 2147483646] and fixed the walletutil.h comment, a re-import with a lower start can change it.

Thanks for testing. Updated the example range in the commit message to `[2146483647, 2147483646]` and fixed the `walletutil.h` comment, a re-import with a lower start can change it.
cprkrn commented 2026-11-25 21:02:22 +00:00 (Migrated from github.com)

tACK 012dd0d

Re-tested with fixes + it works, thanks for taking my suggestion!

tACK 012dd0d Re-tested with fixes + it works, thanks for taking my suggestion!
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!36245
No description provided.