Skip to content

Commit a653f4b

Browse files
committed
Merge bitcoin/bitcoin#25934: wallet, rpc: add label to listsinceblock
4e362c2 doc: add release note for 25934 (brunoerg) fe488b4 test: add coverage for `label` in `listsinceblock` (brunoerg) 722e9a4 wallet, rpc: add `label` to `listsinceblock` (brunoerg) 852891f refactor, wallet: use optional for `label` in `ListTransactions` (brunoerg) Pull request description: This PR adds `label` parameter to `listsinceblock` to be able to fetch all incoming transactions having the specified label since a specific block. It's possible to use it in `listtransactions`, however, it's only possible to set the number of transactions to return, not a specific block to fetch from. `getreceivedbylabel` only returns the total amount received, not the txs info. `listreceivedbylabel` doesn't list all the informations about the transactions and it's not possible to fetch since a block. ACKs for top commit: achow101: ACK 4e362c2 w0xlt: ACK bitcoin/bitcoin@4e362c2 aureleoules: ACK 4e362c2 Tree-SHA512: fbde5db8cebf7a27804154fa61997b5155ad512e978cebb78c17acab9efcb624ea5f39d649899d12e5e675f80d4d0064cae8132b864de0d93a8d1e6fbcb9a737
2 parents bbfcbcf + 4e362c2 commit a653f4b

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

