Skip to content

fix(dash-spv): look at the mempool again when the wallets change - #1516

Draft
PastaPastaPasta wants to merge 2 commits into
devfrom
fix/mempool-replay-on-wallet-change
Draft

PastaPastaPasta wants to merge 2 commits into
devfrom
fix/mempool-replay-on-wallet-change

Conversation

@PastaPastaPasta

Copy link
Copy Markdown
Member

The mempool manager decides only once whether a transaction concerns the wallets, so three cases stay wrong until the transaction confirms:

  • A transaction that one wallet matched, but that spends a coin another wallet's scan had not found yet, is held and never shown to that other wallet. That wallet's balance still counts the spent coin.
  • A wallet registered after filter sync completes never sees payments already in the mempool. With FetchAll nothing is requested again, and peers don't announce a transaction twice.
  • An InstantSend lock that arrives before its transaction is dropped when the transaction then matches no wallet. The InstantSend manager doesn't announce it again, so if a later look finds the transaction relevant, it shows as unlocked.

This builds on #1512 (mempool_during_sync) and contains its commit; only the last commit is new here.

The mempool is now looked at again at three points: the first filter sync completion with mempool_during_sync on, a tick on which the set of wallets has changed, and once the scan of a wallet that was new, or behind at the first completion, reaches the tip. Each time:

  1. Every held transaction that some wallet still holds is passed to the wallet again with its lock, oldest first, in batches of 32. The wallet's write lock is released between batches, so sync and the API aren't starved.
  2. A held transaction that no wallet holds any more, such as a loser swept when a conflicting spend settled, is dropped instead of replayed, so the replay can't bring it back.
  3. The txids found irrelevant are forgotten, and every activated peer is asked for its mempool again. Under BloomFilter the filter is reloaded first. The monitor-hash shortcut is skipped on these refreshes, because a new wallet's scan can finish without changing the hash.

A lock whose transaction matched nothing goes back to the pending map, marked as such, where the retry finds it. When that map is full, the oldest marked lock goes first, then the oldest of the rest, never the new lock.

WalletInterface gains two defaulted methods:

  • wallet_set_revision() defaults to 0, which means "never changes". WalletManager bumps it whenever a wallet is added or removed, so a wallet removed and restored from the same recovery phrase in one session is looked at again.
  • holds_transaction(txid) defaults to true. WalletManager checks whether any account of any wallet records the txid.

Limits, left as follow-ups:

  • Adding an account to an existing wallet doesn't change wallet_set_revision, so it doesn't trigger a refresh.
  • Replay order covers held transactions only. If a held child's parent was irrelevant when it arrived, the parent comes back with the mempool re-request after the replay. The child is then not shown again to a wallet the parent pays until the next refresh.
  • synced_height() is the minimum over wallets, so a wallet stuck behind keeps the after-scan refresh armed. A removed wallet also triggers one refresh. Under FetchAll, each refresh downloads the peers' mempools again, twice per registered wallet.
  • Held locks are kept as they arrived. On dev the mempool manager still gets unverified locks; fix(dash-spv): defer unverified InstantLocks #1336 stops that.
  • A dropped loser's txid is forgotten with the other irrelevant ones, so a peer that still has it in its mempool could hand it over again, and the wallet would record it as new. Peers normally evict a transaction that conflicts with a confirmed or locked one. A follow-up that makes the wallet remember the transactions it dropped closes this.
  • holds_transaction asks whether any wallet records the txid. If the only wallet that matched a held transaction is removed, the transaction is dropped, even if another wallet's scan would later find a coin it spends.

Tests:

  • Unit tests in manager.rs and sync_manager.rs cover:
    • each held transaction replayed with its lock;
    • a wallet registered during the replay being looked at again;
    • the wallet released between batches;
    • the tick refresh on a changed wallet set, and no refresh on an unchanged one;
    • the revision changing when the same wallet is removed and registered again;
    • the Bloom replay after a new wallet's scan;
    • lock retention, and eviction of an unmatched lock first;
    • a wallet behind at the first completion, in both activation modes, looked at once its scan reaches the tip and not again after.
  • wallet_view_tests.rs drives a real WalletManager:
    • a held spend reaches a wallet whose scan finds the coin later;
    • a wallet registered after sync gets mempool payments, and is checked again when its scan completes;
    • a lock that arrived before an unmatched transaction survives the retry;
    • a swept held spend is not brought back by a replay.
  • Fail-first: three branches that each remove part of the fix fail these tests.
  • test_wallet_registered_after_sync_detects_a_payment_already_in_the_mempool runs against dashd under both strategies. It pays a wallet that isn't registered yet, waits, then registers the wallet and checks that both clients detect the payment. Without the fix it fails.
  • cargo fmt --all --check, contrib/run_clippy.py and cargo doc with -D warnings pass, as do the CI wallet and spv groups.

🤖 Generated with Claude Code

PastaPastaPasta and others added 2 commits November 24, 2026 23:13
The mempool manager turns on relay (`filterload`/`filterclear` and `mempool`) only on `FiltersSyncComplete`, so peers announce no transaction or InstantSend lock until the filter scan reaches the tip. A payment to a fresh wallet, InstantSend-locked within seconds, reaches the wallet only minutes later, when sync ends.

`ClientConfig::mempool_during_sync` (off by default, so nothing changes for existing users) activates each peer as it connects instead. InstantSend locks that arrive before the masternode list can verify them wait in the InstantSend manager as before.

