Skip to content

Wallet: "listreceivedby*" fix - #30972

Closed
BrandonOdiwuor wants to merge 3 commits into
bitcoin:masterfrom
BrandonOdiwuor:wallet-listreceivedby-fix
Closed

BrandonOdiwuor wants to merge 3 commits into
bitcoin:masterfrom
BrandonOdiwuor:wallet-listreceivedby-fix

Conversation

@BrandonOdiwuor

Copy link
Copy Markdown
Contributor

Fixes #16159,

This PR builds on #25973, fixing listreceivedby* RPCs by filtering out send addresses using IsMine (see #25973 (comment)). It also breaks down the listreceivedby tests into subtests and adds a test to verify 'listreceivedby*' does not return send addresses

@DrahtBot

DrahtBot commented Sep 25, 2024 •

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

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

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK achow101, rkrux

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34188 (test: Add multiple transactions and error handling tests for getreceivedbyaddress by b-l-u-e)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • “changes addresses” -> “change addresses” [“change addresses” is the correct term for output addresses, “changes” is a verb here and incorrect]

drahtbot_id_4_m

@BrandonOdiwuor
BrandonOdiwuor marked this pull request as draft September 25, 2024 17:08
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Debug: https://github.com/bitcoin/bitcoin/runs/30659059850

Hints

Make sure to run all tests locally, according to the documentation.

The failure may 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.

@BrandonOdiwuor
BrandonOdiwuor force-pushed the wallet-listreceivedby-fix branch from 209303b to a35115f Compare September 26, 2024 04:24
@BrandonOdiwuor BrandonOdiwuor changed the title Wallet listreceivedby fix Wallet: "listreceivedby*" fix Sep 26, 2024
@BrandonOdiwuor
BrandonOdiwuor marked this pull request as ready for review September 26, 2024 05:09
@DrahtBot DrahtBot added Wallet and removed CI failed labels Sep 26, 2024
@polespinasa

polespinasa commented Nov 13, 2024 •

Copy link
Copy Markdown
Member

Lgtm
Tested ack a35115f17caf550326e12ff3044fe617a1a53fd3

I ran the unit test and functional test (without bdb) and all pass.
Also tested manually using regtest the steps in function test_listreceivedby and the behaviour is the expected.

@fanquake

Copy link
Copy Markdown
Member

Maybe @achow101 or @furszy want to review here?

Comment thread src/wallet/rpc/transactions.cpp Outdated
Comment on lines 144 to 146

@furszy furszy Mar 11, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the mapTally loading procedure already performs the IsMine check, we could use that instead of executing it again for the addresses that received some coins.
In other words, if an element exists in mapTally, we can be certain that everything inside it belongs to the wallet.

So, ideally, we could decouple the mapTally existence check from the "include empty" check (the one that is just below this line), which would avoid this second IsMine() call for the non-empty addresses (we would need to execute it for the addresses that have no associated value in mapTally).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code to only do the second IsMine check if the address is not in mapTally

@achow101

achow101 commented Jul 1, 2025

Copy link
Copy Markdown
Member

Concept ACK

@BrandonOdiwuor
BrandonOdiwuor force-pushed the wallet-listreceivedby-fix branch from 61098f2 to 486cd47 Compare August 19, 2025 13:48
@BrandonOdiwuor

Copy link
Copy Markdown
Contributor Author

Rebased and updated to address #30972 (comment)

@rkrux rkrux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK 486cd47

IMO it'd be a good idea to rebase this branch over master so that the changes of #32523 PR are incorporated that removes the outdated ismine enum which is seen quite a lot in this branch currently while reviewing.

