wallet: simplify ListCoins implementation #25659

pull furszy wants to merge 2 commits into bitcoin:master from furszy:2022_wallet_clean_listCoins changing 2 files +17 −40
  1. furszy commented at 2:06 AM on July 21, 2022: member

    Focused on the following changes:

    1. Removed the entire locked coins lookup that was inside ListCoins by including them directly on the AvailableCoins result (where we were skipping them before).
    2. Unified both FindNonChangeParentOutput functions (only called from ListCoins)
  2. DrahtBot added the label Wallet on Jul 21, 2022
  3. DrahtBot commented at 6:45 AM on July 21, 2022: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK aureleoules, theStack, achow101
    Concept ACK josibake
    Stale ACK w0xlt

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #26699 (wallet, gui: bugfix, getAvailableBalance skips selected coins by furszy)
    • #26365 (wallet: GetEffectiveBalance by willcl-ark)
    • #25979 ([WIP] wallet: standardize change output detection process by furszy)

    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.

  4. in src/wallet/spend.cpp:370 in 6451afadd4 outdated
     235 | -    const CTransaction* ptx = &tx;
     236 | -    int n = output;
     237 | +    const CWalletTx* wtx = wallet.GetWalletTx(outpoint.hash);
     238 | +    assert(wtx);
     239 | +
     240 | +    const CTransaction* ptx = wtx->tx.get();
    


    aureleoules commented at 8:30 AM on July 25, 2022:

    nit

        const CWalletTx& wtx{*Assert(wallet.GetWalletTx(outpoint.hash));
        const CTransaction* ptx = wtx.tx.get();
    

    furszy commented at 1:34 PM on July 25, 2022:

    thx, done

  5. aureleoules commented at 9:01 AM on July 25, 2022: member

    ACK 6451afadd468bbff5b88a3fe318d3af1329770ab.

    • Verified that default values have not been changed in the AvailableCoinsParams struct.
    • I understand that skipped coins are skipped in the AvailableCoins function instead of being post-processed to avoid iterating over all coins again.
    • ListCoins is now much more readable.
  6. furszy force-pushed on Jul 25, 2022
  7. furszy commented at 1:33 PM on July 25, 2022: member

    Thanks for the feedback @aureleoules! nit pushed inside adfdd02. Inlined the assert + declaration using the Assert() helper function.

    I understand that skipped coins are skipped in the AvailableCoins function instead of being post-processed to avoid iterating over all coins again.

    On master, by default, we skip the locked coins inside AvailableCoins, which forces ListCoins to do a secondary lookup only to fetch them. As AvailableCoins walks-through the entire wallet txs map (which includes the locked coins), this PR adds a filter option to include the locked coins in the result, instead of discard them, so ListCoins doesn't need to do the secondary lookup after calling AvailableCoins.

  8. w0xlt approved
  9. w0xlt commented at 9:52 PM on July 26, 2022: contributor

    tACK https://github.com/bitcoin/bitcoin/pull/25659/commits/adfdd027d1edc635a2af59ce94757b2ef87e4f1a

    The removed code seems redundant and not discarding the locked coins in AvailableCoins is a better solution.

  10. DrahtBot added the label Needs rebase on Jul 28, 2022
  11. furszy force-pushed on Jul 29, 2022
  12. furszy commented at 11:54 AM on July 29, 2022: member

    Rebased on master due conflicts with recently merged 24584.

    Ready to go again.

  13. DrahtBot removed the label Needs rebase on Jul 29, 2022
  14. DrahtBot added the label Needs rebase on Aug 5, 2022
  15. furszy force-pushed on Aug 5, 2022
  16. furszy commented at 10:21 PM on August 5, 2022: member

    rebased

  17. DrahtBot removed the label Needs rebase on Aug 6, 2022
  18. theStack commented at 5:34 PM on August 9, 2022: contributor

    Concept ACK

  19. w0xlt approved
  20. josibake commented at 12:00 PM on August 11, 2022: member

    Concept ACK on simplifying ListCoins but a little concerned with the general trend of making AvailableCoins a general purpose wallet traversal tool. I'm more thinking out loud here, so feel free to ignore me.

    It seems we've added a few flags recently to AvailableCoins to remove additional lookups in other wallet functions and simplify code, which is great, but I'm worried that instead of simplifying we are instead obfuscating complexity and expanding the responsibility of AvailableCoins to do too many things.

    This is more of a meta concern and not really a criticism of this PR, so don't want to block if other reviewers feel strongly this is the right way to go. Personally, I'm more in favor of leaving it as is for now and instead starting a discussion on a more holistic solution. If we do decide to keep increasing the responsibility of AvailableCoins, I'd recommend we leave the function signature as is and not wrap params in a struct so as to keep complexity explicit rather than hidden.

  21. DrahtBot added the label Needs rebase on Aug 17, 2022
  22. achow101 commented at 7:47 PM on October 12, 2022: member

    Are you still working on this?

  23. achow101 commented at 5:15 PM on November 10, 2022: member

    Closing this as it has not had any activity in a while. If you are interested in continuing work on this, please leave a comment so that it can be reopened.

  24. achow101 closed this on Nov 10, 2022

  25. furszy commented at 8:59 PM on November 27, 2022: member

    @achow101 please re-open it. Will rebase it, it will be smaller now.

  26. achow101 reopened this on Nov 27, 2022

  27. furszy force-pushed on Nov 27, 2022
  28. DrahtBot removed the label Needs rebase on Nov 28, 2022
  29. furszy commented at 1:18 PM on November 28, 2022: member

    rebased, status updated.

  30. furszy force-pushed on Dec 12, 2022
  31. in src/wallet/spend.cpp:370 in 64494e6361 outdated
     367 |      AssertLockHeld(wallet.cs_wallet);
     368 | -    const CTransaction* ptx = &tx;
     369 | -    int n = output;
     370 | +    const CWalletTx* wtx{Assert(wallet.GetWalletTx(outpoint.hash))};
     371 | +
     372 | +    const CTransaction* ptx = wtx->tx.get();
    


    aureleoules commented at 4:07 PM on December 28, 2022:

    nit: maybe assert wtx->tx is not null first?


    furszy commented at 5:36 PM on December 28, 2022:

    cannot be null, it's set on the CWalletTx constructor.

  32. achow101 referenced this in commit 65d7c31b3f on Jan 3, 2023
  33. DrahtBot added the label Needs rebase on Jan 3, 2023
  34. wallet: simplify ListCoins implementation
    Can remove the locked coins lookup if we include them directly
    inside the AvailableCoins result
    b3f4e82737
  35. wallet: unify FindNonChangeParentOutput functions
    The function is only used in ListCoins.
    a2ac6f9582
  36. furszy force-pushed on Jan 3, 2023
  37. furszy commented at 9:07 PM on January 3, 2023: member

    rebased post #25789 merge. Now this PR purely cleans up an ugly flow.

  38. DrahtBot removed the label Needs rebase on Jan 3, 2023
  39. DrahtBot renamed this:
    wallet: simplify ListCoins implementation
    wallet: simplify ListCoins implementation
    on Jan 3, 2023
  40. aureleoules commented at 3:18 PM on January 4, 2023: member

    ACK a2ac6f9582c4c996fa36e4801fa0aac756235754, LGTM

    I verified the call to AvailableCoins takes the same coin controls params as AvailableCoinsListUnspent except for skip_locked which is now false and fAllowWatchOnly to allow the removal of the large and redundant chunk of code below.

  41. furszy requested review from w0xlt on Jan 11, 2023
  42. theStack approved
  43. theStack commented at 5:33 PM on January 17, 2023: contributor

    Code-review ACK a2ac6f9582c4c996fa36e4801fa0aac756235754

    As discussed in IRC, note that the first commit is more than just a refactor, as it introduces some changes in behaviour regarding locked coins, since the filtering in AvailableCoins is much more detailled than the manual loop in ListCoins that is replaced:

    • locked unsafe coins are are included on master, but excluded on the PR (CCoinControl's m_include_unsafe_inputs is false by default)
    • locked immature coinbase coins are included on master, but excluded on the PR (CoinFilterParams' include_immature_coinbase is false by default)
    • locked coins that are unconfirmed but not in the mempool are included in the master, but excluded on the PR (no option available to pass that, but I'm not sure how to even reach such a state)

    However, all of those behaviour changes seem to make sense, the commit could be seen as a bugfix. It doesn't make much sense to display unsafe or immature coins for selection in the GUI (that's the only place where ListCoins is currently used, via the listCoins interface). For a follow-up candidate, would be nice to add a proper test for ListCoins which would have detected the behaviour changes.

  44. furszy commented at 7:01 PM on January 17, 2023: member

    Thanks for the detailed review @theStack!

  45. achow101 commented at 7:17 PM on January 18, 2023: member

    ACK a2ac6f9582c4c996fa36e4801fa0aac756235754

    However, all of those behaviour changes seem to make sense, the commit could be seen as a bugfix. It doesn't make much sense to display unsafe or immature coins for selection in the GUI

    Additionally all locked coins are not able to be selected in the GUI, so this does not materially affect what users are able to choose. However it does affect what is displayed to users.

  46. achow101 merged this on Jan 18, 2023
  47. achow101 closed this on Jan 18, 2023

  48. sidhujag referenced this in commit 6fd04fe78b on Jan 19, 2023
  49. furszy deleted the branch on May 27, 2023
  50. bitcoin locked this on May 26, 2024

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-04-16 00:13 UTC

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