Address nits in ADDR caching

pull/764/head
Gleb Naumenko 4 years ago
parent 81b00f8780
commit 83ad65f31b

@ -2550,10 +2550,10 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
.Write(local_socket_bytes.data(), local_socket_bytes.size()) .Write(local_socket_bytes.data(), local_socket_bytes.size())
.Finalize(); .Finalize();
const auto current_time = GetTime<std::chrono::microseconds>(); const auto current_time = GetTime<std::chrono::microseconds>();
if (m_addr_response_caches.find(cache_id) == m_addr_response_caches.end() || auto r = m_addr_response_caches.emplace(cache_id, CachedAddrResponse{});
m_addr_response_caches[cache_id].m_update_addr_response < current_time) { CachedAddrResponse& cache_entry = r.first->second;
m_addr_response_caches[cache_id].m_addrs_response_cache = GetAddresses(max_addresses, max_pct); if (cache_entry.m_cache_entry_expiration < current_time) { // If emplace() added new one it has expiration 0.
cache_entry.m_addrs_response_cache = GetAddresses(max_addresses, max_pct);
// Choosing a proper cache lifetime is a trade-off between the privacy leak minimization // Choosing a proper cache lifetime is a trade-off between the privacy leak minimization
// and the usefulness of ADDR responses to honest users. // and the usefulness of ADDR responses to honest users.
// //
@ -2578,9 +2578,9 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
// nodes to be "terrible" (see IsTerrible()) if the timestamps are older than 30 days, // nodes to be "terrible" (see IsTerrible()) if the timestamps are older than 30 days,
// max. 24 hours of "penalty" due to cache shouldn't make any meaningful difference // max. 24 hours of "penalty" due to cache shouldn't make any meaningful difference
// in terms of the freshness of the response. // in terms of the freshness of the response.
m_addr_response_caches[cache_id].m_update_addr_response = current_time + std::chrono::hours(21) + GetRandMillis(std::chrono::hours(6)); cache_entry.m_cache_entry_expiration = current_time + std::chrono::hours(21) + GetRandMillis(std::chrono::hours(6));
} }
return m_addr_response_caches[cache_id].m_addrs_response_cache; return cache_entry.m_addrs_response_cache;
} }
bool CConnman::AddNode(const std::string& strNode) bool CConnman::AddNode(const std::string& strNode)

@ -442,7 +442,7 @@ private:
*/ */
struct CachedAddrResponse { struct CachedAddrResponse {
std::vector<CAddress> m_addrs_response_cache; std::vector<CAddress> m_addrs_response_cache;
std::chrono::microseconds m_update_addr_response{0}; std::chrono::microseconds m_cache_entry_expiration{0};
}; };
/** /**
@ -454,10 +454,10 @@ private:
* Indexing by local socket prevents leakage when a node has multiple * Indexing by local socket prevents leakage when a node has multiple
* listening addresses on the same network. * listening addresses on the same network.
* *
* The used memory equals to 1000 CAddress records (or around 32 bytes) per * The used memory equals to 1000 CAddress records (or around 40 bytes) per
* distinct Network (up to 5) we have/had an inbound peer from, * distinct Network (up to 5) we have/had an inbound peer from,
* resulting in at most ~160 KB. Every separate local socket may * resulting in at most ~196 KB. Every separate local socket may
* add up to ~160 KB extra. * add up to ~196 KB extra.
*/ */
std::map<uint64_t, CachedAddrResponse> m_addr_response_caches; std::map<uint64_t, CachedAddrResponse> m_addr_response_caches;

Loading…
Cancel
Save