LCOV - code coverage report
Current view: top level - src - addrman.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 92.6 % 691 640
Test Date: 2024-08-28 04:44:32 Functions: 86.6 % 67 58
Branches: 60.1 % 770 463

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

Generated by: LCOV version 2.0-1