Skip to content

Commit 5ebff43

Browse files
committed
Merge bitcoin/bitcoin#25122: rpc: getreceivedbylabel, return early if no addresses were found in the address book
baa3ddc doc: add release notes about `getreceivedbylabel` returning an error if the label is not in the address book. (furszy) 8897a21 rpc: getreceivedbylabel, don't loop over the entire wallet txs map if no destinations were found for the input label. (furszy) Pull request description: Built on top of #23662, coming from comment bitcoin/bitcoin#23662 (review). If `wallet.GetLabelAddresses()` returns an empty vector (the wallet does not have stored destinations with that label in the addressbook) or if none of the returned destinations are from the wallet, we can return the function right away. Otherwise, we are walking through all the wallet txs + outputs for no reason (`output_scripts` is empty). ACKs for top commit: achow101: ACK baa3ddc theStack: re-ACK baa3ddc w0xlt: ACK bitcoin/bitcoin@baa3ddc Tree-SHA512: 00e10365b179bf008da2f3ef8fbb3ee04a330426374020e3f2d0151b16991baba4ef2b944e4659452f3e4d6cb20f128d0918ddf0453933a25a4d9fd8414a1911
2 parents 66e3b16 + baa3ddc commit 5ebff43

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

doc/release-notes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ Tools and Utilities
8585
Wallet
8686
------
8787

88+
- RPC `getreceivedbylabel` now returns an error, "Label not found
89+
in wallet" (-4), if the label is not in the address book. (#25122)
90+
8891
GUI changes
8992
-----------
9093

src/wallet/rpc/coins.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,31 @@
1818
namespace wallet {
1919
static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
2020
{
21-
std::set<CScript> output_scripts;
22-
21+
std::set<CTxDestination> addresses;
2322
if (by_label) {
2423
// Get the set of addresses assigned to label
25-
std::string label = LabelFromValue(params[0]);
26-
for (const auto& address : wallet.GetLabelAddresses(label)) {
27-
auto output_script{GetScriptForDestination(address)};
28-
if (wallet.IsMine(output_script)) {
29-
output_scripts.insert(output_script);
30-
}
31-
}
24+
addresses = wallet.GetLabelAddresses(LabelFromValue(params[0]));
25+
if (addresses.empty()) throw JSONRPCError(RPC_WALLET_ERROR, "Label not found in wallet");
3226
} else {
3327
// Get the address
3428
CTxDestination dest = DecodeDestination(params[0].get_str());
3529
if (!IsValidDestination(dest)) {
3630
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
3731
}
38-
CScript script_pub_key = GetScriptForDestination(dest);
39-
if (!wallet.IsMine(script_pub_key)) {
40-
throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
32+
addresses.insert(dest);
33+
}
34+
35+
// Filter by own scripts only
36+
std::set<CScript> output_scripts;
37+
for (const auto& address : addresses) {
38+
auto output_script{GetScriptForDestination(address)};
39+
if (wallet.IsMine(output_script)) {
40+
output_scripts.insert(output_script);
4141
}
42-
output_scripts.insert(script_pub_key);
42+
}
43+
44+
if (output_scripts.empty()) {
45+
throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
4346
}
4447

4548
// Minimum confirmations

test/functional/wallet_listreceivedby.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ def run_test(self):
129129
txid = self.nodes[0].sendtoaddress(addr, 0.1)
130130
self.sync_all()
131131

132+
# getreceivedbylabel returns an error if the wallet doesn't own the label
133+
assert_raises_rpc_error(-4, "Label not found in wallet", self.nodes[0].getreceivedbylabel, "dummy")
134+
132135
# listreceivedbylabel should return received_by_label_json because of 0 confirmations
133136
assert_array_result(self.nodes[1].listreceivedbylabel(),
134137
{"label": label},

0 commit comments

Comments
 (0)