Skip to content

fix(key-wallet-manager): keep a conflict for its ChainLock - #1520

Draft
PastaPastaPasta wants to merge 7 commits into
devfrom
fix/conflict-kept-for-chainlock
Draft

PastaPastaPasta wants to merge 7 commits into
devfrom
fix/conflict-kept-for-chainlock

Conversation

@PastaPastaPasta

Copy link
Copy Markdown
Member

A plain block can't overturn an InstantSend-locked record, so when a block transaction conflicts with a locked record, the record stays until a ChainLock covers that block. But nothing swept it then: the ChainLock applied, and the conflicting transaction was no longer at hand. A wallet the block wasn't fetched for, because the block holds none of its scripts, never saw the conflict at all. In both cases the record kept the coins it spends marked spent and its change offered, for a transaction that can never confirm.

This builds on #1519 and contains the commits of #1512, #1516, #1518 and #1519; only the last commit is new here.

  • Wallets the block wasn't fetched for. A block transaction that spends an input of an unconfirmed record in such a wallet sweeps that wallet by the same rule, recording nothing of the transaction. Each wallet's unconfirmed spends are collected once per block, before the derived-address passes, and the sweep runs once per transaction after them.
  • Kept for the ChainLock. A block transaction that conflicts with another transaction's unconfirmed record, swept with a context that isn't ChainLocked, is kept with the txids it beats until a ChainLock covers its block. Then every wallet sweeps with it. A reorg below its block drops it.
  • Descendants. Behind a ChainLocked winner the sweep also follows a loser's InstantSend-locked descendants: nothing built on a transaction a ChainLock beat can exist. A record whose txid the wallet holds a lock for counts as locked, also after a fork reset its context.
  • Never again. A transaction a ChainLocked conflict overturned in any wallet is remembered and refused when it arrives again, locked or not. So is every other spend a kept conflict beat, once its ChainLock lands, even one a plain block already swept whose late lock hasn't come yet. Only the exact txid is refused.
  • WalletManager::awaiting_chain_lock_len and clear_awaiting_chain_lock, for a host that deletes its chain data.
  • WalletInfoInterface gains unconfirmed_spent_outpoints and sweep_conflicts_only, both defaulted.

Limits:

  • Both sets are in memory and capped at 1,000, the lowest block going first. A restart between a block and its ChainLock loses what was kept, and the locked record then stays. Someone who gets many conflicting transactions mined before a ChainLock can push out older entries. Each kept entry holds the whole transaction.
  • unconfirmed_spent_outpoints walks every record of every wallet once per processed block.
  • A spend of an output of a refused transaction is not refused itself.

Tests:

  • conflict_sweep_context_test.rs: a lagging fetched wallet still keeps the conflict; the lock set after a fork; locked descendants behind a ChainLocked winner; only a conflict with another transaction is kept.
  • awaiting_chain_lock_cap_test.rs: the cap drops the lowest block first; clearing the kept conflicts.
  • dismissed_conflict_fetch_tests.rs, with real BIP-158 filters and per-wallet block fetches: the abandoning wallet sweeps with a conflict only another wallet fetched, with the ChainLock before or after the block; a reorg; an unlocked spend a plain block beat goes at the ChainLock whether its late lock comes before or after it.
  • wallet_view_tests.rs: a locked spend a ChainLock overturned is refused when replayed or relayed, while a different spend of the same coins and an unrelated payment are taken.
  • Unit tests for the cap order.
  • Fail-first: without the sweep of unfetched wallets, the sweep at the ChainLock, the refusal, the descendant walk, the lock set, or the memory of what a kept conflict beat, these tests fail.
  • cargo fmt --all --check, contrib/run_clippy.py, cargo doc with -D warnings, and the CI wallet and spv groups pass.

🤖 Generated with Claude Code

PastaPastaPasta and others added 7 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]>
An empty result is now `TransactionCheckResult::default()`, for the checks that return early with nothing found.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A conflict sweep removes a loser that a locked or mined transaction beat, and an abandon removes a transaction the user gave up on, but nothing remembers either. Peers still hold them in their mempools, so a peer, or the mempool manager's replay, delivers them again as new arrivals, and the wallet credits them again.

