Merge pull request #4088

8c4db68 node_rpc_proxy: factor a few RPC calls using get_info (moneromooo-monero)
This commit is contained in:
luigi1111 2018-07-19 14:15:16 -05:00
commit 702a41034d
No known key found for this signature in database
GPG Key ID: F4ACA0183641E010
4 changed files with 58 additions and 86 deletions

View File

@ -4292,12 +4292,7 @@ uint64_t simple_wallet::get_daemon_blockchain_height(std::string& err)
{ {
throw std::runtime_error("simple_wallet null wallet"); throw std::runtime_error("simple_wallet null wallet");
} }
return m_wallet->get_daemon_blockchain_height(err);
COMMAND_RPC_GET_HEIGHT::request req;
COMMAND_RPC_GET_HEIGHT::response res = boost::value_initialized<COMMAND_RPC_GET_HEIGHT::response>();
bool r = m_wallet->invoke_http_json("/getheight", req, res);
err = interpret_rpc_response(r, res.status);
return res.height;
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
bool simple_wallet::show_blockchain_height(const std::vector<std::string>& args) bool simple_wallet::show_blockchain_height(const std::vector<std::string>& args)

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) NodeRPCProxy::NodeRPCProxy(epee::net_utils::http::http_simple_client &http_client, boost::mutex &mutex)
: m_http_client(http_client) : m_http_client(http_client)
, m_daemon_rpc_mutex(mutex) , m_daemon_rpc_mutex(mutex)
, m_height(0) {
, m_height_time(0) invalidate();
, 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)
{}
void NodeRPCProxy::invalidate() void NodeRPCProxy::invalidate()
{ {
m_height = 0; m_height = 0;
m_height_time = 0;
for (size_t n = 0; n < 256; ++n) for (size_t n = 0; n < 256; ++n)
m_earliest_height[n] = 0; m_earliest_height[n] = 0;
m_dynamic_per_kb_fee_estimate = 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_dynamic_per_kb_fee_estimate_grace_blocks = 0;
m_rpc_version = 0; m_rpc_version = 0;
m_target_height = 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 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>(); 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) void NodeRPCProxy::set_height(uint64_t h)
{ {
m_height = 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); 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::request req_t = AUTO_VAL_INIT(req_t);
cryptonote::COMMAND_RPC_GET_INFO::response resp_t = AUTO_VAL_INIT(resp_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(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_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"); 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 = 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; height = m_target_height;
return boost::optional<std::string>(); 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 boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height) const
{ {
if (m_earliest_height[version] == 0) if (m_earliest_height[version] == 0)

View File

@ -47,22 +47,25 @@ public:
boost::optional<std::string> get_height(uint64_t &height) const; boost::optional<std::string> get_height(uint64_t &height) const;
void set_height(uint64_t h); void set_height(uint64_t h);
boost::optional<std::string> get_target_height(uint64_t &height) const; boost::optional<std::string> get_target_height(uint64_t &height) const;
boost::optional<std::string> get_block_size_limit(uint64_t &block_size_limit) const;
boost::optional<std::string> get_earliest_height(uint8_t version, uint64_t &earliest_height) const; boost::optional<std::string> get_earliest_height(uint8_t version, uint64_t &earliest_height) const;
boost::optional<std::string> get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee) const; boost::optional<std::string> get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee) const;
private: private:
boost::optional<std::string> get_info() const;
epee::net_utils::http::http_simple_client &m_http_client; epee::net_utils::http::http_simple_client &m_http_client;
boost::mutex &m_daemon_rpc_mutex; boost::mutex &m_daemon_rpc_mutex;
mutable uint64_t m_height; mutable uint64_t m_height;
mutable time_t m_height_time;
mutable uint64_t m_earliest_height[256]; mutable uint64_t m_earliest_height[256];
mutable uint64_t m_dynamic_per_kb_fee_estimate; mutable uint64_t m_dynamic_per_kb_fee_estimate;
mutable uint64_t m_dynamic_per_kb_fee_estimate_cached_height; mutable uint64_t m_dynamic_per_kb_fee_estimate_cached_height;
mutable uint64_t m_dynamic_per_kb_fee_estimate_grace_blocks; mutable uint64_t m_dynamic_per_kb_fee_estimate_grace_blocks;
mutable uint32_t m_rpc_version; mutable uint32_t m_rpc_version;
mutable uint64_t m_target_height; mutable uint64_t m_target_height;
mutable time_t m_target_height_time; mutable uint64_t m_block_size_limit;
mutable time_t m_get_info_time;
}; };
} }

View File