Before filter sync completes the wallet may not know every address and coin it owns, so a payment to an address the scan derives later, or a spend of a coin it has not found yet, is judged irrelevant, and peers never announce it again. When filter sync first completes and the wallet watches something it did not when the mempool was first requested, the manager forgets the txids it found irrelevant and asks every activated peer for its mempool again, reloading the bloom filter first under `BloomFilter`. Until then the bloom filter is not reloaded on every change the scan makes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The mempool manager decides once whether a transaction concerns the wallets. Three cases stayed wrong until the transaction confirmed:

- A transaction one wallet matched that spends a coin another wallet's scan had not found yet was held, and never shown to that wallet.
- A wallet registered after filter sync completed never saw payments already in the mempool, because with `FetchAll` nothing was requested again.
- A lock that arrived before a transaction that then matched no wallet was dropped, and the InstantSend manager does not announce it again.

When the mempool is looked at again (the first filter sync completion with `mempool_during_sync`, a wallet registered, and again once its scan reaches the tip), every held transaction is now passed to the wallet again with its lock, in batches that release the wallet between them. Then the txids found irrelevant are forgotten and every activated peer is asked for its mempool again. A wallet that already has a transaction changes nothing, and a held transaction no wallet holds any more, such as one swept because a conflicting spend settled, is dropped instead of replayed. A lock whose transaction matched nothing is kept for the retry, and a full pending-lock map drops its oldest entry, preferring one whose transaction was already found irrelevant, instead of the new lock.

`WalletInterface` gains `wallet_set_revision` (default 0), which `WalletManager` bumps whenever a wallet is added or removed, so a wallet removed and restored in the same session counts too, and `holds_transaction` (default true).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@PastaPastaPasta

PastaPastaPasta commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

Pre-review record for head 4684f180575f86ba8e9da023f4c19ba7b9413ce5, the commit after #1512's head 602fac26d0.

An independent automated code review read this commit twice: in full at fdd74b1d80, then the fixes at 813a2d2003. Verdict on the second read: approved with suggestions. This head adds the fixes for that read's two medium findings, and a third fix found while porting a follow-up's tests.

Fixed after the first read (verdict then: requires changes):

  • The replay passed a held transaction that the wallet had swept, because a conflicting spend settled, back to the wallet, which recorded it again with its change. The replay now drops any held transaction no wallet holds (holds_transaction).
  • Under BloomFilter, the refresh after a new wallet's scan was skipped when the monitor hash had already been updated by the tick's filter rebuild. These refreshes now skip the hash check.
  • A wallet that was behind when filter sync first completed got no later look. It is now looked at once its scan reaches the tip.
  • A full pending-lock map evicted locks that could still be used. Doc and import nits were fixed too.

Fixed after the second read:

  • The eviction preference for locks whose transaction matched no wallet relied on the seen-txid entry, which lasts about three minutes and is cleared on each refresh. So on a full map it evicted the newest unmatched locks. Each pending lock now records whether its transaction matched nothing.
  • The look after the scan of a wallet that was behind at the first completion applied only with mempool_during_sync on. It now applies in the default mode too.
  • A wallet removed and restored from the same recovery phrase in one session has the same wallet id, so a hash of the ids didn't change. wallet_set_hash became wallet_set_revision, a counter WalletManager bumps whenever a wallet is added or removed.

After the fixes, the pending-lock tuple became a small private PendingLock struct, with no change in behaviour.

Fail-first: three branches that each remove part of the fix fail the new mempool unit tests, and the dashd test fails without the fix.

Left as is, listed under limits in the description: no refresh when an account is added to an existing wallet, held-child replay ordering, the refresh staying armed while a wallet is stuck behind, a dropped loser re-fetched from a lagging peer, and unverified locks on dev until #1336.


🤖 Posted autonomously by Claude (claude-opus-5-5) on behalf of pasta.

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.80284% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.59%. Comparing base (6e15db8) to head (4684f18).
⚠️ Report is 43 commits behind head on dev.

Files with missing lines Patch % Lines
key-wallet-manager/src/process_block.rs 66.66% 8 Missing ⚠️
key-wallet-manager/src/wallet_interface.rs 0.00% 6 Missing ⚠️
dash-spv/src/sync/mempool/manager.rs 99.05% 2 Missing ⚠️
key-wallet-manager/src/lib.rs 71.42% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1516      +/-   ##
==========================================
+ Coverage   87.53%   87.59%   +0.05%     
==========================================
  Files         270      270              
  Lines       71309    71845     +536     
==========================================
+ Hits        62421    62933     +512     
- Misses       8888     8912      +24     
Flag Coverage Δ
core 83.37% <58.97%> (-0.01%) ⬇️
rpc 83.37% <58.97%> (-0.01%) ⬇️
spv 95.03% <99.61%> (+0.04%) ⬆️
wallet 83.37% <58.97%> (-0.01%) ⬇️
Files with missing lines Coverage Δ
dash-spv/src/client/config.rs 82.38% <100.00%> (+0.57%) ⬆️
dash-spv/src/client/config_test.rs 100.00% <100.00%> (ø)
dash-spv/src/client/lifecycle.rs 93.68% <100.00%> (+0.06%) ⬆️
dash-spv/src/sync/mempool/sync_manager.rs 99.67% <100.00%> (+0.14%) ⬆️
key-wallet-manager/src/accessors.rs 63.03% <100.00%> (+0.45%) ⬆️
dash-spv/src/sync/mempool/manager.rs 98.20% <99.05%> (+0.08%) ⬆️
key-wallet-manager/src/lib.rs 73.23% <71.42%> (-0.04%) ⬇️
key-wallet-manager/src/wallet_interface.rs 19.14% <0.00%> (-2.81%) ⬇️
key-wallet-manager/src/process_block.rs 94.98% <66.66%> (-0.78%) ⬇️

... and 8 files with indirect coverage changes

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-conflict The PR conflicts with the target branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant