listreceivedbyaddress
& listreceivedbylabel
RPCs list balances by receiving address and label respectively. That works by creating a tally (mapTally[address]
) from all the transaction outputs in the wallet and then cross-reference that with the address book in order to calculate which addresses received funds.
If an address is in the address book but not in the tally, it’s considered empty (never received funds) and is not returned unless include_empty=true
. That effectively means that when include_empty=true
the entire address book is included in the result.
This creates 2 issues:
- As reported in #16159, the address book also includes addresses with a “send” purpose which should not be part of the response.
- As reported in #10468, if
include_watchonly=false
the watchonly addresses are not included inmapTally
leading to them being considered empty during the subsequent cross-reference with the address book thus becoming part of the result wheninclude_empty=true
.
This PR fixes #16159 by filtering-out “send” addresses, see #16159 (comment) for more details on the issue. I haven’t thought of a nice solution to fix (2) yet.
Edit: Split wallet_listreceivedby.py
tests for code maintainability as tests for more than one RPC have been added since the creation of the file. Added test coverage for the introduced change.