Skip to content

Commit 83ad65f

Browse files
committed
Address nits in ADDR caching
1 parent 81b00f8 commit 83ad65f

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/net.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,10 +2550,10 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
25502550
.Write(local_socket_bytes.data(), local_socket_bytes.size())
25512551
.Finalize();
25522552
const auto current_time = GetTime<std::chrono::microseconds>();
2553-
if (m_addr_response_caches.find(cache_id) == m_addr_response_caches.end() ||
2554-
m_addr_response_caches[cache_id].m_update_addr_response < current_time) {
2555-
m_addr_response_caches[cache_id].m_addrs_response_cache = GetAddresses(max_addresses, max_pct);
2556-
2553+
auto r = m_addr_response_caches.emplace(cache_id, CachedAddrResponse{});
2554+
CachedAddrResponse& cache_entry = r.first->second;
2555+
if (cache_entry.m_cache_entry_expiration < current_time) { // If emplace() added new one it has expiration 0.
2556+
cache_entry.m_addrs_response_cache = GetAddresses(max_addresses, max_pct);
25572557
// Choosing a proper cache lifetime is a trade-off between the privacy leak minimization
25582558
// and the usefulness of ADDR responses to honest users.
25592559
//
@@ -2578,9 +2578,9 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
25782578
// nodes to be "terrible" (see IsTerrible()) if the timestamps are older than 30 days,
25792579
// max. 24 hours of "penalty" due to cache shouldn't make any meaningful difference
25802580
// in terms of the freshness of the response.
2581-
m_addr_response_caches[cache_id].m_update_addr_response = current_time + std::chrono::hours(21) + GetRandMillis(std::chrono::hours(6));
2581+
cache_entry.m_cache_entry_expiration = current_time + std::chrono::hours(21) + GetRandMillis(std::chrono::hours(6));
25822582
}
2583-
return m_addr_response_caches[cache_id].m_addrs_response_cache;
2583+
return cache_entry.m_addrs_response_cache;
25842584
}
25852585

25862586
bool CConnman::AddNode(const std::string& strNode)

src/net.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ class CConnman
442442
*/
443443
struct CachedAddrResponse {
444444
std::vector<CAddress> m_addrs_response_cache;
445-
std::chrono::microseconds m_update_addr_response{0};
445+
std::chrono::microseconds m_cache_entry_expiration{0};
446446
};
447447

448448
/**
@@ -454,10 +454,10 @@ class CConnman
454454
* Indexing by local socket prevents leakage when a node has multiple
455455
* listening addresses on the same network.
456456
*
457-
* The used memory equals to 1000 CAddress records (or around 32 bytes) per
457+
* The used memory equals to 1000 CAddress records (or around 40 bytes) per
458458
* distinct Network (up to 5) we have/had an inbound peer from,
459-
* resulting in at most ~160 KB. Every separate local socket may
460-
* add up to ~160 KB extra.
459+
* resulting in at most ~196 KB. Every separate local socket may
460+
* add up to ~196 KB extra.
461461
*/
462462
std::map<uint64_t, CachedAddrResponse> m_addr_response_caches;
463463

0 commit comments

Comments
 (0)