refactor: use 'ForEachAddrBookEntry' in RPC 'getaddressesbylabel'

This commit is contained in:
furszy 2022-06-11 11:39:25 -03:00
parent 2b48642499
commit 83e42c4b94
No known key found for this signature in database
GPG Key ID: 5DD23CCC686AA623

View File

@ -637,17 +637,6 @@ RPCHelpMan getaddressinfo()
}; };
} }
/** Convert CAddressBookData to JSON record. */
static UniValue AddressBookDataToJSON(const CAddressBookData& data, const bool verbose)
{
UniValue ret(UniValue::VOBJ);
if (verbose) {
ret.pushKV("name", data.GetLabel());
}
ret.pushKV("purpose", data.purpose);
return ret;
}
RPCHelpMan getaddressesbylabel() RPCHelpMan getaddressesbylabel()
{ {
return RPCHelpMan{"getaddressesbylabel", return RPCHelpMan{"getaddressesbylabel",
@ -680,10 +669,10 @@ RPCHelpMan getaddressesbylabel()
// Find all addresses that have the given label // Find all addresses that have the given label
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
std::set<std::string> addresses; std::set<std::string> addresses;
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->m_address_book) { pwallet->ForEachAddrBookEntry([&](const CTxDestination& _dest, const std::string& _label, const std::string& _purpose, bool _is_change) {
if (item.second.IsChange()) continue; if (_is_change) return;
if (item.second.GetLabel() == label) { if (_label == label) {
std::string address = EncodeDestination(item.first); std::string address = EncodeDestination(_dest);
// CWallet::m_address_book is not expected to contain duplicate // CWallet::m_address_book is not expected to contain duplicate
// address strings, but build a separate set as a precaution just in // address strings, but build a separate set as a precaution just in
// case it does. // case it does.
@ -693,9 +682,11 @@ RPCHelpMan getaddressesbylabel()
// and since duplicate addresses are unexpected (checked with // and since duplicate addresses are unexpected (checked with
// std::set in O(log(N))), UniValue::__pushKV is used instead, // std::set in O(log(N))), UniValue::__pushKV is used instead,
// which currently is O(1). // which currently is O(1).
ret.__pushKV(address, AddressBookDataToJSON(item.second, false)); UniValue value(UniValue::VOBJ);
value.pushKV("purpose", _purpose);
ret.__pushKV(address, value);
} }
} });
if (ret.empty()) { if (ret.empty()) {
throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label)); throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label));