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