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