diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 1e2d3750ef1a4..27917570fb94e 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -923,10 +923,13 @@ void WalletModel::listLockedCoins(std::vector& vOutpts) void WalletModel::loadReceiveRequests(std::vector& vReceiveRequests) { LOCK(wallet->cs_wallet); - for (const auto& item : wallet->mapAddressBook) - for (const PAIRTYPE(std::string, std::string) & item2 : item.second.destdata) + auto it = wallet->NewAddressBookIterator(); + while(it.HasNext()) { + for (const PAIRTYPE(std::string, std::string) &item2 : it.GetValue().destdata) if (item2.first.size() > 2 && item2.first.substr(0, 2) == "rr") // receive request vReceiveRequests.push_back(item2.second); + it.Next(); + } } bool WalletModel::saveReceiveRequest(const std::string& sAddress, const int64_t nId, const std::string& sRequest) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 32c2db3d37a41..9e84763874a3b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2056,24 +2056,21 @@ UniValue ListReceived(const UniValue& params, bool by_label) } } - // Reply - UniValue ret(UniValue::VARR); - std::map label_tally; // Create mapAddressBook iterator // If we aren't filtering, go from begin() to end() - auto start = pwalletMain->mapAddressBook.begin(); - auto end = pwalletMain->mapAddressBook.end(); + auto itAddrBook = pwalletMain->NewAddressBookIterator(); // If we are filtering, find() the applicable entry if (has_filtered_address) { - start = pwalletMain->mapAddressBook.find(filtered_address); - if (start != end) { - end = std::next(start); - } + itAddrBook.SetFilter(filtered_address); } - for (auto item_it = start; item_it != end; ++item_it) { - const CTxDestination& address = item_it->first; - const std::string& label = item_it->second.name; + // Reply + UniValue ret(UniValue::VARR); + std::map label_tally; + + do { + const auto& address = itAddrBook.GetKey(); + const std::string& label = itAddrBook.GetValue().name; auto it = mapTally.find(address); if (it == mapTally.end() && !fIncludeEmpty) continue; @@ -2114,7 +2111,7 @@ UniValue ListReceived(const UniValue& params, bool by_label) obj.push_back(Pair("txids", transactions)); ret.push_back(obj); } - } + } while (itAddrBook.Next()); if (by_label) { for (const auto& entry : label_tally) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 912350f36745f..3563600f5a2bc 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -247,19 +247,28 @@ struct CRecipient class CAddressBookIterator { public: - explicit CAddressBookIterator(std::map& _map) : map(_map), it(_map.begin()) {} + explicit CAddressBookIterator(std::map& _map) : map(_map), it(_map.begin()), itEnd(_map.end()) {} CTxDestination GetKey() { return it->first; } AddressBook::CAddressBookData GetValue() { return it->second; } - bool HasNext() { return it != map.end(); } + bool HasNext() { return it != itEnd; } bool Next() { - if (it == map.end()) return false; + if (!HasNext()) return false; it++; return true; } + void SetFilter(CTxDestination& filter) + { + it = map.find(filter); + if (it != itEnd) { + itEnd = std::next(it); + } + } + private: std::map& map; std::map::iterator it; + std::map::iterator itEnd; }; /**