@ -5653,15 +5653,10 @@ uint32_t wallet2::adjust_priority(uint32_t priority)
} }
// get the current full reward zone // get the current full reward zone
cryptonote::COMMAND_RPC_GET_INFO::request getinfo_req = AUTO_VAL_INIT(getinfo_req); uint64_t block_size_limit = 0;
cryptonote::COMMAND_RPC_GET_INFO::response getinfo_res = AUTO_VAL_INIT(getinfo_res); const auto result = m_node_rpc_proxy.get_block_size_limit(block_size_limit);
m_daemon_rpc_mutex.lock(); throw_on_rpc_response_error(result, "get_info");
bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_info", getinfo_req, getinfo_res, m_http_client); const uint64_t full_reward_zone = block_size_limit / 2;
m_daemon_rpc_mutex.unlock();
THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_info");
THROW_WALLET_EXCEPTION_IF(getinfo_res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_info");
THROW_WALLET_EXCEPTION_IF(getinfo_res.status != CORE_RPC_STATUS_OK, error::get_tx_pool_error);
const uint64_t full_reward_zone = getinfo_res.block_size_limit / 2;
// get the last N block headers and sum the block sizes // get the last N block headers and sum the block sizes
const size_t N = 10; const size_t N = 10;
@ -5675,7 +5670,7 @@ uint32_t wallet2::adjust_priority(uint32_t priority)
m_daemon_rpc_mutex.lock(); m_daemon_rpc_mutex.lock();
getbh_req.start_height = m_blockchain.size() - N; getbh_req.start_height = m_blockchain.size() - N;
getbh_req.end_height = m_blockchain.size() - 1; getbh_req.end_height = m_blockchain.size() - 1;
r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheadersrange", getbh_req, getbh_res, m_http_client, rpc_timeout); bool r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheadersrange", getbh_req, getbh_res, m_http_client, rpc_timeout);
m_daemon_rpc_mutex.unlock(); m_daemon_rpc_mutex.unlock();
THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "getblockheadersrange"); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "getblockheadersrange");
THROW_WALLET_EXCEPTION_IF(getbh_res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "getblockheadersrange"); THROW_WALLET_EXCEPTION_IF(getbh_res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "getblockheadersrange");
@ -9448,32 +9443,16 @@ uint64_t wallet2::get_daemon_blockchain_height(string &err) const
} }
uint64_t wallet2::get_daemon_blockchain_target_height(string &err) uint64_t wallet2::get_daemon_blockchain_target_height(string &err)
{
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);
m_daemon_rpc_mutex.lock();
bool ok = net_utils::invoke_http_json_rpc("/json_rpc", "get_info", req_t, resp_t, m_http_client);
m_daemon_rpc_mutex.unlock();
if (ok)
{
if (resp_t.status == CORE_RPC_STATUS_BUSY)
{
err = "daemon is busy. Please try again later.";
}
else if (resp_t.status != CORE_RPC_STATUS_OK)
{
err = resp_t.status;
}
else // success, cleaning up error message
{ {
err = ""; err = "";
} uint64_t target_height = 0;
} const auto result = m_node_rpc_proxy.get_target_height(target_height);
else if (result && *result != CORE_RPC_STATUS_OK)
{ {
err = "possibly lost connection to daemon"; err= *result;
return 0;
} }
return resp_t.target_height; return target_height;
} }
uint64_t wallet2::get_approximate_blockchain_height() const uint64_t wallet2::get_approximate_blockchain_height() const
@ -10831,15 +10810,10 @@ std::vector<std::pair<uint64_t, uint64_t>> wallet2::estimate_backlog(const std::
THROW_WALLET_EXCEPTION_IF(res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_txpool_backlog"); THROW_WALLET_EXCEPTION_IF(res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_txpool_backlog");
THROW_WALLET_EXCEPTION_IF(res.status != CORE_RPC_STATUS_OK, error::get_tx_pool_error); THROW_WALLET_EXCEPTION_IF(res.status != CORE_RPC_STATUS_OK, error::get_tx_pool_error);
cryptonote::COMMAND_RPC_GET_INFO::request req_t = AUTO_VAL_INIT(req_t); uint64_t block_size_limit = 0;
cryptonote::COMMAND_RPC_GET_INFO::response resp_t = AUTO_VAL_INIT(resp_t); const auto result = m_node_rpc_proxy.get_block_size_limit(block_size_limit);
m_daemon_rpc_mutex.lock(); throw_on_rpc_response_error(result, "get_info");
r = net_utils::invoke_http_json_rpc("/json_rpc", "get_info", req_t, resp_t, m_http_client); uint64_t full_reward_zone = block_size_limit / 2;
m_daemon_rpc_mutex.unlock();
THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_info");
THROW_WALLET_EXCEPTION_IF(resp_t.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_info");
THROW_WALLET_EXCEPTION_IF(resp_t.status != CORE_RPC_STATUS_OK, error::get_tx_pool_error);
uint64_t full_reward_zone = resp_t.block_size_limit / 2;
std::vector<std::pair<uint64_t, uint64_t>> blocks; std::vector<std::pair<uint64_t, uint64_t>> blocks;
for (const auto &fee_level: fee_levels) for (const auto &fee_level: fee_levels)