[refactor] Merge getreceivedby tally into GetReceived function #17579

pull andrewtoth wants to merge 1 commits into bitcoin:master from andrewtoth:getreceivedby-refactor changing 1 files +48 −58
  1. andrewtoth commented at 6:43 pm on November 24, 2019: contributor
    This PR merges the tally code of getreceivedbyaddress and getreceivedbylabel into a single function GetReceived. This reduces repeated code and makes it similar to listreceivedbyaddress and listreceivedbylabel, which use the function ListReceived. It will also make the change in #14707 simpler and easier to review.
  2. fanquake added the label RPC/REST/ZMQ on Nov 24, 2019
  3. fanquake added the label Wallet on Nov 24, 2019
  4. in src/wallet/rpcwallet.cpp:586 in 2377697e36 outdated
    582@@ -583,6 +583,57 @@ static UniValue signmessage(const JSONRPCRequest& request)
    583     return EncodeBase64(vchSig.data(), vchSig.size());
    584 }
    585 
    586+static UniValue GetReceived(interfaces::Chain::Lock& locked_chain, CWallet * const wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet)
    


    MarcoFalke commented at 10:38 pm on November 24, 2019:
    style-nit: Instead of returning an UniValue, I’d prefer to return a CAmount, so that (if needed) the function can also be called from unit tests easily.

    andrewtoth commented at 11:12 pm on November 25, 2019:
    Done

    andrewtoth commented at 0:21 am on November 27, 2019:
    @MarcoFalke can you expand on how returning CAmount makes it easier to test? Is it due to not having UniValue as a dependency? In that case I should remove UniValue as a parameter as well.
  5. DrahtBot commented at 11:53 pm on November 24, 2019: contributor

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #18647 (rpc: remove g_rpc_node & g_rpc_chain by brakmic)

    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.

  6. andrewtoth force-pushed on Nov 25, 2019
  7. in src/wallet/rpcwallet.cpp:616 in 915a40aaec outdated
    623+        }
    624+
    625+        for (const CTxOut& txout : wtx.tx->vout) {
    626+            CTxDestination address;
    627+            if (ExtractDestination(txout.scriptPubKey, address) && wallet->IsMine(address) && addresses.count(address)) {
    628+                amount += txout.nValue;
    


    andrewtoth commented at 8:56 pm on December 2, 2019:
    This uses the multiple address case, used by getreceivedbylabel, and the singular case used by getreceivedbyaddress is no longer needed.
  8. in src/wallet/rpcwallet.cpp:586 in 915a40aaec outdated
    582@@ -583,6 +583,57 @@ static UniValue signmessage(const JSONRPCRequest& request)
    583     return EncodeBase64(vchSig.data(), vchSig.size());
    584 }
    585 
    586+static CAmount GetReceived(interfaces::Chain::Lock& locked_chain, CWallet * const wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet)
    


    theStack commented at 11:08 pm on January 23, 2020:
    Variable naming: s/wallet/pwallet (see e.g. ListReceived() and SendMoney() in the same file) to be more consistent
  9. in src/wallet/rpcwallet.cpp:633 in 915a40aaec outdated
    628+                amount += txout.nValue;
    629+            }
    630+        }
    631+    }
    632+
    633+    return  amount;
    


    theStack commented at 11:10 pm on January 23, 2020:
    nit: Remove extra space after return
  10. in src/wallet/rpcwallet.cpp:611 in 915a40aaec outdated
    618+        }
    619+
    620+        int depth = wtx.GetDepthInMainChain();
    621+        if (depth < min_depth) {
    622+            continue;
    623+        }
    


    theStack commented at 11:15 pm on January 23, 2020:

    More a matter of taste, but this could simply be put in the if condition above and without an extra variable (that is used only once anyway)? Like

    0if (wtx.IsCoinBase() || !locked_chain.checkFinalTx(*wtx.tx) || wtx.GetDepthInMainChain() < min_depth) {
    1    continue;
    2}
    
  11. in src/wallet/rpcwallet.cpp:674 in 915a40aaec outdated
    699-                if (wtx.GetDepthInMainChain() >= nMinDepth)
    700-                    nAmount += txout.nValue;
    701-    }
    702-
    703-    return  ValueFromAmount(nAmount);
    704+    return ValueFromAmount(GetReceived(*locked_chain, pwallet, request.params, false));
    


    theStack commented at 11:21 pm on January 23, 2020:
    nit: put /* by_label */ before boolean parameter false to increase readability.
  12. in src/wallet/rpcwallet.cpp:715 in 915a40aaec outdated
    738-            }
    739-        }
    740-    }
    741-
    742-    return ValueFromAmount(nAmount);
    743+    return ValueFromAmount(GetReceived(*locked_chain, pwallet, request.params, true));
    


    theStack commented at 11:21 pm on January 23, 2020:
    nit: put /* by_label */ before boolean parameter true to increase readability.
  13. theStack commented at 11:23 pm on January 23, 2020: contributor
    Concept ACK, left a view code review comments (mostly nits though)
  14. andrewtoth force-pushed on Jan 25, 2020
  15. andrewtoth commented at 1:27 am on January 25, 2020: contributor
    @theStack thank you for the review. I’ve addressed all comments.
  16. andrewtoth force-pushed on Jan 25, 2020
  17. andrewtoth force-pushed on Jan 26, 2020
  18. theStack approved
  19. sipa commented at 3:27 am on March 27, 2020: member

    utACK 4111967281a37cfe0d34802a80dba0715bfe3ffb

    Nice cleanup, and also nice to hoist the DepthInMainChain check out of the inner loop.

    Optionally, since this is more than a pure code move, some of the variable names/comments could be improved as well (the “addresses” variable name, as well as the comment referring to pubkeys are outdated).

  20. Merge getreceivedby tally into GetReceived function a1d5b12ec0
  21. in src/wallet/rpcwallet.cpp:586 in 4111967281 outdated
    582@@ -583,6 +583,52 @@ static UniValue signmessage(const JSONRPCRequest& request)
    583     return EncodeBase64(vchSig.data(), vchSig.size());
    584 }
    585 
    586+static CAmount GetReceived(interfaces::Chain::Lock& locked_chain, CWallet * const pwallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
    


    MarcoFalke commented at 4:24 pm on March 27, 2020:
    0static CAmount GetReceived(interfaces::Chain::Lock& locked_chain, const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
    
  22. andrewtoth force-pushed on Mar 27, 2020
  23. theStack approved
  24. meshcollider commented at 2:43 am on April 16, 2020: contributor
    utACK a1d5b12ec07d2f7aa9fa955a6dd99e8a2be5cb25
  25. MarcoFalke merged this on Apr 20, 2020
  26. MarcoFalke closed this on Apr 20, 2020

  27. in src/wallet/rpcwallet.cpp:603 in a1d5b12ec0
    598+    }
    599+
    600+    // Minimum confirmations
    601+    int min_depth = 1;
    602+    if (!params[1].isNull())
    603+        min_depth = params[1].get_int();
    


    MarcoFalke commented at 2:15 pm on April 20, 2020:
    0    const int min_depth{params[1].isNull() ? 1 : params[1].get_int()};
    

    can be written shorter

  28. sidhujag referenced this in commit 3782e79671 on Apr 20, 2020
  29. jasonbcox referenced this in commit fa57b76ebd on Oct 15, 2020
  30. bitcoin locked this on Feb 15, 2022
  31. andrewtoth deleted the branch on Aug 17, 2023

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-07-01 13:12 UTC

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