diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -539,7 +539,7 @@ */ struct CachedAddrResponse { std::vector m_addrs_response_cache; - std::chrono::microseconds m_update_addr_response{0}; + std::chrono::microseconds m_cache_entry_expiration{0}; }; /** @@ -551,10 +551,10 @@ * Indexing by local socket prevents leakage when a node has multiple * 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, - * resulting in at most ~160 KB. Every separate local socket may - * add up to ~160 KB extra. + * resulting in at most ~196 KB. Every separate local socket may + * add up to ~196 KB extra. */ std::map m_addr_response_caches; diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -2892,12 +2892,12 @@ .Write(local_socket_bytes.data(), local_socket_bytes.size()) .Finalize(); const auto current_time = GetTime(); - if (m_addr_response_caches.find(cache_id) == m_addr_response_caches.end() || - m_addr_response_caches[cache_id].m_update_addr_response < - current_time) { - m_addr_response_caches[cache_id].m_addrs_response_cache = + auto r = m_addr_response_caches.emplace(cache_id, CachedAddrResponse{}); + CachedAddrResponse &cache_entry = r.first->second; + // New CachedAddrResponse have expiration 0. + if (cache_entry.m_cache_entry_expiration < current_time) { + 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 and the usefulness of ADDR responses to honest // users. @@ -2926,11 +2926,11 @@ // are older than 30 days, max. 24 hours of "penalty" due to cache // shouldn't make any meaningful difference in terms of the freshness of // the response. - m_addr_response_caches[cache_id].m_update_addr_response = + 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) {