Skip to content

wallet: fix abandon crash on inconsistent state - #35115

Closed
tony-ku wants to merge 1 commit into
bitcoin:masterfrom
tony-ku:wallet-34599-abandon-confirmed-descendant
Closed

tony-ku wants to merge 1 commit into
bitcoin:masterfrom
tony-ku:wallet-34599-abandon-confirmed-descendant

Conversation

@tony-ku

@tony-ku tony-ku commented Apr 19, 2026

Copy link
Copy Markdown

Fixes #34599.

CWallet::AbandonTransaction walks the abandoned tx's descendants
via mapTxSpends and asserts that none of them is confirmed or in
the mempool. That holds at the consensus level — if the parent isn't
in the chain, no child can be — but the assertion runs against the
wallet's in-memory per-tx state, which can transiently diverge:

  • during reorgs, where per-tx state transitions are processed
    individually and intermediate states are observable;
  • around RBF candidates, where multiple wallet wtxs share an input
    outpoint via mapTxSpends and so end up linked to the same parent
    output even though only one of them is the "real" descendant.

When the divergence is hit, the node aborts. The fix replaces the
two assert calls with a TxUpdate::UNCHANGED early-return so the
recursion safely halts at the inconsistent descendant instead of
crashing — preserving the original intent ("don't abandon something
that's confirmed or in mempool") and matching the no-assert pattern
already used by the sibling lambdas in MarkConflicted,
transactionAddedToMempool and transactionRemovedFromMempool.

A new wallet_tests/abandon_with_confirmed_descendant unit test
synthesises the inconsistency (parent TxStateInactive, child
TxStateConfirmed linked via mapTxSpends) and exercises the
assertion site directly: pre-fix it aborts, post-fix
AbandonTransaction returns true, marks the parent abandoned and
leaves the child untouched.

Precedent: PR #31757 / commit 9ef429b fixed an analogous wallet
assertion crash on double block disconnection by handling the
inconsistent-state case rather than relying on the assertion.

The lambda inside CWallet::AbandonTransaction asserts that no
descendant of the transaction being abandoned is confirmed or in
the mempool. This is a consensus-level invariant, but the assertion
runs against the wallet's in-memory per-tx state, which can
transiently diverge from the chain -- e.g. during a reorganisation,
or when RBF candidates share inputs and end up linked to the same
parent output via mapTxSpends.

When that divergence is reached the assertion aborts the node.
Replace it with a graceful TxUpdate::UNCHANGED early-return,
preserving the behavioural intent ("do not abandon a confirmed or
mempool descendant") while no longer crashing. This mirrors how the
same lambda already handles isBlockConflicted and isAbandoned
descendants, and how the lambdas in MarkConflicted,
transactionAddedToMempool and transactionRemovedFromMempool handle
unexpected state.

Halting the recursion at the inconsistent descendant is the safe
choice: we cannot know whether the descendant's recorded children
belong to the same chain that confirmed it, so propagating the
abandonment further could compound the inconsistency.

Fixes bitcoin#34599
@DrahtBot

DrahtBot commented Apr 19, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #35163 (Wallet: add consistency check on wallet transactions stored on wallet database when loading the wallet. by Luquitasjeffrey)

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.

@Luquitasjeffrey

Copy link
Copy Markdown

There is a root issue that is not being handled in this pr which is that a child transaction cannot be confirmed if the parent is unconfirmed. This may solve the specific assertion failure here but it leaves the transactions in inconsistent state in the wallet

@tony-ku

tony-ku commented Apr 21, 2026

Copy link
Copy Markdown
Author

Thanks for reading. You're right that on-chain a child can't be confirmed while its parent is unconfirmed, but I think the "inconsistent state" here is narrower than it looks.

mapTxSpends is a std::unordered_multimap<COutPoint, Txid> that records every wtx claiming a given outpoint, not the canonical on-chain spend. An RBF pair (original + replacement) both live under the same input outpoint key, so the recursion in AbandonTransaction walks both — and when the replacement is confirmed while the original is TxStateBlockConflicted (which is exactly what MarkConflicted leaves behind after the replacement's block is connected in AddToWalletIfInvolvingMe), abandoning the original walks into a confirmed wtx and aborts the node. No per-wtx state is inconsistent in that moment — each wtx has a valid state — only the mapTxSpends-derived linkage looks like a parent→child edge when it is really a shared-input RBF edge.

The early-return halts the walk at that descendant without mutating it, which is the same shape the sibling descendant-walk lambdas in MarkConflicted, transactionAddedToMempool and transactionRemovedFromMempool already use. The precedent for tolerating rather than asserting on this kind of transient wallet-state observation is PR #31757 / 9ef429b, which fixed an analogous crash on double block disconnection.

A root-cause fix would mean either making mapTxSpends logical (so RBF siblings don't collide) or reworking descendant discovery to cross-check against per-wtx state before recursing — both are substantially larger than the crash warrants, and neither is done by the sibling lambdas today. Happy to revisit if you see a specific reachable state where a per-wtx field ends up wrong as a result of this change.

@dergoegge

Copy link
Copy Markdown
Member

This PR looks LLM generated, without real understanding from the author.

LLM generated unit tests are not great for proving the existence of bugs, as LLMs can abuse the access to internal state to produce the wanted results.

Please write a functional test that demonstrates the bug, as that would at least increase the confidence in the LLMs analysis.

@achow101

Copy link
Copy Markdown
Member

The assertion exists for a reason, and this PR does not demonstrate why the assumption for that assertion is incorrect. Unit tests can call things out of order and misuse APIs, it does not sufficiently show why the assumptions are incorrect.

I'm going to close this PR as it is clearly LLM generated and there is no evidence that the author understands the mechanism of the issue.

@achow101 achow101 closed this Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wallet: Assertion !wtx.isConfirmed() failed

5 participants