The wallet now keeps the txids it dropped this way, in memory, with abandons and sweeps capped separately so that sweeps, which anyone can cause with a locked double-spend, can't push out the user's abandons. Delivered again unconfirmed and unlocked, such a transaction is ignored, and so is a spend of one of its outputs. Mined or InstantSend-locked, it is real after all and applies as before.

Spends of a dismissed transaction are held back in a third bounded set, because anyone can send them. A dismissal pushed out at capacity releases the spends it held back. A spend is held only while a transaction it spends is dismissed: a lock or block settling that transaction releases it, recursively, unless the wallet dismissed the spend in its own right. A released spend is still remembered so that its own lock has it fetched again. A spend of more than 16 dismissed transactions is held until it settles itself. A transaction the wallet already records is not held back.

`WalletInfoInterface` gains `is_dismissed`, `refetch_on_lock`, `dismiss_spend` and `undismiss`, all defaulted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A transaction one wallet dismissed could still reach the others. The mempool replay passed it to a wallet registered later, a lock for it that arrived after its seen entry expired was ignored, and a locked spend fetched before its abandoned parent came back matched no wallet and was dropped, so the wallet kept offering change it held a verified spend of.

- `WalletManager::process_mempool_transaction` ignores an unlocked transaction any wallet dismissed. An unlocked spend of one is held back in each wallet that dismissed what it spends, and reaches the other wallets only if each dismissed transaction it spends is held by some wallet.
- A block clears the dismissal of each transaction it contains in every wallet, matched or not.
- A validated lock for a txid a wallet dismissed, or held back as a spend, has it requested again. An unvalidated lock doesn't, so a peer can't use one to make the client request arbitrary transactions. The mempool manager now receives the `validated` flag of `InstantLockReceived` and records it with each pending and held lock. Only a validated lock keeps a spend or replays kept spends, and an unvalidated one is not passed to the wallet with a transaction that a wallet dismissed or that spends one, so it can't settle the dismissal.
- The mempool manager keeps a locked transaction that matched no wallet but spends a dismissed transaction or a remembered spend of one, and one a wallet took in while another wallet dismissed what it spends, at most 1,000 for up to 24 hours. It passes it to the wallet again with its lock when a transaction it spends comes back: fetched with its lock, locked while held, or confirmed in a block.

`WalletInterface` gains `is_dismissed`, `refetch_on_lock` and `spends_dismissed_elsewhere`, all defaulted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A conflict sweep removes a loser that a locked or mined transaction beat and frees the inputs only the loser spent, but the coins themselves were dropped when the loser took them. The wallet showed less than it held until a rescan delivered their funding again.

The sweep now credits each freed coin again, with the confirmation and lock its funding transaction has now, but only while an account still holds the transaction that funded it: a fork or the same sweep may have removed it, and then the coin never existed. Outputs of transactions the sweep removed are left out of the released set. A credited coin is a watched outpoint again, so the monitor hash changes and a Bloom filter is rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A plain block can't overturn an InstantSend-locked record, so a locked record that a block transaction conflicts with stays until a ChainLock covers that block. Nothing then swept it: the ChainLock applied, but the conflicting transaction was no longer at hand. And a wallet the block wasn't fetched for, because the block holds none of its scripts, never saw the conflict at all. Its locked or pending record kept the coins it spends marked spent, and its change stayed offered.

- A block transaction that spends an input of an unconfirmed record in a wallet the block wasn't fetched for sweeps that wallet by the same rule. Nothing of the transaction is recorded there. Each wallet's unconfirmed spends are collected once per block.
- A block transaction that conflicts with another transaction's unconfirmed record, swept with a context that isn't ChainLocked, is kept until a ChainLock covers its block; then every wallet sweeps with it. At most 1,000 are kept, the lowest block going first. A reorg below its block drops it, after every wallet's truncation.
- Behind a ChainLocked winner the sweep also follows a loser's InstantSend-locked descendants: nothing built on a transaction a ChainLock beat can exist. A record whose txid the wallet holds a lock for counts as locked, also after a fork reset its context.
- A transaction a ChainLocked conflict overturned in any wallet is remembered for the session, at most 1,000, and refused when it arrives again, locked or not. So is every other unconfirmed spend a kept conflict beat, once its ChainLock lands, even if a plain block already swept it. Only its own txid is refused.
- `WalletManager` reports and clears the conflicts it keeps, for a reset of the chain data.

