LCOV - code coverage report
Current view: top level - src - addrman.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 338 818 41.3 %
Date: 2023-11-06 23:13:05 Functions: 44 67 65.7 %
Branches: 153 1516 10.1 %

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

Generated by: LCOV version 1.14