Skip to content

Commit

Permalink
Add filter to CAddressBookIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Sep 18, 2020
1 parent 65cec73 commit 08dc9ad
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,10 +923,13 @@ void WalletModel::listLockedCoins(std::vector<COutPoint>& vOutpts)
void WalletModel::loadReceiveRequests(std::vector<std::string>& 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)
Expand Down
23 changes: 10 additions & 13 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2056,24 +2056,21 @@ UniValue ListReceived(const UniValue& params, bool by_label)
}
}

// Reply
UniValue ret(UniValue::VARR);
std::map<std::string, tallyitem> 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<std::string, tallyitem> 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;
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 12 additions & 3 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,28 @@ struct CRecipient
class CAddressBookIterator
{
public:
explicit CAddressBookIterator(std::map<CTxDestination, AddressBook::CAddressBookData>& _map) : map(_map), it(_map.begin()) {}
explicit CAddressBookIterator(std::map<CTxDestination, AddressBook::CAddressBookData>& _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<CTxDestination, AddressBook::CAddressBookData>& map;
std::map<CTxDestination, AddressBook::CAddressBookData>::iterator it;
std::map<CTxDestination, AddressBook::CAddressBookData>::iterator itEnd;
};

/**
Expand Down

0 comments on commit 08dc9ad

Please sign in to comment.