Both sets are in memory: a restart between a block and its ChainLock loses what was kept, and the locked record then stays. Each cap can be filled by someone who gets many conflicting transactions mined, which pushes out the oldest entries.

`WalletInfoInterface` gains `unconfirmed_spent_outpoints` and `sweep_conflicts_only`, both defaulted.

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 521fd64418de7234f408191c8e0384bba1c8fc64, the last commit after #1519's head d0cd789c04.

An independent automated code review read this PR at aee00a0362. Verdict: approved with suggestions. This head makes these fixes:

  • Every unconfirmed spend that a kept conflict beat is now remembered with it. When the ChainLock lands, each of those spends is refused, even one a plain block already swept whose late lock hasn't come yet. A test covers the lock arriving both before and after the ChainLock.
  • clear_awaiting_chain_lock gets a test.
  • Doc comments and tests follow the repository's conventions.

Two suggestions are left as limits and listed in the description:

  • Both sets live in memory, capped at 1,000. Someone who gets many conflicting transactions mined can push out older entries.
  • A spend of an output of a refused transaction is not refused itself.

Fail-first: six branches each turn off one part: the sweep of unfetched wallets, the sweep at the ChainLock, the refusal, the descendant walk, the lock set, and the memory of what a kept conflict beat. Each fails the new tests.

Gate on this head: fmt, clippy, doc, and the CI wallet and spv groups pass.


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

PastaPastaPasta added a commit that referenced this pull request Nov 25, 2026
The two TransactionsSwept events #1520 adds set winner_block, the field
#1507 renamed from winner_mined_height.
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.54023% with 76 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.66%. Comparing base (6e15db8) to head (521fd64).
⚠️ Report is 43 commits behind head on dev.

Files with missing lines Patch % Lines
key-wallet-manager/src/process_block.rs 76.00% 24 Missing ⚠️
...allet/managed_wallet_info/wallet_info_interface.rs 57.40% 23 Missing ⚠️
key-wallet-manager/src/wallet_interface.rs 0.00% 15 Missing ⚠️
dash-spv/src/sync/mempool/manager.rs 98.27% 7 Missing ⚠️
key-wallet-manager/src/lib.rs 94.20% 4 Missing ⚠️
...wallet/src/wallet/managed_wallet_info/dismissed.rs 99.10% 2 Missing ⚠️
key-wallet/src/wallet/managed_wallet_info/mod.rs 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1520      +/-   ##
==========================================
+ Coverage   87.53%   87.66%   +0.12%     
==========================================
  Files         270      271       +1     
  Lines       71309    72644    +1335     
==========================================
+ Hits        62421    63682    +1261     
- Misses       8888     8962      +74     
Flag Coverage Δ
core 83.48% <89.76%> (+0.09%) ⬆️
rpc 83.48% <89.76%> (+0.09%) ⬆️
spv 95.07% <99.02%> (+0.09%) ⬆️
wallet 83.48% <89.76%> (+0.09%) ⬆️
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/filters/manager.rs 97.93% <ø> (ø)
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%) ⬆️
.../src/managed_account/managed_core_funds_account.rs 89.82% <100.00%> (+0.05%) ⬆️
...wallet/src/transaction_checking/account_checker.rs 66.92% <100.00%> (-0.44%) ⬇️
...-wallet/src/transaction_checking/wallet_checker.rs 98.72% <100.00%> (+0.04%) ⬆️
...y-wallet/src/wallet/managed_wallet_info/helpers.rs 78.27% <100.00%> (+3.45%) ⬆️
... and 7 more

... and 5 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