Skip to content

Commit 032842a

Browse files
committed
wallet: implement ForEachAddrBookEntry method
1 parent 09649bc commit 032842a

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/wallet/wallet.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,17 +2348,28 @@ void CWallet::MarkDestinationsDirty(const std::set<CTxDestination>& destinations
23482348
}
23492349
}
23502350

2351+
void CWallet::ForEachAddrBookEntry(const ListAddrBookFunc& func) const
2352+
{
2353+
AssertLockHeld(cs_wallet);
2354+
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book) {
2355+
const auto& entry = item.second;
2356+
func(item.first, entry.GetLabel(), entry.purpose, entry.IsChange());
2357+
}
2358+
}
2359+
23512360
std::vector<CTxDestination> CWallet::ListAddrBookAddresses(const std::optional<AddrBookFilter>& _filter) const
23522361
{
23532362
AssertLockHeld(cs_wallet);
23542363
std::vector<CTxDestination> result;
23552364
AddrBookFilter filter = _filter ? *_filter : AddrBookFilter();
2356-
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book) {
2357-
if (filter.ignore_change && item.second.IsChange()) continue;
2358-
const std::string& strName = item.second.GetLabel();
2359-
if (filter.m_op_label && *filter.m_op_label != strName) continue;
2360-
result.emplace_back(item.first);
2361-
}
2365+
ForEachAddrBookEntry([&result, &filter](const CTxDestination& dest, const std::string& label, const std::string& purpose, bool is_change) {
2366+
// Filter by change
2367+
if (filter.ignore_change && is_change) return;
2368+
// Filter by label
2369+
if (filter.m_op_label && *filter.m_op_label != label) return;
2370+
// All good
2371+
result.emplace_back(dest);
2372+
});
23622373
return result;
23632374
}
23642375

src/wallet/wallet.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,13 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
648648
*/
649649
std::vector<CTxDestination> ListAddrBookAddresses(const std::optional<AddrBookFilter>& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
650650

651+
/**
652+
* Walk-through the address book entries.
653+
* Stops when the provided 'ListAddrBookFunc' returns false.
654+
*/
655+
using ListAddrBookFunc = std::function<void(const CTxDestination& dest, const std::string& label, const std::string& purpose, bool is_change)>;
656+
void ForEachAddrBookEntry(const ListAddrBookFunc& func) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
657+
651658
/**
652659
* Marks all outputs in each one of the destinations dirty, so their cache is
653660
* reset and does not return outdated information.

0 commit comments

Comments
 (0)