if (it == mapTally.end() && !fIncludeEmpty) {
return;
} else if (it == mapTally.end()) {
LOCK(wallet.cs_wallet);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a1d90fb "wallet: 'Filter' out 'send' addresses from 'listreceivedby*'"

Instead of trying to lock again, assert that the function is already holding the lock? I checked that both the callers of the ListReceived function are acquiring the required lock.

--- a/src/wallet/rpc/transactions.cpp
+++ b/src/wallet/rpc/transactions.cpp
@@ -73,6 +73,8 @@ struct tallyitem
 
 static UniValue ListReceived(const CWallet& wallet, const UniValue& params, const bool by_label, const bool include_immature_coinbase) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
 {
+    AssertLockHeld(wallet.cs_wallet);
+
     // Minimum confirmations
     int nMinDepth = 1;
     if (!params[0].isNull())
@@ -131,14 +133,13 @@ static UniValue ListReceived(const CWallet& wallet, const UniValue& params, cons
     UniValue ret(UniValue::VARR);
     std::map<std::string, tallyitem> label_tally;
 
-    const auto& func = [&](const CTxDestination& address, const std::string& label, bool is_change, const std::optional<AddressPurpose>& purpose) {
+    const auto& func = [&](const CTxDestination& address, const std::string& label, bool is_change, const std::optional<AddressPurpose>& purpose) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) {
         if (is_change) return; // no change addresses
 
         auto it = mapTally.find(address);
         if (it == mapTally.end() && !fIncludeEmpty) {
             return;
         } else if (it == mapTally.end()) {
-            LOCK(wallet.cs_wallet);
             if (!wallet.IsMine(address)) return; // no send addresses
         }
 


auto it = mapTally.find(address);
if (it == mapTally.end() && !fIncludeEmpty)
if (it == mapTally.end() && !fIncludeEmpty) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a1d90fb "wallet: 'Filter' out 'send' addresses from 'listreceivedby*'"

For making it concise.

--- a/src/wallet/rpc/transactions.cpp
+++ b/src/wallet/rpc/transactions.cpp
@@ -73,6 +73,8 @@ struct tallyitem
         auto it = mapTally.find(address);
-        if (it == mapTally.end() && !fIncludeEmpty) {
-            return;
-        } else if (it == mapTally.end()) {
-            LOCK(wallet.cs_wallet);
+        if (it == mapTally.end()) {
+            if (!fIncludeEmpty) return;
             if (!wallet.IsMine(address)) return; // no send addresses
         }

Comment on lines +122 to +127
assert_array_result(self.nodes[1].listreceivedbyaddress(minconf=0, include_empty=True), {"address": address}, {}, True)

self.log.info("Tests listreceivedbylabel does not return label of address with 'send' purpose")
expected = {"label": label, "amount": Decimal("0.1")}
assert_array_result(self.nodes[0].listreceivedbylabel(minconf=0, include_empty=True), {"label": label}, expected)
assert_array_result(self.nodes[1].listreceivedbylabel(minconf=0, include_empty=True), {"label": label}, {}, True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 486cd47 "test: 'listreceivedby*' does not return send address"

Nit (feel free to ignore): For expressiveness.

--- a/test/functional/wallet_listreceivedby.py
+++ b/test/functional/wallet_listreceivedby.py
@@ -119,12 +119,12 @@ class ReceivedByTest(BitcoinTestFramework):
 
         expected = {"address": address, "label": label, "amount": Decimal("0.1"), "confirmations": 0, "txids": [txid, ]}
         assert_array_result(self.nodes[0].listreceivedbyaddress(minconf=0, include_empty=True), {"address": address}, expected)
-        assert_array_result(self.nodes[1].listreceivedbyaddress(minconf=0, include_empty=True), {"address": address}, {}, True)
+        assert_array_result(self.nodes[1].listreceivedbyaddress(minconf=0, include_empty=True), {"address": address}, {}, should_not_find=True)
 
         self.log.info("Tests listreceivedbylabel does not return label of address with 'send' purpose")
         expected = {"label": label, "amount": Decimal("0.1")}
         assert_array_result(self.nodes[0].listreceivedbylabel(minconf=0, include_empty=True), {"label": label}, expected)
-        assert_array_result(self.nodes[1].listreceivedbylabel(minconf=0, include_empty=True), {"label": label}, {}, True)
+        assert_array_result(self.nodes[1].listreceivedbylabel(minconf=0, include_empty=True), {"label": label}, {}, should_not_find=True)
 

label = "node0address"
address = self.nodes[0].getnewaddress(label)
# using setlabel for an address that does not belong to the wallet assigns a "send" purpose to that address
self.nodes[1].setlabel(address, label) # address has now assigned a "send" purpose on node1

@rkrux rkrux Nov 5, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 486cd47 "test: 'listreceivedby*' does not return send address"

- self.nodes[1].setlabel(address, label) # address has now assigned a "send" purpose on node1
+ self.nodes[1].setlabel(address, label) # address has now been assigned a "send" purpose in node1's wallet

assert_array_result(self.nodes[1].listreceivedbyaddress(0, True), {"address": empty_addr}, expected_tx)

# No returned addy should be a change addr
self.log.info("- does not return changes addresses")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“changes addresses” -> “change addresses” [“change addresses” is the correct term for output addresses, “changes” is a verb here and incorrect]

@achow101

Copy link
Copy Markdown
Member

b419d17 "test: split 'listreceivedby' tests in subtests" contains a large number of stylistic changes that I don't think are meaningfully useful.

@DrahtBot

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

@fanquake
fanquake marked this pull request as draft March 3, 2026 13:33
@fanquake

fanquake commented Mar 3, 2026

Copy link
Copy Markdown
Member

@BrandonOdiwuor Are you still working on this?

@fanquake

Copy link
Copy Markdown
Member

Closing for now. Let us know if you're still working on this, and planning to rebase/address all the outstanding feedback.

@fanquake fanquake closed this May 20, 2026
achow101 added a commit that referenced this pull request Aug 10, 2026
…vedby*

089c883 test: Add coverage for listreceivedby* excluding "send" addresses (pablomartin4btc)
873c054 wallet: Exclude non-owned addresses from listreceivedby* (pablomartin4btc)

Pull request description:

  Fixes #16159.

  `listreceivedbyaddress`/`listreceivedbylabel` with `include_empty=true` walk the entire address book and return every entry that has no matching `mapTally` record — including addresses with a "send" purpose (foreign addresses that got a label via `setlabel`, the GUI, or `addmultisigaddress`) that this wallet never received funds to and doesn't own.

  This excludes those via `IsMine()` rather than the address book's `purpose` field, since `purpose` is set inconsistently across several code paths and `IsMine()` is the same check `mapTally` itself is already built from.

  Picks up prior work by kouloumos in #25973 and BrandonOdiwuor in #30972, both closed for
  inactivity:
  - [#25973](#25973) filtered on `purpose == "send"` directly. ryanofsky pointed out purpose "is set pretty haphazardly in code" and [suggested](#25973 (comment)) `IsMine()` instead.
  - [#30972](#30972) implemented that, then furszy pointed out `IsMine()` only needs to run for addresses missing from `mapTally`, not every one. rkrux further suggested dropping the redundant re-lock in favor of `EXCLUSIVE_LOCKS_REQUIRED` directly on the lambda — matching the existing pattern in `wallet/interfaces.cpp` — and simplifying the branching.

  This PR carries that final approach forward on current master. The regression test is a small, standalone addition rather than reviving the test-file "split into subtests" refactor from the earlier PRs, which achow101 [flagged](#30972 (comment)) on #30972 as unrelated stylistic churn.

ACKs for top commit:
  polespinasa:
    lgtm re-ACK 089c883
  jeanpablojp:
    ACK 089c883
  achow101:
    ACK 089c883

Tree-SHA512: d45488c93b9294258faaab5d1891ca5e8c4b8d0d4feb298403c7c3f20d6aa08989d548cddd25ccd47a1ed969e4a309ee68ad1541c6121fed39ed534c78c256e7
Kino1994 pushed a commit to Kino1994/bitcoin-full-history that referenced this pull request Aug 19, 2026
… from listreceivedby*

089c883c558e01c2a18a92f861fa7de9a7cc607a test: Add coverage for listreceivedby* excluding "send" addresses (pablomartin4btc)
873c0548059b2860b3718551078763d227186bd6 wallet: Exclude non-owned addresses from listreceivedby* (pablomartin4btc)

Pull request description:

  Fixes #16159.

  `listreceivedbyaddress`/`listreceivedbylabel` with `include_empty=true` walk the entire address book and return every entry that has no matching `mapTally` record — including addresses with a "send" purpose (foreign addresses that got a label via `setlabel`, the GUI, or `addmultisigaddress`) that this wallet never received funds to and doesn't own.

  This excludes those via `IsMine()` rather than the address book's `purpose` field, since `purpose` is set inconsistently across several code paths and `IsMine()` is the same check `mapTally` itself is already built from.

  Picks up prior work by kouloumos in #25973 and BrandonOdiwuor in #30972, both closed for
  inactivity:
  - [#25973](bitcoin/bitcoin#25973) filtered on `purpose == "send"` directly. ryanofsky pointed out purpose "is set pretty haphazardly in code" and [suggested](bitcoin/bitcoin#25973 (comment)) `IsMine()` instead.
  - [#30972](bitcoin/bitcoin#30972) implemented that, then furszy pointed out `IsMine()` only needs to run for addresses missing from `mapTally`, not every one. rkrux further suggested dropping the redundant re-lock in favor of `EXCLUSIVE_LOCKS_REQUIRED` directly on the lambda — matching the existing pattern in `wallet/interfaces.cpp` — and simplifying the branching.

  This PR carries that final approach forward on current master. The regression test is a small, standalone addition rather than reviving the test-file "split into subtests" refactor from the earlier PRs, which achow101 [flagged](bitcoin/bitcoin#30972 (comment)) on #30972 as unrelated stylistic churn.

ACKs for top commit:
  polespinasa:
    lgtm re-ACK 089c883c558e01c2a18a92f861fa7de9a7cc607a
  jeanpablojp:
    ACK 089c883c558e01c2a18a92f861fa7de9a7cc607a
  achow101:
    ACK 089c883c558e01c2a18a92f861fa7de9a7cc607a

Tree-SHA512: d45488c93b9294258faaab5d1891ca5e8c4b8d0d4feb298403c7c3f20d6aa08989d548cddd25ccd47a1ed969e4a309ee68ad1541c6121fed39ed534c78c256e7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

listrecievedbyaddress with include_empty not filtering out "send" side of address book

8 participants