Branch data Line data Source code
1 : : // Copyright (c) 2012 Pieter Wuille
2 : : // Copyright (c) 2012-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #include <bitcoin-build-config.h> // IWYU pragma: keep
7 : :
8 : : #include <addrman.h>
9 : : #include <addrman_impl.h>
10 : :
11 : : #include <hash.h>
12 : : #include <logging/timer.h>
13 : : #include <netaddress.h>
14 : : #include <netgroup.h>
15 : : #include <protocol.h>
16 : : #include <random.h>
17 : : #include <serialize.h>
18 : : #include <streams.h>
19 : : #include <tinyformat.h>
20 : : #include <uint256.h>
21 : : #include <util/check.h>
22 : : #include <util/log.h>
23 : : #include <util/time.h>
24 : :
25 : : #include <cmath>
26 : : #include <optional>
27 : :
28 : :
29 : 15883269 : int AddrInfo::GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
30 : : {
31 [ + - + - ]: 15883269 : uint64_t hash1 = (HashWriter{} << nKey << GetKey()).GetCheapHash();
32 [ + - + - : 15883269 : uint64_t hash2 = (HashWriter{} << nKey << netgroupman.GetGroup(*this) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
+ - ]
33 : 15883269 : return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
34 : : }
35 : :
36 : 16436004 : int AddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const
37 : : {
38 : 16436004 : std::vector<unsigned char> vchSourceGroupKey = netgroupman.GetGroup(src);
39 [ + - + - : 32872008 : uint64_t hash1 = (HashWriter{} << nKey << netgroupman.GetGroup(*this) << vchSourceGroupKey).GetCheapHash();
+ - + - +
- ]
40 [ + - + - : 16436004 : uint64_t hash2 = (HashWriter{} << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
+ - + - +
- ]
41 : 16436004 : return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
42 : 16436004 : }
43 : :
44 : 46508003 : int AddrInfo::GetBucketPosition(const uint256& nKey, bool fNew, int bucket) const
45 : : {
46 [ + + + - : 62391272 : uint64_t hash1 = (HashWriter{} << nKey << (fNew ? uint8_t{'N'} : uint8_t{'K'}) << bucket << GetKey()).GetCheapHash();
+ - ]
47 : 46508003 : return hash1 % ADDRMAN_BUCKET_SIZE;
48 : : }
49 : :
50 : 3987919 : bool AddrInfo::IsTerrible(NodeSeconds now) const
51 : : {
52 [ + + ]: 3987919 : if (now - m_last_try <= 1min) { // never remove things tried in the last minute
53 : : return false;
54 : : }
55 : :
56 [ + + ]: 3121130 : if (nTime > now + 10min) { // came in a flying DeLorean
57 : : return true;
58 : : }
59 : :
60 [ + + ]: 1671626 : if (now - nTime > ADDRMAN_HORIZON) { // not seen in recent history
61 : : return true;
62 : : }
63 : :
64 [ + + + + ]: 60074 : if (TicksSinceEpoch<std::chrono::seconds>(m_last_success) == 0 && nAttempts >= ADDRMAN_RETRIES) { // tried N times and never a success
65 : : return true;
66 : : }
67 : :
68 [ + + + + ]: 60024 : if (now - m_last_success > ADDRMAN_MIN_FAIL && nAttempts >= ADDRMAN_MAX_FAILURES) { // N successive failures in the last week
69 : 26 : return true;
70 : : }
71 : :
72 : : return false;
73 : : }
74 : :
75 : 5093 : double AddrInfo::GetChance(NodeSeconds now) const
76 : : {
77 : 5093 : double fChance = 1.0;
78 : :
79 : : // deprioritize very recent attempts away
80 [ + + ]: 5093 : if (now - m_last_try < 10min) {
81 : 2574 : fChance *= 0.01;
82 : : }
83 : :
84 : : // deprioritize 66% after each failed attempt, but at most 1/28th to avoid the search taking forever or overly penalizing outages.
85 [ + + ]: 5093 : fChance *= pow(0.66, std::min(nAttempts, 8));
86 : :
87 : 5093 : return fChance;
88 : : }
89 : :
90 : 21678 : AddrManImpl::AddrManImpl(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio)
91 : 21678 : : insecure_rand{deterministic}
92 [ + + ]: 21678 : , nKey{deterministic ? uint256{1} : insecure_rand.rand256()}
93 : 21678 : , m_consistency_check_ratio{consistency_check_ratio}
94 : 43356 : , m_netgroupman{netgroupman}
95 : : {
96 [ + + ]: 22219950 : for (auto& bucket : vvNew) {
97 [ + + ]: 1442887680 : for (auto& entry : bucket) {
98 : 1420689408 : entry = -1;
99 : : }
100 : : }
101 [ + + ]: 5571246 : for (auto& bucket : vvTried) {
102 [ + + ]: 360721920 : for (auto& entry : bucket) {
103 : 355172352 : entry = -1;
104 : : }
105 : : }
106 : 21678 : }
107 : :
108 : 21678 : AddrManImpl::~AddrManImpl()
109 : : {
110 : 21678 : nKey.SetNull();
111 : 21678 : }
112 : :
113 : : template <typename Stream>
114 : 3547 : void AddrManImpl::Serialize(Stream& s_) const
115 : : {
116 [ + - ]: 3547 : LOCK(cs);
117 : :
118 : : /**
119 : : * Serialized format.
120 : : * * format version byte (@see `Format`)
121 : : * * lowest compatible format version byte. This is used to help old software decide
122 : : * whether to parse the file. For example:
123 : : * * Bitcoin Core version N knows how to parse up to format=3. If a new format=4 is
124 : : * introduced in version N+1 that is compatible with format=3 and it is known that
125 : : * version N will be able to parse it, then version N+1 will write
126 : : * (format=4, lowest_compatible=3) in the first two bytes of the file, and so
127 : : * version N will still try to parse it.
128 : : * * Bitcoin Core version N+2 introduces a new incompatible format=5. It will write
129 : : * (format=5, lowest_compatible=5) and so any versions that do not know how to parse
130 : : * format=5 will not try to read the file.
131 : : * * nKey
132 : : * * nNew
133 : : * * nTried
134 : : * * number of "new" buckets XOR 2**30
135 : : * * all new addresses (total count: nNew)
136 : : * * all tried addresses (total count: nTried)
137 : : * * for each new bucket:
138 : : * * number of elements
139 : : * * for each element: index in the serialized "all new addresses"
140 : : * * asmap version
141 : : *
142 : : * 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it
143 : : * as incompatible. This is necessary because it did not check the version number on
144 : : * deserialization.
145 : : *
146 : : * vvNew, vvTried, mapInfo, mapAddr and vRandom are never encoded explicitly;
147 : : * they are instead reconstructed from the other information.
148 : : *
149 : : * This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
150 : : * changes to the ADDRMAN_ parameters without breaking the on-disk structure.
151 : : *
152 : : * We don't use SERIALIZE_METHODS since the serialization and deserialization code has
153 : : * very little in common.
154 : : */
155 : :
156 : : // Always serialize in the latest version (FILE_FORMAT).
157 : 3547 : ParamsStream s{s_, CAddress::V2_DISK};
158 : :
159 [ + - ]: 3547 : s << static_cast<uint8_t>(FILE_FORMAT);
160 : :
161 : : // Increment `lowest_compatible` iff a newly introduced format is incompatible with
162 : : // the previous one.
163 : : static constexpr uint8_t lowest_compatible = Format::V4_MULTIPORT;
164 [ + - ]: 3547 : s << static_cast<uint8_t>(INCOMPATIBILITY_BASE + lowest_compatible);
165 : :
166 [ + - ]: 3547 : s << nKey;
167 [ + - ]: 3547 : s << nNew;
168 [ + - ]: 3547 : s << nTried;
169 : :
170 [ + - ]: 3547 : int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
171 : 3547 : s << nUBuckets;
172 : 3547 : std::unordered_map<nid_type, int> mapUnkIds;
173 : 3547 : int nIds = 0;
174 [ + + + - ]: 8392872 : for (const auto& entry : mapInfo) {
175 [ + - ]: 8389325 : mapUnkIds[entry.first] = nIds;
176 : 8389325 : const AddrInfo& info = entry.second;
177 [ + + ]: 8389325 : if (info.nRefCount) {
178 [ - + ]: 4777126 : assert(nIds != nNew); // this means nNew was wrong, oh ow
179 : 4777126 : s << info;
180 : 4777126 : nIds++;
181 : : }
182 : : }
183 : 3547 : nIds = 0;
184 [ + + + + ]: 8392872 : for (const auto& entry : mapInfo) {
185 : 8389325 : const AddrInfo& info = entry.second;
186 [ + + ]: 8389325 : if (info.fInTried) {
187 [ - + ]: 3612199 : assert(nIds != nTried); // this means nTried was wrong, oh ow
188 : 3612199 : s << info;
189 : 3612199 : nIds++;
190 : : }
191 : : }
192 [ + + ]: 3635675 : for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
193 : : int nSize = 0;
194 [ + + ]: 236088320 : for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
195 [ + + ]: 232456192 : if (vvNew[bucket][i] != -1)
196 : 4970684 : nSize++;
197 : : }
198 : 236088320 : s << nSize;
199 [ + + ]: 236088320 : for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
200 [ + + ]: 232456192 : if (vvNew[bucket][i] != -1) {
201 [ + - + - ]: 4970684 : int nIndex = mapUnkIds[vvNew[bucket][i]];
202 : 232456192 : s << nIndex;
203 : : }
204 : : }
205 : : }
206 : : // Store asmap version after bucket entries so that it
207 : : // can be ignored by older clients for backward compatibility.
208 [ + - ]: 7094 : s << m_netgroupman.GetAsmapVersion();
209 [ + - ]: 7094 : }
210 : :
211 : : template <typename Stream>
212 : 6019 : void AddrManImpl::Unserialize(Stream& s_)
213 : : {
214 : 6019 : LOCK(cs);
215 : :
216 [ - + ]: 6019 : assert(vRandom.empty());
217 : :
218 : : Format format;
219 [ + + ]: 6019 : s_ >> Using<CustomUintFormatter<1>>(format);
220 : :
221 [ + + + + ]: 5961 : const auto ser_params = (format >= Format::V3_BIP155 ? CAddress::V2_DISK : CAddress::V1_DISK);
222 [ + + ]: 5961 : ParamsStream s{s_, ser_params};
223 : :
224 : : uint8_t compat;
225 : 5874 : s >> compat;
226 [ + + ]: 5874 : if (compat < INCOMPATIBILITY_BASE) {
227 [ + - + - ]: 548 : throw std::ios_base::failure(strprintf(
228 : : "Corrupted addrman database: The compat value (%u) "
229 : : "is lower than the expected minimum value %u.",
230 : : compat, INCOMPATIBILITY_BASE));
231 : : }
232 : 5600 : const uint8_t lowest_compatible = compat - INCOMPATIBILITY_BASE;
233 [ + + ]: 5600 : if (lowest_compatible > FILE_FORMAT) {
234 : 788 : throw InvalidAddrManVersionError(strprintf(
235 : : "Unsupported format of addrman database: %u. It is compatible with formats >=%u, "
236 : : "but the maximum supported by this version of %s is %u.",
237 [ + - ]: 394 : uint8_t{format}, lowest_compatible, CLIENT_NAME, uint8_t{FILE_FORMAT}));
238 : : }
239 : :
240 [ + + ]: 5206 : s >> nKey;
241 [ + + ]: 5188 : s >> nNew;
242 [ + + ]: 5186 : s >> nTried;
243 [ + + ]: 5185 : int nUBuckets = 0;
244 : 5177 : s >> nUBuckets;
245 [ + + ]: 5177 : if (format >= Format::V1_DETERMINISTIC) {
246 : 3367 : nUBuckets ^= (1 << 30);
247 : : }
248 : :
249 [ + + ]: 5177 : if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nNew < 0) {
250 [ + - ]: 138 : throw std::ios_base::failure(
251 : 69 : strprintf("Corrupt AddrMan serialization: nNew=%d, should be in [0, %d]",
252 : : nNew,
253 [ + - ]: 69 : ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE));
254 : : }
255 : :
256 [ + + ]: 5108 : if (nTried > ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nTried < 0) {
257 [ + - ]: 78 : throw std::ios_base::failure(
258 : 39 : strprintf("Corrupt AddrMan serialization: nTried=%d, should be in [0, %d]",
259 : : nTried,
260 [ + - ]: 39 : ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE));
261 : : }
262 : :
263 : : // Deserialize entries from the new table.
264 [ + + ]: 4256121 : for (int n = 0; n < nNew; n++) {
265 [ + - + + ]: 4252042 : AddrInfo& info = mapInfo[n];
266 : 4251052 : s >> info;
267 [ + - ]: 4251052 : mapAddr[info] = n;
268 [ - + ]: 4251052 : info.nRandomPos = vRandom.size();
269 [ + - ]: 4251052 : vRandom.push_back(n);
270 [ + - + - ]: 4251052 : m_network_counts[info.GetNetwork()].n_new++;
271 : : }
272 : 4079 : nIdCount = nNew;
273 : :
274 : : // Deserialize entries from the tried table.
275 : 4079 : int nLost = 0;
276 [ + + ]: 3655926 : for (int n = 0; n < nTried; n++) {
277 [ + - ]: 3651847 : AddrInfo info;
278 : 3651244 : s >> info;
279 [ + - ]: 3651244 : int nKBucket = info.GetTriedBucket(nKey, m_netgroupman);
280 [ + - ]: 3651244 : int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
281 [ + - ]: 3651244 : if (info.IsValid()
282 [ + + + + ]: 3651244 : && vvTried[nKBucket][nKBucketPos] == -1) {
283 [ - + ]: 3580940 : info.nRandomPos = vRandom.size();
284 : 3580940 : info.fInTried = true;
285 [ + - ]: 3580940 : vRandom.push_back(nIdCount);
286 [ + - ]: 3580940 : mapInfo[nIdCount] = info;
287 [ + - ]: 3580940 : mapAddr[info] = nIdCount;
288 : 3580940 : vvTried[nKBucket][nKBucketPos] = nIdCount;
289 : 3580940 : nIdCount++;
290 [ + - + - ]: 3580940 : m_network_counts[info.GetNetwork()].n_tried++;
291 : : } else {
292 : 70304 : nLost++;
293 : : }
294 : : }
295 : 3476 : nTried -= nLost;
296 : :
297 : : // Store positions in the new table buckets to apply later (if possible).
298 : : // An entry may appear in up to ADDRMAN_NEW_BUCKETS_PER_ADDRESS buckets,
299 : : // so we store all bucket-entry_index pairs to iterate through later.
300 : 3476 : std::vector<std::pair<int, int>> bucket_entries;
301 : :
302 [ + + ]: 1582533 : for (int bucket = 0; bucket < nUBuckets; ++bucket) {
303 [ + + ]: 1579167 : int num_entries{0};
304 : 7220921 : s >> num_entries;
305 [ + + ]: 7220921 : for (int n = 0; n < num_entries; ++n) {
306 [ + + ]: 5641864 : int entry_index{0};
307 : 5641789 : s >> entry_index;
308 [ + + + + ]: 5641789 : if (entry_index >= 0 && entry_index < nNew) {
309 [ + - ]: 5562088 : bucket_entries.emplace_back(bucket, entry_index);
310 : : }
311 : : }
312 : : }
313 : :
314 : : // If the bucket count and asmap version haven't changed, then attempt
315 : : // to restore the entries to the buckets/positions they were in before
316 : : // serialization.
317 [ + - ]: 3366 : uint256 supplied_asmap_version{m_netgroupman.GetAsmapVersion()};
318 : 3366 : uint256 serialized_asmap_version;
319 [ + + ]: 3366 : if (format >= Format::V2_ASMAP) {
320 : 3345 : s >> serialized_asmap_version;
321 : : }
322 [ + + ]: 3345 : const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT &&
323 [ + + ]: 1510 : serialized_asmap_version == supplied_asmap_version};
324 : :
325 : : if (!restore_bucketing) {
326 [ + - - + : 1841 : LogDebug(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n");
- - ]
327 : : }
328 : :
329 [ + + ]: 4709853 : for (auto bucket_entry : bucket_entries) {
330 : 4706508 : int bucket{bucket_entry.first};
331 : 4706508 : const int entry_index{bucket_entry.second};
332 [ + - ]: 4706508 : AddrInfo& info = mapInfo[entry_index];
333 : :
334 : : // Don't store the entry in the new bucket if it's not a valid address for our addrman
335 [ + - + + ]: 4706508 : if (!info.IsValid()) continue;
336 : :
337 : : // The entry shouldn't appear in more than
338 : : // ADDRMAN_NEW_BUCKETS_PER_ADDRESS. If it has already, just skip
339 : : // this bucket_entry.
340 [ + + ]: 4675194 : if (info.nRefCount >= ADDRMAN_NEW_BUCKETS_PER_ADDRESS) continue;
341 : :
342 [ + - ]: 4599622 : int bucket_position = info.GetBucketPosition(nKey, true, bucket);
343 [ + + + + ]: 4599622 : if (restore_bucketing && vvNew[bucket][bucket_position] == -1) {
344 : : // Bucketing has not changed, using existing bucket positions for the new table
345 : 4134175 : vvNew[bucket][bucket_position] = entry_index;
346 : 4134175 : ++info.nRefCount;
347 : : } else {
348 : : // In case the new table data cannot be used (bucket count wrong or new asmap),
349 : : // try to give them a reference based on their primary source address.
350 [ + - ]: 465447 : bucket = info.GetNewBucket(nKey, m_netgroupman);
351 [ + - ]: 465447 : bucket_position = info.GetBucketPosition(nKey, true, bucket);
352 [ + + ]: 465447 : if (vvNew[bucket][bucket_position] == -1) {
353 : 3738 : vvNew[bucket][bucket_position] = entry_index;
354 : 3738 : ++info.nRefCount;
355 : : }
356 : : }
357 : : }
358 : :
359 : : // Prune new entries with refcount 0 (as a result of collisions or invalid address).
360 : 3345 : int nLostUnk = 0;
361 [ + + + + ]: 7694132 : for (auto it = mapInfo.cbegin(); it != mapInfo.cend(); ) {
362 [ + + + + ]: 7690787 : if (it->second.fInTried == false && it->second.nRefCount == 0) {
363 [ + - ]: 138117 : const auto itCopy = it++;
364 [ + - ]: 138117 : Delete(itCopy->first);
365 : 138117 : ++nLostUnk;
366 : : } else {
367 : 7552670 : ++it;
368 : : }
369 : : }
370 [ + + ]: 3345 : if (nLost + nLostUnk > 0) {
371 [ + - - + : 909 : LogDebug(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions or invalid addresses\n", nLostUnk, nLost);
- - ]
372 : : }
373 : :
374 [ + - ]: 3345 : const int check_code{CheckAddrman()};
375 [ + + ]: 3345 : if (check_code != 0) {
376 [ + - + - ]: 604 : throw std::ios_base::failure(strprintf(
377 : : "Corrupt data. Consistency check failed with code %s",
378 : : check_code));
379 : : }
380 [ + - ]: 6519 : }
381 : :
382 : 20627229 : AddrInfo* AddrManImpl::Find(const CService& addr, nid_type* pnId)
383 : : {
384 : 20627229 : AssertLockHeld(cs);
385 : :
386 : 20627229 : const auto it = mapAddr.find(addr);
387 [ + + ]: 20627229 : if (it == mapAddr.end())
388 : : return nullptr;
389 [ + + ]: 7896912 : if (pnId)
390 : 7871533 : *pnId = (*it).second;
391 : 7896912 : const auto it2 = mapInfo.find((*it).second);
392 [ + - ]: 7896912 : if (it2 != mapInfo.end())
393 : 7896912 : return &(*it2).second;
394 : : return nullptr;
395 : : }
396 : :
397 : 11751843 : AddrInfo* AddrManImpl::Create(const CAddress& addr, const CNetAddr& addrSource, nid_type* pnId)
398 : : {
399 : 11751843 : AssertLockHeld(cs);
400 : :
401 : 11751843 : nid_type nId = nIdCount++;
402 [ + - ]: 11751843 : mapInfo[nId] = AddrInfo(addr, addrSource);
403 : 11751843 : mapAddr[addr] = nId;
404 [ - + ]: 11751843 : mapInfo[nId].nRandomPos = vRandom.size();
405 : 11751843 : vRandom.push_back(nId);
406 : 11751843 : nNew++;
407 : 11751843 : m_network_counts[addr.GetNetwork()].n_new++;
408 [ + - ]: 11751843 : if (pnId)
409 : 11751843 : *pnId = nId;
410 : 11751843 : return &mapInfo[nId];
411 : : }
412 : :
413 : 4207956 : void AddrManImpl::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
414 : : {
415 : 4207956 : AssertLockHeld(cs);
416 : :
417 [ + + ]: 4207956 : if (nRndPos1 == nRndPos2)
418 : : return;
419 : :
420 [ - + + - : 3287345 : assert(nRndPos1 < vRandom.size() && nRndPos2 < vRandom.size());
- + ]
421 : :
422 : 3287345 : nid_type nId1 = vRandom[nRndPos1];
423 : 3287345 : nid_type nId2 = vRandom[nRndPos2];
424 : :
425 : 3287345 : const auto it_1{mapInfo.find(nId1)};
426 : 3287345 : const auto it_2{mapInfo.find(nId2)};
427 [ - + ]: 3287345 : assert(it_1 != mapInfo.end());
428 [ + - ]: 3287345 : assert(it_2 != mapInfo.end());
429 : :
430 : 3287345 : it_1->second.nRandomPos = nRndPos2;
431 : 3287345 : it_2->second.nRandomPos = nRndPos1;
432 : :
433 : 3287345 : vRandom[nRndPos1] = nId2;
434 : 3287345 : vRandom[nRndPos2] = nId1;
435 : : }
436 : :
437 : 3392562 : void AddrManImpl::Delete(nid_type nId)
438 : : {
439 : 3392562 : AssertLockHeld(cs);
440 : :
441 [ - + ]: 3392562 : assert(mapInfo.contains(nId));
442 : 3392562 : AddrInfo& info = mapInfo[nId];
443 [ - + ]: 3392562 : assert(!info.fInTried);
444 [ - + ]: 3392562 : assert(info.nRefCount == 0);
445 : :
446 [ - + ]: 3392562 : SwapRandom(info.nRandomPos, vRandom.size() - 1);
447 : 3392562 : m_network_counts[info.GetNetwork()].n_new--;
448 : 3392562 : vRandom.pop_back();
449 : 3392562 : mapAddr.erase(info);
450 : 3392562 : mapInfo.erase(nId);
451 : 3392562 : nNew--;
452 : 3392562 : }
453 : :
454 : 11199793 : void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
455 : : {
456 : 11199793 : AssertLockHeld(cs);
457 : :
458 : : // if there is an entry in the specified bucket, delete it.
459 [ + + ]: 11199793 : if (vvNew[nUBucket][nUBucketPos] != -1) {
460 : 2493100 : nid_type nIdDelete = vvNew[nUBucket][nUBucketPos];
461 : 2493100 : AddrInfo& infoDelete = mapInfo[nIdDelete];
462 [ - + ]: 2493100 : assert(infoDelete.nRefCount > 0);
463 : 2493100 : infoDelete.nRefCount--;
464 : 2493100 : vvNew[nUBucket][nUBucketPos] = -1;
465 [ - + - - ]: 2493100 : LogDebug(BCLog::ADDRMAN, "Removed %s from new[%i][%i]\n", infoDelete.ToStringAddrPort(), nUBucket, nUBucketPos);
466 [ + + ]: 2493100 : if (infoDelete.nRefCount == 0) {
467 : 2383401 : Delete(nIdDelete);
468 : : }
469 : : }
470 : 11199793 : }
471 : :
472 : 3615156 : void AddrManImpl::MakeTried(AddrInfo& info, nid_type nId)
473 : : {
474 : 3615156 : AssertLockHeld(cs);
475 : :
476 : : // remove the entry from all new buckets
477 : 3615156 : const int start_bucket{info.GetNewBucket(nKey, m_netgroupman)};
478 [ + - ]: 9066947 : for (int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; ++n) {
479 : 9066947 : const int bucket{(start_bucket + n) % ADDRMAN_NEW_BUCKET_COUNT};
480 : 9066947 : const int pos{info.GetBucketPosition(nKey, true, bucket)};
481 [ + + ]: 9066947 : if (vvNew[bucket][pos] == nId) {
482 : 3626894 : vvNew[bucket][pos] = -1;
483 : 3626894 : info.nRefCount--;
484 [ + + ]: 3626894 : if (info.nRefCount == 0) break;
485 : : }
486 : : }
487 : 3615156 : nNew--;
488 : 3615156 : m_network_counts[info.GetNetwork()].n_new--;
489 : :
490 [ - + ]: 3615156 : assert(info.nRefCount == 0);
491 : :
492 : : // which tried bucket to move the entry to
493 : 3615156 : int nKBucket = info.GetTriedBucket(nKey, m_netgroupman);
494 : 3615156 : int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
495 : :
496 : : // first make space to add it (the existing tried entry there is moved to new, deleting whatever is there).
497 [ + + ]: 3615156 : if (vvTried[nKBucket][nKBucketPos] != -1) {
498 : : // find an item to evict
499 : 3283 : nid_type nIdEvict = vvTried[nKBucket][nKBucketPos];
500 [ - + ]: 3283 : assert(mapInfo.contains(nIdEvict));
501 : 3283 : AddrInfo& infoOld = mapInfo[nIdEvict];
502 : :
503 : : // Remove the to-be-evicted item from the tried set.
504 : 3283 : infoOld.fInTried = false;
505 : 3283 : vvTried[nKBucket][nKBucketPos] = -1;
506 : 3283 : nTried--;
507 : 3283 : m_network_counts[infoOld.GetNetwork()].n_tried--;
508 : :
509 : : // find which new bucket it belongs to
510 : 3283 : int nUBucket = infoOld.GetNewBucket(nKey, m_netgroupman);
511 : 3283 : int nUBucketPos = infoOld.GetBucketPosition(nKey, true, nUBucket);
512 : 3283 : ClearNew(nUBucket, nUBucketPos);
513 [ - + ]: 3283 : assert(vvNew[nUBucket][nUBucketPos] == -1);
514 : :
515 : : // Enter it into the new set again.
516 : 3283 : infoOld.nRefCount = 1;
517 : 3283 : vvNew[nUBucket][nUBucketPos] = nIdEvict;
518 : 3283 : nNew++;
519 : 3283 : m_network_counts[infoOld.GetNetwork()].n_new++;
520 [ - + - - ]: 3283 : LogDebug(BCLog::ADDRMAN, "Moved %s from tried[%i][%i] to new[%i][%i] to make space\n",
521 : : infoOld.ToStringAddrPort(), nKBucket, nKBucketPos, nUBucket, nUBucketPos);
522 : : }
523 [ - + ]: 3615156 : assert(vvTried[nKBucket][nKBucketPos] == -1);
524 : :
525 : 3615156 : vvTried[nKBucket][nKBucketPos] = nId;
526 : 3615156 : nTried++;
527 : 3615156 : info.fInTried = true;
528 : 3615156 : m_network_counts[info.GetNetwork()].n_tried++;
529 : 3615156 : }
530 : :
531 : 14466289 : bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::chrono::seconds time_penalty)
532 : : {
533 : 14466289 : AssertLockHeld(cs);
534 : :
535 [ + + ]: 14466289 : if (!addr.IsRoutable())
536 : : return false;
537 : :
538 : 14278561 : nid_type nId;
539 : 14278561 : AddrInfo* pinfo = Find(addr, &nId);
540 : :
541 : : // Do not set a penalty for a source's self-announcement
542 [ + + ]: 14278561 : if (addr == source) {
543 : 194015 : time_penalty = 0s;
544 : : }
545 : :
546 [ + + ]: 14278561 : if (pinfo) {
547 : : // periodically update nTime
548 : 2526718 : const bool currently_online{NodeClock::now() - addr.nTime < 24h};
549 [ + + ]: 2526718 : const auto update_interval{currently_online ? 1h : 24h};
550 [ + + ]: 2526718 : if (pinfo->nTime < addr.nTime - update_interval - time_penalty) {
551 : 136688 : pinfo->nTime = std::max(NodeSeconds{0s}, addr.nTime - time_penalty);
552 : : }
553 : :
554 : : // add services
555 : 2526718 : pinfo->nServices = ServiceFlags(pinfo->nServices | addr.nServices);
556 : :
557 : : // do not update if no new information is present
558 [ + + ]: 2526718 : if (addr.nTime <= pinfo->nTime) {
559 : : return false;
560 : : }
561 : :
562 : : // do not update if the entry was already in the "tried" table
563 [ + + ]: 2137442 : if (pinfo->fInTried)
564 : : return false;
565 : :
566 : : // do not update if the max reference count is reached
567 [ + + ]: 1344917 : if (pinfo->nRefCount == ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
568 : : return false;
569 : :
570 : : // stochastic test: previous nRefCount == N: 2^N times harder to increase it
571 [ + - ]: 1343435 : if (pinfo->nRefCount > 0) {
572 : 1343435 : const int nFactor{1 << pinfo->nRefCount};
573 [ + + ]: 1343435 : if (insecure_rand.randrange(nFactor) != 0) return false;
574 : : }
575 : : } else {
576 : 11751843 : pinfo = Create(addr, source, &nId);
577 : 11751843 : pinfo->nTime = std::max(NodeSeconds{0s}, pinfo->nTime - time_penalty);
578 : : }
579 : :
580 : 12352118 : int nUBucket = pinfo->GetNewBucket(nKey, source, m_netgroupman);
581 : 12352118 : int nUBucketPos = pinfo->GetBucketPosition(nKey, true, nUBucket);
582 : 12352118 : bool fInsert = vvNew[nUBucket][nUBucketPos] == -1;
583 [ + + ]: 12352118 : if (vvNew[nUBucket][nUBucketPos] != nId) {
584 [ + + ]: 12086108 : if (!fInsert) {
585 : 3382369 : AddrInfo& infoExisting = mapInfo[vvNew[nUBucket][nUBucketPos]];
586 [ + + + + : 3382369 : if (infoExisting.IsTerrible() || (infoExisting.nRefCount > 1 && pinfo->nRefCount == 0)) {
+ + ]
587 : : // Overwrite the existing new table entry.
588 : : fInsert = true;
589 : : }
590 : : }
591 [ + + ]: 9593337 : if (fInsert) {
592 : 11196510 : ClearNew(nUBucket, nUBucketPos);
593 : 11196510 : pinfo->nRefCount++;
594 : 11196510 : vvNew[nUBucket][nUBucketPos] = nId;
595 : 11196510 : const auto mapped_as{m_netgroupman.GetMappedAS(addr)};
596 [ - + - - : 11196510 : LogDebug(BCLog::ADDRMAN, "Added %s%s to new[%i][%i]\n",
- - - - ]
597 : : addr.ToStringAddrPort(), (mapped_as ? strprintf(" mapped to AS%i", mapped_as) : ""), nUBucket, nUBucketPos);
598 : : } else {
599 [ + + ]: 889598 : if (pinfo->nRefCount == 0) {
600 : 871044 : Delete(nId);
601 : : }
602 : : }
603 : : }
604 : : return fInsert;
605 : : }
606 : :
607 : 6251620 : bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, NodeSeconds time)
608 : : {
609 : 6251620 : AssertLockHeld(cs);
610 : :
611 : 6251620 : nid_type nId;
612 : :
613 : 6251620 : m_last_good = time;
614 : :
615 : 6251620 : AddrInfo* pinfo = Find(addr, &nId);
616 : :
617 : : // if not found, bail out
618 [ + + ]: 6251620 : if (!pinfo) return false;
619 : :
620 : 5344815 : AddrInfo& info = *pinfo;
621 : :
622 : : // update info
623 : 5344815 : info.m_last_success = time;
624 : 5344815 : info.m_last_try = time;
625 : 5344815 : info.nAttempts = 0;
626 : : // nTime is not updated here, to avoid leaking information about
627 : : // currently-connected peers.
628 : :
629 : : // if it is already in the tried set, don't do anything else
630 [ + + ]: 5344815 : if (info.fInTried) return false;
631 : :
632 : : // if it is not in new, something bad happened
633 [ + - ]: 4974946 : if (!Assume(info.nRefCount > 0)) return false;
634 : :
635 : :
636 : : // which tried bucket to move the entry to
637 : 4974946 : int tried_bucket = info.GetTriedBucket(nKey, m_netgroupman);
638 : 4974946 : int tried_bucket_pos = info.GetBucketPosition(nKey, false, tried_bucket);
639 : :
640 : : // Will moving this address into tried evict another entry?
641 [ + + + + ]: 4974946 : if (test_before_evict && (vvTried[tried_bucket][tried_bucket_pos] != -1)) {
642 [ + + ]: 1359790 : if (m_tried_collisions.size() < ADDRMAN_SET_TRIED_COLLISION_SIZE) {
643 : 33571 : m_tried_collisions.insert(nId);
644 : : }
645 : : // Output the entry we'd be colliding with, for debugging purposes
646 : 1359790 : auto colliding_entry = mapInfo.find(vvTried[tried_bucket][tried_bucket_pos]);
647 [ - + - - : 1359790 : LogDebug(BCLog::ADDRMAN, "Collision with %s while attempting to move %s to tried table. Collisions=%d",
- - - - -
- ]
648 : : colliding_entry != mapInfo.end() ? colliding_entry->second.ToStringAddrPort() : "<unknown-addr>",
649 : : addr.ToStringAddrPort(),
650 : : m_tried_collisions.size());
651 : 1359790 : return false;
652 : : } else {
653 : : // move nId to the tried tables
654 : 3615156 : MakeTried(info, nId);
655 : 3615156 : const auto mapped_as{m_netgroupman.GetMappedAS(addr)};
656 [ - + - - : 3615156 : LogDebug(BCLog::ADDRMAN, "Moved %s%s to tried[%i][%i]\n",
- - - - ]
657 : : addr.ToStringAddrPort(), (mapped_as ? strprintf(" mapped to AS%i", mapped_as) : ""), tried_bucket, tried_bucket_pos);
658 : 3615156 : return true;
659 : : }
660 : : }
661 : :
662 : 10856768 : bool AddrManImpl::Add_(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
663 : : {
664 : 10856768 : int added{0};
665 [ + + ]: 25323057 : for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++) {
666 [ + + ]: 17736068 : added += AddSingle(*it, source, time_penalty) ? 1 : 0;
667 : : }
668 [ + + ]: 10856768 : if (added > 0) {
669 [ - + - - : 8852830 : LogDebug(BCLog::ADDRMAN, "Added %i addresses (of %i) from %s: %i tried, %i new\n", added, vAddr.size(), source.ToStringAddr(), nTried, nNew);
- - ]
670 : : }
671 : 10856768 : return added > 0;
672 : : }
673 : :
674 : 8533 : void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, NodeSeconds time)
675 : : {
676 : 8533 : AssertLockHeld(cs);
677 : :
678 : 8533 : AddrInfo* pinfo = Find(addr);
679 : :
680 : : // if not found, bail out
681 [ + + ]: 8533 : if (!pinfo)
682 : : return;
683 : :
684 : 4823 : AddrInfo& info = *pinfo;
685 : :
686 : : // update info
687 : 4823 : info.m_last_try = time;
688 [ + + + + ]: 4823 : if (fCountFailure && info.m_last_count_attempt < m_last_good) {
689 : 1748 : info.m_last_count_attempt = time;
690 : 1748 : info.nAttempts++;
691 : : }
692 : : }
693 : :
694 : 2110 : std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, const std::unordered_set<Network>& networks) const
695 : : {
696 : 2110 : AssertLockHeld(cs);
697 : :
698 [ + + ]: 2110 : if (vRandom.empty()) return {};
699 : :
700 : 1442 : size_t new_count = nNew;
701 : 1442 : size_t tried_count = nTried;
702 : :
703 [ + + ]: 1442 : if (!networks.empty()) {
704 : 288 : new_count = 0;
705 : 288 : tried_count = 0;
706 [ + + ]: 1560 : for (auto& network : networks) {
707 : 1272 : auto it = m_network_counts.find(network);
708 [ + + ]: 1272 : if (it == m_network_counts.end()) {
709 : 670 : continue;
710 : : }
711 : 602 : auto counts = it->second;
712 : 602 : new_count += counts.n_new;
713 : 602 : tried_count += counts.n_tried;
714 : : }
715 : : }
716 : :
717 [ + + ]: 1442 : if (new_only && new_count == 0) return {};
718 [ + + ]: 1431 : if (new_count + tried_count == 0) return {};
719 : :
720 : : // Decide if we are going to search the new or tried table
721 : : // If either option is viable, use a 50% chance to choose
722 : 1409 : bool search_tried;
723 [ + + ]: 1409 : if (new_only || tried_count == 0) {
724 : : search_tried = false;
725 [ + + ]: 317 : } else if (new_count == 0) {
726 : : search_tried = true;
727 : : } else {
728 : 275 : search_tried = insecure_rand.randbool();
729 : : }
730 : :
731 [ + + ]: 275 : const int bucket_count{search_tried ? ADDRMAN_TRIED_BUCKET_COUNT : ADDRMAN_NEW_BUCKET_COUNT};
732 : :
733 : : // Loop through the addrman table until we find an appropriate entry
734 : 1409 : double chance_factor = 1.0;
735 : 3384865 : while (1) {
736 : : // Pick a bucket, and an initial position in that bucket.
737 : 3384865 : int bucket = insecure_rand.randrange(bucket_count);
738 : 3384865 : int initial_position = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
739 : :
740 : : // Iterate over the positions of that bucket, starting at the initial one,
741 : : // and looping around.
742 : 3384865 : int i, position;
743 : 3384865 : nid_type node_id;
744 [ + + ]: 219833817 : for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
745 : 216454045 : position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
746 : 216454045 : node_id = GetEntry(search_tried, bucket, position);
747 [ + + ]: 216454045 : if (node_id != -1) {
748 [ + + ]: 25597 : if (!networks.empty()) {
749 : 21403 : const auto it{mapInfo.find(node_id)};
750 [ + - + - : 42806 : if (Assume(it != mapInfo.end()) && networks.contains(it->second.GetNetwork())) break;
+ + ]
751 : : } else {
752 : : break;
753 : : }
754 : : }
755 : : }
756 : :
757 : : // If the bucket is entirely empty, start over with a (likely) different one.
758 [ + + ]: 3384865 : if (i == ADDRMAN_BUCKET_SIZE) continue;
759 : :
760 : : // Find the entry to return.
761 : 5093 : const auto it_found{mapInfo.find(node_id)};
762 [ + - ]: 5093 : assert(it_found != mapInfo.end());
763 : 5093 : const AddrInfo& info{it_found->second};
764 : :
765 : : // With probability GetChance() * chance_factor, return the entry.
766 [ + + ]: 5093 : if (insecure_rand.randbits<30>() < chance_factor * info.GetChance() * (1 << 30)) {
767 [ - + - - : 1409 : LogDebug(BCLog::ADDRMAN, "Selected %s from %s\n", info.ToStringAddrPort(), search_tried ? "tried" : "new");
- - ]
768 : 1409 : return {info, info.m_last_try};
769 : : }
770 : :
771 : : // Otherwise start over with a (likely) different bucket, and increased chance factor.
772 : 3684 : chance_factor *= 1.2;
773 : : }
774 : : }
775 : :
776 : 216535965 : nid_type AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
777 : : {
778 : 216535965 : AssertLockHeld(cs);
779 : :
780 [ + + ]: 216535965 : if (use_tried) {
781 [ + - + - ]: 12252370 : if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_TRIED_BUCKET_COUNT)) {
782 : 12252370 : return vvTried[bucket][position];
783 : : }
784 : : } else {
785 [ + - + - ]: 204283595 : if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_NEW_BUCKET_COUNT)) {
786 : 204283595 : return vvNew[bucket][position];
787 : : }
788 : : }
789 : :
790 : : return -1;
791 : : }
792 : :
793 : 11941 : std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered) const
794 : : {
795 : 11941 : AssertLockHeld(cs);
796 [ - + ]: 11941 : Assume(max_pct <= 100);
797 : :
798 [ - + ]: 11941 : size_t nNodes = vRandom.size();
799 [ + + ]: 11941 : if (max_pct != 0) {
800 [ + - ]: 3338 : max_pct = std::min(max_pct, size_t{100});
801 : 3338 : nNodes = max_pct * nNodes / 100;
802 : : }
803 [ + + ]: 11941 : if (max_addresses != 0) {
804 [ + + ]: 6736 : nNodes = std::min(nNodes, max_addresses);
805 : : }
806 : :
807 : : // gather a list of random nodes, skipping those of low quality
808 : 11941 : const auto now{Now<NodeSeconds>()};
809 : 11941 : std::vector<CAddress> addresses;
810 [ + - ]: 11941 : addresses.reserve(nNodes);
811 [ - + + + ]: 827335 : for (unsigned int n = 0; n < vRandom.size(); n++) {
812 [ - + + + ]: 815581 : if (addresses.size() >= nNodes)
813 : : break;
814 : :
815 : 815394 : int nRndPos = insecure_rand.randrange(vRandom.size() - n) + n;
816 [ + - ]: 815394 : SwapRandom(n, nRndPos);
817 : 815394 : const auto it{mapInfo.find(vRandom[n])};
818 [ - + ]: 815394 : assert(it != mapInfo.end());
819 : :
820 [ + + ]: 815394 : const AddrInfo& ai{it->second};
821 : :
822 : : // Filter by network (optional)
823 [ + + + - : 815394 : if (network != std::nullopt && ai.GetNetClass() != network) continue;
+ + ]
824 : :
825 : : // Filter for quality
826 [ + - + + : 605550 : if (ai.IsTerrible(now) && filtered) continue;
+ + ]
827 : :
828 [ + - ]: 514734 : addresses.push_back(ai);
829 : : }
830 [ + - + + : 11941 : LogDebug(BCLog::ADDRMAN, "GetAddr returned %d random addresses\n", addresses.size());
- + + - ]
831 : 11941 : return addresses;
832 : 0 : }
833 : :
834 : 2 : std::vector<std::pair<AddrInfo, AddressPosition>> AddrManImpl::GetEntries_(bool from_tried) const
835 : : {
836 : 2 : AssertLockHeld(cs);
837 : :
838 [ + + ]: 2 : const int bucket_count = from_tried ? ADDRMAN_TRIED_BUCKET_COUNT : ADDRMAN_NEW_BUCKET_COUNT;
839 : 2 : std::vector<std::pair<AddrInfo, AddressPosition>> infos;
840 [ + + ]: 1282 : for (int bucket = 0; bucket < bucket_count; ++bucket) {
841 [ + + ]: 83200 : for (int position = 0; position < ADDRMAN_BUCKET_SIZE; ++position) {
842 [ + - ]: 81920 : nid_type id = GetEntry(from_tried, bucket, position);
843 [ - + ]: 81920 : if (id >= 0) {
844 [ # # ]: 0 : AddrInfo info = mapInfo.at(id);
845 : 0 : AddressPosition location = AddressPosition(
846 : : from_tried,
847 : : /*multiplicity_in=*/from_tried ? 1 : info.nRefCount,
848 : : bucket,
849 [ # # # # ]: 0 : position);
850 [ # # ]: 0 : infos.emplace_back(info, location);
851 : 0 : }
852 : : }
853 : : }
854 : :
855 : 2 : return infos;
856 : 0 : }
857 : :
858 : 13421 : void AddrManImpl::Connected_(const CService& addr, NodeSeconds time)
859 : : {
860 : 13421 : AssertLockHeld(cs);
861 : :
862 : 13421 : AddrInfo* pinfo = Find(addr);
863 : :
864 : : // if not found, bail out
865 [ + + ]: 13421 : if (!pinfo)
866 : : return;
867 : :
868 : 3521 : AddrInfo& info = *pinfo;
869 : :
870 : : // update info
871 : 3521 : const auto update_interval{20min};
872 [ + + ]: 3521 : if (time - info.nTime > update_interval) {
873 : 760 : info.nTime = time;
874 : : }
875 : : }
876 : :
877 : 75094 : void AddrManImpl::SetServices_(const CService& addr, ServiceFlags nServices)
878 : : {
879 : 75094 : AssertLockHeld(cs);
880 : :
881 : 75094 : AddrInfo* pinfo = Find(addr);
882 : :
883 : : // if not found, bail out
884 [ + + ]: 75094 : if (!pinfo)
885 : : return;
886 : :
887 : 17035 : AddrInfo& info = *pinfo;
888 : :
889 : : // update info
890 : 17035 : info.nServices = nServices;
891 : : }
892 : :
893 : 11058 : void AddrManImpl::ResolveCollisions_()
894 : : {
895 : 11058 : AssertLockHeld(cs);
896 : :
897 [ + + ]: 28616 : for (std::set<nid_type>::iterator it = m_tried_collisions.begin(); it != m_tried_collisions.end();) {
898 : 17558 : nid_type id_new = *it;
899 : :
900 : 17558 : bool erase_collision = false;
901 : :
902 : : // If id_new not found in mapInfo remove it from m_tried_collisions
903 [ + + ]: 17558 : if (!mapInfo.contains(id_new)) {
904 : : erase_collision = true;
905 : : } else {
906 : 17287 : AddrInfo& info_new = mapInfo[id_new];
907 : :
908 : : // Which tried bucket to move the entry to.
909 : 17287 : int tried_bucket = info_new.GetTriedBucket(nKey, m_netgroupman);
910 : 17287 : int tried_bucket_pos = info_new.GetBucketPosition(nKey, false, tried_bucket);
911 [ + - ]: 17287 : if (!info_new.IsValid()) { // id_new may no longer map to a valid address
912 : : erase_collision = true;
913 [ + - ]: 17287 : } else if (vvTried[tried_bucket][tried_bucket_pos] != -1) { // The position in the tried bucket is not empty
914 : :
915 : : // Get the to-be-evicted address that is being tested
916 : 17287 : nid_type id_old = vvTried[tried_bucket][tried_bucket_pos];
917 : 17287 : AddrInfo& info_old = mapInfo[id_old];
918 : :
919 : 17287 : const auto current_time{Now<NodeSeconds>()};
920 : :
921 : : // Has successfully connected in last X hours
922 [ + + ]: 17287 : if (current_time - info_old.m_last_success < ADDRMAN_REPLACEMENT) {
923 : : erase_collision = true;
924 [ + + ]: 9180 : } else if (current_time - info_old.m_last_try < ADDRMAN_REPLACEMENT) { // attempted to connect and failed in last X hours
925 : :
926 : : // Give address at least 60 seconds to successfully connect
927 [ + + ]: 692 : if (current_time - info_old.m_last_try > 60s) {
928 [ - + - - : 3 : LogDebug(BCLog::ADDRMAN, "Replacing %s with %s in tried table\n", info_old.ToStringAddrPort(), info_new.ToStringAddrPort());
- - ]
929 : :
930 : : // Replaces an existing address already in the tried table with the new address
931 : 3 : Good_(info_new, false, current_time);
932 : 3 : erase_collision = true;
933 : : }
934 [ + + ]: 8488 : } else if (current_time - info_new.m_last_success > ADDRMAN_TEST_WINDOW) {
935 : : // If the collision hasn't resolved in some reasonable amount of time,
936 : : // just evict the old entry -- we must not be able to
937 : : // connect to it for some reason.
938 [ - + - - : 3280 : LogDebug(BCLog::ADDRMAN, "Unable to test; replacing %s with %s in tried table anyway\n", info_old.ToStringAddrPort(), info_new.ToStringAddrPort());
- - ]
939 : 3280 : Good_(info_new, false, current_time);
940 : 3280 : erase_collision = true;
941 : : }
942 : : } else { // Collision is not actually a collision anymore
943 : 0 : Good_(info_new, false, Now<NodeSeconds>());
944 : 0 : erase_collision = true;
945 : : }
946 : : }
947 : :
948 : 3283 : if (erase_collision) {
949 : 11661 : m_tried_collisions.erase(it++);
950 : : } else {
951 : 5897 : it++;
952 : : }
953 : : }
954 : 11058 : }
955 : :
956 : 79119 : std::pair<CAddress, NodeSeconds> AddrManImpl::SelectTriedCollision_()
957 : : {
958 : 79119 : AssertLockHeld(cs);
959 : :
960 [ + + ]: 79119 : if (m_tried_collisions.size() == 0) return {};
961 : :
962 : 51608 : std::set<nid_type>::iterator it = m_tried_collisions.begin();
963 : :
964 : : // Selects a random element from m_tried_collisions
965 : 51608 : std::advance(it, insecure_rand.randrange(m_tried_collisions.size()));
966 : 51608 : nid_type id_new = *it;
967 : :
968 : : // If id_new not found in mapInfo remove it from m_tried_collisions
969 [ + + ]: 51608 : if (!mapInfo.contains(id_new)) {
970 : 540 : m_tried_collisions.erase(it);
971 : 540 : return {};
972 : : }
973 : :
974 : 51068 : const AddrInfo& newInfo = mapInfo[id_new];
975 : :
976 : : // which tried bucket to move the entry to
977 : 51068 : int tried_bucket = newInfo.GetTriedBucket(nKey, m_netgroupman);
978 : 51068 : int tried_bucket_pos = newInfo.GetBucketPosition(nKey, false, tried_bucket);
979 : :
980 : 51068 : const AddrInfo& info_old = mapInfo[vvTried[tried_bucket][tried_bucket_pos]];
981 : 51068 : return {info_old, info_old.m_last_try};
982 : : }
983 : :
984 : 0 : std::optional<AddressPosition> AddrManImpl::FindAddressEntry_(const CAddress& addr)
985 : : {
986 : 0 : AssertLockHeld(cs);
987 : :
988 : 0 : AddrInfo* addr_info = Find(addr);
989 : :
990 [ # # ]: 0 : if (!addr_info) return std::nullopt;
991 : :
992 [ # # ]: 0 : if(addr_info->fInTried) {
993 : 0 : int bucket{addr_info->GetTriedBucket(nKey, m_netgroupman)};
994 : 0 : return AddressPosition(/*tried_in=*/true,
995 : : /*multiplicity_in=*/1,
996 : : /*bucket_in=*/bucket,
997 : 0 : /*position_in=*/addr_info->GetBucketPosition(nKey, false, bucket));
998 : : } else {
999 : 0 : int bucket{addr_info->GetNewBucket(nKey, m_netgroupman)};
1000 : 0 : return AddressPosition(/*tried_in=*/false,
1001 : : /*multiplicity_in=*/addr_info->nRefCount,
1002 : : /*bucket_in=*/bucket,
1003 : 0 : /*position_in=*/addr_info->GetBucketPosition(nKey, true, bucket));
1004 : : }
1005 : : }
1006 : :
1007 : 7725279 : size_t AddrManImpl::Size_(std::optional<Network> net, std::optional<bool> in_new) const
1008 : : {
1009 : 7725279 : AssertLockHeld(cs);
1010 : :
1011 [ + + ]: 7725279 : if (!net.has_value()) {
1012 [ + + ]: 7725063 : if (in_new.has_value()) {
1013 [ + + ]: 91 : return *in_new ? nNew : nTried;
1014 : : } else {
1015 [ - + ]: 7724972 : return vRandom.size();
1016 : : }
1017 : : }
1018 [ + + ]: 216 : if (auto it = m_network_counts.find(*net); it != m_network_counts.end()) {
1019 [ + + ]: 110 : auto net_count = it->second;
1020 [ + + ]: 110 : if (in_new.has_value()) {
1021 [ + + ]: 76 : return *in_new ? net_count.n_new : net_count.n_tried;
1022 : : } else {
1023 : 34 : return net_count.n_new + net_count.n_tried;
1024 : : }
1025 : : }
1026 : : return 0;
1027 : : }
1028 : :
1029 : 50063324 : void AddrManImpl::Check() const
1030 : : {
1031 : 50063324 : AssertLockHeld(cs);
1032 : :
1033 : : // Run consistency checks 1 in m_consistency_check_ratio times if enabled
1034 [ - + ]: 50063324 : if (m_consistency_check_ratio == 0) return;
1035 [ # # ]: 0 : if (insecure_rand.randrange(m_consistency_check_ratio) >= 1) return;
1036 : :
1037 : 0 : const int err{CheckAddrman()};
1038 [ # # ]: 0 : if (err) {
1039 : 0 : LogError("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i", err);
1040 : 0 : assert(false);
1041 : : }
1042 : : }
1043 : :
1044 : 3345 : int AddrManImpl::CheckAddrman() const
1045 : : {
1046 : 3345 : AssertLockHeld(cs);
1047 : :
1048 [ - + + - : 6690 : LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(
+ - ]
1049 : : strprintf("new %i, tried %i, total %u", nNew, nTried, vRandom.size()), BCLog::ADDRMAN);
1050 : :
1051 [ - + ]: 3345 : std::unordered_set<nid_type> setTried;
1052 [ - + ]: 3345 : std::unordered_map<nid_type, int> mapNew;
1053 : 3345 : std::unordered_map<Network, NewTriedCount> local_counts;
1054 : :
1055 [ - + + - ]: 3345 : if (vRandom.size() != (size_t)(nTried + nNew))
1056 : : return -7;
1057 : :
1058 [ + + + + ]: 7554575 : for (const auto& entry : mapInfo) {
1059 : 7551524 : nid_type n = entry.first;
1060 : 7551524 : const AddrInfo& info = entry.second;
1061 [ + + ]: 7551524 : if (info.fInTried) {
1062 [ + + ]: 3574148 : if (!TicksSinceEpoch<std::chrono::seconds>(info.m_last_success)) {
1063 : : return -1;
1064 : : }
1065 [ + - ]: 3574114 : if (info.nRefCount)
1066 : : return -2;
1067 [ + - ]: 3574114 : setTried.insert(n);
1068 [ + - + - ]: 3574114 : local_counts[info.GetNetwork()].n_tried++;
1069 : : } else {
1070 [ + - ]: 3977376 : if (info.nRefCount < 0 || info.nRefCount > ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
1071 : : return -3;
1072 [ + - ]: 3977376 : if (!info.nRefCount)
1073 : : return -4;
1074 [ + - ]: 3977376 : mapNew[n] = info.nRefCount;
1075 [ + - + - ]: 3977376 : local_counts[info.GetNetwork()].n_new++;
1076 : : }
1077 [ + - ]: 7551490 : const auto it{mapAddr.find(info)};
1078 [ + + + + ]: 7551490 : if (it == mapAddr.end() || it->second != n) {
1079 : : return -5;
1080 : : }
1081 [ + - - + : 7551447 : if (info.nRandomPos < 0 || (size_t)info.nRandomPos >= vRandom.size() || vRandom[info.nRandomPos] != n)
+ - + - ]
1082 : : return -14;
1083 [ + - ]: 7551447 : if (info.m_last_try < NodeSeconds{0s}) {
1084 : : return -6;
1085 : : }
1086 [ + + ]: 7551447 : if (info.m_last_success < NodeSeconds{0s}) {
1087 : : return -8;
1088 : : }
1089 : : }
1090 : :
1091 [ + - ]: 3051 : if (setTried.size() != (size_t)nTried)
1092 : : return -9;
1093 [ + - ]: 3051 : if (mapNew.size() != (size_t)nNew)
1094 : : return -10;
1095 : :
1096 [ + + ]: 784107 : for (int n = 0; n < ADDRMAN_TRIED_BUCKET_COUNT; n++) {
1097 [ + + ]: 50768640 : for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
1098 [ + + ]: 49987584 : if (vvTried[n][i] != -1) {
1099 [ + - ]: 3573568 : if (!setTried.contains(vvTried[n][i]))
1100 : : return -11;
1101 : 3573568 : const auto it{mapInfo.find(vvTried[n][i])};
1102 [ + - + - : 3573568 : if (it == mapInfo.end() || it->second.GetTriedBucket(nKey, m_netgroupman) != n) {
+ - ]
1103 : 0 : return -17;
1104 : : }
1105 [ + - + - ]: 3573568 : if (it->second.GetBucketPosition(nKey, false, n) != i) {
1106 : : return -18;
1107 : : }
1108 : 3573568 : setTried.erase(vvTried[n][i]);
1109 : : }
1110 : : }
1111 : : }
1112 : :
1113 [ + + ]: 3127275 : for (int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; n++) {
1114 [ + + ]: 203074560 : for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
1115 [ + + ]: 199950336 : if (vvNew[n][i] != -1) {
1116 [ + - ]: 4137317 : if (!mapNew.contains(vvNew[n][i]))
1117 : : return -12;
1118 : 4137317 : const auto it{mapInfo.find(vvNew[n][i])};
1119 [ + - + - : 4137317 : if (it == mapInfo.end() || it->second.GetBucketPosition(nKey, true, n) != i) {
+ - ]
1120 : 0 : return -19;
1121 : : }
1122 [ + - + + ]: 4137317 : if (--mapNew[vvNew[n][i]] == 0)
1123 : 3976951 : mapNew.erase(vvNew[n][i]);
1124 : : }
1125 : : }
1126 : : }
1127 : :
1128 [ + - ]: 3051 : if (setTried.size())
1129 : : return -13;
1130 [ + - ]: 3051 : if (mapNew.size())
1131 : : return -15;
1132 [ + + ]: 6102 : if (nKey.IsNull())
1133 : : return -16;
1134 : :
1135 : : // It's possible that m_network_counts may have all-zero entries that local_counts
1136 : : // doesn't have if addrs from a network were being added and then removed again in the past.
1137 [ + - ]: 3043 : if (m_network_counts.size() < local_counts.size()) {
1138 : : return -20;
1139 : : }
1140 [ + + + - ]: 12534 : for (const auto& [net, count] : m_network_counts) {
1141 [ + - + - : 18378 : if (local_counts[net].n_new != count.n_new || local_counts[net].n_tried != count.n_tried) {
+ - ]
1142 : 0 : return -21;
1143 : : }
1144 : : }
1145 : :
1146 : : return 0;
1147 : 3345 : }
1148 : :
1149 : 7725279 : size_t AddrManImpl::Size(std::optional<Network> net, std::optional<bool> in_new) const
1150 : : {
1151 : 7725279 : LOCK(cs);
1152 [ + - ]: 7725279 : Check();
1153 [ + - ]: 7725279 : auto ret = Size_(net, in_new);
1154 [ + - ]: 7725279 : Check();
1155 [ + - ]: 7725279 : return ret;
1156 : 7725279 : }
1157 : :
1158 : 10856768 : bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
1159 : : {
1160 : 10856768 : LOCK(cs);
1161 [ + - ]: 10856768 : Check();
1162 [ + - ]: 10856768 : auto ret = Add_(vAddr, source, time_penalty);
1163 [ + - ]: 10856768 : Check();
1164 [ + - ]: 10856768 : return ret;
1165 : 10856768 : }
1166 : :
1167 : 6248337 : bool AddrManImpl::Good(const CService& addr, NodeSeconds time)
1168 : : {
1169 : 6248337 : LOCK(cs);
1170 [ + - ]: 6248337 : Check();
1171 [ + - ]: 6248337 : auto ret = Good_(addr, /*test_before_evict=*/true, time);
1172 [ + - ]: 6248337 : Check();
1173 [ + - ]: 6248337 : return ret;
1174 : 6248337 : }
1175 : :
1176 : 8533 : void AddrManImpl::Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
1177 : : {
1178 : 8533 : LOCK(cs);
1179 [ + - ]: 8533 : Check();
1180 [ + - ]: 8533 : Attempt_(addr, fCountFailure, time);
1181 [ + - ]: 8533 : Check();
1182 : 8533 : }
1183 : :
1184 : 11058 : void AddrManImpl::ResolveCollisions()
1185 : : {
1186 : 11058 : LOCK(cs);
1187 [ + - ]: 11058 : Check();
1188 [ + - ]: 11058 : ResolveCollisions_();
1189 [ + - ]: 11058 : Check();
1190 : 11058 : }
1191 : :
1192 : 79119 : std::pair<CAddress, NodeSeconds> AddrManImpl::SelectTriedCollision()
1193 : : {
1194 : 79119 : LOCK(cs);
1195 [ + - ]: 79119 : Check();
1196 [ + - ]: 79119 : auto ret = SelectTriedCollision_();
1197 [ + - ]: 79119 : Check();
1198 [ + - ]: 79119 : return ret;
1199 : 79119 : }
1200 : :
1201 : 2110 : std::pair<CAddress, NodeSeconds> AddrManImpl::Select(bool new_only, const std::unordered_set<Network>& networks) const
1202 : : {
1203 : 2110 : LOCK(cs);
1204 [ + - ]: 2110 : Check();
1205 [ + - ]: 2110 : auto addrRet = Select_(new_only, networks);
1206 [ + - ]: 2110 : Check();
1207 [ + - ]: 2110 : return addrRet;
1208 : 2110 : }
1209 : :
1210 : 11941 : std::vector<CAddress> AddrManImpl::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered) const
1211 : : {
1212 : 11941 : LOCK(cs);
1213 [ + - ]: 11941 : Check();
1214 [ + - ]: 11941 : auto addresses = GetAddr_(max_addresses, max_pct, network, filtered);
1215 [ + - ]: 11941 : Check();
1216 [ + - ]: 11941 : return addresses;
1217 : 11941 : }
1218 : :
1219 : 2 : std::vector<std::pair<AddrInfo, AddressPosition>> AddrManImpl::GetEntries(bool from_tried) const
1220 : : {
1221 : 2 : LOCK(cs);
1222 [ + - ]: 2 : Check();
1223 [ + - ]: 2 : auto addrInfos = GetEntries_(from_tried);
1224 [ + - ]: 2 : Check();
1225 [ + - ]: 2 : return addrInfos;
1226 : 2 : }
1227 : :
1228 : 13421 : void AddrManImpl::Connected(const CService& addr, NodeSeconds time)
1229 : : {
1230 : 13421 : LOCK(cs);
1231 [ + - ]: 13421 : Check();
1232 [ + - ]: 13421 : Connected_(addr, time);
1233 [ + - ]: 13421 : Check();
1234 : 13421 : }
1235 : :
1236 : 75094 : void AddrManImpl::SetServices(const CService& addr, ServiceFlags nServices)
1237 : : {
1238 : 75094 : LOCK(cs);
1239 [ + - ]: 75094 : Check();
1240 [ + - ]: 75094 : SetServices_(addr, nServices);
1241 [ + - ]: 75094 : Check();
1242 : 75094 : }
1243 : :
1244 : 0 : std::optional<AddressPosition> AddrManImpl::FindAddressEntry(const CAddress& addr)
1245 : : {
1246 : 0 : LOCK(cs);
1247 [ # # ]: 0 : Check();
1248 [ # # ]: 0 : auto entry = FindAddressEntry_(addr);
1249 [ # # ]: 0 : Check();
1250 [ # # ]: 0 : return entry;
1251 : 0 : }
1252 : :
1253 : 21678 : AddrMan::AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio)
1254 : 21678 : : m_impl(std::make_unique<AddrManImpl>(netgroupman, deterministic, consistency_check_ratio)) {}
1255 : :
1256 : 21678 : AddrMan::~AddrMan() = default;
1257 : :
1258 : : template <typename Stream>
1259 : 3547 : void AddrMan::Serialize(Stream& s_) const
1260 : : {
1261 : 3547 : m_impl->Serialize<Stream>(s_);
1262 : 3547 : }
1263 : :
1264 : : template <typename Stream>
1265 : 6019 : void AddrMan::Unserialize(Stream& s_)
1266 : : {
1267 : 6019 : m_impl->Unserialize<Stream>(s_);
1268 : 3043 : }
1269 : :
1270 : : // explicit instantiation
1271 : : template void AddrMan::Serialize(HashedSourceWriter<AutoFile>&) const;
1272 : : template void AddrMan::Serialize(DataStream&) const;
1273 : : template void AddrMan::Unserialize(AutoFile&);
1274 : : template void AddrMan::Unserialize(HashVerifier<AutoFile>&);
1275 : : template void AddrMan::Unserialize(DataStream&);
1276 : : template void AddrMan::Unserialize(HashVerifier<DataStream>&);
1277 : :
1278 : 7725279 : size_t AddrMan::Size(std::optional<Network> net, std::optional<bool> in_new) const
1279 : : {
1280 : 7725279 : return m_impl->Size(net, in_new);
1281 : : }
1282 : :
1283 : 10856768 : bool AddrMan::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
1284 : : {
1285 : 10856768 : return m_impl->Add(vAddr, source, time_penalty);
1286 : : }
1287 : :
1288 : 6248337 : bool AddrMan::Good(const CService& addr, NodeSeconds time)
1289 : : {
1290 : 6248337 : return m_impl->Good(addr, time);
1291 : : }
1292 : :
1293 : 8533 : void AddrMan::Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
1294 : : {
1295 : 8533 : m_impl->Attempt(addr, fCountFailure, time);
1296 : 8533 : }
1297 : :
1298 : 11058 : void AddrMan::ResolveCollisions()
1299 : : {
1300 : 11058 : m_impl->ResolveCollisions();
1301 : 11058 : }
1302 : :
1303 : 79119 : std::pair<CAddress, NodeSeconds> AddrMan::SelectTriedCollision()
1304 : : {
1305 : 79119 : return m_impl->SelectTriedCollision();
1306 : : }
1307 : :
1308 : 2110 : std::pair<CAddress, NodeSeconds> AddrMan::Select(bool new_only, const std::unordered_set<Network>& networks) const
1309 : : {
1310 : 2110 : return m_impl->Select(new_only, networks);
1311 : : }
1312 : :
1313 : 11941 : std::vector<CAddress> AddrMan::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered) const
1314 : : {
1315 : 11941 : return m_impl->GetAddr(max_addresses, max_pct, network, filtered);
1316 : : }
1317 : :
1318 : 2 : std::vector<std::pair<AddrInfo, AddressPosition>> AddrMan::GetEntries(bool use_tried) const
1319 : : {
1320 : 2 : return m_impl->GetEntries(use_tried);
1321 : : }
1322 : :
1323 : 13421 : void AddrMan::Connected(const CService& addr, NodeSeconds time)
1324 : : {
1325 : 13421 : m_impl->Connected(addr, time);
1326 : 13421 : }
1327 : :
1328 : 75094 : void AddrMan::SetServices(const CService& addr, ServiceFlags nServices)
1329 : : {
1330 : 75094 : m_impl->SetServices(addr, nServices);
1331 : 75094 : }
1332 : :
1333 : 0 : std::optional<AddressPosition> AddrMan::FindAddressEntry(const CAddress& addr)
1334 : : {
1335 : 0 : return m_impl->FindAddressEntry(addr);
1336 : : }
|