From fa9f2ab8fd53075d2a3ec93ddac4908e73525c46 Mon Sep 17 00:00:00 2001 From: furszy Date: Sat, 11 Jun 2022 11:46:14 -0300 Subject: [PATCH] refactor: RPC 'listlabels', encapsulate 'CWallet::ListAddrBookLabels' functionality Mainly to not access 'm_address_book' externally. --- src/wallet/rpc/addresses.cpp | 8 +------- src/wallet/wallet.cpp | 14 ++++++++++++++ src/wallet/wallet.h | 5 +++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/wallet/rpc/addresses.cpp b/src/wallet/rpc/addresses.cpp index 55dd2f935c..da4cc44ee6 100644 --- a/src/wallet/rpc/addresses.cpp +++ b/src/wallet/rpc/addresses.cpp @@ -733,13 +733,7 @@ RPCHelpMan listlabels() } // Add to a set to sort by label name, then insert into Univalue array - std::set label_set; - for (const std::pair& entry : pwallet->m_address_book) { - if (entry.second.IsChange()) continue; - if (purpose.empty() || entry.second.purpose == purpose) { - label_set.insert(entry.second.GetLabel()); - } - } + std::set label_set = pwallet->ListAddrBookLabels(purpose); UniValue ret(UniValue::VARR); for (const std::string& name : label_set) { diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index f7eb0bbc03..3c211fb348 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2373,6 +2373,20 @@ std::vector CWallet::ListAddrBookAddresses(const std::optional CWallet::ListAddrBookLabels(const std::string& purpose) const +{ + AssertLockHeld(cs_wallet); + std::set label_set; + ForEachAddrBookEntry([&](const CTxDestination& _dest, const std::string& _label, + const std::string& _purpose, bool _is_change) { + if (_is_change) return; + if (purpose.empty() || _purpose == purpose) { + label_set.insert(_label); + } + }); + return label_set; +} + bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool internal, bilingual_str& error) { m_spk_man = pwallet->GetScriptPubKeyMan(type, internal); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 3775f325ba..8bc1189bec 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -648,6 +648,11 @@ public: */ std::vector ListAddrBookAddresses(const std::optional& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + /** + * Retrieve all the known labels in the address book + */ + std::set ListAddrBookLabels(const std::string& purpose) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + /** * Walk-through the address book entries. * Stops when the provided 'ListAddrBookFunc' returns false.