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

pull achow101 wants to merge 19 commits into bitcoin:master from achow101:wallet-unspent-txos changing 16 files +475 −157
  1. achow101 commented at 6:40 PM on June 12, 2023: 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.

    Depends on #27286

  2. DrahtBot commented at 6:40 PM on June 12, 2023: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline 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 <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #35302 (Silent Payments: Sending (take 2) by Eunovo)
    • #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)
    • #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)
    • #32763 (wallet: Replace CWalletTx::mapValue and vOrderForm with explicit class members 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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. DrahtBot added the label Wallet on Jun 12, 2023
  4. DrahtBot added the label CI failed on Jun 12, 2023
  5. DrahtBot added the label Needs rebase on Jun 28, 2023
  6. achow101 force-pushed on Jun 28, 2023
  7. DrahtBot removed the label Needs rebase on Jun 28, 2023
  8. achow101 force-pushed on Jun 28, 2023
  9. achow101 force-pushed on Jun 28, 2023
  10. DrahtBot added the label Needs rebase on Sep 6, 2023
  11. achow101 force-pushed on Sep 7, 2023
  12. DrahtBot removed the label Needs rebase on Sep 7, 2023
  13. achow101 commented at 9:32 PM on September 7, 2023: member

    @t-bast You may be interested in this for very large wallets.

  14. t-bast commented at 12:26 PM on September 11, 2023: contributor

    Neat, thanks @achow101, @remyers will give that a try and will review the related PRs!

  15. remyers commented at 7:49 PM on September 12, 2023: contributor

    Concept ACK - on a large synthetic test wallet I'm seeing a 250x speed up.

    I created a large wallet in regtest with many fanout style transactions - 100,000 blocks each creating 1000 utxos. There are only 117 utxos currently, but the wallet.dat file is 370M.

    Using the same wallet.dat file and (transaction history) fundrawtransaction saw a substantial speedup: ~5 seconds using v25.99.0-a36134fcc7b4 ~20 milliseconds using v25.99.0-d9c4dcfe68c5 (latest wallet-unspent-txos)

    I will try to recreate my large test wallet.dat file tomorrow so I can confirm the steps to reproduce it. Is there anything in particular I can do to help? Would you like more testing? code review?

    NB: I had to export my original wallet descriptors and re-import them to work around the issue fixed in #27920. Before the export/import the wallet.dat file was 1.1G, but the speed of fundrawtransaction was 5 sec both in the original file and the tested one created via importing descriptors.

  16. achow101 commented at 8:55 PM on September 12, 2023: member

    Is there anything in particular I can do to help? Would you like more testing? code review?

    The prerequisite PR (#27286) will need review and testing before this can be merged. That should also provide some speed up.

  17. remyers commented at 9:53 AM on September 13, 2023: contributor

    The prerequisite PR (#27286) will need review and testing before this can be merged. That should also provide some speed up.

    Will do. I'm getting 400-500ms for the same test using just #27286, so indeed also ~100x speed-up.

  18. DrahtBot added the label Needs rebase on Sep 14, 2023
  19. achow101 force-pushed on Sep 14, 2023
  20. DrahtBot removed the label Needs rebase on Sep 14, 2023
  21. achow101 force-pushed on Sep 15, 2023
  22. achow101 force-pushed on Sep 15, 2023
  23. DrahtBot added the label Needs rebase on Sep 19, 2023
  24. achow101 force-pushed on Sep 27, 2023
  25. DrahtBot removed the label Needs rebase on Sep 27, 2023
  26. DrahtBot added the label Needs rebase on Oct 16, 2023
  27. achow101 force-pushed on Oct 16, 2023
  28. DrahtBot removed the label Needs rebase on Oct 16, 2023
  29. DrahtBot added the label Needs rebase on Oct 23, 2023
  30. achow101 force-pushed on Oct 24, 2023
  31. DrahtBot removed the label Needs rebase on Oct 24, 2023
  32. achow101 force-pushed on Oct 24, 2023
  33. DrahtBot added the label Needs rebase on Oct 29, 2023
  34. achow101 force-pushed on Nov 13, 2023
  35. DrahtBot removed the label Needs rebase on Nov 13, 2023
  36. DrahtBot added the label Needs rebase on Nov 15, 2023
  37. achow101 force-pushed on Nov 15, 2023
  38. DrahtBot removed the label Needs rebase on Nov 15, 2023
  39. DrahtBot added the label Needs rebase on Nov 24, 2023
  40. achow101 force-pushed on Nov 28, 2023
  41. DrahtBot removed the label Needs rebase on Nov 28, 2023
  42. DrahtBot added the label Needs rebase on Dec 8, 2023
  43. achow101 force-pushed on Dec 8, 2023
  44. DrahtBot removed the label Needs rebase on Dec 8, 2023
  45. DrahtBot added the label Needs rebase on Dec 11, 2023
  46. achow101 force-pushed on Dec 11, 2023
  47. DrahtBot removed the label Needs rebase on Dec 11, 2023
  48. DrahtBot added the label Needs rebase on Dec 12, 2023
  49. achow101 force-pushed on Dec 19, 2023
  50. DrahtBot removed the label Needs rebase on Dec 19, 2023
  51. murchandamus commented at 7:47 PM on December 28, 2023: member

    Concept ACK

  52. DrahtBot added the label Needs rebase on Feb 3, 2024
  53. achow101 force-pushed on Feb 3, 2024
  54. DrahtBot removed the label Needs rebase on Feb 3, 2024
  55. DrahtBot added the label Needs rebase on Feb 20, 2024
  56. achow101 force-pushed on Feb 20, 2024
  57. DrahtBot removed the label Needs rebase on Feb 21, 2024
  58. DrahtBot added the label Needs rebase on Mar 27, 2024
  59. achow101 force-pushed on Apr 1, 2024
  60. DrahtBot removed the label Needs rebase on Apr 1, 2024
  61. DrahtBot added the label Needs rebase on Apr 8, 2024
  62. achow101 force-pushed on Apr 25, 2024
  63. DrahtBot removed the label Needs rebase on Apr 25, 2024
  64. achow101 force-pushed on Jun 6, 2024
  65. DrahtBot added the label Needs rebase on Aug 27, 2024
  66. achow101 force-pushed on Aug 29, 2024
  67. DrahtBot removed the label Needs rebase on Aug 29, 2024
  68. achow101 force-pushed on Sep 10, 2024
  69. DrahtBot added the label Needs rebase on Oct 24, 2024
  70. achow101 force-pushed on Oct 24, 2024
  71. DrahtBot removed the label Needs rebase on Oct 25, 2024
  72. achow101 force-pushed on Oct 28, 2024
  73. achow101 force-pushed on Oct 28, 2024
  74. achow101 force-pushed on Oct 28, 2024
  75. DrahtBot removed the label CI failed on Oct 28, 2024
  76. jonatack commented at 12:35 PM on November 26, 2024: member

    Concept ACK, pending #27286, if this improves performance for wallets with many transactions.

  77. DrahtBot added the label Needs rebase on Mar 13, 2025
  78. achow101 force-pushed on Apr 10, 2025
  79. DrahtBot removed the label Needs rebase on Apr 10, 2025
  80. DrahtBot added the label Needs rebase on Apr 25, 2025
  81. achow101 force-pushed on Apr 25, 2025
  82. DrahtBot removed the label Needs rebase on Apr 25, 2025
  83. DrahtBot added the label Needs rebase on May 7, 2025
  84. achow101 force-pushed on May 7, 2025
  85. DrahtBot removed the label Needs rebase on May 7, 2025
  86. achow101 force-pushed on May 7, 2025
  87. DrahtBot added the label Needs rebase on May 14, 2025
  88. achow101 force-pushed on May 15, 2025
  89. DrahtBot removed the label Needs rebase on May 15, 2025
  90. achow101 force-pushed on May 16, 2025
  91. DrahtBot added the label Needs rebase on May 16, 2025
  92. achow101 force-pushed on May 16, 2025
  93. DrahtBot removed the label Needs rebase on May 16, 2025
  94. achow101 force-pushed on May 16, 2025
  95. DrahtBot added the label CI failed on May 19, 2025
  96. achow101 force-pushed on May 19, 2025
  97. DrahtBot removed the label CI failed on May 19, 2025
  98. DrahtBot added the label Needs rebase on May 19, 2025
  99. achow101 force-pushed on May 20, 2025
  100. DrahtBot removed the label Needs rebase on May 20, 2025
  101. DrahtBot added the label Needs rebase on May 21, 2025
  102. achow101 force-pushed on May 21, 2025
  103. DrahtBot removed the label Needs rebase on May 21, 2025
  104. DrahtBot added the label Needs rebase on May 30, 2025
  105. achow101 force-pushed on May 30, 2025
  106. DrahtBot removed the label Needs rebase on May 30, 2025
  107. in src/wallet/test/util.cpp:26 in 9554a4d79a outdated
      27 | -    }
      28 | +    bilingual_str error;
      29 | +    std::vector<bilingual_str> warnings;
      30 | +    auto wallet = CWallet::Create(context, "", CreateMockableWalletDatabase(), WALLET_FLAG_DESCRIPTORS, error, warnings);
      31 | +
      32 | +    // Allow the fallback fee with it's default
    


    maflcko commented at 7:17 AM on June 2, 2025:

    “with it's default” -> “with its default” [“it's” is a contraction of “it is,” but here the possessive “its” is required]


    achow101 commented at 7:37 PM on June 2, 2025:

    Fixed

  108. achow101 force-pushed on Jun 2, 2025
  109. DrahtBot added the label Needs rebase on Jun 25, 2025
  110. achow101 force-pushed on Jun 27, 2025
  111. DrahtBot added the label CI failed on Jun 27, 2025
  112. DrahtBot commented at 3:08 AM on June 27, 2025: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task lint: https://github.com/bitcoin/bitcoin/runs/44895352538</sub> <sub>LLM reason (✨ experimental): The failure is caused by a Python lint error due to an unused variable in the wallet_backwards_compatibility.py test.</sub>

    <details><summary>Hints</summary>

    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.

    </details>

  113. DrahtBot removed the label Needs rebase on Jun 27, 2025
  114. achow101 force-pushed on Jun 27, 2025
  115. achow101 force-pushed on Jun 27, 2025
  116. DrahtBot removed the label CI failed on Jun 28, 2025
  117. achow101 force-pushed on Jul 1, 2025
  118. achow101 marked this as ready for review on Jul 1, 2025
  119. in src/wallet/transaction.h:222 in 25a16d0a25 outdated
     214 | @@ -215,6 +215,11 @@ class CWalletTx
     215 |       * CWallet::ComputeTimeSmart().
     216 |       */
     217 |      unsigned int nTimeSmart;
     218 | +    /**
     219 | +     * For every isminetype, we want to track whether the transaction spends any
     220 | +     * inputs that match that isminetype.
     221 | +     */
     222 | +    std::optional<bool> m_from_me;
    


    w0xlt commented at 12:26 AM on July 3, 2025:

    Why is it optional instead of false by default?


    achow101 commented at 8:50 PM on July 9, 2025:

    Using a default value has the potential very problematic because its value is expected to be correct. By using an optional, we can detect when there is a programming error that did not set the value.

  120. DrahtBot added the label Needs rebase on Jul 7, 2025
  121. achow101 force-pushed on Jul 7, 2025
  122. DrahtBot removed the label Needs rebase on Jul 7, 2025
  123. Zeegaths commented at 12:54 AM on August 1, 2025: none

    Performance Testing Results

    Tested this PR against master using regtest with high-activity wallets.

    Test Setup:

    • Ubuntu 22.04, CMake build
    • Fresh regtest environment for both branches
    • 1,110 transactions (200 initial coinbase + 500 small sends + 100 coinbase + 300 large sends + 10 final coinbase)
    • Identical test patterns on both branches

    Results:

    Operation Master PR Branch Performance Change
    getwalletinfo 0.008s 0.011s +38% slower
    getbalance 0.009s 0.010s +11% slower
    listunspent 0.051s 0.035s -31% faster
    sendtoaddress 0.036s 0.037s +3% slower

    Observations:

    The TXO separation optimization shows its intended benefit in listunspent (31% improvement), which makes sense given that this operation iterates through unspent outputs - exactly what the PR optimizes. Other operations show minimal performance differences within expected measurement variance.

    Results suggest the optimization is working as designed for the primary use case.

  124. DrahtBot added the label Needs rebase on Aug 16, 2025
  125. achow101 force-pushed on Aug 16, 2025
  126. DrahtBot removed the label Needs rebase on Aug 16, 2025
  127. DrahtBot added the label Needs rebase on Aug 19, 2025
  128. achow101 force-pushed on Aug 19, 2025
  129. DrahtBot removed the label Needs rebase on Aug 19, 2025
  130. achow101 force-pushed on Aug 19, 2025
  131. achow101 force-pushed on Aug 20, 2025
  132. DrahtBot added the label Needs rebase on Aug 21, 2025
  133. achow101 force-pushed on Aug 21, 2025
  134. DrahtBot removed the label Needs rebase on Aug 21, 2025
  135. achow101 force-pushed on Sep 1, 2025
  136. DrahtBot added the label CI failed on Sep 1, 2025
  137. DrahtBot commented at 10:58 PM on September 1, 2025: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task lint: https://github.com/bitcoin/bitcoin/runs/49358787700</sub> <sub>LLM reason (✨ experimental): Lint failure: unused variable fee in wallet_listtransactions.py (F841) causing the CI to fail.</sub>

    <details><summary>Hints</summary>

    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.

    </details>

  138. achow101 force-pushed on Sep 2, 2025
  139. DrahtBot removed the label CI failed on Sep 2, 2025
  140. DrahtBot added the label Needs rebase on Sep 12, 2025
  141. achow101 force-pushed on Sep 23, 2025
  142. achow101 force-pushed on Sep 23, 2025
  143. DrahtBot removed the label Needs rebase on Sep 24, 2025
  144. rkrux commented at 8:00 AM on October 8, 2025: contributor

    Concept ACK 7b2718f

    Seems like a natural step after #27286, will get to this PR soon.

  145. DrahtBot added the label Needs rebase on Oct 31, 2025
  146. achow101 force-pushed on Nov 10, 2025
  147. DrahtBot removed the label Needs rebase on Nov 11, 2025
  148. DrahtBot added the label Needs rebase on Dec 27, 2025
  149. achow101 force-pushed on Jan 2, 2026
  150. DrahtBot removed the label Needs rebase on Jan 2, 2026
  151. in src/wallet/transaction.cpp:68 in 3d454eb45e
      67 | +}
      68 | +
      69 | +void CWalletTx::SetState(const TxState& state)
      70 | +{
      71 | +    m_state = state;
      72 | +    for (auto [_, txo] : m_txos) {
    


    w0xlt commented at 9:38 PM on January 14, 2026:

    nit:

        for (const auto& [_, txo] : m_txos) {
    

    achow101 commented at 8:56 PM on January 15, 2026:

    Done

  152. in src/wallet/wallet.cpp:4592 in 3d454eb45e outdated
    4592 |  {
    4593 |      AssertLockHeld(cs_wallet);
    4594 | -    for (const auto& [_, wtx] : mapWallet) {
    4595 | -        RefreshTXOsFromTx(wtx);
    4596 | +    for (auto& [_, wtx] : wtxOrdered) {
    4597 | +        bool from_me_before = *wtx->m_from_me;
    


    w0xlt commented at 9:56 PM on January 14, 2026:

    Can m_from_me be nullopt here ? If so, can this be UB ?

            std::optional<bool> from_me_before = wtx->m_from_me;
    

    achow101 commented at 7:36 PM on January 15, 2026:

    No, it cannot be nullopt. It is set when a transaction is added to the wallet, or immediately after loading a transaction from disk.

  153. in src/wallet/wallet.cpp:4560 in 3d454eb45e
    4556 | @@ -4448,35 +4557,108 @@ void CWallet::WriteBestBlock() const
    4557 |      }
    4558 |  }
    4559 |  
    4560 | +using TXOMap = std::unordered_map<COutPoint, WalletTXO, SaltedOutpointHasher>;
    


    w0xlt commented at 1:22 AM on January 15, 2026:

    nit: It can be removed since it's already declared in src/wallet/wallet.h.

    -std::pair<TXOMap::iterator, TXOMap::iterator> CWallet::MarkTXOUnusable(const COutPoint& outpoint)
    +std::pair<CWallet::TXOMap::iterator, CWallet::TXOMap::iterator> CWallet::MarkTXOUnusable(const COutPoint& outpoint)
     {
         AssertLockHeld(cs_wallet);
         auto txos_it = m_txos.find(outpoint);
    @@ -4646,7 +4645,7 @@ std::pair<TXOMap::iterator, TXOMap::iterator> CWallet::MarkTXOUnusable(const COu
         return {txos_it, unusable_txos_it};
     }
     
    -std::pair<TXOMap::iterator, TXOMap::iterator> CWallet::MarkTXOUsable(const COutPoint& outpoint)
    +std::pair<CWallet::TXOMap::iterator, CWallet::TXOMap::iterator> CWallet::MarkTXOUsable(const COutPoint& outpoint)
     {
         AssertLockHeld(cs_wallet);
         auto txos_it = m_txos.end();
    

    achow101 commented at 8:56 PM on January 15, 2026:

    Done

  154. achow101 force-pushed on Jan 15, 2026
  155. in src/wallet/transaction.h:256 in 52b677d455 outdated
     252 | @@ -250,6 +253,8 @@ class CWalletTx
     253 |      mutable bool fChangeCached;
     254 |      mutable CAmount nChangeCached;
     255 |  
     256 | +    mutable std::unordered_map<uint32_t, WalletTXO*> m_txos;
    


    w0xlt commented at 4:30 AM on January 20, 2026:

    This naming collision (m_txos in both CWallet::m_txos and CWalletTx::m_txos) can be confusing.

    Since CWalletTx::m_txos stores pointers into the wallet-level maps (CWallet::m_txos and m_unusable_txos), keyed by output index (0, 1, 2, ...), a clearer naming might be:

        mutable std::unordered_map<uint32_t, WalletTXO*> m_txo_ptrs;
    
  156. w0xlt referenced this in commit 489d6321b9 on Jan 20, 2026
  157. w0xlt referenced this in commit 3c60c44384 on Jan 21, 2026
  158. w0xlt referenced this in commit ed60740d28 on Jan 21, 2026
  159. w0xlt referenced this in commit f186efcf4a on Jan 21, 2026
  160. w0xlt referenced this in commit 59a6d51882 on Jan 21, 2026
  161. w0xlt commented at 3:22 AM on January 21, 2026: contributor

    Results with #34360 on my machine:

    Branch ns/op ops/s Speedup
    Master (no optimization) 3,529,535 283 baseline
    PR #27865 (with optimization) 6,951 143,867 ~508x faster
  162. in src/wallet/wallet.cpp:4045 in 52b677d455 outdated
    4041 |          // solvable_wallet doesn't need to be checked because transactions for those scripts weren't being watched for
    4042 | -        bool is_mine = IsMine(*wtx->tx) || IsFromMe(*wtx->tx);
    4043 | +        bool mine = IsMine(*wtx->tx);
    4044 | +        bool from_me = IsFromMe(*wtx->tx);
    4045 | +        bool is_mine = mine || from_me;
    4046 | +        if (is_mine) {
    


    brunoerg commented at 12:34 PM on January 21, 2026:

    Unkilled mutant:

    diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
    index c68390c82e..d0b9488e20 100644
    --- a/src/wallet/wallet.cpp
    +++ b/src/wallet/wallet.cpp
    @@ -4042,7 +4042,7 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
             bool mine = IsMine(*wtx->tx);
             bool from_me = IsFromMe(*wtx->tx);
             bool is_mine = mine || from_me;
    -        if (is_mine) {
    +        if (1==1) {
                 wtx->m_from_me = from_me;
                 // Rewrite all txs that are "mine" to ensure that any tx upgrades, including the from_me update, is written
                 local_wallet_batch.WriteTx(*wtx);
    

    achow101 commented at 6:56 PM on January 21, 2026:

    This isn't really testable as it is performing an upgrade that would happen on startup anyways. The if is there as an optimization to avoid rewriting transaction records that are going to be deleted later.

  163. in src/wallet/wallet.cpp:1406 in 52b677d455
    1401 | +                    continue;
    1402 | +                }
    1403 | +
    1404 | +                if (std::get_if<TxStateBlockConflicted>(&unusable_txo_it->second.GetState()) ||
    1405 | +                    std::get_if<TxStateConfirmed>(&unusable_txo_it->second.GetState())) {
    1406 | +                    continue;
    


    brunoerg commented at 12:34 PM on January 21, 2026:

    Unkilled mutant:

    diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
    index c68390c82e..b43d35596c 100644
    --- a/src/wallet/wallet.cpp
    +++ b/src/wallet/wallet.cpp
    @@ -1403,7 +1403,7 @@ void CWallet::RecursiveUpdateTxState(WalletBatch* batch, const Txid& tx_hash, co
    
                     if (std::get_if<TxStateBlockConflicted>(&unusable_txo_it->second.GetState()) ||
                         std::get_if<TxStateConfirmed>(&unusable_txo_it->second.GetState())) {
    -                    continue;
    +                    break;
                     }
    
                     MarkTXOUsable(txin.prevout);
    

    achow101 commented at 9:36 PM on January 29, 2026:

    Spent a while looking at this and I've come to the conclusion that this entire loop is actually unnecessary, so I've removed it.

  164. in src/wallet/wallet.cpp:898 in 52b677d455 outdated
     893 | @@ -883,13 +894,14 @@ DBErrors CWallet::ReorderTransactions()
     894 |                  continue;
     895 |  
     896 |              // Since we're changing the order, write it back
     897 | -            if (!batch.WriteTx(*pwtx))
     898 | -                return DBErrors::LOAD_FAIL;
     899 | +            if (!batch.WriteTx(*pwtx)) {
     900 | +                return false;
    


    brunoerg commented at 12:36 PM on January 21, 2026:

    Unkilled mutant:

    diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
    index c68390c82e..3f660e2c87 100644
    --- a/src/wallet/wallet.cpp
    +++ b/src/wallet/wallet.cpp
    @@ -895,7 +895,7 @@ bool CWallet::ReorderTransactions(WalletBatch& batch)
    
                 // Since we're changing the order, write it back
                 if (!batch.WriteTx(*pwtx)) {
    -                return false;
    +                return true;
                 }
             }
         }
    

    achow101 commented at 6:25 PM on January 21, 2026:

    ReorderTransaction is basically impossible to test for as it requires a wallet created in 0.6.0 or earlier.

  165. brunoerg commented at 12:41 PM on January 21, 2026: contributor

    I ran an incremental mutation testing for src/wallet/wallet.cpp - it generated 58 mutants and the analysis showed a mutation score of 94.8% (excellent). Left some comments with the unkilled mutants, feel free to address them or ignore, in any case a feedback would be valuable.

  166. DrahtBot requested review from jonatack on Jan 21, 2026
  167. DrahtBot requested review from murchandamus on Jan 21, 2026
  168. DrahtBot requested review from rkrux on Jan 21, 2026
  169. achow101 force-pushed on Jan 29, 2026
  170. sedited requested review from w0xlt on Mar 12, 2026
  171. in src/wallet/transaction.h:412 in 3fb18453cd outdated
     404 | @@ -389,20 +405,35 @@ struct WalletTxOrderComparator {
     405 |  class WalletTXO
     406 |  {
     407 |  private:
     408 | -    const CWalletTx& m_wtx;
     409 |      const CTxOut& m_output;
     410 | +    TxState m_tx_state;
     411 | +    bool m_tx_coinbase;
     412 | +    bool m_tx_from_me;
     413 | +    int64_t m_tx_time;
    


    w0xlt commented at 9:42 PM on March 12, 2026:
        int64_t m_tx_time;
        const uint32_t m_tx_version;
    

    To keep the same pattern, m_tx_version could be private with the getter uint32_t GetTxVersion() const { return m_tx_version; }.

    Ant then:

    diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp
    index 043f533b55..d68f441e89 100644
    --- a/src/wallet/spend.cpp
    +++ b/src/wallet/spend.cpp
    @@ -284,10 +284,10 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
                     input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
                 }
                 if (wallet.GetTxStateDepthInMainChain(txo->GetState()) == 0) {
    -                if (txo->m_tx_version == TRUC_VERSION && coin_control.m_version != TRUC_VERSION) {
    +                if (txo->GetTxVersion() == TRUC_VERSION && coin_control.m_version != TRUC_VERSION) {
                         return util::Error{strprintf(_("Can't spend unconfirmed version 3 pre-selected input with a version %d tx"), coin_control.m_version)};
    -                } else if (coin_control.m_version == TRUC_VERSION && txo->m_tx_version != TRUC_VERSION) {
    -                    return util::Error{strprintf(_("Can't spend unconfirmed version %d pre-selected input with a version 3 tx"), txo->m_tx_version)};
    +                } else if (coin_control.m_version == TRUC_VERSION && txo->GetTxVersion() != TRUC_VERSION) {
    +                    return util::Error{strprintf(_("Can't spend unconfirmed version %d pre-selected input with a version 3 tx"), txo->GetTxVersion())};
                     }
                 }
             } else {
    @@ -475,7 +475,7 @@ CoinsResult AvailableCoins(const CWallet& wallet,
     
             auto available_output_type = GetOutputType(type, is_from_p2sh);
             auto available_output = COutput(outpoint, output, nDepth, input_bytes, solvable, tx_safe, txo.GetTxTime(), tx_from_me, feerate);
    -        if (txo.m_tx_version == TRUC_VERSION && nDepth == 0 && params.check_version_trucness) {
    +        if (txo.GetTxVersion() == TRUC_VERSION && nDepth == 0 && params.check_version_trucness) {
                 unconfirmed_truc_coins.emplace_back(available_output_type, available_output);
                 auto [it, _] = truc_txid_by_value.try_emplace(outpoint.hash, 0);
                 it->second += output.nValue;
    

    achow101 commented at 9:23 PM on March 23, 2026:

    I don't think that matters as m_tx_version is const.

  172. in src/wallet/wallet.cpp:1396 in 3fb18453cd
    1392 | @@ -1394,6 +1393,7 @@ void CWallet::RecursiveUpdateTxState(WalletBatch* batch, const Txid& tx_hash, co
    1393 |              // If a transaction changes its tx state, that usually changes the balance
    1394 |              // available of the outputs it spends. So force those to be recomputed
    1395 |              MarkInputsDirty(wtx.tx);
    1396 | +            // Make the non-conflicted inputs usable again
    


    w0xlt commented at 9:44 PM on March 12, 2026:

    Dangling comment ?


    achow101 commented at 9:51 PM on March 23, 2026:

    Removed

  173. in src/wallet/wallet.cpp:4566 in 3fb18453cd
    4563 | +            if (m_last_block_processed_height >= 0 && IsSpent(outpoint, /*min_depth=*/1)) {
    4564 | +                std::tie(txo_it, txos_inserted) = m_unusable_txos.emplace(outpoint, WalletTXO{txout, wtx.GetState(), wtx.IsCoinBase(), *wtx.m_from_me, wtx.GetTxTime(), wtx.tx->version});
    4565 | +                assert(txos_inserted);
    4566 | +            } else {
    4567 | +                std::tie(txo_it, txos_inserted) = m_txos.emplace(outpoint, WalletTXO{txout, wtx.GetState(), wtx.IsCoinBase(), *wtx.m_from_me, wtx.GetTxTime(), wtx.tx->version});
    4568 | +            }
    


    w0xlt commented at 9:46 PM on March 12, 2026:

    This can be simplified.

                WalletTXO new_txo{txout, wtx.GetState(), wtx.IsCoinBase(), *wtx.m_from_me, wtx.GetTxTime(), wtx.tx->version};
                TXOMap& target = (m_last_block_processed_height >= 0 && IsSpent(outpoint, /*min_depth=*/1)) ? m_unusable_txos : m_txos;
                auto [txo_it, txos_inserted] = target.emplace(outpoint, std::move(new_txo));
                assert(txos_inserted);
    
    

    achow101 commented at 9:52 PM on March 23, 2026:

    Taken the suggestion

  174. sedited requested review from w0xlt on Mar 16, 2026
  175. sedited removed review request from w0xlt on Mar 16, 2026
  176. achow101 force-pushed on Mar 23, 2026
  177. achow101 commented at 9:52 PM on March 23, 2026: member

    Rebased for silent merge conflicts.

  178. w0xlt commented at 6:16 PM on March 25, 2026: contributor

    reACK 412008bcb454b8d02f9e00f7e7a0fbb2aae5f690

  179. DrahtBot added the label Needs rebase on Apr 23, 2026
  180. achow101 force-pushed on Apr 23, 2026
  181. DrahtBot removed the label Needs rebase on Apr 23, 2026
  182. sedited requested review from w0xlt on May 8, 2026
  183. DrahtBot added the label Needs rebase on May 13, 2026
  184. achow101 force-pushed on May 15, 2026
  185. DrahtBot removed the label Needs rebase on May 15, 2026
  186. in src/wallet/receive.cpp:213 in 29a953a948 outdated
     223 | +    if (!std::holds_alternative<TxStateInMempool>(state)) return false;
     224 | +
     225 | +    const CWalletTx* wtx = wallet.GetWalletTx(txid);
     226 | +    assert(wtx);
     227 | +
     228 | +    if (!wallet.m_spend_zero_conf_change || !wtx->m_from_me) return false;
    


    w0xlt commented at 11:56 PM on May 22, 2026:

    m_from_me = false passes because a populated optional converts to true:

        if (!wallet.m_spend_zero_conf_change || !wtx->m_from_me.value_or(false)) return false;
    

    Example:

    #include <iostream>
    #include <optional>
    
    int main()
    {
        std::optional<bool> false_value{false};
        std::cout << "false_value in if: " << (static_cast<bool>(false_value) ? "true" : "false") << "\n";
    }
    

    Test:

    diff --git a/src/wallet/receive.cpp b/src/wallet/receive.cpp
    index aaf1b40955..0089da789b 100644
    --- a/src/wallet/receive.cpp
    +++ b/src/wallet/receive.cpp
    @@ -210,7 +210,7 @@ bool CachedTxIsTrusted(const CWallet& wallet, const TxState& state, const Txid&
         const CWalletTx* wtx = wallet.GetWalletTx(txid);
         assert(wtx);
     
    -    if (!wallet.m_spend_zero_conf_change || !wtx->m_from_me) return false;
    +    if (!wallet.m_spend_zero_conf_change || !wtx->m_from_me.value_or(false)) return false;
     
         // Trusted if all inputs are from us and are in the mempool:
         for (const CTxIn& txin : wtx->tx->vin)
    diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp
    index c5183c1c98..47ed036dab 100644
    --- a/src/wallet/test/wallet_tests.cpp
    +++ b/src/wallet/test/wallet_tests.cpp
    @@ -90,6 +90,36 @@ BOOST_FIXTURE_TEST_CASE(update_non_range_descriptor, TestingSetup)
         }
     }
     
    +BOOST_AUTO_TEST_CASE(cached_tx_is_trusted_checks_from_me_value)
    +{
    +    CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
    +    {
    +        LOCK(wallet.cs_wallet);
    +        wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
    +        wallet.m_spend_zero_conf_change = true;
    +    }
    +
    +    const CKey key{GenerateRandomKey()};
    +    AddKey(wallet, key);
    +    const CScript script_pub_key{GetScriptForRawPubKey(key.GetPubKey())};
    +
    +    CMutableTransaction parent_tx;
    +    parent_tx.vout.emplace_back(COIN, script_pub_key);
    +    CWalletTx* parent_wtx{wallet.AddToWallet(MakeTransactionRef(parent_tx), TxStateConfirmed{GetRandHash(), /*height=*/1, /*index=*/0})};
    +    BOOST_REQUIRE(parent_wtx);
    +
    +    CMutableTransaction child_tx;
    +    child_tx.vin.emplace_back(CTxIn{parent_wtx->GetHash(), /*nIn=*/0});
    +    child_tx.vout.emplace_back(COIN - DEFAULT_TRANSACTION_MAXFEE, script_pub_key);
    +    CWalletTx* child_wtx{wallet.AddToWallet(MakeTransactionRef(child_tx), TxStateInMempool{})};
    +    BOOST_REQUIRE(child_wtx);
    +
    +    LOCK(wallet.cs_wallet);
    +    BOOST_CHECK(*child_wtx->m_from_me);
    +    child_wtx->m_from_me = false;
    +    BOOST_CHECK(!CachedTxIsTrusted(wallet, *child_wtx));
    +}
    +
     BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
     {
         // Cap last block file size, and mine new block in a new block file.
    

    achow101 commented at 10:55 PM on May 26, 2026:

    Fixed. The unit test is not meaningfully helpful.

  187. in src/wallet/wallet.cpp:4126 in 29a953a948
    4120 | @@ -4040,19 +4121,32 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
    4121 |          }
    4122 |      }
    4123 |      for (const auto& [_pos, wtx] : wtxOrdered) {
    4124 | +        // First update the UTXOs
    4125 | +        wtx->m_txos.clear();
    4126 | +        RefreshTXOsFromTx(*wtx);
    


    w0xlt commented at 7:05 AM on May 23, 2026:

    RefreshTXOsFromTx(*wtx) creates or updates WalletTXO entries using the current wtx->m_from_me value. During migration that value may still be the legacy-wallet value, and it is recomputed later with wtx->m_from_me = from_me, so the refresh needs to happen after that assignment.

    diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
    index 73bd712910..ab2902efea 100644
    --- a/src/wallet/wallet.cpp
    +++ b/src/wallet/wallet.cpp
    @@ -4121,9 +4121,7 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
             }
         }
         for (const auto& [_pos, wtx] : wtxOrdered) {
    -        // First update the UTXOs
             wtx->m_txos.clear();
    -        RefreshTXOsFromTx(*wtx);
             // Check it is the watchonly wallet's
             // solvable_wallet doesn't need to be checked because transactions for those scripts weren't being watched for
             bool mine = IsMine(*wtx->tx);
    @@ -4131,6 +4129,7 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
             bool is_mine = mine || from_me;
             if (is_mine) {
                 wtx->m_from_me = from_me;
    +            RefreshTXOsFromTx(*wtx);
                 // Rewrite all txs that are "mine" to ensure that any tx upgrades, including the from_me update, is written
                 local_wallet_batch.WriteTx(*wtx);
             }
    

    Test:

    <details>

    <summary>diff</summary>

    diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp
    index c5183c1c98..4a18393be4 100644
    --- a/src/wallet/test/wallet_tests.cpp
    +++ b/src/wallet/test/wallet_tests.cpp
    @@ -90,6 +90,47 @@ BOOST_FIXTURE_TEST_CASE(update_non_range_descriptor, TestingSetup)
         }
     }
     
    +BOOST_AUTO_TEST_CASE(migration_refreshes_txos_after_from_me_update)
    +{
    +    CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
    +    const CKey key{GenerateRandomKey()};
    +    const CPubKey pubkey{key.GetPubKey()};
    +    const CScript script_pub_key{GetScriptForDestination(PKHash{pubkey})};
    +
    +    LOCK(wallet.cs_wallet);
    +    auto* legacy_spkm{wallet.GetOrCreateLegacyDataSPKM()};
    +    BOOST_REQUIRE(legacy_spkm);
    +    BOOST_REQUIRE(legacy_spkm->LoadKey(key, pubkey));
    +
    +    bilingual_str error;
    +    auto migration_data{wallet.GetDescriptorsForLegacy(error)};
    +    BOOST_REQUIRE_MESSAGE(migration_data, error.original);
    +
    +    CMutableTransaction tx;
    +    tx.vout.emplace_back(COIN, script_pub_key);
    +    CTransactionRef tx_ref{MakeTransactionRef(tx)};
    +    const Txid txid{tx_ref->GetHash()};
    +    auto [it, inserted]{wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid), std::forward_as_tuple(tx_ref, TxStateInMempool{}))};
    +    BOOST_REQUIRE(inserted);
    +    CWalletTx& wtx{it->second};
    +    wtx.m_from_me = true;
    +    wtx.nOrderPos = 0;
    +    wtx.m_it_wtxOrdered = wallet.wtxOrdered.emplace(wtx.nOrderPos, &wtx);
    +
    +    WalletBatch batch{wallet.GetDatabase()};
    +    BOOST_REQUIRE(batch.WriteBestBlock(CBlockLocator{}));
    +    auto res{wallet.ApplyMigrationData(batch, *migration_data)};
    +    BOOST_REQUIRE_MESSAGE(res, util::ErrorString(res).original);
    +
    +    const CWalletTx& migrated_wtx{wallet.mapWallet.at(txid)};
    +    BOOST_REQUIRE(migrated_wtx.m_from_me.has_value());
    +    BOOST_CHECK(!*migrated_wtx.m_from_me);
    +
    +    auto txo{wallet.GetTXO(COutPoint{txid, 0})};
    +    BOOST_REQUIRE(txo);
    +    BOOST_CHECK(!txo->GetTxFromMe());
    +}
    +
     BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
     {
         // Cap last block file size, and mine new block in a new block file.
    

    </details>


    achow101 commented at 10:56 PM on May 26, 2026:

    Fixed, but the issue is not reachable and the unit test is not meaningfully helpful. The in memory state after migration should never be used, I'm pretty sure there are a few other places where it is out of sync. We specifically reload the wallet in order to ensure consistency before the wallet goes back into use.

  188. w0xlt commented at 7:57 AM on May 23, 2026: contributor

    CWallet::RemoveTxs() deletes wallet transactions, but it was not keeping the new TXO caches (m_txos and m_unusable_txos) in sync.

    Suggestion:

    diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
    index 73bd712910..a8faa6be39 100644
    --- a/src/wallet/wallet.cpp
    +++ b/src/wallet/wallet.cpp
    @@ -2522,6 +2522,7 @@ util::Result<void> CWallet::RemoveTxs(WalletBatch& batch, std::vector<Txid>& txs
     
         // Register callback to update the memory state only when the db txn is actually dumped to disk
         batch.RegisterTxnListener({.on_commit=[&, erased_txs]() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) {
    +        std::vector<COutPoint> spent_outpoints;
             // Update the in-memory state and notify upper layers about the removals
             for (const auto& it : erased_txs) {
                 const Txid hash{it->first};
    @@ -2534,14 +2535,24 @@ util::Result<void> CWallet::RemoveTxs(WalletBatch& batch, std::vector<Txid>& txs
                             break;
                         }
                     }
    +                spent_outpoints.push_back(txin.prevout);
                 }
                 for (unsigned int i = 0; i < it->second.tx->vout.size(); ++i) {
    -                m_txos.erase(COutPoint(hash, i));
    +                const COutPoint outpoint{hash, i};
    +                m_txos.erase(outpoint);
    +                m_unusable_txos.erase(outpoint);
                 }
                 mapWallet.erase(it);
                 NotifyTransactionChanged(hash, CT_DELETED);
             }
     
    +        for (const auto& outpoint : spent_outpoints) {
    +            auto txo_it = m_unusable_txos.find(outpoint);
    +            if (txo_it != m_unusable_txos.end() && !std::get_if<TxStateBlockConflicted>(&txo_it->second.GetState()) && !IsSpent(outpoint, /*min_depth=*/1)) {
    +                MarkTXOUsable(outpoint);
    +            }
    +        }
    +
             MarkDirty();
         }, .on_abort={}});
    

    Test:

    <details> <summary>diff</summary>

    diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp
    index c5183c1c98..a8ae79f51b 100644
    --- a/src/wallet/test/wallet_tests.cpp
    +++ b/src/wallet/test/wallet_tests.cpp
    @@ -705,22 +705,52 @@ BOOST_FIXTURE_TEST_CASE(RemoveTxs, TestChain100Setup)
         m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
         auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
         CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
    +    const CTransaction block_tx_ref{block_tx};
    +    auto child_tx = TestSimpleSpend(block_tx_ref, 0, key, GetScriptForRawPubKey(key.GetPubKey()));
    +    CreateAndProcessBlock({child_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
    +    const CTransaction child_tx_ref{child_tx};
    +    auto grandchild_tx = TestSimpleSpend(child_tx_ref, 0, key, GetScriptForRawPubKey(key.GetPubKey()));
    +    CreateAndProcessBlock({grandchild_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
     
         m_node.validation_signals->SyncWithValidationInterfaceQueue();
     
         {
    -        auto block_hash = block_tx.GetHash();
    +        const Txid block_hash = block_tx.GetHash();
    +        const Txid child_hash = child_tx.GetHash();
    +        const Txid grandchild_hash = grandchild_tx.GetHash();
             auto prev_tx = m_coinbase_txns[0];
    +        const COutPoint block_outpoint{block_hash, 0};
    +        const COutPoint child_outpoint{child_hash, 0};
    +        const COutPoint grandchild_outpoint{grandchild_hash, 0};
     
             LOCK(wallet->cs_wallet);
             BOOST_CHECK(wallet->HasWalletSpend(prev_tx));
             BOOST_CHECK(wallet->mapWallet.contains(block_hash));
    -
    -        std::vector<Txid> vHashIn{ block_hash };
    +        BOOST_CHECK(wallet->HasWalletSpend(MakeTransactionRef(block_tx_ref)));
    +        BOOST_CHECK(wallet->mapWallet.contains(child_hash));
    +        BOOST_CHECK(wallet->mapWallet.contains(grandchild_hash));
    +
    +        BOOST_CHECK(wallet->GetTXO(block_outpoint));
    +        BOOST_CHECK(!wallet->GetTXOs().contains(block_outpoint));
    +        BOOST_CHECK(wallet->GetTXO(child_outpoint));
    +        BOOST_CHECK(!wallet->GetTXOs().contains(child_outpoint));
    +        BOOST_CHECK(wallet->GetTXO(grandchild_outpoint));
    +        BOOST_CHECK(wallet->GetTXOs().contains(grandchild_outpoint));
    +
    +        std::vector<Txid> vHashIn{child_hash};
             BOOST_CHECK(wallet->RemoveTxs(vHashIn));
     
    -        BOOST_CHECK(!wallet->HasWalletSpend(prev_tx));
    -        BOOST_CHECK(!wallet->mapWallet.contains(block_hash));
    +        BOOST_CHECK(wallet->HasWalletSpend(prev_tx));
    +        BOOST_CHECK(wallet->mapWallet.contains(block_hash));
    +        BOOST_CHECK(!wallet->HasWalletSpend(MakeTransactionRef(block_tx_ref)));
    +        BOOST_CHECK(!wallet->mapWallet.contains(child_hash));
    +        BOOST_CHECK(wallet->mapWallet.contains(grandchild_hash));
    +
    +        BOOST_CHECK(wallet->GetTXO(block_outpoint));
    +        BOOST_CHECK(wallet->GetTXOs().contains(block_outpoint));
    +        BOOST_CHECK(!wallet->GetTXO(child_outpoint));
    +        BOOST_CHECK(wallet->GetTXO(grandchild_outpoint));
    +        BOOST_CHECK(wallet->GetTXOs().contains(grandchild_outpoint));
         }
     
         TestUnloadWallet(std::move(wallet));
    

    </details>

  189. achow101 commented at 10:53 PM on May 26, 2026: member

    CWallet::RemoveTxs() deletes wallet transactions, but it was not keeping the new TXO caches (m_txos and m_unusable_txos) in sync.

    I'm not convinced that MarkTXOUsable is correct or relevant. Please write a functional test that fails.

  190. achow101 force-pushed on May 26, 2026
  191. DrahtBot added the label CI failed on May 26, 2026
  192. DrahtBot removed the label CI failed on May 28, 2026
  193. sedited requested review from w0xlt on May 28, 2026
  194. sedited removed review request from w0xlt on May 28, 2026
  195. sedited requested review from polespinasa on May 28, 2026
  196. sedited requested review from w0xlt on May 28, 2026
  197. w0xlt commented at 6:40 PM on June 2, 2026: contributor

    I'm not convinced that MarkTXOUsable is correct or relevant. Please write a functional test that fails.

    Done. The test below passes on master and on 61ba701b9e with the suggested patch, but fails on 61ba701b9e without it.

    diff --git a/test/functional/wallet_importprunedfunds.py b/test/functional/wallet_importprunedfunds.py
    index 95e2a5b3a4..bd08819fdc 100755
    --- a/test/functional/wallet_importprunedfunds.py
    +++ b/test/functional/wallet_importprunedfunds.py
    @@ -157,6 +157,29 @@ 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 restores inputs of removed confirmed spend")
    +        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.sendall(
    +            recipients=[w1.getnewaddress()], inputs=[parent_utxo]
    +        )["txid"]
    +        self.generate(node, 1)
    +
    +        def parent_is_unspent():
    +            return any(
    +                (utxo["txid"], utxo["vout"]) == parent_outpoint
    +                for utxo in node.listunspent(minconf=0)
    +            )
    +
    +        assert_equal(parent_is_unspent(), False)
    +        node.removeprunedfunds(child_txid)
    +        assert_equal(parent_is_unspent(), True)
    +
     
     if __name__ == '__main__':
         ImportPrunedFundsTest(__file__).main()
    
  198. achow101 force-pushed on Jun 6, 2026
  199. DrahtBot added the label CI failed on Jun 6, 2026
  200. DrahtBot commented at 6:12 AM on June 6, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/27053283280/job/79852692010</sub> <sub>LLM reason (✨ experimental): CI failed due to a Clang compilation error in src/test/util/__/wallet/test/util.cpp: CWallet::CreateNew is called with 6 arguments but 7 are required.</sub>

    <details><summary>Hints</summary>

    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.

    </details>

  201. test: Test removedprunedfunds makes input TXOs spendable 53224840ab
  202. wallet: Add m_from_me to cache "from me" status
    m_from_me is used to track whether a transaction is "from me", i.e. has
    any inputs which belong to the wallet.
    b03c42144f
  203. wallet: Replace CachedTxIsFromMe with direct m_from_me lookup
    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.
    ca8f107a8c
  204. wallet: Store the TxState in each WalletTXO as well db1c4af13f
  205. wallet: Get the depth in main chain for a TxState 3fcdd768f7
  206. wallet: Get the coinbase maturity state for a TXO and TxState b51cd4d26b
  207. wallet: Use WalletTXO stored state and coinbase rather than wtx ae385efdee
  208. walletdb: Move ReorderTransactions to immediately after loading txs
    Perform the transaction reorder upgrade immediately after loading txs
    instead of waiting for the end of loading.
    157e3453ce
  209. wallet: Store a copy of m_from_me in WalletTXOs and use for "from me"
    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.
    c16472d5f6
  210. wallet: Have WalletTXOs also store parent tx time
    WalletTXOs need to know their parent tx's timestamp for AvailableCoins
    to work.
    14be82ee50
  211. wallet: Make CWalletTx::m_state private with {get,set}ters 896491b366
  212. wallet: have CWalletTx also be aware of it's WalletTXOs ceb4759211
  213. wallet: Also update a CWalletTx's WalletTXOs states
    When the state of a CWalletTx changes, we need to change the state in
    the WalletTXOs too.
    c852564b64
  214. wallet: Include transaction version in WalletTXO fb74b62693
  215. wallet: Remove unused WalletTXO::GetWalletTx() 79cc0e8d48
  216. wallet: Have IsSpent take a min_conf
    A min_conf parameter is added to IsSpent so that it can set a
    confirmation threshold for whether something is considered spent.
    e6766dbc7c
  217. wallet: Iterate block txs in reverse on blockDisconnected
    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.
    37f3ee3486
  218. wallet, tests: Have CreateSyncedWallet use CWallet::Create
    CWallet::Create will properly connect the wallet to the chain, so we
    should be doing that rather than ad-hoc chain connection.
    85ba19a651
  219. wallet: Move definintely unusable TXOs to a separate container
    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.
    e8fc840837
  220. in src/wallet/test/util.cpp:25 in 00a3a5e5bb
      26 | -        LOCK2(wallet->cs_wallet, ::cs_main);
      27 | -        wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
      28 | -    }
      29 | +    bilingual_str error;
      30 | +    std::vector<bilingual_str> warnings;
      31 | +    auto wallet = CWallet::CreateNew(context, "", CreateMockableWalletDatabase(), WALLET_FLAG_DESCRIPTORS, error, warnings);
    


    w0xlt commented at 8:26 AM on June 7, 2026:

    This should fix the CI error.

        auto wallet = CWallet::CreateNew(context, "", CreateMockableWalletDatabase(), WALLET_FLAG_DESCRIPTORS, /*born_encrypted=*/false, error, warnings);
    
  221. achow101 force-pushed on Jun 10, 2026
  222. DrahtBot removed the label CI failed on Jun 10, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-06-11 10:52 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me