| 1 | |
| 2 | #include <algorithm> |
| 3 | #include <boost/thread/locks.hpp> |
| 4 | #include <boost/thread/mutex.hpp> |
| 5 | |
| 6 | #include "cryptonote_core/cryptonote_core.h" |
| 7 | |
| 8 | namespace cryptonote |
| 9 | { |
| 10 | namespace rpc |
| 11 | { |
| 12 | namespace |
| 13 | { |
| 14 | output_distribution_data |
| 15 | process_distribution(bool cumulative, std::uint64_t start_height, std::vector<std::uint64_t> distribution, std::uint64_t base) |
| 16 | { |
| 17 | if (!cumulative && !distribution.empty()) |
| 18 | { |
| 19 | for (std::size_t n = distribution.size() - 1; 0 < n; --n) |
| 20 | distribution[n] -= distribution[n - 1]; |
| 21 | distribution[0] -= base; |
| 22 | } |
| 23 | |
| 24 | return {std::move(distribution), start_height, base}; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | boost::optional<output_distribution_data> |
| 29 | RpcHandler::get_output_distribution(const std::function<bool(uint64_t, uint64_t, uint64_t, uint64_t&, std::vector<uint64_t>&, uint64_t&)> &f, uint64_t amount, uint64_t from_height, uint64_t to_height, const std::function<crypto::hash(uint64_t)> &get_hash, bool cumulative, uint64_t blockchain_height) |
| 30 | { |
| 31 | static struct D |
| 32 | { |
| 33 | boost::mutex mutex; |
| 34 | std::vector<std::uint64_t> cached_distribution; |
| 35 | std::uint64_t cached_from, cached_to, cached_start_height, cached_base; |
| 36 | crypto::hash cached_m10_hash; |
| 37 | crypto::hash cached_top_hash; |
| 38 | bool cached; |
| 39 | D(): cached_from(0), cached_to(0), cached_start_height(0), cached_base(0), cached_m10_hash(crypto::null_hash), cached_top_hash(crypto::null_hash), cached(false) {} |
| 40 | } d; |
| 41 | const boost::unique_lock<boost::mutex> lock(d.mutex); |
| 42 | |
| 43 | crypto::hash top_hash = crypto::null_hash; |
| 44 | if (d.cached_to < blockchain_height) |
| 45 | top_hash = get_hash(d.cached_to); |
| 46 | if (d.cached && amount == 0 && d.cached_from == from_height && d.cached_to == to_height && d.cached_top_hash == top_hash) |
| 47 | return process_distribution(cumulative, d.cached_start_height, d.cached_distribution, d.cached_base); |
| 48 | |
| 49 | std::vector<std::uint64_t> distribution; |
| 50 | std::uint64_t start_height, base; |
| 51 | |
| 52 | // see if we can extend the cache - a common case |
| 53 | bool can_extend = d.cached && amount == 0 && d.cached_from == from_height && to_height > d.cached_to && top_hash == d.cached_top_hash; |
| 54 | if (!can_extend) |
| 55 | { |
| 56 | // we kept track of the hash 10 blocks below, if it exists, so if it matches, |
| 57 | // we can still pop the last 10 cached slots and try again |
| 58 | if (d.cached && amount == 0 && d.cached_from == from_height && d.cached_to - d.cached_from >= 10 && to_height > d.cached_to - 10) |
| 59 | { |
| 60 | crypto::hash hash10 = get_hash(d.cached_to - 10); |
| 61 | if (hash10 == d.cached_m10_hash) |
| 62 | { |
| 63 | d.cached_to -= 10; |
| 64 | d.cached_top_hash = hash10; |
| 65 | d.cached_m10_hash = crypto::null_hash; |
| 66 | CHECK_AND_ASSERT_MES(d.cached_distribution.size() >= 10, boost::none, "Cached distribution size does not match cached bounds"); |
| 67 | for (int p = 0; p < 10; ++p) |
| 68 | d.cached_distribution.pop_back(); |
| 69 | can_extend = true; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | if (can_extend) |
| 74 | { |
| 75 | std::vector<std::uint64_t> new_distribution; |
| 76 | if (!f(amount, d.cached_to + 1, to_height, start_height, new_distribution, base)) |
| 77 | return boost::none; |
| 78 | distribution = d.cached_distribution; |
| 79 | distribution.reserve(distribution.size() + new_distribution.size()); |
| 80 | for (const auto &e: new_distribution) |
| 81 | distribution.push_back(e); |
| 82 | start_height = d.cached_start_height; |
| 83 | base = d.cached_base; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | if (!f(amount, from_height, to_height, start_height, distribution, base)) |
| 88 | return boost::none; |
| 89 | } |
| 90 | |
| 91 | if (to_height > 0 && to_height >= from_height) |
| 92 | { |
| 93 | const std::uint64_t offset = std::max(from_height, start_height); |
| 94 | if (offset <= to_height && to_height - offset + 1 < distribution.size()) |
| 95 | distribution.resize(to_height - offset + 1); |
| 96 | } |
| 97 | |
| 98 | if (amount == 0) |
| 99 | { |
| 100 | d.cached_from = from_height; |
| 101 | d.cached_to = to_height; |
| 102 | d.cached_top_hash = get_hash(d.cached_to); |
| 103 | d.cached_m10_hash = d.cached_to >= 10 ? get_hash(d.cached_to - 10) : crypto::null_hash; |
| 104 | d.cached_distribution = distribution; |
| 105 | d.cached_start_height = start_height; |
| 106 | d.cached_base = base; |
| 107 | d.cached = true; |
| 108 | } |
| 109 | |
| 110 | return process_distribution(cumulative, start_height, std::move(distribution), base); |
| 111 | } |
| 112 | } // rpc |
| 113 | } // cryptonote |