Coverage Report

Created: 2025-06-10 13:21

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