Debian Codegraph @

monero/src/cryptonote_protocol/block_queue.cpp

raw symbols SCIP
1 // Copyright (c) 2017-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <vector>
32 #include <unordered_map>
33 #include <boost/uuid/nil_generator.hpp>
34 #include <boost/uuid/uuid_io.hpp>
35 #include "string_tools.h"
36 #include "cryptonote_protocol_defs.h"
37 #include "common/pruning.h"
38 #include "block_queue.h"
39
40 #undef MONERO_DEFAULT_LOG_CATEGORY
41 #define MONERO_DEFAULT_LOG_CATEGORY "cn.block_queue"
42
43 namespace cryptonote
44 {
45
46 void block_queue::add_blocks(uint64_t height, std::vector<cryptonote::block_complete_entry> bcel, const boost::uuids::uuid &connection_id, const epee::net_utils::network_address &addr, float rate, size_t size)
47 {
48 boost::unique_lock<boost::recursive_mutex> lock(mutex);
49 std::vector<crypto::hash> hashes;
50 bool has_hashes = remove_span(height, &hashes);
51 blocks.insert(span(height, std::move(bcel), connection_id, addr, rate, size));
52 if (has_hashes)
53 {
54 for (std::size_t i = 0; i < hashes.size(); ++i)
55 {
56 requested_hashes.insert(hashes[i]);
57 have_blocks.emplace(hashes[i], height + i);
58 }
59 set_span_hashes(height, connection_id, hashes);
60 }
61 }
62
63 void block_queue::add_blocks(uint64_t height, uint64_t nblocks, const boost::uuids::uuid &connection_id, const epee::net_utils::network_address &addr, boost::posix_time::ptime time)
64 {
65 CHECK_AND_ASSERT_THROW_MES(nblocks > 0, "Empty span");
66 boost::unique_lock<boost::recursive_mutex> lock(mutex);
67 blocks.insert(span(height, nblocks, connection_id, addr, time));
68 }
69
70 void block_queue::flush_spans(const boost::uuids::uuid &connection_id, bool all)
71 {
72 boost::unique_lock<boost::recursive_mutex> lock(mutex);
73 block_map::iterator i = blocks.begin();
74 while (i != blocks.end())
75 {
76 block_map::iterator j = i++;
77 if (j->connection_id == connection_id && (all || j->blocks.size() == 0))
78 {
79 erase_block(j);
80 }
81 }
82 }
83
84 void block_queue::erase_block(block_map::iterator j)
85 {
86 CHECK_AND_ASSERT_THROW_MES(j != blocks.end(), "Invalid iterator");
87 for (const crypto::hash &h: j->hashes)
88 {
89 requested_hashes.erase(h);
90 have_blocks.erase(h);
91 }
92 blocks.erase(j);
93 }
94
95 void block_queue::flush_stale_spans(const std::set<boost::uuids::uuid> &live_connections)
96 {
97 boost::unique_lock<boost::recursive_mutex> lock(mutex);
98 block_map::iterator i = blocks.begin();
99 while (i != blocks.end())
100 {
101 block_map::iterator j = i++;
102 if (j->blocks.empty() && live_connections.find(j->connection_id) == live_connections.end())
103 {
104 erase_block(j);
105 }
106 }
107 }
108
109 bool block_queue::remove_span(uint64_t start_block_height, std::vector<crypto::hash> *hashes)
110 {
111 boost::unique_lock<boost::recursive_mutex> lock(mutex);
112 for (block_map::iterator i = blocks.begin(); i != blocks.end(); ++i)
113 {
114 if (i->start_block_height == start_block_height)
115 {
116 if (hashes)
117 *hashes = std::move(i->hashes);
118 erase_block(i);
119 return true;
120 }
121 }
122 return false;
123 }
124
125 void block_queue::remove_spans(const boost::uuids::uuid &connection_id, uint64_t start_block_height)
126 {
127 boost::unique_lock<boost::recursive_mutex> lock(mutex);
128 for (block_map::iterator i = blocks.begin(); i != blocks.end(); )
129 {
130 block_map::iterator j = i++;
131 if (j->connection_id == connection_id && j->start_block_height <= start_block_height)
132 {
133 erase_block(j);
134 }
135 }
136 }
137
138 uint64_t block_queue::get_max_block_height() const
139 {
140 boost::unique_lock<boost::recursive_mutex> lock(mutex);
141 uint64_t height = 0;
142 for (const auto &span: blocks)
143 {
144 const uint64_t h = span.start_block_height + span.nblocks - 1;
145 if (h > height)
146 height = h;
147 }
148 return height;
149 }
150
151 uint64_t block_queue::get_next_needed_height(uint64_t blockchain_height) const
152 {
153 boost::unique_lock<boost::recursive_mutex> lock(mutex);
154 if (blocks.empty())
155 return blockchain_height;
156 uint64_t last_needed_height = blockchain_height;
157 bool first = true;
158 for (const auto &span: blocks)
159 {
160 if (span.start_block_height + span.nblocks - 1 < blockchain_height)
161 continue;
162 if (span.start_block_height != last_needed_height || (first && span.blocks.empty()))
163 return last_needed_height;
164 last_needed_height = span.start_block_height + span.nblocks;
165 first = false;
166 }
167 return last_needed_height;
168 }
169
170 void block_queue::print() const
171 {
172 boost::unique_lock<boost::recursive_mutex> lock(mutex);
173 MDEBUG("Block queue has " << blocks.size() << " spans");
174 for (const auto &span: blocks)
175 MDEBUG(" " << span.start_block_height << " - " << (span.start_block_height+span.nblocks-1) << " (" << span.nblocks << ") - " << (span.blocks.empty() ? "scheduled" : "filled ") << " " << span.connection_id << " (" << ((unsigned)(span.rate*10/1024.f))/10.f << " kB/s)");
176 }
177
178 std::string block_queue::get_overview(uint64_t blockchain_height) const
179 {
180 boost::unique_lock<boost::recursive_mutex> lock(mutex);
181 if (blocks.empty())
182 return "[]";
183 block_map::const_iterator i = blocks.begin();
184 std::string s = std::string("[");
185 uint64_t expected = blockchain_height;
186 while (i != blocks.end())
187 {
188 if (expected > i->start_block_height)
189 {
190 s += "<";
191 }
192 else
193 {
194 if (expected < i->start_block_height)
195 s += std::string(std::max((uint64_t)1, (i->start_block_height - expected) / (i->nblocks ? i->nblocks : 1)), '_');
196 s += i->blocks.empty() ? "." : i->start_block_height == blockchain_height ? "m" : "o";
197 expected = i->start_block_height + i->nblocks;
198 }
199 ++i;
200 }
201 s += "]";
202 return s;
203 }
204
205 inline bool block_queue::requested_internal(const crypto::hash &hash) const
206 {
207 return requested_hashes.find(hash) != requested_hashes.end();
208 }
209
210 bool block_queue::requested(const crypto::hash &hash) const
211 {
212 boost::unique_lock<boost::recursive_mutex> lock(mutex);
213 return requested_internal(hash);
214 }
215
216 bool block_queue::have(const crypto::hash &hash) const
217 {
218 boost::unique_lock<boost::recursive_mutex> lock(mutex);
219 return have_blocks.find(hash) != have_blocks.end();
220 }
221
222 std::uint64_t block_queue::have_height(const crypto::hash &hash) const
223 {
224 boost::unique_lock<boost::recursive_mutex> lock(mutex);
225 const auto elem = have_blocks.find(hash);
226 if (elem == have_blocks.end())
227 return std::numeric_limits<std::uint64_t>::max();
228 return elem->second;
229 }
230
231
232 std::pair<uint64_t, uint64_t> block_queue::reserve_span(uint64_t first_block_height, uint64_t last_block_height, uint64_t max_blocks, const boost::uuids::uuid &connection_id, const epee::net_utils::network_address &addr, bool sync_pruned_blocks, uint32_t local_pruning_seed, uint32_t pruning_seed, uint64_t blockchain_height, const std::vector<std::pair<crypto::hash, uint64_t>> &block_hashes, boost::posix_time::ptime time)
233 {
234 boost::unique_lock<boost::recursive_mutex> lock(mutex);
235
236 MDEBUG("reserve_span: first_block_height " << first_block_height << ", last_block_height " << last_block_height
237 << ", max " << max_blocks << ", peer seed " << epee::string_tools::to_string_hex(pruning_seed) << ", blockchain_height " <<
238 blockchain_height << ", block hashes size " << block_hashes.size() << ", local seed " << epee::string_tools::to_string_hex(local_pruning_seed)
239 << ", sync_pruned_blocks " << sync_pruned_blocks);
240 if (last_block_height < first_block_height || max_blocks == 0)
241 {
242 MDEBUG("reserve_span: early out: first_block_height " << first_block_height << ", last_block_height " << last_block_height << ", max_blocks " << max_blocks);
243 return std::make_pair(0, 0);
244 }
245 if (block_hashes.size() > last_block_height)
246 {
247 MDEBUG("reserve_span: more block hashes than fit within last_block_height: " << block_hashes.size() << " and " << last_block_height);
248 return std::make_pair(0, 0);
249 }
250
251 // skip everything we've already requested
252 uint64_t span_start_height = last_block_height - block_hashes.size() + 1;
253 std::vector<std::pair<crypto::hash, uint64_t>>::const_iterator i = block_hashes.begin();
254 while (i != block_hashes.end() && requested_internal((*i).first))
255 {
256 ++i;
257 ++span_start_height;
258 }
259
260 if (!sync_pruned_blocks)
261 {
262 // if the peer's pruned for the starting block and its unpruned stripe comes next, start downloading from there
263 const uint32_t next_unpruned_height = tools::get_next_unpruned_block_height(span_start_height, blockchain_height, pruning_seed);
264 MDEBUG("reserve_span: next_unpruned_height " << next_unpruned_height << " from " << span_start_height << " and seed "
265 << epee::string_tools::to_string_hex(pruning_seed) << ", limit " << span_start_height + CRYPTONOTE_PRUNING_STRIPE_SIZE);
266 if (next_unpruned_height > span_start_height && next_unpruned_height < span_start_height + CRYPTONOTE_PRUNING_STRIPE_SIZE)
267 {
268 MDEBUG("We can download from next span: ideal height " << span_start_height << ", next unpruned height " << next_unpruned_height <<
269 "(+" << next_unpruned_height - span_start_height << "), current seed " << pruning_seed);
270 span_start_height = next_unpruned_height;
271 }
272 }
273 MDEBUG("span_start_height: " <<span_start_height);
274 const uint64_t block_hashes_start_height = last_block_height - block_hashes.size() + 1;
275 if (span_start_height >= block_hashes.size() + block_hashes_start_height)
276 {
277 MDEBUG("Out of hashes, cannot reserve");
278 return std::make_pair(0, 0);
279 }
280
281 i = block_hashes.begin() + span_start_height - block_hashes_start_height;
282 while (i != block_hashes.end() && requested_internal((*i).first))
283 {
284 ++i;
285 ++span_start_height;
286 }
287
288 uint64_t span_length = 0;
289 std::vector<crypto::hash> hashes;
290 bool first_is_pruned = sync_pruned_blocks && !tools::has_unpruned_block(span_start_height + span_length, blockchain_height, local_pruning_seed);
291 while (i != block_hashes.end() && span_length < max_blocks && (sync_pruned_blocks || tools::has_unpruned_block(span_start_height + span_length, blockchain_height, pruning_seed)))
292 {
293 // if we want to sync pruned blocks, stop at the first block for which we need full data
294 if (sync_pruned_blocks && first_is_pruned == tools::has_unpruned_block(span_start_height + span_length, blockchain_height, local_pruning_seed))
295 {
296 MDEBUG("Stopping at " << span_start_height + span_length << " for peer on stripe " << tools::get_pruning_stripe(pruning_seed) << " as we need full data for " << tools::get_pruning_stripe(local_pruning_seed));
297 break;
298 }
299 hashes.push_back((*i).first);
300 ++i;
301 ++span_length;
302 }
303 if (span_length == 0)
304 {
305 MDEBUG("span_length 0, cannot reserve");
306 return std::make_pair(0, 0);
307 }
308 MDEBUG("Reserving span " << span_start_height << " - " << (span_start_height + span_length - 1) << " for " << connection_id);
309 add_blocks(span_start_height, span_length, connection_id, addr, time);
310 set_span_hashes(span_start_height, connection_id, hashes);
311 return std::make_pair(span_start_height, span_length);
312 }
313
314 std::pair<uint64_t, uint64_t> block_queue::get_next_span_if_scheduled(std::vector<crypto::hash> &hashes, boost::uuids::uuid &connection_id, boost::posix_time::ptime &time) const
315 {
316 boost::unique_lock<boost::recursive_mutex> lock(mutex);
317 if (blocks.empty())
318 return std::make_pair(0, 0);
319 block_map::const_iterator i = blocks.begin();
320 if (i == blocks.end())
321 return std::make_pair(0, 0);
322 if (!i->blocks.empty())
323 return std::make_pair(0, 0);
324 hashes = i->hashes;
325 connection_id = i->connection_id;
326 time = i->time;
327 return std::make_pair(i->start_block_height, i->nblocks);
328 }
329
330 void block_queue::reset_next_span_time(boost::posix_time::ptime t)
331 {
332 boost::unique_lock<boost::recursive_mutex> lock(mutex);
333 CHECK_AND_ASSERT_THROW_MES(!blocks.empty(), "No next span to reset time");
334 block_map::iterator i = blocks.begin();
335 CHECK_AND_ASSERT_THROW_MES(i != blocks.end(), "No next span to reset time");
336 CHECK_AND_ASSERT_THROW_MES(i->blocks.empty(), "Next span is not empty");
337 (boost::posix_time::ptime&)i->time = t; // sod off, time doesn't influence sorting
338 }
339
340 void block_queue::set_span_hashes(uint64_t start_height, const boost::uuids::uuid &connection_id, std::vector<crypto::hash> hashes)
341 {
342 boost::unique_lock<boost::recursive_mutex> lock(mutex);
343 for (block_map::iterator i = blocks.begin(); i != blocks.end(); ++i)
344 {
345 if (i->start_block_height == start_height && i->connection_id == connection_id)
346 {
347 span s = *i;
348 erase_block(i);
349 s.hashes = std::move(hashes);
350 for (const crypto::hash &h: s.hashes)
351 requested_hashes.insert(h);
352 blocks.insert(s);
353 return;
354 }
355 }
356 }
357
358 bool block_queue::get_next_span(uint64_t &height, std::vector<cryptonote::block_complete_entry> &bcel, boost::uuids::uuid &connection_id, epee::net_utils::network_address &addr, bool filled) const
359 {
360 boost::unique_lock<boost::recursive_mutex> lock(mutex);
361 if (blocks.empty())
362 return false;
363 block_map::const_iterator i = blocks.begin();
364 for (; i != blocks.end(); ++i)
365 {
366 if (!filled || !i->blocks.empty())
367 {
368 height = i->start_block_height;
369 bcel = i->blocks;
370 connection_id = i->connection_id;
371 addr = i->origin;
372 return true;
373 }
374 }
375 return false;
376 }
377
378 bool block_queue::has_next_span(const boost::uuids::uuid &connection_id, bool &filled, boost::posix_time::ptime &time) const
379 {
380 boost::unique_lock<boost::recursive_mutex> lock(mutex);
381 if (blocks.empty())
382 return false;
383 block_map::const_iterator i = blocks.begin();
384 if (i == blocks.end())
385 return false;
386 if (i->connection_id != connection_id)
387 return false;
388 filled = !i->blocks.empty();
389 time = i->time;
390 return true;
391 }
392
393 bool block_queue::has_next_span(uint64_t height, bool &filled, boost::posix_time::ptime &time, boost::uuids::uuid &connection_id) const
394 {
395 boost::unique_lock<boost::recursive_mutex> lock(mutex);
396 if (blocks.empty())
397 return false;
398 block_map::const_iterator i = blocks.begin();
399 if (i == blocks.end())
400 return false;
401 if (i->start_block_height > height)
402 return false;
403 filled = !i->blocks.empty();
404 time = i->time;
405 connection_id = i->connection_id;
406 return true;
407 }
408
409 size_t block_queue::get_data_size() const
410 {
411 boost::unique_lock<boost::recursive_mutex> lock(mutex);
412 size_t size = 0;
413 for (const auto &span: blocks)
414 size += span.size;
415 return size;
416 }
417
418 size_t block_queue::get_num_filled_spans_prefix() const
419 {
420 boost::unique_lock<boost::recursive_mutex> lock(mutex);
421
422 if (blocks.empty())
423 return 0;
424 block_map::const_iterator i = blocks.begin();
425 size_t size = 0;
426 while (i != blocks.end() && !i->blocks.empty())
427 {
428 ++i;
429 ++size;
430 }
431 return size;
432 }
433
434 size_t block_queue::get_num_filled_spans() const
435 {
436 boost::unique_lock<boost::recursive_mutex> lock(mutex);
437 size_t size = 0;
438 for (const auto &span: blocks)
439 if (!span.blocks.empty())
440 ++size;
441 return size;
442 }
443
444 crypto::hash block_queue::get_last_known_hash(const boost::uuids::uuid &connection_id) const
445 {
446 boost::unique_lock<boost::recursive_mutex> lock(mutex);
447 crypto::hash hash = crypto::null_hash;
448 uint64_t highest_height = 0;
449 for (const auto &span: blocks)
450 {
451 if (span.connection_id != connection_id)
452 continue;
453 uint64_t h = span.start_block_height + span.nblocks - 1;
454 if (h > highest_height && span.hashes.size() == span.nblocks)
455 {
456 hash = span.hashes.back();
457 highest_height = h;
458 }
459 }
460 return hash;
461 }
462
463 bool block_queue::has_spans(const boost::uuids::uuid &connection_id) const
464 {
465 for (const auto &span: blocks)
466 {
467 if (span.connection_id == connection_id)
468 return true;
469 }
470 return false;
471 }
472
473 float block_queue::get_speed(const boost::uuids::uuid &connection_id) const
474 {
475 boost::unique_lock<boost::recursive_mutex> lock(mutex);
476 std::unordered_map<boost::uuids::uuid, float, boost::hash<boost::uuids::uuid>> speeds;
477 for (const auto &span: blocks)
478 {
479 if (span.blocks.empty())
480 continue;
481 // note that the average below does not average over the whole set, but over the
482 // previous pseudo average and the latest rate: this gives much more importance
483 // to the latest measurements, which is fine here
484 const auto i = speeds.find(span.connection_id);
485 if (i == speeds.end())
486 speeds.insert(std::make_pair(span.connection_id, span.rate));
487 else
488 i->second = (i->second + span.rate) / 2;
489 }
490 float conn_rate = -1, best_rate = 0;
491 for (const auto &i: speeds)
492 {
493 if (i.first == connection_id)
494 conn_rate = i.second;
495 if (i.second > best_rate)
496 best_rate = i.second;
497 }
498
499 if (conn_rate <= 0)
500 return 1.0f; // not found, assume good speed
501 if (best_rate == 0)
502 return 1.0f; // everything dead ? Can't happen, but let's trap anyway
503
504 const float speed = conn_rate / best_rate;
505 MTRACE(" Relative speed for " << connection_id << ": " << speed << " (" << conn_rate << "/" << best_rate);
506 return speed;
507 }
508
509 float block_queue::get_download_rate(const boost::uuids::uuid &connection_id) const
510 {
511 boost::unique_lock<boost::recursive_mutex> lock(mutex);
512 float conn_rate = -1.f;
513 for (const auto &span: blocks)
514 {
515 if (span.blocks.empty())
516 continue;
517 if (span.connection_id != connection_id)
518 continue;
519 // note that the average below does not average over the whole set, but over the
520 // previous pseudo average and the latest rate: this gives much more importance
521 // to the latest measurements, which is fine here
522 if (conn_rate < 0.f)
523 conn_rate = span.rate;
524 else
525 conn_rate = (conn_rate + span.rate) / 2;
526 }
527
528 if (conn_rate < 0)
529 conn_rate = 0.0f;
530 MTRACE("Download rate for " << connection_id << ": " << conn_rate << " b/s");
531 return conn_rate;
532 }
533
534 bool block_queue::foreach(std::function<bool(const span&)> f) const
535 {
536 boost::unique_lock<boost::recursive_mutex> lock(mutex);
537 block_map::const_iterator i = blocks.begin();
538 while (i != blocks.end())
539 if (!f(*i++))
540 return false;
541 return true;
542 }
543
544 }