Bitcoin Core Fuzz Coverage Report

Coverage Report

Created: 2026-03-24 13:57

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