Skip to content
Permalink
Browse files

Add and cache top height to get_output_distribution for rct

  • Loading branch information...
miltonf committed Feb 16, 2019
1 parent bbc12da commit 52443015541cb32786a4fcd5ea67ced90e1f0b1e
Showing with 37 additions and 0 deletions.
  1. +33 −0 src/rpc/core_rpc_server.cpp
  2. +2 −0 src/rpc/core_rpc_server_commands_defs.h
  3. +2 −0 src/wallet/wallet2.cpp
@@ -2090,6 +2090,22 @@ namespace cryptonote
{
for (uint64_t amount: req.amounts)
{
static struct D
{
boost::mutex mutex;
std::vector<uint64_t> cached_distribution;
uint64_t cached_from, cached_to, cached_start_height, cached_base;
bool cached;
D(): cached_from(0), cached_to(0), cached_start_height(0), cached_base(0), cached(false) {}
} d;
boost::unique_lock<boost::mutex> lock(d.mutex);

if (d.cached && amount == 0 && d.cached_from == req.from_height && d.cached_to == req.to_height)
{
res.distributions.push_back({amount, d.cached_start_height, d.cached_distribution, d.cached_base});
continue;
}

std::vector<uint64_t> distribution;
uint64_t start_height, base;
if (!m_core.get_output_distribution(amount, req.from_height, start_height, distribution, base))
@@ -2098,12 +2114,29 @@ namespace cryptonote
error_resp.message = "Failed to get rct distribution";
return false;
}
if (req.to_height > 0 && req.to_height >= req.from_height)
{
uint64_t offset = std::max(req.from_height, start_height);
if (offset <= req.to_height && req.to_height - offset + 1 < distribution.size())
distribution.resize(req.to_height - offset + 1);
}
if (req.cumulative)
{
distribution[0] += base;
for (size_t n = 1; n < distribution.size(); ++n)
distribution[n] += distribution[n-1];
}

if (amount == 0)
{
d.cached_from = req.from_height;
d.cached_to = req.to_height;
d.cached_distribution = distribution;
d.cached_start_height = start_height;
d.cached_base = base;
d.cached = true;
}

res.distributions.push_back({amount, start_height, std::move(distribution), base});
}
}
@@ -2227,11 +2227,13 @@ namespace cryptonote
{
std::vector<uint64_t> amounts;
uint64_t from_height;
uint64_t to_height;
bool cumulative;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(amounts)
KV_SERIALIZE_OPT(from_height, (uint64_t)0)
KV_SERIALIZE_OPT(to_height, (uint64_t)0)
KV_SERIALIZE_OPT(cumulative, false)
END_KV_SERIALIZE_MAP()
};
@@ -5950,6 +5950,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>>
auto end = std::unique(req_t.amounts.begin(), req_t.amounts.end());
req_t.amounts.resize(std::distance(req_t.amounts.begin(), end));
req_t.from_height = std::max<uint64_t>(segregation_fork_height, RECENT_OUTPUT_BLOCKS) - RECENT_OUTPUT_BLOCKS;
req_t.to_height = segregation_fork_height + 1;
req_t.cumulative = true;
m_daemon_rpc_mutex.lock();
bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_distribution", req_t, resp_t, m_http_client, rpc_timeout);
@@ -5969,6 +5970,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>>
{
THROW_WALLET_EXCEPTION_IF(d.start_height > segregation_fork_height, error::get_output_distribution, "Distribution start_height too high");
THROW_WALLET_EXCEPTION_IF(segregation_fork_height - d.start_height >= d.distribution.size(), error::get_output_distribution, "Distribution size too small");
THROW_WALLET_EXCEPTION_IF(segregation_fork_height - RECENT_OUTPUT_BLOCKS - d.start_height >= d.distribution.size(), error::get_output_distribution, "Distribution size too small");
THROW_WALLET_EXCEPTION_IF(segregation_fork_height <= RECENT_OUTPUT_BLOCKS, error::wallet_internal_error, "Fork height too low");
THROW_WALLET_EXCEPTION_IF(segregation_fork_height - RECENT_OUTPUT_BLOCKS < d.start_height, error::get_output_distribution, "Bad start height");
uint64_t till_fork = d.distribution[segregation_fork_height - d.start_height];

0 comments on commit 5244301

Please sign in to comment.
You can’t perform that action at this time.