From 71d186566e30ca2da5527cfe69c50952014538f0 Mon Sep 17 00:00:00 2001 From: stoffu Date: Fri, 16 Feb 2018 09:49:29 +0900 Subject: [PATCH 1/2] replace invoke_http_json("/json_rpc",...) with invoke_http_json_rpc("/json_rpc",methodname,...) to reduce boilerplate --- src/wallet/node_rpc_proxy.cpp | 64 +++++------- src/wallet/wallet2.cpp | 186 ++++++++++++++-------------------- 2 files changed, 104 insertions(+), 146 deletions(-) diff --git a/src/wallet/node_rpc_proxy.cpp b/src/wallet/node_rpc_proxy.cpp index f11ff9295..c5d869354 100644 --- a/src/wallet/node_rpc_proxy.cpp +++ b/src/wallet/node_rpc_proxy.cpp @@ -70,18 +70,15 @@ boost::optional NodeRPCProxy::get_rpc_version(uint32_t &rpc_version { 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"; + cryptonote::COMMAND_RPC_GET_VERSION::request req_t = AUTO_VAL_INIT(req_t); + cryptonote::COMMAND_RPC_GET_VERSION::response resp_t = AUTO_VAL_INIT(resp_t); m_daemon_rpc_mutex.lock(); - bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client, rpc_timeout); + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_version", req_t, resp_t, 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(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; + 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 daemon RPC version"); + m_rpc_version = resp_t.version; } rpc_version = m_rpc_version; return boost::optional(); @@ -118,20 +115,17 @@ boost::optional NodeRPCProxy::get_target_height(uint64_t &height) c const time_t now = time(NULL); if (m_target_height == 0 || now >= m_target_height_time + 30) // re-cache every 30 seconds { - epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); - epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_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); - req_t.jsonrpc = "2.0"; - req_t.id = epee::serialization::storage_entry(0); - req_t.method = "get_info"; m_daemon_rpc_mutex.lock(); - bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client, rpc_timeout); + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_info", req_t, resp_t, 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(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 target blockchain height"); - m_target_height = resp_t.result.target_height; + 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_target_height = resp_t.target_height; m_target_height_time = now; } height = m_target_height; @@ -142,20 +136,17 @@ boost::optional NodeRPCProxy::get_earliest_height(uint8_t version, { if (m_earliest_height[version] == 0) { - epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); - epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_t); + cryptonote::COMMAND_RPC_HARD_FORK_INFO::request req_t = AUTO_VAL_INIT(req_t); + cryptonote::COMMAND_RPC_HARD_FORK_INFO::response resp_t = AUTO_VAL_INIT(resp_t); m_daemon_rpc_mutex.lock(); - req_t.jsonrpc = "2.0"; - req_t.id = epee::serialization::storage_entry(0); - req_t.method = "hard_fork_info"; - req_t.params.version = version; - bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client, rpc_timeout); + req_t.version = version; + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "hard_fork_info", req_t, resp_t, 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(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 hard fork status"); - m_earliest_height[version] = resp_t.result.enabled ? resp_t.result.earliest_height : std::numeric_limits::max(); + 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 hard fork status"); + m_earliest_height[version] = resp_t.enabled ? resp_t.earliest_height : std::numeric_limits::max(); } earliest_height = m_earliest_height[version]; @@ -172,20 +163,17 @@ boost::optional NodeRPCProxy::get_dynamic_per_kb_fee_estimate(uint6 if (m_dynamic_per_kb_fee_estimate_cached_height != height || m_dynamic_per_kb_fee_estimate_grace_blocks != grace_blocks) { - epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); - epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_t); + cryptonote::COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::request req_t = AUTO_VAL_INIT(req_t); + cryptonote::COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::response resp_t = AUTO_VAL_INIT(resp_t); m_daemon_rpc_mutex.lock(); - req_t.jsonrpc = "2.0"; - req_t.id = epee::serialization::storage_entry(0); - req_t.method = "get_fee_estimate"; - req_t.params.grace_blocks = grace_blocks; - bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client, rpc_timeout); + req_t.grace_blocks = grace_blocks; + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_fee_estimate", req_t, resp_t, 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(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 fee estimate"); - m_dynamic_per_kb_fee_estimate = resp_t.result.fee; + 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 fee estimate"); + m_dynamic_per_kb_fee_estimate = resp_t.fee; m_dynamic_per_kb_fee_estimate_cached_height = height; m_dynamic_per_kb_fee_estimate_grace_blocks = grace_blocks; } diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 04d8c799a..cb5e7bc67 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3540,20 +3540,17 @@ bool wallet2::check_connection(uint32_t *version, uint32_t timeout) if (version) { - 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"; - bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client); + cryptonote::COMMAND_RPC_GET_VERSION::request req_t = AUTO_VAL_INIT(req_t); + cryptonote::COMMAND_RPC_GET_VERSION::response resp_t = AUTO_VAL_INIT(resp_t); + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_version", req_t, resp_t, m_http_client); if(!r) { *version = 0; return false; } - if (resp_t.result.status != CORE_RPC_STATUS_OK) + if (resp_t.status != CORE_RPC_STATUS_OK) *version = 0; else - *version = resp_t.result.version; + *version = resp_t.version; } return true; @@ -3688,19 +3685,16 @@ void wallet2::trim_hashchain() if (!m_blockchain.empty() && m_blockchain.size() == m_blockchain.offset()) { MINFO("Fixing empty hashchain"); - epee::json_rpc::request req = AUTO_VAL_INIT(req); - epee::json_rpc::response res = AUTO_VAL_INIT(res); + cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request req = AUTO_VAL_INIT(req); + cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response res = AUTO_VAL_INIT(res); m_daemon_rpc_mutex.lock(); - req.jsonrpc = "2.0"; - req.id = epee::serialization::storage_entry(0); - req.method = "getblockheaderbyheight"; - req.params.height = m_blockchain.size() - 1; - bool r = net_utils::invoke_http_json("/json_rpc", req, res, m_http_client, rpc_timeout); + req.height = m_blockchain.size() - 1; + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheaderbyheight", req, res, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); - if (r && res.result.status == CORE_RPC_STATUS_OK) + if (r && res.status == CORE_RPC_STATUS_OK) { crypto::hash hash; - epee::string_tools::hex_to_pod(res.result.block_header.hash, hash); + epee::string_tools::hex_to_pod(res.block_header.hash, hash); m_blockchain.refill(hash); } else @@ -5160,18 +5154,15 @@ uint32_t wallet2::adjust_priority(uint32_t priority) } // get the current full reward zone - epee::json_rpc::request getinfo_req = AUTO_VAL_INIT(getinfo_req); - epee::json_rpc::response getinfo_res = AUTO_VAL_INIT(getinfo_res); + cryptonote::COMMAND_RPC_GET_INFO::request getinfo_req = AUTO_VAL_INIT(getinfo_req); + cryptonote::COMMAND_RPC_GET_INFO::response getinfo_res = AUTO_VAL_INIT(getinfo_res); m_daemon_rpc_mutex.lock(); - getinfo_req.jsonrpc = "2.0"; - getinfo_req.id = epee::serialization::storage_entry(0); - getinfo_req.method = "get_info"; - bool r = net_utils::invoke_http_json("/json_rpc", getinfo_req, getinfo_res, m_http_client); + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_info", getinfo_req, getinfo_res, m_http_client); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_info"); - THROW_WALLET_EXCEPTION_IF(getinfo_res.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_info"); - THROW_WALLET_EXCEPTION_IF(getinfo_res.result.status != CORE_RPC_STATUS_OK, error::get_tx_pool_error); - const uint64_t full_reward_zone = getinfo_res.result.block_size_limit / 2; + 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 const size_t N = 10; @@ -5180,26 +5171,23 @@ uint32_t wallet2::adjust_priority(uint32_t priority) MERROR("The blockchain is too short"); return priority; } - epee::json_rpc::request getbh_req = AUTO_VAL_INIT(getbh_req); - epee::json_rpc::response getbh_res = AUTO_VAL_INIT(getbh_res); + cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::request getbh_req = AUTO_VAL_INIT(getbh_req); + cryptonote::COMMAND_RPC_GET_BLOCK_HEADERS_RANGE::response getbh_res = AUTO_VAL_INIT(getbh_res); m_daemon_rpc_mutex.lock(); - getbh_req.jsonrpc = "2.0"; - getbh_req.id = epee::serialization::storage_entry(0); - getbh_req.method = "getblockheadersrange"; - getbh_req.params.start_height = m_blockchain.size() - N; - getbh_req.params.end_height = m_blockchain.size() - 1; - r = net_utils::invoke_http_json("/json_rpc", getbh_req, getbh_res, m_http_client, rpc_timeout); + getbh_req.start_height = m_blockchain.size() - N; + 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); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "getblockheadersrange"); - THROW_WALLET_EXCEPTION_IF(getbh_res.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "getblockheadersrange"); - THROW_WALLET_EXCEPTION_IF(getbh_res.result.status != CORE_RPC_STATUS_OK, error::get_blocks_error, getbh_res.result.status); - if (getbh_res.result.headers.size() != N) + 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_OK, error::get_blocks_error, getbh_res.status); + if (getbh_res.headers.size() != N) { MERROR("Bad blockheaders size"); return priority; } size_t block_size_sum = 0; - for (const cryptonote::block_header_response &i : getbh_res.result.headers) + for (const cryptonote::block_header_response &i : getbh_res.headers) { block_size_sum += i.block_size; } @@ -5464,24 +5452,21 @@ void wallet2::get_outs(std::vector> if (fake_outputs_count > 0) { // get histogram for the amounts we need - epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); - epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_t); + cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::request req_t = AUTO_VAL_INIT(req_t); + cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::response resp_t = AUTO_VAL_INIT(resp_t); m_daemon_rpc_mutex.lock(); - req_t.jsonrpc = "2.0"; - req_t.id = epee::serialization::storage_entry(0); - req_t.method = "get_output_histogram"; for(size_t idx: selected_transfers) - req_t.params.amounts.push_back(m_transfers[idx].is_rct() ? 0 : m_transfers[idx].amount()); - std::sort(req_t.params.amounts.begin(), req_t.params.amounts.end()); - auto end = std::unique(req_t.params.amounts.begin(), req_t.params.amounts.end()); - req_t.params.amounts.resize(std::distance(req_t.params.amounts.begin(), end)); - req_t.params.unlocked = true; - req_t.params.recent_cutoff = time(NULL) - RECENT_OUTPUT_ZONE; - bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client, rpc_timeout); + req_t.amounts.push_back(m_transfers[idx].is_rct() ? 0 : m_transfers[idx].amount()); + std::sort(req_t.amounts.begin(), req_t.amounts.end()); + 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.unlocked = true; + req_t.recent_cutoff = time(NULL) - RECENT_OUTPUT_ZONE; + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_histogram", req_t, resp_t, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "transfer_selected"); - THROW_WALLET_EXCEPTION_IF(resp_t.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_output_histogram"); - THROW_WALLET_EXCEPTION_IF(resp_t.result.status != CORE_RPC_STATUS_OK, error::get_histogram_error, resp_t.result.status); + THROW_WALLET_EXCEPTION_IF(resp_t.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_output_histogram"); + THROW_WALLET_EXCEPTION_IF(resp_t.status != CORE_RPC_STATUS_OK, error::get_histogram_error, resp_t.status); // we ask for more, to have spares if some outputs are still locked size_t base_requested_outputs_count = (size_t)((fake_outputs_count + 1) * 1.5 + 1); @@ -5505,7 +5490,7 @@ void wallet2::get_outs(std::vector> // if there are just enough outputs to mix with, use all of them. // Eventually this should become impossible. uint64_t num_outs = 0, num_recent_outs = 0; - for (const auto &he: resp_t.result.histogram) + for (const auto &he: resp_t.histogram) { if (he.amount == amount) { @@ -7502,25 +7487,22 @@ std::vector wallet2::get_unspent_amounts_vector() const //---------------------------------------------------------------------------------------------------- std::vector wallet2::select_available_outputs_from_histogram(uint64_t count, bool atleast, bool unlocked, bool allow_rct, bool trusted_daemon) { - epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); - epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_t); + cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::request req_t = AUTO_VAL_INIT(req_t); + cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::response resp_t = AUTO_VAL_INIT(resp_t); m_daemon_rpc_mutex.lock(); - req_t.jsonrpc = "2.0"; - req_t.id = epee::serialization::storage_entry(0); - req_t.method = "get_output_histogram"; if (trusted_daemon) - req_t.params.amounts = get_unspent_amounts_vector(); - req_t.params.min_count = count; - req_t.params.max_count = 0; - req_t.params.unlocked = unlocked; - bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client, rpc_timeout); + req_t.amounts = get_unspent_amounts_vector(); + req_t.min_count = count; + req_t.max_count = 0; + req_t.unlocked = unlocked; + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_histogram", req_t, resp_t, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "select_available_outputs_from_histogram"); - THROW_WALLET_EXCEPTION_IF(resp_t.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_output_histogram"); - THROW_WALLET_EXCEPTION_IF(resp_t.result.status != CORE_RPC_STATUS_OK, error::get_histogram_error, resp_t.result.status); + THROW_WALLET_EXCEPTION_IF(resp_t.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_output_histogram"); + THROW_WALLET_EXCEPTION_IF(resp_t.status != CORE_RPC_STATUS_OK, error::get_histogram_error, resp_t.status); std::set mixable; - for (const auto &i: resp_t.result.histogram) + for (const auto &i: resp_t.histogram) { mixable.insert(i.amount); } @@ -7543,24 +7525,21 @@ std::vector wallet2::select_available_outputs_from_histogram(uint64_t co //---------------------------------------------------------------------------------------------------- uint64_t wallet2::get_num_rct_outputs() { - epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); - epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_t); + cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::request req_t = AUTO_VAL_INIT(req_t); + cryptonote::COMMAND_RPC_GET_OUTPUT_HISTOGRAM::response resp_t = AUTO_VAL_INIT(resp_t); m_daemon_rpc_mutex.lock(); - req_t.jsonrpc = "2.0"; - req_t.id = epee::serialization::storage_entry(0); - req_t.method = "get_output_histogram"; - req_t.params.amounts.push_back(0); - req_t.params.min_count = 0; - req_t.params.max_count = 0; - bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client, rpc_timeout); + req_t.amounts.push_back(0); + req_t.min_count = 0; + req_t.max_count = 0; + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_histogram", req_t, resp_t, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_num_rct_outputs"); - THROW_WALLET_EXCEPTION_IF(resp_t.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_output_histogram"); - THROW_WALLET_EXCEPTION_IF(resp_t.result.status != CORE_RPC_STATUS_OK, error::get_histogram_error, resp_t.result.status); - THROW_WALLET_EXCEPTION_IF(resp_t.result.histogram.size() != 1, error::get_histogram_error, "Expected exactly one response"); - THROW_WALLET_EXCEPTION_IF(resp_t.result.histogram[0].amount != 0, error::get_histogram_error, "Expected 0 amount"); + THROW_WALLET_EXCEPTION_IF(resp_t.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_output_histogram"); + THROW_WALLET_EXCEPTION_IF(resp_t.status != CORE_RPC_STATUS_OK, error::get_histogram_error, resp_t.status); + THROW_WALLET_EXCEPTION_IF(resp_t.histogram.size() != 1, error::get_histogram_error, "Expected exactly one response"); + THROW_WALLET_EXCEPTION_IF(resp_t.histogram[0].amount != 0, error::get_histogram_error, "Expected 0 amount"); - return resp_t.result.histogram[0].total_instances; + return resp_t.histogram[0].total_instances; } //---------------------------------------------------------------------------------------------------- const wallet2::transfer_details &wallet2::get_transfer_details(size_t idx) const @@ -8476,23 +8455,20 @@ uint64_t wallet2::get_daemon_blockchain_height(string &err) const uint64_t wallet2::get_daemon_blockchain_target_height(string &err) { - epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); - epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_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); m_daemon_rpc_mutex.lock(); - req_t.jsonrpc = "2.0"; - req_t.id = epee::serialization::storage_entry(0); - req_t.method = "get_info"; - bool ok = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client); + 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.result.status == CORE_RPC_STATUS_BUSY) + if (resp_t.status == CORE_RPC_STATUS_BUSY) { err = "daemon is busy. Please try again later."; } - else if (resp_t.result.status != CORE_RPC_STATUS_OK) + else if (resp_t.status != CORE_RPC_STATUS_OK) { - err = resp_t.result.status; + err = resp_t.status; } else // success, cleaning up error message { @@ -8503,7 +8479,7 @@ uint64_t wallet2::get_daemon_blockchain_target_height(string &err) { err = "possibly lost connection to daemon"; } - return resp_t.result.target_height; + return resp_t.target_height; } uint64_t wallet2::get_approximate_blockchain_height() const @@ -9739,30 +9715,24 @@ std::vector> wallet2::estimate_backlog(const std:: } // get txpool backlog - epee::json_rpc::request req = AUTO_VAL_INIT(req); - epee::json_rpc::response res = AUTO_VAL_INIT(res); + cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::request req = AUTO_VAL_INIT(req); + cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::response res = AUTO_VAL_INIT(res); m_daemon_rpc_mutex.lock(); - req.jsonrpc = "2.0"; - req.id = epee::serialization::storage_entry(0); - req.method = "get_txpool_backlog"; - bool r = net_utils::invoke_http_json("/json_rpc", req, res, m_http_client, rpc_timeout); + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_txpool_backlog", req, res, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "Failed to connect to daemon"); - THROW_WALLET_EXCEPTION_IF(res.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_txpool_backlog"); - THROW_WALLET_EXCEPTION_IF(res.result.status != CORE_RPC_STATUS_OK, error::get_tx_pool_error); + 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); - epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); - epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_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); m_daemon_rpc_mutex.lock(); - req_t.jsonrpc = "2.0"; - req_t.id = epee::serialization::storage_entry(0); - req_t.method = "get_info"; - r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client); + r = net_utils::invoke_http_json_rpc("/json_rpc", "get_info", req_t, resp_t, m_http_client); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_info"); - THROW_WALLET_EXCEPTION_IF(resp_t.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_info"); - THROW_WALLET_EXCEPTION_IF(resp_t.result.status != CORE_RPC_STATUS_OK, error::get_tx_pool_error); - uint64_t full_reward_zone = resp_t.result.block_size_limit / 2; + 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> blocks; for (const auto &fee_level: fee_levels) @@ -9770,7 +9740,7 @@ std::vector> wallet2::estimate_backlog(const std:: const double our_fee_byte_min = fee_level.first; const double our_fee_byte_max = fee_level.second; uint64_t priority_size_min = 0, priority_size_max = 0; - for (const auto &i: res.result.backlog) + for (const auto &i: res.backlog) { if (i.blob_size == 0) { From a7266d6d7bdf9f33ab5b17aef08b19eca1829de5 Mon Sep 17 00:00:00 2001 From: stoffu Date: Fri, 16 Feb 2018 10:19:39 +0900 Subject: [PATCH 2/2] wallet2+cli+rpc: eliminate redundant m_http_client from cli/rpc and delegate calls to wallet2 --- src/simplewallet/simplewallet.cpp | 11 +++++------ src/simplewallet/simplewallet.h | 1 - src/wallet/wallet2.h | 19 +++++++++++++++++++ src/wallet/wallet_rpc_server.cpp | 8 +++----- src/wallet/wallet_rpc_server.h | 1 - 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 3668df7b9..93221c2bd 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -2683,7 +2683,6 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) if (!m_trusted_daemon) message_writer() << (boost::format(tr("Warning: using an untrusted daemon at %s, privacy will be lessened")) % m_wallet->get_daemon_address()).str(); - m_http_client.set_server(m_wallet->get_daemon_address(), m_wallet->get_daemon_login()); m_wallet->callback(this); return true; @@ -3216,7 +3215,7 @@ bool simple_wallet::start_mining(const std::vector& args) } COMMAND_RPC_START_MINING::response res; - bool r = net_utils::invoke_http_json("/start_mining", req, res, m_http_client); + bool r = m_wallet->invoke_http_json("/start_mining", req, res); std::string err = interpret_rpc_response(r, res.status); if (err.empty()) success_msg_writer() << tr("Mining started in daemon"); @@ -3238,7 +3237,7 @@ bool simple_wallet::stop_mining(const std::vector& args) COMMAND_RPC_STOP_MINING::request req; COMMAND_RPC_STOP_MINING::response res; - bool r = net_utils::invoke_http_json("/stop_mining", req, res, m_http_client); + bool r = m_wallet->invoke_http_json("/stop_mining", req, res); std::string err = interpret_rpc_response(r, res.status); if (err.empty()) success_msg_writer() << tr("Mining stopped in daemon"); @@ -3295,7 +3294,7 @@ bool simple_wallet::save_bc(const std::vector& args) } COMMAND_RPC_SAVE_BC::request req; COMMAND_RPC_SAVE_BC::response res; - bool r = net_utils::invoke_http_json("/save_bc", req, res, m_http_client); + bool r = m_wallet->invoke_http_json("/save_bc", req, res); std::string err = interpret_rpc_response(r, res.status); if (err.empty()) success_msg_writer() << tr("Blockchain saved"); @@ -3641,7 +3640,7 @@ uint64_t simple_wallet::get_daemon_blockchain_height(std::string& err) COMMAND_RPC_GET_HEIGHT::request req; COMMAND_RPC_GET_HEIGHT::response res = boost::value_initialized(); - bool r = net_utils::invoke_http_json("/getheight", req, res, m_http_client); + bool r = m_wallet->invoke_http_json("/getheight", req, res); err = interpret_rpc_response(r, res.status); return res.height; } @@ -3763,7 +3762,7 @@ bool simple_wallet::print_ring_members(const std::vectorinvoke_http_bin("/get_outs.bin", req, res); err = interpret_rpc_response(r, res.status); if (!err.empty()) { diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 4c7818bf1..6f344439e 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -328,7 +328,6 @@ namespace cryptonote epee::console_handlers_binder m_cmd_binder; std::unique_ptr m_wallet; - epee::net_utils::http::http_simple_client m_http_client; refresh_progress_reporter_t m_refresh_progress_reporter; std::atomic m_idle_run; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 9accc65ca..d2e484e05 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -1027,6 +1027,25 @@ namespace tools crypto::public_key get_multisig_signing_public_key(size_t idx) const; crypto::public_key get_multisig_signing_public_key(const crypto::secret_key &skey) const; + template + inline bool invoke_http_json(const boost::string_ref uri, const t_request& req, t_response& res, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET") + { + boost::lock_guard lock(m_daemon_rpc_mutex); + return epee::net_utils::invoke_http_json(uri, req, res, m_http_client, timeout, http_method); + } + template + inline bool invoke_http_bin(const boost::string_ref uri, const t_request& req, t_response& res, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET") + { + boost::lock_guard lock(m_daemon_rpc_mutex); + return epee::net_utils::invoke_http_bin(uri, req, res, m_http_client, timeout, http_method); + } + template + inline bool invoke_http_json_rpc(const boost::string_ref uri, const std::string& method_name, const t_request& req, t_response& res, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET", const std::string& req_id = "0") + { + boost::lock_guard lock(m_daemon_rpc_mutex); + return epee::net_utils::invoke_http_json_rpc(uri, method_name, req, res, m_http_client, timeout, http_method, req_id); + } + private: /*! * \brief Stores wallet information to wallet file. diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 220dbbc58..70874c65a 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -229,8 +229,6 @@ namespace tools assert(bool(http_login)); } // end auth enabled - m_http_client.set_server(walvars->get_daemon_address(), walvars->get_daemon_login()); - m_net_server.set_threads_prefix("RPC"); auto rng = [](size_t len, uint8_t *ptr) { return crypto::rand(len, ptr); }; return epee::http_server_impl_base::init( @@ -2257,7 +2255,7 @@ namespace tools daemon_req.ignore_battery = req.ignore_battery; cryptonote::COMMAND_RPC_START_MINING::response daemon_res; - bool r = net_utils::invoke_http_json("/start_mining", daemon_req, daemon_res, m_http_client); + bool r = m_wallet->invoke_http_json("/start_mining", daemon_req, daemon_res); if (!r || daemon_res.status != CORE_RPC_STATUS_OK) { er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; @@ -2271,7 +2269,7 @@ namespace tools { cryptonote::COMMAND_RPC_STOP_MINING::request daemon_req; cryptonote::COMMAND_RPC_STOP_MINING::response daemon_res; - bool r = net_utils::invoke_http_json("/stop_mining", daemon_req, daemon_res, m_http_client); + bool r = m_wallet->invoke_http_json("/stop_mining", daemon_req, daemon_res); if (!r || daemon_res.status != CORE_RPC_STATUS_OK) { er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; @@ -2351,7 +2349,7 @@ namespace tools cryptonote::COMMAND_RPC_GET_HEIGHT::request hreq; cryptonote::COMMAND_RPC_GET_HEIGHT::response hres; hres.height = 0; - bool r = net_utils::invoke_http_json("/getheight", hreq, hres, m_http_client); + bool r = wal->invoke_http_json("/getheight", hreq, hres); wal->set_refresh_from_block_height(hres.height); crypto::secret_key dummy_key; try { diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h index bb458aa99..63e886dda 100644 --- a/src/wallet/wallet_rpc_server.h +++ b/src/wallet/wallet_rpc_server.h @@ -224,7 +224,6 @@ namespace tools tools::private_file rpc_login_file; std::atomic m_stop; bool m_trusted_daemon; - epee::net_utils::http::http_simple_client m_http_client; const boost::program_options::variables_map *m_vm; }; }