2018-10-20 02:06:03 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/thread/locks.hpp>
|
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
|
|
|
|
#include "cryptonote_core/cryptonote_core.h"
|
|
|
|
|
|
|
|
namespace cryptonote
|
|
|
|
{
|
|
|
|
namespace rpc
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
output_distribution_data
|
|
|
|
process_distribution(bool cumulative, std::uint64_t start_height, std::vector<std::uint64_t> distribution, std::uint64_t base)
|
|
|
|
{
|
|
|
|
if (!cumulative && !distribution.empty())
|
|
|
|
{
|
|
|
|
for (std::size_t n = distribution.size() - 1; 0 < n; --n)
|
|
|
|
distribution[n] -= distribution[n - 1];
|
|
|
|
distribution[0] -= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {std::move(distribution), start_height, base};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<output_distribution_data>
|
2019-04-25 16:41:33 +00:00
|
|
|
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)
|
2018-10-20 02:06:03 +00:00
|
|
|
{
|
|
|
|
static struct D
|
|
|
|
{
|
|
|
|
boost::mutex mutex;
|
|
|
|
std::vector<std::uint64_t> cached_distribution;
|
|
|
|
std::uint64_t cached_from, cached_to, cached_start_height, cached_base;
|
2019-04-25 16:41:33 +00:00
|
|
|
crypto::hash cached_m10_hash;
|
|
|
|
crypto::hash cached_top_hash;
|
2018-10-20 02:06:03 +00:00
|
|
|
bool cached;
|
2019-04-25 16:41:33 +00:00
|
|
|
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) {}
|
2018-10-20 02:06:03 +00:00
|
|
|
} d;
|
|
|
|
const boost::unique_lock<boost::mutex> lock(d.mutex);
|
|
|
|
|
2019-04-25 16:41:33 +00:00
|
|
|
crypto::hash top_hash = crypto::null_hash;
|
|
|
|
if (d.cached_to < blockchain_height)
|
|
|
|
top_hash = get_hash(d.cached_to);
|
|
|
|
if (d.cached && amount == 0 && d.cached_from == from_height && d.cached_to == to_height && d.cached_top_hash == top_hash)
|
2018-10-20 02:06:03 +00:00
|
|
|
return process_distribution(cumulative, d.cached_start_height, d.cached_distribution, d.cached_base);
|
|
|
|
|
|
|
|
std::vector<std::uint64_t> distribution;
|
|
|
|
std::uint64_t start_height, base;
|
2018-11-07 14:57:41 +00:00
|
|
|
|
|
|
|
// see if we can extend the cache - a common case
|
2019-04-25 16:41:33 +00:00
|
|
|
bool can_extend = d.cached && amount == 0 && d.cached_from == from_height && to_height > d.cached_to && top_hash == d.cached_top_hash;
|
|
|
|
if (!can_extend)
|
|
|
|
{
|
|
|
|
// we kept track of the hash 10 blocks below, if it exists, so if it matches,
|
|
|
|
// we can still pop the last 10 cached slots and try again
|
|
|
|
if (d.cached && amount == 0 && d.cached_from == from_height && d.cached_to - d.cached_from >= 10 && to_height > d.cached_to - 10)
|
|
|
|
{
|
|
|
|
crypto::hash hash10 = get_hash(d.cached_to - 10);
|
|
|
|
if (hash10 == d.cached_m10_hash)
|
|
|
|
{
|
|
|
|
d.cached_to -= 10;
|
|
|
|
d.cached_top_hash = hash10;
|
|
|
|
d.cached_m10_hash = crypto::null_hash;
|
rpc: work around a GCC 7.4.0 (at least) bug
In static member function ‘static boost::optional<cryptonote::rpc::output_distribution_data> cryptonote::rpc::RpcHandler::get_output_distribution(const std::function<bool(long unsigned int, long unsigned int, long unsigned int, long unsigned int&, std::vector<long unsigned int>&, long unsigned int&)>&, uint64_t, uint64_t, uint64_t, const std::function<crypto::hash(long unsigned int)>&, bool, uint64_t)’:
cc1plus: warning: ‘void* __builtin_memset(void*, int, long unsigned int)’: specified size 18446744073709551536 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]
2019-06-12 22:26:06 +00:00
|
|
|
CHECK_AND_ASSERT_MES(d.cached_distribution.size() >= 10, boost::none, "Cached distribution size does not match cached bounds");
|
|
|
|
for (int p = 0; p < 10; ++p)
|
|
|
|
d.cached_distribution.pop_back();
|
2019-04-25 16:41:33 +00:00
|
|
|
can_extend = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (can_extend)
|
2018-11-07 14:57:41 +00:00
|
|
|
{
|
|
|
|
std::vector<std::uint64_t> new_distribution;
|
|
|
|
if (!f(amount, d.cached_to + 1, to_height, start_height, new_distribution, base))
|
|
|
|
return boost::none;
|
|
|
|
distribution = d.cached_distribution;
|
|
|
|
distribution.reserve(distribution.size() + new_distribution.size());
|
|
|
|
for (const auto &e: new_distribution)
|
|
|
|
distribution.push_back(e);
|
|
|
|
start_height = d.cached_start_height;
|
|
|
|
base = d.cached_base;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!f(amount, from_height, to_height, start_height, distribution, base))
|
|
|
|
return boost::none;
|
|
|
|
}
|
2018-10-20 02:06:03 +00:00
|
|
|
|
|
|
|
if (to_height > 0 && to_height >= from_height)
|
|
|
|
{
|
|
|
|
const std::uint64_t offset = std::max(from_height, start_height);
|
|
|
|
if (offset <= to_height && to_height - offset + 1 < distribution.size())
|
|
|
|
distribution.resize(to_height - offset + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amount == 0)
|
|
|
|
{
|
|
|
|
d.cached_from = from_height;
|
|
|
|
d.cached_to = to_height;
|
2019-04-25 16:41:33 +00:00
|
|
|
d.cached_top_hash = get_hash(d.cached_to);
|
|
|
|
d.cached_m10_hash = d.cached_to >= 10 ? get_hash(d.cached_to - 10) : crypto::null_hash;
|
2018-10-20 02:06:03 +00:00
|
|
|
d.cached_distribution = distribution;
|
|
|
|
d.cached_start_height = start_height;
|
|
|
|
d.cached_base = base;
|
|
|
|
d.cached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return process_distribution(cumulative, start_height, std::move(distribution), base);
|
|
|
|
}
|
|
|
|
} // rpc
|
|
|
|
} // cryptonote
|