refactor: implement general 'ListAddrBookAddresses' for addressbook destinations lookup

This commit is contained in:
furszy 2022-06-11 11:06:22 -03:00
parent 192eb1e61c
commit 09649bc95d
No known key found for this signature in database
GPG Key ID: 5DD23CCC686AA623
3 changed files with 22 additions and 12 deletions

View File

@ -18,10 +18,10 @@
namespace wallet {
static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
{
std::set<CTxDestination> addresses;
std::vector<CTxDestination> addresses;
if (by_label) {
// Get the set of addresses assigned to label
addresses = wallet.GetLabelAddresses(LabelFromValue(params[0]));
addresses = wallet.ListAddrBookAddresses(CWallet::AddrBookFilter{LabelFromValue(params[0])});
if (addresses.empty()) throw JSONRPCError(RPC_WALLET_ERROR, "Label not found in wallet");
} else {
// Get the address
@ -29,7 +29,7 @@ static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool b
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
}
addresses.insert(dest);
addresses.emplace_back(dest);
}
// Filter by own scripts only

View File

@ -2348,17 +2348,16 @@ void CWallet::MarkDestinationsDirty(const std::set<CTxDestination>& destinations
}
}
std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) const
std::vector<CTxDestination> CWallet::ListAddrBookAddresses(const std::optional<AddrBookFilter>& _filter) const
{
AssertLockHeld(cs_wallet);
std::set<CTxDestination> result;
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book)
{
if (item.second.IsChange()) continue;
const CTxDestination& address = item.first;
std::vector<CTxDestination> result;
AddrBookFilter filter = _filter ? *_filter : AddrBookFilter();
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book) {
if (filter.ignore_change && item.second.IsChange()) continue;
const std::string& strName = item.second.GetLabel();
if (strName == label)
result.insert(address);
if (filter.m_op_label && *filter.m_op_label != strName) continue;
result.emplace_back(item.first);
}
return result;
}

View File

@ -635,7 +635,18 @@ public:
std::optional<int64_t> GetOldestKeyPoolTime() const;
std::set<CTxDestination> GetLabelAddresses(const std::string& label) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
// Filter struct for 'ListAddrBookAddresses'
struct AddrBookFilter {
// Fetch addresses with the provided label
std::optional<std::string> m_op_label{std::nullopt};
// Don't include change addresses by default
bool ignore_change{true};
};
/**
* Filter and retrieve destinations stored in the addressbook
*/
std::vector<CTxDestination> ListAddrBookAddresses(const std::optional<AddrBookFilter>& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
/**
* Marks all outputs in each one of the destinations dirty, so their cache is