| 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 | |
| 157 | uint64_t covered_until = blockchain_height; |
| 158 | |
| 159 | for (const auto &span: blocks) |
| 160 | { |
| 161 | // Ignore spans entirely below current chain height |
| 162 | const uint64_t span_end = span.start_block_height + span.nblocks - 1; |
| 163 | if (span_end < blockchain_height) |
| 164 | continue; |
| 165 | |
| 166 | // If this span starts after what we already have/scheduled, we found the first gap |
| 167 | if (span.start_block_height > covered_until) |
| 168 | return covered_until; |
| 169 | |
| 170 | // This span overlaps or is adjacent; extend coverage regardless of filled/scheduled |
| 171 | if (span.start_block_height <= covered_until) |
| 172 | covered_until = std::max(covered_until, span.start_block_height + span.nblocks); |
| 173 | } |
| 174 | |
| 175 | return covered_until; |
| 176 | } |
| 177 | |
| 178 | void block_queue::print() const |
| 179 | { |
| 180 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 181 | MDEBUG("Block queue has " << blocks.size() << " spans"); |
| 182 | for (const auto &span: blocks) |
| 183 | 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)"); |
| 184 | } |
| 185 | |
| 186 | std::string block_queue::get_overview(uint64_t blockchain_height) const |
| 187 | { |
| 188 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 189 | if (blocks.empty()) |
| 190 | return "[]"; |
| 191 | block_map::const_iterator i = blocks.begin(); |
| 192 | std::string s = std::string("["); |
| 193 | uint64_t expected = blockchain_height; |
| 194 | while (i != blocks.end()) |
| 195 | { |
| 196 | if (expected > i->start_block_height) |
| 197 | { |
| 198 | s += "<"; |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | if (expected < i->start_block_height) |
| 203 | s += std::string(std::max((uint64_t)1, (i->start_block_height - expected) / (i->nblocks ? i->nblocks : 1)), '_'); |
| 204 | s += i->blocks.empty() ? "." : i->start_block_height == blockchain_height ? "m" : "o"; |
| 205 | expected = i->start_block_height + i->nblocks; |
| 206 | } |
| 207 | ++i; |
| 208 | } |
| 209 | s += "]"; |
| 210 | return s; |
| 211 | } |
| 212 | |
| 213 | inline bool block_queue::requested_internal(const crypto::hash &hash) const |
| 214 | { |
| 215 | return requested_hashes.find(hash) != requested_hashes.end(); |
| 216 | } |
| 217 | |
| 218 | bool block_queue::requested(const crypto::hash &hash) const |
| 219 | { |
| 220 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 221 | return requested_internal(hash); |
| 222 | } |
| 223 | |
| 224 | bool block_queue::have(const crypto::hash &hash) const |
| 225 | { |
| 226 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 227 | return have_blocks.find(hash) != have_blocks.end(); |
| 228 | } |
| 229 | |
| 230 | std::uint64_t block_queue::have_height(const crypto::hash &hash) const |
| 231 | { |
| 232 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 233 | const auto elem = have_blocks.find(hash); |
| 234 | if (elem == have_blocks.end()) |
| 235 | return std::numeric_limits<std::uint64_t>::max(); |
| 236 | return elem->second; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | 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) |
| 241 | { |
| 242 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 243 | |
| 244 | MDEBUG("reserve_span: first_block_height " << first_block_height << ", last_block_height " << last_block_height |
| 245 | << ", max " << max_blocks << ", peer seed " << epee::string_tools::to_string_hex(pruning_seed) << ", blockchain_height " << |
| 246 | blockchain_height << ", block hashes size " << block_hashes.size() << ", local seed " << epee::string_tools::to_string_hex(local_pruning_seed) |
| 247 | << ", sync_pruned_blocks " << sync_pruned_blocks); |
| 248 | if (last_block_height < first_block_height || max_blocks == 0) |
| 249 | { |
| 250 | MDEBUG("reserve_span: early out: first_block_height " << first_block_height << ", last_block_height " << last_block_height << ", max_blocks " << max_blocks); |
| 251 | return std::make_pair(0, 0); |
| 252 | } |
| 253 | if (block_hashes.size() > last_block_height) |
| 254 | { |
| 255 | MDEBUG("reserve_span: more block hashes than fit within last_block_height: " << block_hashes.size() << " and " << last_block_height); |
| 256 | return std::make_pair(0, 0); |
| 257 | } |
| 258 | |
| 259 | // skip everything we've already requested |
| 260 | uint64_t span_start_height = last_block_height - block_hashes.size() + 1; |
| 261 | std::vector<std::pair<crypto::hash, uint64_t>>::const_iterator i = block_hashes.begin(); |
| 262 | while (i != block_hashes.end() && requested_internal((*i).first)) |
| 263 | { |
| 264 | ++i; |
| 265 | ++span_start_height; |
| 266 | } |
| 267 | |
| 268 | if (!sync_pruned_blocks) |
| 269 | { |
| 270 | // if the peer's pruned for the starting block and its unpruned stripe comes next, start downloading from there |
| 271 | const uint32_t next_unpruned_height = tools::get_next_unpruned_block_height(span_start_height, blockchain_height, pruning_seed); |
| 272 | MDEBUG("reserve_span: next_unpruned_height " << next_unpruned_height << " from " << span_start_height << " and seed " |
| 273 | << epee::string_tools::to_string_hex(pruning_seed) << ", limit " << span_start_height + CRYPTONOTE_PRUNING_STRIPE_SIZE); |
| 274 | if (next_unpruned_height > span_start_height && next_unpruned_height < span_start_height + CRYPTONOTE_PRUNING_STRIPE_SIZE) |
| 275 | { |
| 276 | MDEBUG("We can download from next span: ideal height " << span_start_height << ", next unpruned height " << next_unpruned_height << |
| 277 | "(+" << next_unpruned_height - span_start_height << "), current seed " << pruning_seed); |
| 278 | span_start_height = next_unpruned_height; |
| 279 | } |
| 280 | } |
| 281 | MDEBUG("span_start_height: " <<span_start_height); |
| 282 | const uint64_t block_hashes_start_height = last_block_height - block_hashes.size() + 1; |
| 283 | if (span_start_height >= block_hashes.size() + block_hashes_start_height) |
| 284 | { |
| 285 | MDEBUG("Out of hashes, cannot reserve"); |
| 286 | return std::make_pair(0, 0); |
| 287 | } |
| 288 | |
| 289 | i = block_hashes.begin() + span_start_height - block_hashes_start_height; |
| 290 | while (i != block_hashes.end() && requested_internal((*i).first)) |
| 291 | { |
| 292 | ++i; |
| 293 | ++span_start_height; |
| 294 | } |
| 295 | |
| 296 | uint64_t span_length = 0; |
| 297 | std::vector<crypto::hash> hashes; |
| 298 | bool first_is_pruned = sync_pruned_blocks && !tools::has_unpruned_block(span_start_height + span_length, blockchain_height, local_pruning_seed); |
| 299 | 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))) |
| 300 | { |
| 301 | // if we want to sync pruned blocks, stop at the first block for which we need full data |
| 302 | if (sync_pruned_blocks && first_is_pruned == tools::has_unpruned_block(span_start_height + span_length, blockchain_height, local_pruning_seed)) |
| 303 | { |
| 304 | 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)); |
| 305 | break; |
| 306 | } |
| 307 | hashes.push_back((*i).first); |
| 308 | ++i; |
| 309 | ++span_length; |
| 310 | } |
| 311 | if (span_length == 0) |
| 312 | { |
| 313 | MDEBUG("span_length 0, cannot reserve"); |
| 314 | return std::make_pair(0, 0); |
| 315 | } |
| 316 | MDEBUG("Reserving span " << span_start_height << " - " << (span_start_height + span_length - 1) << " for " << connection_id); |
| 317 | add_blocks(span_start_height, span_length, connection_id, addr, time); |
| 318 | set_span_hashes(span_start_height, connection_id, hashes); |
| 319 | return std::make_pair(span_start_height, span_length); |
| 320 | } |
| 321 | |
| 322 | 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 |
| 323 | { |
| 324 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 325 | if (blocks.empty()) |
| 326 | return std::make_pair(0, 0); |
| 327 | block_map::const_iterator i = blocks.begin(); |
| 328 | if (i == blocks.end()) |
| 329 | return std::make_pair(0, 0); |
| 330 | if (!i->blocks.empty()) |
| 331 | return std::make_pair(0, 0); |
| 332 | hashes = i->hashes; |
| 333 | connection_id = i->connection_id; |
| 334 | time = i->time; |
| 335 | return std::make_pair(i->start_block_height, i->nblocks); |
| 336 | } |
| 337 | |
| 338 | void block_queue::reset_next_span_time(boost::posix_time::ptime t) |
| 339 | { |
| 340 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 341 | CHECK_AND_ASSERT_MES_NO_RET(!blocks.empty(), "No next span to reset time"); |
| 342 | block_map::iterator i = blocks.begin(); |
| 343 | CHECK_AND_ASSERT_MES_NO_RET(i != blocks.end(), "No next span to reset time"); |
| 344 | CHECK_AND_ASSERT_MES_NO_RET(i->blocks.empty(), "Next span is not empty"); |
| 345 | (boost::posix_time::ptime&)i->time = t; // sod off, time doesn't influence sorting |
| 346 | } |
| 347 | |
| 348 | void block_queue::set_span_hashes(uint64_t start_height, const boost::uuids::uuid &connection_id, std::vector<crypto::hash> hashes) |
| 349 | { |
| 350 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 351 | for (block_map::iterator i = blocks.begin(); i != blocks.end(); ++i) |
| 352 | { |
| 353 | if (i->start_block_height == start_height && i->connection_id == connection_id) |
| 354 | { |
| 355 | span s = *i; |
| 356 | erase_block(i); |
| 357 | s.hashes = std::move(hashes); |
| 358 | for (const crypto::hash &h: s.hashes) |
| 359 | requested_hashes.insert(h); |
| 360 | blocks.insert(s); |
| 361 | return; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | 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 |
| 367 | { |
| 368 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 369 | if (blocks.empty()) |
| 370 | return false; |
| 371 | block_map::const_iterator i = blocks.begin(); |
| 372 | for (; i != blocks.end(); ++i) |
| 373 | { |
| 374 | if (!filled || !i->blocks.empty()) |
| 375 | { |
| 376 | height = i->start_block_height; |
| 377 | bcel = i->blocks; |
| 378 | connection_id = i->connection_id; |
| 379 | addr = i->origin; |
| 380 | return true; |
| 381 | } |
| 382 | } |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | bool block_queue::has_next_span(const boost::uuids::uuid &connection_id, bool &filled, boost::posix_time::ptime &time) const |
| 387 | { |
| 388 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 389 | if (blocks.empty()) |
| 390 | return false; |
| 391 | block_map::const_iterator i = blocks.begin(); |
| 392 | if (i == blocks.end()) |
| 393 | return false; |
| 394 | if (i->connection_id != connection_id) |
| 395 | return false; |
| 396 | filled = !i->blocks.empty(); |
| 397 | time = i->time; |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | bool block_queue::has_next_span(uint64_t height, bool &filled, boost::posix_time::ptime &time, boost::uuids::uuid &connection_id) const |
| 402 | { |
| 403 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 404 | if (blocks.empty()) |
| 405 | return false; |
| 406 | block_map::const_iterator i = blocks.begin(); |
| 407 | if (i == blocks.end()) |
| 408 | return false; |
| 409 | if (i->start_block_height > height) |
| 410 | return false; |
| 411 | filled = !i->blocks.empty(); |
| 412 | time = i->time; |
| 413 | connection_id = i->connection_id; |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | size_t block_queue::get_data_size() const |
| 418 | { |
| 419 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 420 | size_t size = 0; |
| 421 | for (const auto &span: blocks) |
| 422 | size += span.size; |
| 423 | return size; |
| 424 | } |
| 425 | |
| 426 | size_t block_queue::get_num_filled_spans_prefix() const |
| 427 | { |
| 428 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 429 | |
| 430 | if (blocks.empty()) |
| 431 | return 0; |
| 432 | block_map::const_iterator i = blocks.begin(); |
| 433 | size_t size = 0; |
| 434 | while (i != blocks.end() && !i->blocks.empty()) |
| 435 | { |
| 436 | ++i; |
| 437 | ++size; |
| 438 | } |
| 439 | return size; |
| 440 | } |
| 441 | |
| 442 | size_t block_queue::get_num_filled_spans() const |
| 443 | { |
| 444 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 445 | size_t size = 0; |
| 446 | for (const auto &span: blocks) |
| 447 | if (!span.blocks.empty()) |
| 448 | ++size; |
| 449 | return size; |
| 450 | } |
| 451 | |
| 452 | crypto::hash block_queue::get_last_known_hash(const boost::uuids::uuid &connection_id) const |
| 453 | { |
| 454 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 455 | crypto::hash hash = crypto::null_hash; |
| 456 | uint64_t highest_height = 0; |
| 457 | for (const auto &span: blocks) |
| 458 | { |
| 459 | if (span.connection_id != connection_id) |
| 460 | continue; |
| 461 | uint64_t h = span.start_block_height + span.nblocks - 1; |
| 462 | if (h > highest_height && span.hashes.size() == span.nblocks) |
| 463 | { |
| 464 | hash = span.hashes.back(); |
| 465 | highest_height = h; |
| 466 | } |
| 467 | } |
| 468 | return hash; |
| 469 | } |
| 470 | |
| 471 | bool block_queue::has_spans(const boost::uuids::uuid &connection_id) const |
| 472 | { |
| 473 | for (const auto &span: blocks) |
| 474 | { |
| 475 | if (span.connection_id == connection_id) |
| 476 | return true; |
| 477 | } |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | float block_queue::get_speed(const boost::uuids::uuid &connection_id) const |
| 482 | { |
| 483 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 484 | std::unordered_map<boost::uuids::uuid, float, boost::hash<boost::uuids::uuid>> speeds; |
| 485 | for (const auto &span: blocks) |
| 486 | { |
| 487 | if (span.blocks.empty()) |
| 488 | continue; |
| 489 | // note that the average below does not average over the whole set, but over the |
| 490 | // previous pseudo average and the latest rate: this gives much more importance |
| 491 | // to the latest measurements, which is fine here |
| 492 | const auto i = speeds.find(span.connection_id); |
| 493 | if (i == speeds.end()) |
| 494 | speeds.insert(std::make_pair(span.connection_id, span.rate)); |
| 495 | else |
| 496 | i->second = (i->second + span.rate) / 2; |
| 497 | } |
| 498 | float conn_rate = -1, best_rate = 0; |
| 499 | for (const auto &i: speeds) |
| 500 | { |
| 501 | if (i.first == connection_id) |
| 502 | conn_rate = i.second; |
| 503 | if (i.second > best_rate) |
| 504 | best_rate = i.second; |
| 505 | } |
| 506 | |
| 507 | if (conn_rate <= 0) |
| 508 | return 1.0f; // not found, assume good speed |
| 509 | if (best_rate == 0) |
| 510 | return 1.0f; // everything dead ? Can't happen, but let's trap anyway |
| 511 | |
| 512 | const float speed = conn_rate / best_rate; |
| 513 | MTRACE(" Relative speed for " << connection_id << ": " << speed << " (" << conn_rate << "/" << best_rate); |
| 514 | return speed; |
| 515 | } |
| 516 | |
| 517 | float block_queue::get_download_rate(const boost::uuids::uuid &connection_id) const |
| 518 | { |
| 519 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 520 | float conn_rate = -1.f; |
| 521 | for (const auto &span: blocks) |
| 522 | { |
| 523 | if (span.blocks.empty()) |
| 524 | continue; |
| 525 | if (span.connection_id != connection_id) |
| 526 | continue; |
| 527 | // note that the average below does not average over the whole set, but over the |
| 528 | // previous pseudo average and the latest rate: this gives much more importance |
| 529 | // to the latest measurements, which is fine here |
| 530 | if (conn_rate < 0.f) |
| 531 | conn_rate = span.rate; |
| 532 | else |
| 533 | conn_rate = (conn_rate + span.rate) / 2; |
| 534 | } |
| 535 | |
| 536 | if (conn_rate < 0) |
| 537 | conn_rate = 0.0f; |
| 538 | MTRACE("Download rate for " << connection_id << ": " << conn_rate << " b/s"); |
| 539 | return conn_rate; |
| 540 | } |
| 541 | |
| 542 | bool block_queue::foreach(std::function<bool(const span&)> f) const |
| 543 | { |
| 544 | boost::unique_lock<boost::recursive_mutex> lock(mutex); |
| 545 | block_map::const_iterator i = blocks.begin(); |
| 546 | while (i != blocks.end()) |
| 547 | if (!f(*i++)) |
| 548 | return false; |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | } |