Skip to content

wallet: avoid MarkUnused P2MR full top-up - #31

Merged
kiwidream merged 3 commits into
0.1.xfrom
investigate-issue-14
Jun 29, 2026
Merged

kiwidream merged 3 commits into
0.1.xfrom
investigate-issue-14

Conversation

@kiwidream

@kiwidream kiwidream commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Avoid full synchronous keypool top-up from MarkUnusedAddresses for ranged P2MR descriptors when the receive pool is still above the low watermark.
  • Run bounded P2MR low-watermark refill from MarkUnusedAddresses, while preserving full lookahead for recovery/rescan paths.
  • Persist completed deferred-top-up state when address use advances P2MR descriptor next_index.
  • Add wallet tests covering above-watermark behavior, generated scripts, bounded refill, recovery lookahead, locked wallets, and non-P2MR behavior.

Base

Validation

  • cmake --build build_dev_mode --target test_bitcoin -j8
  • build_dev_mode/bin/test_qbit '--run_test=wallet_tests/MarkUnusedAddresses*'\n- build_dev_mode/bin/test_qbit '--run_test=wallet_tests/P2MRGetNew*'\n- Docker lint: DOCKER_BUILDKIT=1 docker build -t qbit-linter-issue14 --file "./ci/lint_imagefile" . && docker run --rm -v "$PWD":/bitcoin -v /Users/miller/conductor/repos/qbit-v1/.git:/Users/miller/conductor/repos/qbit-v1/.git qbit-linter-issue14\n\n## Related\n- Addresses Avoid full synchronous P2MR keypool top-up from MarkUnusedAddresses #14.

Note

Medium Risk
Changes wallet keypool refill timing on transaction detection and rescan for P2MR descriptors; incorrect behavior could affect address availability but is scoped and heavily tested.

Overview
Ranged P2MR descriptors no longer trigger a full synchronous keypool top-up from MarkUnusedAddresses when the receive pool is still above the low watermark. Instead they use bounded low-watermark refill via TopUpWithInternalHintResult, skip refill for scripts already consumed by GetNewDestination, and defer refill when the wallet is locked (with logging).

A new MarkUnusedAddressesOptions (internal_hint, preserve_full_keypool_lookahead) is threaded through the SPKM API. CWallet::AddToWalletIfInvolvingMe sets preserve_full_keypool_lookahead during old-block rescan so recovery still restores full lookahead; non-P2MR descriptors keep the previous full top-up behavior.

Deferred create keypool state is persisted when address use advances next_index (including clearing the flag on P2MR GetNewDestination when appropriate). Wallet tests cover above-watermark, generated scripts, bounded refill, recovery lookahead, locked wallets, and non-P2MR.

Reviewed by Cursor Bugbot for commit 23a400c. Bugbot is set up for automated code reviews on this repo. Configure here.

@kiwidream
kiwidream changed the base branch from getnewaddress-slow-rpc to 0.1.x June 26, 2026 19:42
@kiwidream
kiwidream force-pushed the investigate-issue-14 branch from 0bbf38e to 7918b76 Compare June 26, 2026 19:44
@kiwidream

Copy link
Copy Markdown
Member Author

PR Review

Findings

No blocking findings from this review pass.

Consensus Impact

Consensus-adjacent

This does not change transaction or block validity, but it changes P2MR wallet keypool refill timing, descriptor persistence, and private-key material availability around transaction sync/rescan paths. That is wallet/funds-safety adjacent, especially for P2MR-only wallets.

Review Scope

Reviewed issue #14, PR body, commit metadata, changed files, full diff, surrounding wallet/SPKM code, direct call sites, comments/reviews, review-thread state, and visible CI status. The PR has no comments, reviews, or unresolved review threads at the time of review.

Deep Checks Performed

Traced CWallet::AddToWalletIfInvolvingMe into DescriptorScriptPubKeyMan::MarkUnusedAddresses, including internal_hint, rescanning_old_block, next_index advancement, descriptor persistence, low-watermark checks, locked-wallet handling, and non-P2MR fallback behavior. Also checked the top-up target math for normal bounded refill versus full lookahead preservation and verified the deferred-create-top-up path remains separate.

Issue Fit

The PR matches #14’s main contract: ranged P2MR descriptors avoid unconditional full TopUp() above the low watermark, low-watermark normal handling is bounded, old-block recovery can preserve full lookahead, locked wallets do not fail while cached entries remain, deferred-create top-up is preserved, and non-P2MR descriptors keep full top-up behavior. The added unit tests cover those cases directly.

qbit-Specific Checks

Checked P2MR descriptor keypool behavior, PQC private-key persistence assumptions, receive/change internal hinting, recovery lookahead, locked encrypted wallet behavior, explicit keypoolrefill separation, and non-P2MR compatibility. AuxPoW, validation consensus rules, PHOTON, RPC response shapes, and release surfaces are not touched.

Validation Reviewed

PR body reports local test_bitcoin build, targeted wallet_tests/MarkUnusedAddresses*, targeted wallet_tests/P2MRGetNew*, and Docker lint. I locally checked git diff --check against the PR base. At review time, CI was still in progress: classify validation, public docs lint, and release validator checks had passed, while focused unit suites, build smoke, functional smoke, Windows jobs, Cursor Bugbot, and Required Merge Gate were still pending, so CI logs for the new wallet tests were not yet available.

Residual Risk

I did not rerun the wallet unit tests locally because no existing build directory was present. I also did not benchmark the MarkUnused path; this review relies on code/test inspection for the “no full top-up above watermark” claim. The PR uses direct unit coverage for MarkUnusedAddresses; it does not add a functional rescan test that exercises the full wallet ingestion path end to end.

@kiwidream
kiwidream force-pushed the investigate-issue-14 branch from 7918b76 to 23a400c Compare June 28, 2026 15:42
@djh58
djh58 self-requested a review June 29, 2026 14:57
@kiwidream
kiwidream merged commit 22a7a4f into 0.1.x Jun 29, 2026
33 checks passed
kiwidream added a commit that referenced this pull request Jun 29, 2026
* wallet: avoid MarkUnused P2MR full top-up

* wallet: avoid MarkUnused lock inversion

* wallet: preserve P2MR recovery lookahead
djh58 pushed a commit that referenced this pull request Jul 2, 2026
* Accept legacy AuxPoW RPC payloads (#23)

* Prevent AuxPoW signalling in permissionless GBT (#22)

* wallet: defer P2MR getnewaddress keypool refill (#13)

* wallet: defer P2MR getnewaddress keypool refill

* wallet: bound internal P2MR keypool refill

* wallet: align P2MR top-up lock ordering

* wallet: schedule raw change P2MR refills

* wallet: report paused P2MR refills as failed

* wallet: log inline P2MR refill failures

* ci: fix BuildKit helper lint

* Document AuxPoW commitment order fields (#35)

* Restrict explicit wallet output types on P2MR-only chains (#25)

* Restrict explicit wallet output types

* test: run BECH32 wallet suites on unrestricted regtest

* fuzz: run wallet transaction target on unrestricted regtest

* Fix BuildKit helper lint

* Report missing wallet output managers accurately

* Allow managerless wallets with default output options

* test: add independent P2MR and CTV script vectors (#29)

* Add independent P2MR PQC witness vector tests (#30)

* test: add independent P2MR PQC witness vector

* test: verify P2MR wrong-domain vector signature

* Fix AuxPoW legacy payload disambiguation

* wallet: avoid MarkUnused P2MR full top-up (#31)

* wallet: avoid MarkUnused P2MR full top-up

* wallet: avoid MarkUnused lock inversion

* wallet: preserve P2MR recovery lookahead

* Split P2MR wallet unit tests (#52)

* Remove unused P2MR wallet test import (#58)

* Shard P2MR wallet unit tests (#60)

* Add independent CHECKDATASIGPQC vector tests (#38)

* test: add P2MR data-hash proof fixtures (#43)

* test: add independent P2MR commitment vectors (#47)

* Reject invalid raw funding outputs (#37)

* Validate witness-pruned assumevalid before wipe (#59)

* Add independent CHECKDATASIGADDPQC vector tests (#42)

* qt: recommend wallet encryption by default (#62)

* Verify libbitcoinpqc tagged subtree imports (#41)

* ci: verify libbitcoinpqc tagged subtree imports

* ci: handle peeled libbitcoinpqc tag refs

* ci: peel libbitcoinpqc tag objects before compare

* ci: pass auth to libbitcoinpqc verifier

* ci: pin libbitcoinpqc verifier inputs

* ci: verify libbitcoinpqc import tree

* Add P2MR PQC annex sighash vectors (#54)

* Add P2MR PQC annex sighash vectors

* Guard annex mutation vector test

* Add P2MR data signature support to Qt Sign/Verify (#63)

* qt: add P2MR data signature dialog

* qt: support text data in P2MR signatures

* qt: align P2MR proof verification with RPC

* test: add P2MR CHECKSIGPQC sighash vectors (#55)

* Add rawmr future-leaf coverage (#64)

* test: cover P2MR SIGHASH_SINGLE missing outputs (#57)

* test: add P2MR PQC codeseparator vectors (#56)

* Fix PQC data-hash counter accounting (#46)

* Fix PQC data-hash counter accounting

* Fix PQC data-hash leaf error priority

* Report final PQC slot failures as exhausted

* doc: add v0.1.2-testnet4 release notes

* build: bump version to v0.1.2-testnet4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants