Skip to content

wallet: Track no-longer-spendable TXOs separately#27865

Open
achow101 wants to merge 19 commits into
bitcoin:masterfrom
achow101:wallet-unspent-txos
Open

wallet: Track no-longer-spendable TXOs separately#27865
achow101 wants to merge 19 commits into
bitcoin:masterfrom
achow101:wallet-unspent-txos

Conversation

@achow101

@achow101 achow101 commented Jun 12, 2023

Copy link
Copy Markdown
Member

In #27286, the wallet keeps track of all of its transaction outputs, even if they are already spent or are otherwise unspendable. This TXO set is iterated for balance checking and coin selection preparation, which can still be slow for wallets that have had a lot of activity. This PR aims to improve the performance of such wallets by moving UTXOs that are definitely no longer spendable to a different map in the wallet so that far fewer TXOs need to be iterated for the aforementioned functions.

Unspendable TXOs (not to be confused with Unspent TXOs) are those which have a spending transaction that has been confirmed, or are no longer valid due to reorgs. TXOs that are spent in unconfirmed transactions remain in the primary TXO set, and are filtered out of balance and coin selection as before.

@DrahtBot

DrahtBot commented Jun 12, 2023

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/27865.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK remyers, murchandamus, jonatack, rkrux
Stale ACK w0xlt

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:

  • #35716 (wallet: Replace mapWallet and wtxOrdered with a boost::multi_index by achow101)
  • #35501 (wallet: store all witness variants of a transaction by achow101)
  • #35294 (wallet: Update tx chain state during loading during AttachChain instead of before by achow101)
  • #35151 (wallet, follow-up: Refactor IsSpent to use HowSpent by musaHaruna)
  • #34909 (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux)
  • #34872 (wallet: fix mixed-input transaction accounting in history RPCs by w0xlt)
  • #34681 (wallet: move rescan logic into ChainScanner and wallet/scan by Eunovo)
  • #34400 (wallet: parallel fast rescan (approx 5x speed up with 8 threads) by Eunovo)
  • #33034 (wallet: Store transactions in a separate sqlite table by achow101)
  • #32895 (wallet: Prepare for future upgrades by recording versions of last client to open and decrypt by achow101)
  • #29278 (Wallet: Add maxfeerate wallet startup option by ismaelsadeeq)
  • #25722 (refactor: Use util::Result class for wallet loading by ryanofsky)

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.

This was referenced Jun 12, 2023
@achow101 achow101 force-pushed the wallet-unspent-txos branch from 6925928 to 7a50755 Compare June 28, 2023 17:53
@achow101 achow101 force-pushed the wallet-unspent-txos branch 2 times, most recently from 8c4b04a to 9730ec0 Compare June 28, 2023 22:26
@w0xlt

w0xlt commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

