Fix get balance #13566

pull jnewbery wants to merge 6 commits into bitcoin:master from jnewbery:fix_get_balance changing 5 files +73 −89
  1. jnewbery commented at 7:05 pm on June 28, 2018: member

    #12953 inadvertently removed the functionality to call getbalance "*" <int> <bool> to get the wallet’s balance with either minconfs or include_watchonly.

    This restores that functionality (when -deprecatedrpc=accounts), and also makes it possible to call ``getbalance minconf= include_watchonly=` when accounts are not being used.

  2. jnewbery commented at 7:06 pm on June 28, 2018: member

    Feedback to address from @ryanofsky

    Some thoughts looking at code:

    • It seems like it would be good to raise an error if IsDeprecatedRPCEnabled("accounts") and the account argument is not null or “*”. Otherwise the argument would be silently ignored.
    • Documentation seems to say default minconf is 1, while implementation uses 0 or 1.
    • Maybe could dedup caching code in GetAvailableCredit a little with something like
    0CAmount* cache = filter == ISMINE_SPENDABLE  ? nAvailableCreditCached : 
    1                 filter == ISMINE_WATCH_ONLY ? nAvailableWatchCreditCached :
    2                 nullptr;
    
  3. MarcoFalke added this to the milestone 0.17.0 on Jun 28, 2018
  4. jnewbery commented at 9:00 pm on June 28, 2018: member

    I guess that would not work for min dept of 0 (unconfirmed).

    Are you saying that we also need to check pcoin->InMempool() for coins with 0 depth?

  5. [wallet] GetBalance can take an isminefilter filter.
    GetBalance() can now take an ismine filter, which is passed down to
    GetAvailableCredit. This allows GetBalance to be used to get watch-only
    balances.
    4279da4785
  6. [wallet] Factor out GetWatchOnlyBalance() ef7bc8893c
  7. [wallet] deduplicate GetAvailableCredit logic 7110c830f8
  8. [wallet] factor out GetAvailableWatchOnlyBalance() 0f3d6e9ab7
  9. [wallet] GetBalance can take a min_depth argument. cf15761f6d
  10. jnewbery force-pushed on Jun 28, 2018
  11. jnewbery commented at 9:59 pm on June 28, 2018: member

    I believe that I’ve addressed all of @ryanofsky’s feedback. Russ - can you rereview and confirm?

    Need to address @jonasschnelli’s comment. Jonas - can you please confirm my question above? Thanks!

  12. fanquake added the label Wallet on Jun 28, 2018
  13. in src/wallet/rpcwallet.cpp:929 in c57f2009f6 outdated
    948-        if(!include_watchonly.isNull())
    949-            if(include_watchonly.get_bool())
    950-                filter = filter | ISMINE_WATCH_ONLY;
    951-
    952-        return ValueFromAmount(pwallet->GetLegacyBalance(filter, nMinDepth, account));
    953+        if (!IsDeprecatedRPCEnabled("accounts") && *account != "") {
    


    ryanofsky commented at 1:39 pm on June 29, 2018:

    In commit “[RPC] [wallet] allow getbalance to use min_conf and watch_only”

    I think the safest thing here would be to raise an error whenever accounts are deprecated and account is non-null, because previously getbalance(null), getbalance("*"), and getbalance("") each did different things, and it doesn’t seem good to silently ignore a requested behavior. Another slightly less safe but maybe acceptable option would be to change "" to "*" on this line, since getbalance("*") and getbalance(null) at least were more similar than getbalance("*") and getbalance("")

    Previously, and currently with the deprecated accounts feature enabled:

    • getbalance(null) would return total balance
    • getbalance("*") would return total balance with legacy accounting method
    • getbalance("account") would return balance associated with a named account
    • getbalance("") would return leftover balance not associated with any named accounts

    ryanofsky commented at 2:23 pm on June 29, 2018:

    #13566 (review)

    Also want to note that another approach we could take here would not be to deprecate the account argument at all, but instead just rename it to label and pass it down as an option to the GetBalance() and GetAvailableCredit methods where it could be used to filter txouts there by address label. This would be a simple code change, just adding if (!label || *label == GetLabelName(txout.scriptPubKey)).

  14. ryanofsky commented at 1:51 pm on June 29, 2018: member

    utACK c57f2009f65ffedc7531644243d5d7ddd786edde if getbalance("") is not treated like getbalance(null), (see comment).

    It looks like we might have had tests for this previously which were deleted. It could be better in the future to avoid dropping tests for deprecated features until the features are actually removed, though it is sometimes nice to simplify tests.

  15. jnewbery force-pushed on Jun 29, 2018
  16. jnewbery commented at 2:25 pm on June 29, 2018: member

    Thanks @ryanofsky . Updated to treat * as an acceptable dummy argument.

    I don’t think we want to entirely remove the possibility of calling this method with positional arguments.

  17. ryanofsky commented at 3:42 pm on June 29, 2018: member
    utACK 43937db46edab8c226ce190e185d7a3285b3b364, only change since last review is requiring “*” instead of "" to signify all accounts. Thanks for implementing this and all the changes.
  18. jnewbery commented at 3:48 pm on June 29, 2018: member
    Thanks for the review @ryanofsky !
  19. Empact commented at 4:44 pm on June 29, 2018: member
    This calls for a test, no?
  20. [RPC] [wallet] allow getbalance to use min_conf and watch_only without accounts. 702ae1e21a
  21. jnewbery force-pushed on Jun 29, 2018
  22. jnewbery commented at 7:55 pm on June 29, 2018: member
    @Empact - test added. Review plz :slightly_smiling_face:
  23. in src/wallet/wallet.cpp:2197 in 702ae1e21a
    2193@@ -2224,7 +2194,7 @@ CAmount CWallet::GetUnconfirmedWatchOnlyBalance() const
    2194         {
    2195             const CWalletTx* pcoin = &entry.second;
    2196             if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && pcoin->InMempool())
    2197-                nTotal += pcoin->GetAvailableWatchOnlyCredit();
    2198+                nTotal += pcoin->GetAvailableCredit(true, ISMINE_WATCH_ONLY);
    


    Empact commented at 8:19 pm on June 29, 2018:
    nit: comment bool arg?

    jnewbery commented at 3:16 pm on November 1, 2018:
    I don’t think it’s the project’s coding style to always comment arguments (at least, I don’t see that elsewhere in the codebase as the norm).
  24. Empact commented at 8:24 pm on June 29, 2018: member
    Should CWallet::GetUnconfirmedBalance use the cache?
  25. in src/wallet/wallet.cpp:1989 in 702ae1e21a
    1986+        cache = &nAvailableCreditCached;
    1987+        cache_used = &fAvailableCreditCached;
    1988+    } else if (filter == ISMINE_WATCH_ONLY) {
    1989+        cache = &nAvailableWatchCreditCached;
    1990+        cache_used = &fAvailableWatchCreditCached;
    1991+    }
    


    Empact commented at 8:30 pm on June 29, 2018:
    nit: could move this into a fUseCache block, connected with 1991 below.

    promag commented at 10:52 pm on July 4, 2018:
    What if filter == ISMINE_ALL — which can be when include_watchonly=true in RPC getbalance — could sum both if cached?

    jnewbery commented at 3:54 pm on November 1, 2018:
    No, cache and cache_used are updated below even if fUseCache is not set.

    jnewbery commented at 3:57 pm on November 1, 2018:
    Seems like a reasonable optimization, but I don’t think it’s required. Could be done in a separate PR.
  26. in src/wallet/rpcwallet.cpp:921 in 702ae1e21a
    931+        min_depth = request.params[1].get_int();
    932+    }
    933+
    934+    isminefilter filter = ISMINE_SPENDABLE;
    935+    if (!request.params[2].isNull() && request.params[2].get_bool()) {
    936+        filter = filter | ISMINE_WATCH_ONLY;
    


    Empact commented at 8:34 pm on June 29, 2018:
    nit: |=
  27. in src/wallet/rpcwallet.cpp:4426 in 702ae1e21a
    4422@@ -4416,7 +4423,7 @@ static const CRPCCommand commands[] =
    4423     { "wallet",             "dumpwallet",                       &dumpwallet,                    {"filename"} },
    4424     { "wallet",             "encryptwallet",                    &encryptwallet,                 {"passphrase"} },
    4425     { "wallet",             "getaddressinfo",                   &getaddressinfo,                {"address"} },
    4426-    { "wallet",             "getbalance",                       &getbalance,                    {"account","minconf","include_watchonly"} },
    4427+    { "wallet",             "getbalance",                       &getbalance,                    {"account|dummy","minconf","include_watchonly"} },
    


    Empact commented at 8:37 pm on June 29, 2018:
    Maybe show * here instead of dummy?

    jnewbery commented at 3:20 pm on November 1, 2018:
    dummy is the name of the argument. * is the value that it needs to be set to.
  28. in src/wallet/rpcwallet.cpp:925 in 702ae1e21a
    935+    if (!request.params[2].isNull() && request.params[2].get_bool()) {
    936+        filter = filter | ISMINE_WATCH_ONLY;
    937+    }
    938+
    939+    if (!account_value.isNull()) {
    940 
    


    Empact commented at 8:44 pm on June 29, 2018:
    nit: whitespace
  29. in src/wallet/rpcwallet.cpp:856 in 702ae1e21a
    852@@ -853,8 +853,9 @@ static UniValue getbalance(const JSONRPCRequest& request)
    853         return NullUniValue;
    854     }
    855 
    856-    if (request.fHelp || (request.params.size() > 3 && IsDeprecatedRPCEnabled("accounts")) || (request.params.size() != 0 && !IsDeprecatedRPCEnabled("accounts")))
    857+    if (request.fHelp || (request.params.size() > 3 ))
    


    Empact commented at 8:45 pm on June 29, 2018:
    nit: whitespace
  30. Empact commented at 8:54 pm on June 29, 2018: member
    utACK 702ae1e
  31. DrahtBot commented at 11:36 am on June 30, 2018: member
    • #13637 (wallet: Add GetBalances to calculate all balances by promag)
    • #13631 (Add CMerkleTx::IsImmatureCoinBase method by Empact)
    • #13083 (Add compile time checking for cs_main runtime locking assertions by practicalswift)
    • #11634 (wallet: Add missing cs_wallet/cs_KeyStore locks to wallet by practicalswift)
    • #11020 ([wallet] getbalance: Add option to include non-mempool UTXOs by kallewoof)

    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.

  32. laanwj commented at 1:06 pm on July 4, 2018: member
    utACK 702ae1e21a09d8c31406839c4ea507c5fa276898
  33. in src/wallet/wallet.h:463 in 702ae1e21a
    459@@ -460,9 +460,8 @@ class CWalletTx : public CMerkleTx
    460     CAmount GetDebit(const isminefilter& filter) const;
    461     CAmount GetCredit(const isminefilter& filter) const;
    462     CAmount GetImmatureCredit(bool fUseCache=true) const;
    463-    CAmount GetAvailableCredit(bool fUseCache=true) const;
    464+    CAmount GetAvailableCredit(bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE) const;
    


    promag commented at 11:06 pm on July 4, 2018:
    nit, drop &?
  34. in src/wallet/wallet.cpp:2150 in 702ae1e21a
    2148         for (const auto& entry : mapWallet)
    2149         {
    2150             const CWalletTx* pcoin = &entry.second;
    2151-            if (pcoin->IsTrusted())
    2152-                nTotal += pcoin->GetAvailableCredit();
    2153+            if (pcoin->IsTrusted() && pcoin->GetDepthInMainChain() >= min_depth) {
    


    promag commented at 11:12 pm on July 4, 2018:

    IsTrusted() already computes GetDepthInMainChain(). Suggestion:

    0bool IsTrusted(int min_depth = 1)
    

    promag commented at 10:49 pm on July 16, 2018:
    Ignore my comment, I tried that refactor and it doesn’t look good.
  35. promag commented at 11:17 pm on July 4, 2018: member
    utACK 702ae1e. Consider last comment as a way to not degrade GetBalance performance.
  36. sipa commented at 1:27 am on July 14, 2018: member
    utACK 702ae1e21a09d8c31406839c4ea507c5fa276898
  37. sipa merged this on Jul 14, 2018
  38. sipa closed this on Jul 14, 2018

  39. sipa referenced this in commit ad552a54c5 on Jul 14, 2018
  40. jnewbery commented at 2:38 am on July 16, 2018: member
    @Empact @promag - thanks for the reviews. I’ll look at addressing nits in a follow-up.
  41. jnewbery commented at 4:48 pm on November 1, 2018: member
    @Empact @promag - I’ve addressed the remaining nits in https://github.com/jnewbery/bitcoin/tree/13566_nits, but there isn’t much in there. I’m not sure if there’s much point in opening a new PR.
  42. Empact commented at 2:21 pm on November 2, 2018: member
    Yeah I would leave it as-is at this point.
  43. promag commented at 2:34 pm on November 2, 2018: member
    @jnewbery agree, not worth it.
  44. jnewbery deleted the branch on Nov 2, 2018
  45. jnewbery commented at 3:34 pm on November 2, 2018: member
    Agree. Thanks for the input. Sorry those changes didn’t make it!
  46. xdustinface referenced this in commit 9a3d528e1b on Dec 22, 2020
  47. xdustinface referenced this in commit 341b5a9714 on Dec 22, 2020
  48. zkbot referenced this in commit 5b194067ea on Aug 12, 2021
  49. MarcoFalke locked this on Sep 8, 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: 2024-12-18 18:12 UTC

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