node_rpc_proxy: factor a few RPC calls using get_info

Takes advantage of caching
This commit is contained in:
moneromooo-monero
2018-06-30 22:06:09 +01:00
parent a9b83f5a6e
commit 8c4db68ff7
4 changed files with 58 additions and 86 deletions

View File

@@ -41,21 +41,13 @@ static const std::chrono::seconds rpc_timeout = std::chrono::minutes(3) + std::c
NodeRPCProxy::NodeRPCProxy(epee::net_utils::http::http_simple_client &http_client, boost::mutex &mutex)
: m_http_client(http_client)
, m_daemon_rpc_mutex(mutex)
, m_height(0)
, m_height_time(0)
, m_earliest_height()
, m_dynamic_per_kb_fee_estimate(0)
, m_dynamic_per_kb_fee_estimate_cached_height(0)
, m_dynamic_per_kb_fee_estimate_grace_blocks(0)
, m_rpc_version(0)
, m_target_height(0)
, m_target_height_time(0)
{}
{
invalidate();
}
void NodeRPCProxy::invalidate()
{
m_height = 0;
m_height_time = 0;
for (size_t n = 0; n < 256; ++n)
m_earliest_height[n] = 0;
m_dynamic_per_kb_fee_estimate = 0;
@@ -63,7 +55,8 @@ void NodeRPCProxy::invalidate()
m_dynamic_per_kb_fee_estimate_grace_blocks = 0;
m_rpc_version = 0;
m_target_height = 0;
m_target_height_time = 0;
m_block_size_limit = 0;
m_get_info_time = 0;
}
boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version) const
@@ -84,36 +77,15 @@ boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version
return boost::optional<std::string>();
}
boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height) const
{
const time_t now = time(NULL);
if (m_height == 0 || now >= m_height_time + 30) // re-cache every 30 seconds
{
cryptonote::COMMAND_RPC_GET_HEIGHT::request req = AUTO_VAL_INIT(req);
cryptonote::COMMAND_RPC_GET_HEIGHT::response res = AUTO_VAL_INIT(res);
m_daemon_rpc_mutex.lock();
bool r = net_utils::invoke_http_json("/getheight", req, res, m_http_client, rpc_timeout);
m_daemon_rpc_mutex.unlock();
CHECK_AND_ASSERT_MES(r, std::string(), "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(res.status != CORE_RPC_STATUS_BUSY, res.status, "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(res.status == CORE_RPC_STATUS_OK, res.status, "Failed to get current blockchain height");
m_height = res.height;
m_height_time = now;
}
height = m_height;
return boost::optional<std::string>();
}
void NodeRPCProxy::set_height(uint64_t h)
{
m_height = h;
}
boost::optional<std::string> NodeRPCProxy::get_target_height(uint64_t &height) const
boost::optional<std::string> NodeRPCProxy::get_info() const
{
const time_t now = time(NULL);
if (m_target_height == 0 || now >= m_target_height_time + 30) // re-cache every 30 seconds
if (now >= m_get_info_time + 30) // re-cache every 30 seconds
{
cryptonote::COMMAND_RPC_GET_INFO::request req_t = AUTO_VAL_INIT(req_t);
cryptonote::COMMAND_RPC_GET_INFO::response resp_t = AUTO_VAL_INIT(resp_t);
@@ -125,13 +97,41 @@ boost::optional<std::string> NodeRPCProxy::get_target_height(uint64_t &height) c
CHECK_AND_ASSERT_MES(r, std::string(), "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(resp_t.status != CORE_RPC_STATUS_BUSY, resp_t.status, "Failed to connect to daemon");
CHECK_AND_ASSERT_MES(resp_t.status == CORE_RPC_STATUS_OK, resp_t.status, "Failed to get target blockchain height");
m_height = resp_t.height;
m_target_height = resp_t.target_height;
m_target_height_time = now;
m_block_size_limit = resp_t.block_size_limit;
m_get_info_time = now;
}
return boost::optional<std::string>();
}
boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height) const
{
auto res = get_info();
if (res)
return res;
height = m_height;
return boost::optional<std::string>();
}
boost::optional<std::string> NodeRPCProxy::get_target_height(uint64_t &height) const
{
auto res = get_info();
if (res)
return res;
height = m_target_height;
return boost::optional<std::string>();
}
boost::optional<std::string> NodeRPCProxy::get_block_size_limit(uint64_t &block_size_limit) const
{
auto res = get_info();
if (res)
return res;
block_size_limit = m_block_size_limit;
return boost::optional<std::string>();
}
boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height) const
{
if (m_earliest_height[version] == 0)