I think the earlier RemoveTxs() suggestion (#27865 (comment)) may still be needed here.

The unconditional MarkTXOUsable(txin.prevout) works when the removed tx is the only confirmed spend, but if another conflicting wallet tx is already confirmed, the same outpoint should stay unusable.

Collecting the inputs in spent_outpoints lets us erase the removed txs from mapWallet first, then run the suggested !IsSpent(outpoint, /*min_depth=*/1) check against the remaining wallet transactions before moving anything back to m_txos.

Suggested test:

diff --git a/test/functional/wallet_importprunedfunds.py b/test/functional/wallet_importprunedfunds.py
index bd08819fdc..0b2c05c0f7 100755
--- a/test/functional/wallet_importprunedfunds.py
+++ b/test/functional/wallet_importprunedfunds.py
@@ -157,6 +157,36 @@ class ImportPrunedFundsTest(BitcoinTestFramework):
         available_utxos = [u["txid"] for u in node.listunspent(minconf=0)]
         assert utxo["txid"] not in available_utxos, "UTXO should still be spent by conflicting tx"
 
+        self.log.info("Test removeprunedfunds preserves inputs spent by another confirmed tx")
+        parent_txid = node.sendtoaddress(node.getnewaddress(), 1)
+        self.generate(node, 1)
+        parent_utxo = next(
+            utxo for utxo in node.listunspent() if utxo["txid"] == parent_txid
+        )
+        parent_outpoint = (parent_txid, parent_utxo["vout"])
+
+        child_txid = node.send(outputs=[{node.getnewaddress(): Decimal("0.5")}], inputs=[parent_utxo])["txid"]
+        child_fee = node.gettransaction(child_txid)["fee"]
+
+        # Replace child_txid with a conflicting tx and confirm it. Removing
+        # child_txid must not make parent_outpoint spendable again.
+        output_value = parent_utxo["amount"] + child_fee - Decimal("0.00001")
+        raw_conflict_tx = node.createrawtransaction(inputs=[parent_utxo], outputs=[{node.getnewaddress(): output_value}])
+        signed_conflict_tx = node.signrawtransactionwithwallet(raw_conflict_tx)
+        conflict_txid = node.sendrawtransaction(signed_conflict_tx["hex"])
+        assert_not_equal(conflict_txid, child_txid)
+        self.generate(node, 1)
+
+        def confirmed_parent_is_unspent():
+            return any(
+                (utxo["txid"], utxo["vout"]) == parent_outpoint
+                for utxo in node.listunspent(minconf=0)
+            )
+
+        assert_equal(confirmed_parent_is_unspent(), False)
+        node.removeprunedfunds(child_txid)
+        assert_equal(confirmed_parent_is_unspent(), False)
+
         self.log.info("Test removeprunedfunds restores inputs of removed confirmed spend")
         parent_txid = node.sendtoaddress(node.getnewaddress(), 1)
         self.generate(node, 1)

@achow101

achow101 commented Jul 1, 2026

Copy link
Copy Markdown
Member Author

I think the earlier RemoveTxs() suggestion (#27865 (comment)) may still be needed here.

Checking the states to revert them was starting to get a bit complicated, so I ended up doing the overkill thing of recomputing all of the TXOs after removing any transaction. That should resolve any issues here.

Suggested test:

Included the test.


Also ended up reworking how the tx states are set so that CWalletTx does not need to hold pointers to this that might disappear out from under it.

@DrahtBot

DrahtBot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/actions/runs/28552964176/job/84654149872
LLM reason (✨ experimental): CI failed because the build stopped on Clang -Wthread-safety-analysis errors in wallet.cpp (accessing m_txos/m_unusable_txos without holding cs_wallet, treated as -Werror).

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.

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AddToWallet()'s isInactive() branch marks the inputs of an inactive tx usable again without checking whether another confirmed tx still spends them, which lets a confirmed-spent TXO back into m_txos and trips the new Assert(!wallet.IsSpent(outpoint, 1)) in GetBalance(), aborting the node on every subsequent balance query.

Sequence:

  1. UTXO O is spent by T1, which confirms → O moves to m_unusable_txos.
  2. send with O as a preset input builds T2: FetchSelectedInputs resolves it via GetTXO() (which now also returns unusable TXOs) and has no IsSpent check on the preset path.
  3. CommitTransaction calls AddToWallet(T2, TxStateInactive{}) before broadcasting; the isInactive() branch runs MarkTXOUsable(O). The broadcast then fails with bad-txns-inputs-missingorspent, but the wallet state was already mutated.
  4. Next getbalances:
wallet/receive.cpp:256 GetBalance: Assertion `!wallet.IsSpent(outpoint, 1)' failed.

Suggestion:

     } else if (wtx.isInactive()) {
-        // When a transaction becomes inactive, we need to mark its inputs as usable again
+        // When a transaction becomes inactive, we need to mark its inputs as usable again,
+        // unless an input is still spent by a different confirmed transaction
         for (const CTxIn& txin : wtx.tx->vin) {
-            MarkTXOUsable(txin.prevout);
+            if (!IsSpent(txin.prevout, /*min_depth=*/1)) {
+                MarkTXOUsable(txin.prevout);
+            }
         }
     }

(Alternatively the guard could live inside MarkTXOUsable() itself, which would protect the other/future callers too.)

The test below crashes the node at getbalances on the current head and passes with the patch:

diff
#!/usr/bin/env python3
"""Test that committing a tx that spends an already confirmed-spent input
does not resurrect the spent TXO into the usable TXO set."""

from decimal import Decimal

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal


class WalletSpentTXOResurrectTest(BitcoinTestFramework):
    def set_test_params(self):
        self.num_nodes = 1

    def skip_test_if_missing_module(self):
        self.skip_if_no_wallet()

    def run_test(self):
        node = self.nodes[0]
        node.createwallet("resurrect")
        wallet = node.get_wallet_rpc("resurrect")
        default = node.get_wallet_rpc(self.default_wallet_name)

        self.log.info("Fund the wallet with UTXO O")
        txid = default.sendtoaddress(wallet.getnewaddress(), 1)
        self.generate(node, 1)
        utxo = next(u for u in wallet.listunspent() if u["txid"] == txid)
        outpoint = {"txid": utxo["txid"], "vout": utxo["vout"]}

        self.log.info("Spend O in T1 and confirm it -> O becomes definitely unspendable")
        t1 = wallet.send(outputs=[{default.getnewaddress(): Decimal("0.9")}],
                         inputs=[outpoint], add_inputs=False)
        assert t1["complete"]
        self.generate(node, 1)

        self.log.info("Try to double-spend O via a preset input")
        # The broadcast fails, but the tx is committed to the wallet first.
        try:
            wallet.send(outputs=[{default.getnewaddress(): Decimal("0.8")}],
                        inputs=[outpoint], add_inputs=False)
        except Exception as e:
            self.log.info(f"send reported: {e}")

        self.log.info("Balance queries must still work and O must remain spent")
        wallet.getbalances()  # aborts the node without the fix
        assert_equal(utxo["txid"] in [u["txid"] for u in wallet.listunspent()], False)


if __name__ == "__main__":
    WalletSpentTXOResurrectTest(__file__).main()

@achow101

achow101 commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

AddToWallet()'s isInactive() branch marks the inputs of an inactive tx usable again without checking whether another confirmed tx still spends them, which lets a confirmed-spent TXO back into m_txos and trips the new Assert(!wallet.IsSpent(outpoint, 1)) in GetBalance(), aborting the node on every subsequent balance query.

I've changed MarkTXOUsable to always do an IsSpent check. Added a test in an earlier commit as well.

w0xlt and others added 19 commits July 13, 2026 17:19
The send RPC can be used to create a double spend of a confirmed
transaction which is added unconditionally to the wallet as an inactive
transaction. The test ensures that the transaction is in the wallet and
that the balance is still being counted correctly.
m_from_me is used to track whether a transaction is "from me", i.e. has
any inputs which belong to the wallet.
Instead of looking at the cached amounts or searching every input of a
transaction each time we want to determine whether it is "from me", use
m_from_me  which stores this value for us.
The states will be updated whenever CWaleltTx::SetState is called too.
This is achieved by having CWalletTx::SetState take a function that
applies the new state to the specified TXO. This ensures that CWalletTx
states and WalletTXO states are kept in sync.
Perform the transaction reorder upgrade immediately after loading txs
instead of waiting for the end of loading.
Since we need to know whether the transaction that creates a WalletTXO
is "from me", we should store this state in the WalletTXO too, copied
from its parent CWalletTx.
WalletTXOs need to know their parent tx's timestamp for AvailableCoins
to work.
A min_conf parameter is added to IsSpent so that it can set a
confirmation threshold for whether something is considered spent.
When a block is disconnected, we need to process the transactions in
reverse order so that the wallet's TXO set is updated in the correct
order.
CWallet::Create will properly connect the wallet to the chain, so we
should be doing that rather than ad-hoc chain connection.
Definitely unusable TXOs are those that are spent by a confirmed
transaction or were produced by a now conflicted transaction. However,
we still need them for GetDebit, so we store them in a separate
m_unusable_txos container. MarkConflicted, AbandonTransaction, and
loading (via PruneSpentTXOs) will ensure that these unusable TXOs are
properly moved.
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.