doc/release-notes-25934.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Low-level changes
2+
=================
3+
4+
RPC
5+
---
6+
7+
- RPC `listsinceblock` now accepts an optional `label` argument
8+
to fetch incoming transactions having the specified label. (#25934)

src/wallet/rpc/transactions.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
316316
*/
317317
template <class Vec>
318318
static void ListTransactions(const CWallet& wallet, const CWalletTx& wtx, int nMinDepth, bool fLong,
319-
Vec& ret, const isminefilter& filter_ismine, const std::string* filter_label,
319+
Vec& ret, const isminefilter& filter_ismine, const std::optional<std::string>& filter_label,
320320
bool include_change = false)
321321
EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
322322
{
@@ -329,7 +329,7 @@ static void ListTransactions(const CWallet& wallet, const CWalletTx& wtx, int nM
329329
bool involvesWatchonly = CachedTxIsFromMe(wallet, wtx, ISMINE_WATCH_ONLY);
330330

331331
// Sent
332-
if (!filter_label)
332+
if (!filter_label.has_value())
333333
{
334334
for (const COutputEntry& s : listSent)
335335
{
@@ -362,7 +362,7 @@ static void ListTransactions(const CWallet& wallet, const CWalletTx& wtx, int nM
362362
if (address_book_entry) {
363363
label = address_book_entry->GetLabel();
364364
}
365-
if (filter_label && label != *filter_label) {
365+
if (filter_label.has_value() && label != filter_label.value()) {
366366
continue;
367367
}
368368
UniValue entry(UniValue::VOBJ);
@@ -485,10 +485,10 @@ RPCHelpMan listtransactions()
485485
// the user could have gotten from another RPC command prior to now
486486
pwallet->BlockUntilSyncedToCurrentChain();
487487

488-
const std::string* filter_label = nullptr;
488+
std::optional<std::string> filter_label;
489489
if (!request.params[0].isNull() && request.params[0].get_str() != "*") {
490-
filter_label = &request.params[0].get_str();
491-
if (filter_label->empty()) {
490+
filter_label = request.params[0].get_str();
491+
if (filter_label.value().empty()) {
492492
throw JSONRPCError(RPC_INVALID_PARAMETER, "Label argument must be a valid label name or \"*\".");
493493
}
494494
}
@@ -552,6 +552,7 @@ RPCHelpMan listsinceblock()
552552
{"include_removed", RPCArg::Type::BOOL, RPCArg::Default{true}, "Show transactions that were removed due to a reorg in the \"removed\" array\n"
553553
"(not guaranteed to work on pruned nodes)"},
554554
{"include_change", RPCArg::Type::BOOL, RPCArg::Default{false}, "Also add entries for change outputs.\n"},
555+
{"label", RPCArg::Type::STR, RPCArg::Optional::OMITTED_NAMED_ARG, "Return only incoming transactions paying to addresses with the specified label.\n"},
555556
},
556557
RPCResult{
557558
RPCResult::Type::OBJ, "", "",
@@ -634,6 +635,11 @@ RPCHelpMan listsinceblock()
634635
bool include_removed = (request.params[3].isNull() || request.params[3].get_bool());
635636
bool include_change = (!request.params[4].isNull() && request.params[4].get_bool());
636637

638+
std::optional<std::string> filter_label;
639+
if (!request.params[5].isNull()) {
640+
filter_label = request.params[5].get_str();
641+
}
642+
637643
int depth = height ? wallet.GetLastBlockHeight() + 1 - *height : -1;
638644

639645
UniValue transactions(UniValue::VARR);
@@ -642,7 +648,7 @@ RPCHelpMan listsinceblock()
642648
const CWalletTx& tx = pairWtx.second;
643649

644650
if (depth == -1 || abs(wallet.GetTxDepthInMainChain(tx)) < depth) {
645-
ListTransactions(wallet, tx, 0, true, transactions, filter, /*filter_label=*/nullptr, /*include_change=*/include_change);
651+
ListTransactions(wallet, tx, 0, true, transactions, filter, filter_label, include_change);
646652
}
647653
}
648654

@@ -659,7 +665,7 @@ RPCHelpMan listsinceblock()
659665
if (it != wallet.mapWallet.end()) {
660666
// We want all transactions regardless of confirmation count to appear here,
661667
// even negative confirmation ones, hence the big negative.
662-
ListTransactions(wallet, it->second, -100000000, true, removed, filter, /*filter_label=*/nullptr, /*include_change=*/include_change);
668+
ListTransactions(wallet, it->second, -100000000, true, removed, filter, filter_label, include_change);
663669
}
664670
}
665671
blockId = block.hashPrevBlock;
@@ -777,7 +783,7 @@ RPCHelpMan gettransaction()
777783
WalletTxToJSON(*pwallet, wtx, entry);
778784

779785
UniValue details(UniValue::VARR);
780-
ListTransactions(*pwallet, wtx, 0, false, details, filter, /*filter_label=*/nullptr);
786+
ListTransactions(*pwallet, wtx, 0, false, details, filter, /*filter_label=*/std::nullopt);
781787
entry.pushKV("details", details);
782788

783789
std::string strHex = EncodeHexTx(*wtx.tx, pwallet->chain().rpcSerializationFlags());

test/functional/wallet_listsinceblock.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def run_test(self):
4949
self.test_desc()
5050
self.test_send_to_self()
5151
self.test_op_return()
52+
self.test_label()
5253

5354
def test_no_blockhash(self):
5455
self.log.info("Test no blockhash")
@@ -465,6 +466,20 @@ def test_op_return(self):
465466

466467
assert 'address' not in op_ret_tx
467468

469+
def test_label(self):
470+
self.log.info('Test passing "label" argument fetches incoming transactions having the specified label')
471+
new_addr = self.nodes[1].getnewaddress(label="new_addr", address_type="bech32")
472+
473+
self.nodes[2].sendtoaddress(address=new_addr, amount="0.001")
474+
self.generate(self.nodes[2], 1)
475+
476+
for label in ["new_addr", ""]:
477+
new_addr_transactions = self.nodes[1].listsinceblock(label=label)["transactions"]
478+
assert_equal(len(new_addr_transactions), 1)
479+
assert_equal(new_addr_transactions[0]["label"], label)
480+
if label == "new_addr":
481+
assert_equal(new_addr_transactions[0]["address"], new_addr)
482+
468483

469484
if __name__ == '__main__':
470485
ListSinceBlockTest().main()

0 commit comments

Comments
 (0)