[wallet] Move min_depth and max_depth to coin control #15906

pull amitiuttarwar wants to merge 1 commits into bitcoin:master from amitiuttarwar:refactor_available_coins changing 5 files +18 −6
  1. amitiuttarwar commented at 1:26 AM on April 27, 2019: contributor
    • Refactor AvailableCoins to pull min & max depths from coin control.

    • Add m_max_depth to coin control to support this.

    • Addresses issue #15823, see thread for further details.

  2. fanquake added the label Refactoring on Apr 27, 2019
  3. fanquake added the label Wallet on Apr 27, 2019
  4. in src/wallet/wallet.h:973 in df41aa17d5 outdated
     804 | @@ -805,7 +805,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
     805 |      /**
     806 |       * populate vCoins with vector of available COutputs.
     807 |       */
     808 | -    void AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<COutput>& vCoins, bool fOnlySafe=true, const CCoinControl *coinControl = nullptr, const CAmount& nMinimumAmount = 1, const CAmount& nMaximumAmount = MAX_MONEY, const CAmount& nMinimumSumAmount = MAX_MONEY, const uint64_t nMaximumCount = 0, const int nMinDepth = 0, const int nMaxDepth = 9999999) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
    


    luke-jr commented at 1:58 AM on April 27, 2019:

    It's ugly if code calling this with nMinDepth (eg, other PRs or out-of-tree patches) can magically have it become nMaxDepth instead.

    Maybe nMaxDepth ought to be moved to CCoinControl also? (Or removed? Is there a use case for this?)


    luke-jr commented at 1:59 AM on April 27, 2019:

    What is the rationale for making CCoinControl a required parameter?

    If CCoinControl is made required, it should probably be in a separate commit, or at least the rationale documented in the commit message.


    MarcoFalke commented at 12:53 PM on April 27, 2019:

    It seems fragile to rely on the fact that the fallback defaults are the same if no cc is passed or a default constructed cc is passed.


    amitiuttarwar commented at 12:22 AM on April 28, 2019:

    It's ugly if code calling this with nMinDepth (eg, other PRs or out-of-tree patches) can magically have it become nMaxDepth instead.

    agreed.

    Maybe nMaxDepth ought to be moved to CCoinControl also?

    was thinking the same. any suggestions on how to go about answering that? or should I just go for it and pull nMaxDepth out?

    Or removed? Is there a use case for this?

    looks like the listunspent rpc accepts a nMaxDepth & passes that through here


    amitiuttarwar commented at 1:17 AM on April 28, 2019:

    hm, so these are the tradeoffs I see-

    optional: closer to current function signature, less duplicated code constructing default cc, don't have this problem with callers

    required: expectations are more explicit from the POV of a caller, not a big of deal to make the change since this function isn't invoked frequently.

    I don't feel familiar enough with the codebase / C++ to have much of an opinion here. Marco, how strongly do you hold the preference for required? Seems like luke-jr & empact both prefer keeping it optional.


    MarcoFalke commented at 1:25 PM on April 28, 2019:

    Oh sure, go for what luke and empact prefer. I consider it a stylistic preference.


    MarcoFalke commented at 6:32 PM on July 16, 2019:
    It's ugly if code calling this with nMinDepth (eg, other PRs or out-of-tree patches) can magically have it become nMaxDepth instead.

    Could solve this by swapping the arguments bool fOnlySafe and CCoinControl of the function, so that all out-of-tree patches have a (silent) merge conflict and won't compile anymore?

    Alternative is to also pass nMaxDepth through with the coincontrol.


    amitiuttarwar commented at 4:55 PM on July 17, 2019:

    pulled onto coincontrol

  5. in src/wallet/wallet.cpp:2187 in df41aa17d5 outdated
    2219 | @@ -2220,7 +2220,14 @@ CAmount CWallet::GetAvailableBalance(const CCoinControl* coinControl) const
    2220 |  
    2221 |      CAmount balance = 0;
    2222 |      std::vector<COutput> vCoins;
    2223 | -    AvailableCoins(*locked_chain, vCoins, true, coinControl);
    2224 | +
    2225 | +    // Make sure we pass a CC
    2226 | +    if (!coinControl) {
    2227 | +        CCoinControl defaultCc;
    2228 | +        coinControl = &defaultCc;
    


    Empact commented at 5:27 AM on April 27, 2019:

    This in invalid because the var is out of scope when you dereference it below, suspect that's behind the AddressSanitizer failure on CI.

    That said, I would keep coinControl optional, as the default value of 0 for nMinDepth is equivalent to the absent / null condition for coinControl, in that it never fails the check. Would simplify diff overall.

  6. promag commented at 3:17 PM on April 29, 2019: member

    Concept ACK.

  7. jnewbery commented at 9:15 PM on April 30, 2019: member

    Concept ACK

  8. amitiuttarwar force-pushed on May 1, 2019
  9. amitiuttarwar force-pushed on May 10, 2019
  10. amitiuttarwar force-pushed on May 10, 2019
  11. DrahtBot commented at 8:24 AM on May 24, 2019: member

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    No conflicts as of last run.

  12. DrahtBot added the label Needs rebase on Jun 18, 2019
  13. laanwj commented at 12:37 PM on July 4, 2019: member

    Concept and light code-review ACK

  14. amitiuttarwar force-pushed on Jul 16, 2019
  15. amitiuttarwar commented at 5:12 PM on July 16, 2019: contributor

    this PR has been rebased & is ready for review.

  16. DrahtBot removed the label Needs rebase on Jul 16, 2019
  17. amitiuttarwar force-pushed on Jul 16, 2019
  18. amitiuttarwar commented at 6:20 PM on July 16, 2019: contributor

    fixed a bug pointed out by @MarcoFalke (thank you!) where I forgot to set min depth on coin control from listunspent rpc. since it wasn't caught by tests, I'd like to add coverage in a separate commit or PR.

  19. in src/wallet/wallet.cpp:2449 in fc7c870b9c outdated
    2441 | @@ -2442,7 +2442,11 @@ void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<
    2442 |              continue;
    2443 |          }
    2444 |  
    2445 | -        if (nDepth < nMinDepth || nDepth > nMaxDepth)
    2446 | +        int min_depth = 0;
    2447 | +        if (coinControl)
    2448 | +            min_depth = coinControl->m_min_depth;
    2449 | +
    2450 | +        if (nDepth < min_depth || nDepth > nMaxDepth)
    


    MarcoFalke commented at 6:28 PM on July 16, 2019:

    style-nit: Add those {}

            if (nDepth < min_depth || nDepth > nMaxDepth) {
    
  20. in src/wallet/wallet.cpp:2447 in fc7c870b9c outdated
    2441 | @@ -2442,7 +2442,11 @@ void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<
    2442 |              continue;
    2443 |          }
    2444 |  
    2445 | -        if (nDepth < nMinDepth || nDepth > nMaxDepth)
    2446 | +        int min_depth = 0;
    2447 | +        if (coinControl)
    2448 | +            min_depth = coinControl->m_min_depth;
    


    MarcoFalke commented at 6:28 PM on July 16, 2019:

    Instead of running this once per loop, could move it to the beginning of the function (out of the loop). That would also avoid hiding the default value 0 deep in the function body.

    const int min_depth{coinControl?coinControl->m_min_depth:0};
    
  21. MarcoFalke commented at 6:33 PM on July 16, 2019: member

    Please update OP to reflect the current state of the pull

  22. amitiuttarwar force-pushed on Jul 17, 2019
  23. amitiuttarwar renamed this:
    [wallet] Remove AvailableCoins nMinDepth argument
    [wallet] Refactor AvailableCoins to pull depth from coin control
    on Jul 17, 2019
  24. jnewbery commented at 3:18 PM on July 18, 2019: member

    utACK aa910716c92cfcaa88f396e42465eb1f79522a8e

    Code changes look good. From my reading this is a pure refactor that doesn't change behaviour.

  25. elichai commented at 4:11 PM on July 18, 2019: contributor

    utACK, read the code. https://github.com/bitcoin/bitcoin/commit/aa910716c92cfcaa88f396e42465eb1f79522a8e The only thing is maybe m_min_depth(0) and m_max_depth(9999999) could be static constants, instead of defined as default value and defined again in AvailableCoins

  26. amitiuttarwar commented at 6:20 PM on July 18, 2019: contributor

    good point @elichai. I'm going to leave it for now to leave the acks, but if other changes are needed I will update. thanks for reviewing :)

  27. in src/wallet/wallet.cpp:2435 in 3de1ecb073 outdated
    2431 | @@ -2432,6 +2432,7 @@ void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<
    2432 |      // Either the WALLET_FLAG_AVOID_REUSE flag is not set (in which case we always allow), or we default to avoiding, and only in the case where
    2433 |      // a coin control object is provided, and has the avoid address reuse flag set to false, do we allow already used addresses
    2434 |      bool allow_used_addresses = !IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE) || (coinControl && !coinControl->m_avoid_address_reuse);
    2435 | +    const int min_depth = { coinControl ? coinControl->m_min_depth : 0 };
    


    promag commented at 10:22 PM on July 18, 2019:

    nit, could drop { }?


    MarcoFalke commented at 11:00 PM on July 18, 2019:

    I'd rather drop the =. Note that the {} enable the -Wnarrowing compiler check


    MarcoFalke commented at 11:00 PM on July 18, 2019:

    Or keep it as is, since the compiler checks seems to work even with the =


    amitiuttarwar commented at 1:25 AM on July 19, 2019:

    hm. In a simple example c++ program, I get a compiler error when I drop the =. so I added it.


    MarcoFalke commented at 2:05 PM on July 19, 2019:

    Huh? Both pass for me

    $ echo 'int main(){ const int min_depth = { true ? 1 : 0 };}' | g++ -x c++ -
    $ echo 'int main(){ const int min_depth   { true ? 1 : 0 };}' | g++ -x c++ -
    

    elichai commented at 2:13 PM on July 19, 2019:

    FYI, even without optimizations these two are equivalent https://godbolt.org/z/Q4sjcx

  28. promag commented at 10:25 PM on July 18, 2019: member

    ACK aa910716c92cfcaa88f396e42465eb1f79522a8e, I'd squash though. Also reword to "wallet: Move min_depth and max_depth to coin control".

    Eventually we could make CoinControl required - allows to have defaults in one place only.

  29. amitiuttarwar renamed this:
    [wallet] Refactor AvailableCoins to pull depth from coin control
    [wallet] Move min_depth and max_depth to coin control
    on Jul 19, 2019
  30. in src/wallet/coincontrol.h:44 in aa910716c9 outdated
      39 | @@ -40,6 +40,8 @@ class CCoinControl
      40 |      FeeEstimateMode m_fee_mode;
      41 |      //! Minimum chain depth value for coin availability
      42 |      int m_min_depth{0};
      43 | +    //! Maximum chain depth value for coin availability
      44 | +    int m_max_depth{9999999};
    


    jonatack commented at 9:53 AM on July 19, 2019:

    Perhaps for another PR but could be nice to document the "why" behind the value of 9999999 and hoist it to a static constant:

    src/wallet/coincontrol.h:44:   int m_max_depth{9999999};
    src/wallet/rpcwallet.cpp:2822: int nMaxDepth = 9999999;
    src/wallet/wallet.cpp:2436:    const int max_depth = { coinControl ? coinControl->m_max_depth : 9999999 };
    
  31. jonatack commented at 9:58 AM on July 19, 2019: member

    ACK aa910716c92cfcaa88f396e42465eb1f79522a8e - code review, compiled, ran unit and extended functional tests.

    I'd squash though. Also reword to "wallet: Move min_depth and max_depth to coin control".

    Agree, the commits made more sense to me when reviewed together and @promag's commit message is clearer to me as well. Happy to re-review in that case.

  32. jonatack commented at 1:39 PM on July 19, 2019: member

    fixed a bug pointed out by @MarcoFalke (thank you!) where I forgot to set min depth on coin control from listunspent rpc. since it wasn't caught by tests, I'd like to add coverage in a separate commit or PR.

    Coverage issue reproduced by removing src/wallet/rpcwallet.cpp:L2880 cctl.m_min_depth = nMinDepth;

    compiler warns

    wallet/rpcwallet.cpp: In function ‘UniValue listunspent(const JSONRPCRequest&)’:
    wallet/rpcwallet.cpp:2816:9: warning: variable ‘nMinDepth’ set but not used [-Wunused-but-set-variable]
         int nMinDepth = 1;
             ^~~~~~~~~
    

    but unit tests and functional tests pass.

  33. jonatack commented at 2:17 PM on July 19, 2019: member

    FWIW, same issue for src/wallet/rpcwallet.cpp:L2881 cctl.m_max_depth = nMaxDepth; ... compiler warning, unit tests pass, functional tests pass.

  34. remyers commented at 4:01 PM on July 19, 2019: none

    It looks like CCoinControl:SetNull should also reset the values of: m_min_depth m_max_depth

    This is called by sendcoinsdialog.cpp (h/t @jnewbery)

  35. amitiuttarwar force-pushed on Jul 22, 2019
  36. extract min & max depth onto coin control 80ba4241a6
  37. amitiuttarwar force-pushed on Jul 22, 2019
  38. laanwj commented at 10:02 AM on July 31, 2019: member

    ACK 80ba4241a6773590f6b2c18dae758097b5adc02e

  39. laanwj merged this on Jul 31, 2019
  40. laanwj closed this on Jul 31, 2019

  41. laanwj referenced this in commit 00922b8720 on Jul 31, 2019
  42. sidhujag referenced this in commit 217e4058c3 on Aug 1, 2019
  43. jasonbcox referenced this in commit 327d1005b2 on Jul 28, 2020
  44. DrahtBot locked this on Dec 16, 2021

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-17 06:14 UTC

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