From 6fd4b827fb3cd976b10c1983fc4c88a340855a3c Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Mon, 27 Feb 2017 17:44:06 +0000 Subject: [PATCH] node_rpc_proxy: allow caching daemon RPC version --- src/wallet/node_rpc_proxy.cpp | 24 ++++++++++++++++++++++++ src/wallet/node_rpc_proxy.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/src/wallet/node_rpc_proxy.cpp b/src/wallet/node_rpc_proxy.cpp index 9c16c7dd..b5f5ef78 100644 --- a/src/wallet/node_rpc_proxy.cpp +++ b/src/wallet/node_rpc_proxy.cpp @@ -45,6 +45,7 @@ NodeRPCProxy::NodeRPCProxy(epee::net_utils::http::http_simple_client &http_clien , 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) {} void NodeRPCProxy::invalidate() @@ -56,6 +57,29 @@ void NodeRPCProxy::invalidate() 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; +} + +boost::optional NodeRPCProxy::get_rpc_version(uint32_t &rpc_version) +{ + const time_t now = time(NULL); + if (m_rpc_version == 0) + { + epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); + epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_t); + req_t.jsonrpc = "2.0"; + req_t.id = epee::serialization::storage_entry(0); + req_t.method = "get_version"; + m_daemon_rpc_mutex.lock(); + bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client); + m_daemon_rpc_mutex.unlock(); + CHECK_AND_ASSERT_MES(r, std::string(), "Failed to connect to daemon"); + CHECK_AND_ASSERT_MES(resp_t.result.status != CORE_RPC_STATUS_BUSY, resp_t.result.status, "Failed to connect to daemon"); + CHECK_AND_ASSERT_MES(resp_t.result.status == CORE_RPC_STATUS_OK, resp_t.result.status, "Failed to get daemon RPC version"); + m_rpc_version = resp_t.result.version; + } + rpc_version = m_rpc_version; + return boost::optional(); } boost::optional NodeRPCProxy::get_height(uint64_t &height) diff --git a/src/wallet/node_rpc_proxy.h b/src/wallet/node_rpc_proxy.h index 57cfbbc4..02d1d8d9 100644 --- a/src/wallet/node_rpc_proxy.h +++ b/src/wallet/node_rpc_proxy.h @@ -43,6 +43,7 @@ public: void invalidate(); + boost::optional get_rpc_version(uint32_t &version); boost::optional get_height(uint64_t &height); void set_height(uint64_t h); boost::optional get_earliest_height(uint8_t version, uint64_t &earliest_height); @@ -58,6 +59,7 @@ private: uint64_t m_dynamic_per_kb_fee_estimate; uint64_t m_dynamic_per_kb_fee_estimate_cached_height; uint64_t m_dynamic_per_kb_fee_estimate_grace_blocks; + uint32_t m_rpc_version; }; }