wallet: implement ForEachAddrBookEntry method

This commit is contained in:
furszy 2022-06-11 11:35:14 -03:00
parent 09649bc95d
commit 032842ae41
No known key found for this signature in database
GPG Key ID: 5DD23CCC686AA623
2 changed files with 24 additions and 6 deletions

View File

@ -2348,17 +2348,28 @@ void CWallet::MarkDestinationsDirty(const std::set<CTxDestination>& destinations
} }
} }
void CWallet::ForEachAddrBookEntry(const ListAddrBookFunc& func) const
{
AssertLockHeld(cs_wallet);
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book) {
const auto& entry = item.second;
func(item.first, entry.GetLabel(), entry.purpose, entry.IsChange());
}
}
std::vector<CTxDestination> CWallet::ListAddrBookAddresses(const std::optional<AddrBookFilter>& _filter) const std::vector<CTxDestination> CWallet::ListAddrBookAddresses(const std::optional<AddrBookFilter>& _filter) const
{ {
AssertLockHeld(cs_wallet); AssertLockHeld(cs_wallet);
std::vector<CTxDestination> result; std::vector<CTxDestination> result;
AddrBookFilter filter = _filter ? *_filter : AddrBookFilter(); AddrBookFilter filter = _filter ? *_filter : AddrBookFilter();
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book) { ForEachAddrBookEntry([&result, &filter](const CTxDestination& dest, const std::string& label, const std::string& purpose, bool is_change) {
if (filter.ignore_change && item.second.IsChange()) continue; // Filter by change
const std::string& strName = item.second.GetLabel(); if (filter.ignore_change && is_change) return;
if (filter.m_op_label && *filter.m_op_label != strName) continue; // Filter by label
result.emplace_back(item.first); if (filter.m_op_label && *filter.m_op_label != label) return;
} // All good
result.emplace_back(dest);
});
return result; return result;
} }

View File

@ -648,6 +648,13 @@ public:
*/ */
std::vector<CTxDestination> ListAddrBookAddresses(const std::optional<AddrBookFilter>& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); std::vector<CTxDestination> ListAddrBookAddresses(const std::optional<AddrBookFilter>& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
/**
* Walk-through the address book entries.
* Stops when the provided 'ListAddrBookFunc' returns false.
*/
using ListAddrBookFunc = std::function<void(const CTxDestination& dest, const std::string& label, const std::string& purpose, bool is_change)>;
void ForEachAddrBookEntry(const ListAddrBookFunc& func) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
/** /**
* Marks all outputs in each one of the destinations dirty, so their cache is * Marks all outputs in each one of the destinations dirty, so their cache is
* reset and does not return outdated information. * reset and does not return outdated information.