mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:44:38 +00:00
wallet, rpc: add verify_balance option to importdescriptors #36236
No reviewers
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!36236
Loading…
Reference in a new issue
No description provided.
Delete branch "wallet/importdescriptors-utxo-check"
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?
Another Attempt to fix #28898
When importing descriptors, users may provide a timestamp that is too recent. This causes the rescan to miss older payments, leaving the wallet with an incomplete balance.
This PR adds an optional
verify_balanceargument to importdescriptors, which is false by default.When enabled, it scans the chainstate UTXO set once for outputs belonging to the wallet’s known scripts. If it finds outputs missing from the wallet, it uses the earliest missing output’s block height to extend the rescan backwards where needed, then scans forward. An earlier requested timestamp is still respected so older transaction history is scanned as well.
After rescanning, it compares the wallet’s confirmed mature UTXO outpoints with those collected from the initial chainstate scan. The response includes an info object showing the verification result, UTXO counts and scan details.
For example:
Example response when verification recovers payments:
If the sets still differ, verification reports
"utxo_check": falsewith an error explaining the mismatch. For example, the info object may contain:Failure Response
Verification failures leave the descriptors imported and keep any transactions already discovered. Required blocks being unavailable is reported explicitly. No automatic retry or fallback rescan is performed.
A match verifies the compared UTXO sets; it does not guarantee complete transaction history. Older spent transactions still require an appropriate timestamp or a full rescan. Changes to relevant UTXOs during verification can also cause a mismatch because the final wallet check is compared with the initial chainstate scan.
Functional tests cover recovery with recent timestamps, preserving older requested history, mismatches, mempool transactions, invalid requests and unavailable blocks.
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:
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.
LLM Linter (✨ experimental)
Possible typos and grammar issues:
This error could potentially caused by data corruption.->This error could potentially be caused by data corruption.[missing “be”; current wording is grammatically broken]Possible places where comparison-specific test macros should replace generic comparisons:
assert result["info"]["scan_start_height"] <= first_header["height"]-> replace withassert_greater_than_or_equal(first_header["height"], result["info"]["scan_start_height"])assert node.pruneblockchain(funding_height) >= funding_height-> replace withassert_greater_than_or_equal(node.pruneblockchain(funding_height), funding_height)2026-09-17 20:08:47
🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/34745132536/job/103691544345LLM reason (✨ experimental): CI failed because the IWYU (include-what-you-use) check reported missing/incorrect includes and exited with “Failure generated from IWYU.”
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.
🐙 This pull request conflicts with the target branch and needs rebase.
🐙 This pull request conflicts with the target branch and needs rebase.
Rebased on the latest master and resolved the conflict with #34861.
Since #34861 moved descriptor importing and rescanning into the shared importer, I moved the UTXO verification scan into
src/wallet/imports.cppas well. The RPC now passesverify_balancetoProcessDescriptorsImport(...),andbackup.cpponly handles parsing the RPC argument and formatting the result. compareBase:
09e22fbbb0caafbd2167657c2a8d409ae435b2ccHead:
4da013f50bb8a3808cfddc2ca567a345fc58d296Design and approach
The chainstate scan cannot be interrupted by
abortrescan(src/node/coin.cpp:45, head)When
verify_balance=true, this loop traverses the chainstate cursor without checking the wallet scanner’s abort flag.abortrescansets that flag, but the import checks it only afterScanWalletUTXOSetreturns, so an accepted abort cannot stop this potentially chainstate-wide phase. Pass a cancellation check to the scan and poll it during traversal so abort can end the work promptly.🟡 Minor
Iterator errors can be reported as a complete UTXO scan (src/node/coin.cpp:54, head)
If the underlying LevelDB iterator encounters a read error while advancing, it can become invalid and report the error through
status(). The cursor interface does not expose that status, so this loop treats the invalid cursor as normal exhaustion and returns success with a potentially incomplete set; verification could then report a false match. Expose and check terminal iterator status before declaring the scan complete. No ordinary caller producing an iterator I/O error was identified, so production impact is conditional on such a storage failure.Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review did not complete.
Full review report
🐙 This pull request conflicts with the target branch and needs rebase.
Following what I said in https://github.com/bitcoin/bitcoin/issues/28898#issuecomment-6575096663
I tend to concept NACK.
If the user knows or thinks that the birth time might be wrong he should just set an older birth time, or just set it to
0.If he already imported and knows that the balance is wrong they can just rescan.
If what we want is to help users that don't know that they might be missing some funds I would prefer to add this UTXOset check by default, without adding a new parameter to
importdescriptorsand inform the user with a warning (importdescriptorsalready returns warnings). If I am not wrong we could even inform the user at which hight those funds are.Thanks @polespinasa for the feedback.
I think this could be nice to have. I’ll close this PR and plan to open another with the above suggested approach. Before opening a replacement PR, I’ll benchmark UTXO set scans to assess whether the performance cost is acceptable for a default check.