diff options
author | Riccardo Spagni <ric@spagni.net> | 2017-02-02 19:32:01 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2017-02-02 19:32:01 +0200 |
commit | 5fb3f97a55a9b8f31a69119c41557a9774deffb5 (patch) | |
tree | abf27b7fd1bf7fc164ce91f5179dbddf899112db /src | |
parent | Merge pull request #1628 (diff) | |
parent | Updates to epee HTTP client code (diff) | |
download | monero-5fb3f97a55a9b8f31a69119c41557a9774deffb5.tar.xz |
Merge pull request #1629
c02e1cb9 Updates to epee HTTP client code - http_simple_client now uses std::chrono for timeouts - http_simple_client accepts timeouts per connect / invoke call - shortened names of epee http invoke functions - invoke command functions only take relative path, connection is not automatically performed (Lee Clagett)
Diffstat (limited to 'src')
-rw-r--r-- | src/common/http_connection.h | 17 | ||||
-rw-r--r-- | src/common/rpc_client.h | 35 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.cpp | 13 | ||||
-rw-r--r-- | src/wallet/api/wallet.cpp | 12 | ||||
-rw-r--r-- | src/wallet/api/wallet.h | 2 | ||||
-rw-r--r-- | src/wallet/api/wallet_manager.cpp | 41 | ||||
-rw-r--r-- | src/wallet/node_rpc_proxy.cpp | 28 | ||||
-rw-r--r-- | src/wallet/node_rpc_proxy.h | 6 | ||||
-rw-r--r-- | src/wallet/wallet2.cpp | 47 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 10 |
10 files changed, 90 insertions, 121 deletions
diff --git a/src/common/http_connection.h b/src/common/http_connection.h index 156474cdd..8a786361a 100644 --- a/src/common/http_connection.h +++ b/src/common/http_connection.h @@ -28,6 +28,7 @@ #pragma once +#include <chrono> #include "string_tools.h" #include "net/http_client.h" @@ -38,20 +39,16 @@ private: epee::net_utils::http::http_simple_client * mp_http_client; bool m_ok; public: - static unsigned int const TIMEOUT = 200000; + static constexpr std::chrono::seconds TIMEOUT() + { + return std::chrono::minutes(3) + std::chrono::seconds(30); + } - t_http_connection( - epee::net_utils::http::http_simple_client * p_http_client - , uint32_t ip - , uint16_t port - ) + t_http_connection(epee::net_utils::http::http_simple_client* p_http_client) : mp_http_client(p_http_client) , m_ok(false) { - // TODO fix http client so that it accepts properly typed arguments - std::string ip_str = epee::string_tools::get_ip_string_from_int32(ip); - std::string port_str = boost::lexical_cast<std::string>(port); - m_ok = mp_http_client->connect(ip_str, port_str, TIMEOUT); + m_ok = mp_http_client->connect(TIMEOUT()); } ~t_http_connection() diff --git a/src/common/rpc_client.h b/src/common/rpc_client.h index 9c0036198..f5ecc8b50 100644 --- a/src/common/rpc_client.h +++ b/src/common/rpc_client.h @@ -34,7 +34,6 @@ #include "storages/http_abstract_invoke.h" #include "net/http_client.h" #include "string_tools.h" -#include <boost/lexical_cast.hpp> namespace tools { @@ -42,27 +41,16 @@ namespace tools { private: epee::net_utils::http::http_simple_client m_http_client; - uint32_t m_ip; - uint16_t m_port; public: t_rpc_client( uint32_t ip , uint16_t port ) : m_http_client{} - , m_ip{ip} - , m_port{port} - {} - - std::string build_url(std::string const & relative_url) const { - std::string result = - "http://" - + epee::string_tools::get_ip_string_from_int32(m_ip) - + ":" - + boost::lexical_cast<std::string>(m_port) - + relative_url; - return result; + m_http_client.set_server( + epee::string_tools::get_ip_string_from_int32(ip), std::to_string(port) + ); } template <typename T_req, typename T_res> @@ -72,8 +60,7 @@ namespace tools , std::string const & method_name ) { - std::string rpc_url = build_url("/json_rpc"); - t_http_connection connection(&m_http_client, m_ip, m_port); + t_http_connection connection(&m_http_client); bool ok = connection.is_open(); if (!ok) @@ -81,7 +68,7 @@ namespace tools fail_msg_writer() << "Couldn't connect to daemon"; return false; } - ok = ok && epee::net_utils::invoke_http_json_rpc(rpc_url, method_name, req, res, m_http_client); + ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT()); if (!ok) { fail_msg_writer() << "Daemon request failed"; @@ -101,11 +88,10 @@ namespace tools , std::string const & fail_msg ) { - std::string rpc_url = build_url("/json_rpc"); - t_http_connection connection(&m_http_client, m_ip, m_port); + t_http_connection connection(&m_http_client); bool ok = connection.is_open(); - ok = ok && epee::net_utils::invoke_http_json_rpc(rpc_url, method_name, req, res, m_http_client); + ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT()); if (!ok) { fail_msg_writer() << "Couldn't connect to daemon"; @@ -130,11 +116,10 @@ namespace tools , std::string const & fail_msg ) { - std::string rpc_url = build_url(relative_url); - t_http_connection connection(&m_http_client, m_ip, m_port); + t_http_connection connection(&m_http_client); bool ok = connection.is_open(); - ok = ok && epee::net_utils::invoke_http_json_remote_command2(rpc_url, req, res, m_http_client); + ok = ok && epee::net_utils::invoke_http_json(relative_url, req, res, m_http_client, t_http_connection::TIMEOUT()); if (!ok) { fail_msg_writer() << "Couldn't connect to daemon"; @@ -153,7 +138,7 @@ namespace tools bool check_connection() { - t_http_connection connection(&m_http_client, m_ip, m_port); + t_http_connection connection(&m_http_client); return connection.is_open(); } }; diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index e006cf5f3..c7487cae8 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -1163,6 +1163,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) } catch (const std::exception &e) { } + m_http_client.set_server(m_wallet->get_daemon_address()); m_wallet->callback(this); return true; } @@ -1560,7 +1561,7 @@ bool simple_wallet::start_mining(const std::vector<std::string>& args) } COMMAND_RPC_START_MINING::response res; - bool r = net_utils::invoke_http_json_remote_command2(m_wallet->get_daemon_address() + "/start_mining", req, res, m_http_client); + bool r = net_utils::invoke_http_json("/start_mining", req, res, m_http_client); std::string err = interpret_rpc_response(r, res.status); if (err.empty()) success_msg_writer() << tr("Mining started in daemon"); @@ -1577,7 +1578,7 @@ bool simple_wallet::stop_mining(const std::vector<std::string>& args) assert(m_wallet); COMMAND_RPC_STOP_MINING::request req; COMMAND_RPC_STOP_MINING::response res; - bool r = net_utils::invoke_http_json_remote_command2(m_wallet->get_daemon_address() + "/stop_mining", req, res, m_http_client); + bool r = net_utils::invoke_http_json("/stop_mining", req, res, m_http_client); std::string err = interpret_rpc_response(r, res.status); if (err.empty()) success_msg_writer() << tr("Mining stopped in daemon"); @@ -1594,7 +1595,7 @@ bool simple_wallet::save_bc(const std::vector<std::string>& args) assert(m_wallet); COMMAND_RPC_SAVE_BC::request req; COMMAND_RPC_SAVE_BC::response res; - bool r = net_utils::invoke_http_json_remote_command2(m_wallet->get_daemon_address() + "/save_bc", req, res, m_http_client); + bool r = net_utils::invoke_http_json("/save_bc", req, res, m_http_client); std::string err = interpret_rpc_response(r, res.status); if (err.empty()) success_msg_writer() << tr("Blockchain saved"); @@ -1883,7 +1884,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<COMMAND_RPC_GET_HEIGHT::response>(); - bool r = net_utils::invoke_http_json_remote_command2(m_wallet->get_daemon_address() + "/getheight", req, res, m_http_client); + bool r = net_utils::invoke_http_json("/getheight", req, res, m_http_client); err = interpret_rpc_response(r, res.status); return res.height; } @@ -1994,7 +1995,7 @@ bool simple_wallet::print_ring_members(const std::vector<tools::wallet2::pending req.outputs[j].index = absolute_offsets[j]; } COMMAND_RPC_GET_OUTPUTS_BIN::response res = AUTO_VAL_INIT(res); - bool r = net_utils::invoke_http_bin_remote_command2(m_wallet->get_daemon_address() + "/get_outs.bin", req, res, m_http_client); + bool r = net_utils::invoke_http_bin("/get_outs.bin", req, res, m_http_client); err = interpret_rpc_response(r, res.status); if (!err.empty()) { @@ -3196,7 +3197,7 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) COMMAND_RPC_GET_TRANSACTIONS::request req; COMMAND_RPC_GET_TRANSACTIONS::response res; req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid)); - if (!net_utils::invoke_http_json_remote_command2(m_wallet->get_daemon_address() + "/gettransactions", req, res, m_http_client) || + if (!net_utils::invoke_http_json("/gettransactions", req, res, m_http_client) || (res.txs.size() != 1 && res.txs_as_hex.size() != 1)) { fail_msg_writer() << tr("failed to get transaction from daemon"); diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index c1cc5d10d..9e40d2e02 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -646,12 +646,12 @@ string WalletImpl::keysFilename() const bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit) { clearStatus(); - doInit(daemon_address, upper_transaction_size_limit); + if (!doInit(daemon_address, upper_transaction_size_limit)) + return false; bool result = this->refresh(); // enabling background refresh thread startRefresh(); return result; - } void WalletImpl::initAsync(const string &daemon_address, uint64_t upper_transaction_size_limit) @@ -1362,9 +1362,10 @@ bool WalletImpl::isNewWallet() const return !(blockChainHeight() > 1 || m_recoveringFromSeed || m_rebuildWalletCache) && !watchOnly(); } -void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit) +bool WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit) { - m_wallet->init(daemon_address, upper_transaction_size_limit); + if (!m_wallet->init(daemon_address, upper_transaction_size_limit)) + return false; // in case new wallet, this will force fast-refresh (pulling hashes instead of blocks) // If daemon isn't synced a calculated block height will be used instead @@ -1383,8 +1384,7 @@ void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction this->setTrustedDaemon(false); m_refreshIntervalMillis = DEFAULT_REMOTE_NODE_REFRESH_INTERVAL_MILLIS; } - - + return true; } bool WalletImpl::parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector<std::string> &unknown_parameters, std::string &error) diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 16a7f5d95..7daf63e43 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -133,7 +133,7 @@ private: bool daemonSynced() const; void stopRefresh(); bool isNewWallet() const; - void doInit(const std::string &daemon_address, uint64_t upper_transaction_size_limit); + bool doInit(const std::string &daemon_address, uint64_t upper_transaction_size_limit); private: diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index fcb39d1f3..c761cc6d2 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -43,6 +43,15 @@ namespace epee { unsigned int g_test_dbg_lock_sleep = 0; } +namespace { + template<typename Request, typename Response> + bool connect_and_invoke(const std::string& address, const std::string& path, const Request& request, Response& response) + { + epee::net_utils::http::http_simple_client client{}; + return client.set_server(address) && epee::net_utils::invoke_http_json(path, request, response, client); + } +} + namespace Monero { Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password, @@ -161,9 +170,7 @@ bool WalletManagerImpl::connected(uint32_t *version) const req_t.jsonrpc = "2.0"; req_t.id = epee::serialization::storage_entry(0); req_t.method = "get_version"; - epee::net_utils::http::http_simple_client http_client; - bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/json_rpc", req_t, resp_t, http_client); - if (!r) + if (!connect_and_invoke(m_daemonAddress, "/json_rpc", req_t, resp_t)) return false; if (version) @@ -209,8 +216,7 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req; cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res; req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid)); - epee::net_utils::http::http_simple_client http_client; - if (!epee::net_utils::invoke_http_json_remote_command2(daemon_address + "/gettransactions", req, res, http_client) || + if (!connect_and_invoke(m_daemonAddress, "/gettransactions", req, res) || (res.txs.size() != 1 && res.txs_as_hex.size() != 1)) { error = tr("failed to get transaction from daemon"); @@ -328,8 +334,7 @@ uint64_t WalletManagerImpl::blockchainHeight() const cryptonote::COMMAND_RPC_GET_INFO::request ireq; cryptonote::COMMAND_RPC_GET_INFO::response ires; - epee::net_utils::http::http_simple_client http_client; - if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires)) return 0; return ires.height; } @@ -339,8 +344,7 @@ uint64_t WalletManagerImpl::blockchainTargetHeight() const cryptonote::COMMAND_RPC_GET_INFO::request ireq; cryptonote::COMMAND_RPC_GET_INFO::response ires; - epee::net_utils::http::http_simple_client http_client; - if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires)) return 0; return ires.target_height >= ires.height ? ires.target_height : ires.height; } @@ -350,8 +354,7 @@ uint64_t WalletManagerImpl::networkDifficulty() const cryptonote::COMMAND_RPC_GET_INFO::request ireq; cryptonote::COMMAND_RPC_GET_INFO::response ires; - epee::net_utils::http::http_simple_client http_client; - if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires)) return 0; return ires.difficulty; } @@ -362,7 +365,7 @@ double WalletManagerImpl::miningHashRate() const cryptonote::COMMAND_RPC_MINING_STATUS::response mres; epee::net_utils::http::http_simple_client http_client; - if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/mining_status", mreq, mres, http_client)) + if (!connect_and_invoke(m_daemonAddress, "/mining_status", mreq, mres)) return 0.0; if (!mres.active) return 0.0; @@ -382,7 +385,7 @@ void WalletManagerImpl::hardForkInfo(uint8_t &version, uint64_t &earliest_height req_t.id = epee::serialization::storage_entry(0); req_t.method = "hard_fork_info"; req_t.params.version = 0; - bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/json_rpc", req_t, resp_t, http_client); + bool r = connect_and_invoke(m_daemonAddress, "/json_rpc", req_t, resp_t); if (!r || resp_t.result.status != CORE_RPC_STATUS_OK) return; version = resp_t.result.version; @@ -394,8 +397,7 @@ uint64_t WalletManagerImpl::blockTarget() const cryptonote::COMMAND_RPC_GET_INFO::request ireq; cryptonote::COMMAND_RPC_GET_INFO::response ires; - epee::net_utils::http::http_simple_client http_client; - if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires)) return 0; return ires.target; } @@ -405,8 +407,7 @@ bool WalletManagerImpl::isMining() const cryptonote::COMMAND_RPC_MINING_STATUS::request mreq; cryptonote::COMMAND_RPC_MINING_STATUS::response mres; - epee::net_utils::http::http_simple_client http_client; - if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/mining_status", mreq, mres, http_client)) + if (!connect_and_invoke(m_daemonAddress, "/mining_status", mreq, mres)) return false; return mres.active; } @@ -419,8 +420,7 @@ bool WalletManagerImpl::startMining(const std::string &address, uint32_t threads mreq.miner_address = address; mreq.threads_count = threads; - epee::net_utils::http::http_simple_client http_client; - if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/start_mining", mreq, mres, http_client)) + if (!connect_and_invoke(m_daemonAddress, "/start_mining", mreq, mres)) return false; return mres.status == CORE_RPC_STATUS_OK; } @@ -430,8 +430,7 @@ bool WalletManagerImpl::stopMining() cryptonote::COMMAND_RPC_STOP_MINING::request mreq; cryptonote::COMMAND_RPC_STOP_MINING::response mres; - epee::net_utils::http::http_simple_client http_client; - if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/stop_mining", mreq, mres, http_client)) + if (!connect_and_invoke(m_daemonAddress, "/stop_mining", mreq, mres)) return false; return mres.status == CORE_RPC_STATUS_OK; } diff --git a/src/wallet/node_rpc_proxy.cpp b/src/wallet/node_rpc_proxy.cpp index d7f755e36..cc249b5cc 100644 --- a/src/wallet/node_rpc_proxy.cpp +++ b/src/wallet/node_rpc_proxy.cpp @@ -36,18 +36,16 @@ using namespace epee; namespace tools { -void NodeRPCProxy::init(const std::string &daemon_address) -{ - m_daemon_address = daemon_address; - - m_height = 0; - m_height_time = 0; - for (auto &slot: m_earliest_height) - slot = 0; - 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; -} +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) +{} boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height) { @@ -58,7 +56,7 @@ boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height) cryptonote::COMMAND_RPC_GET_HEIGHT::response res = AUTO_VAL_INIT(res); m_daemon_rpc_mutex.lock(); - bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/getheight", req, res, m_http_client); + bool r = net_utils::invoke_http_json("/getheight", req, res, m_http_client); 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"); @@ -87,7 +85,7 @@ boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, 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_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client); + 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"); @@ -117,7 +115,7 @@ boost::optional<std::string> NodeRPCProxy::get_dynamic_per_kb_fee_estimate(uint6 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_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client); + 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"); diff --git a/src/wallet/node_rpc_proxy.h b/src/wallet/node_rpc_proxy.h index 1ae716dab..e2f42d541 100644 --- a/src/wallet/node_rpc_proxy.h +++ b/src/wallet/node_rpc_proxy.h @@ -39,10 +39,7 @@ namespace tools class NodeRPCProxy { public: - NodeRPCProxy(epee::net_utils::http::http_simple_client &http_client, boost::mutex &mutex): - m_http_client(http_client), m_daemon_rpc_mutex(mutex) { init(""); } - - void init(const std::string &daemon_address); + NodeRPCProxy(epee::net_utils::http::http_simple_client &http_client, boost::mutex &mutex); boost::optional<std::string> get_height(uint64_t &height); void set_height(uint64_t h); @@ -50,7 +47,6 @@ public: boost::optional<std::string> get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee); private: - std::string m_daemon_address; epee::net_utils::http::http_simple_client &m_http_client; boost::mutex &m_daemon_rpc_mutex; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 14f180e00..2e4550879 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -164,7 +164,7 @@ std::unique_ptr<tools::wallet2> make_basic(const boost::program_options::variabl daemon_address = std::string("http://") + daemon_host + ":" + std::to_string(daemon_port); std::unique_ptr<tools::wallet2> wallet(new tools::wallet2(testnet, restricted)); - wallet->init(daemon_address); + wallet->init(std::move(daemon_address)); return wallet; } @@ -418,6 +418,7 @@ namespace tools // for now, limit to 30 attempts. TODO: discuss a good number to limit to. const size_t MAX_SPLIT_ATTEMPTS = 30; +constexpr const std::chrono::seconds wallet2::rpc_timeout; const char* wallet2::tr(const char* str) { return i18n_translate(str, "tools::wallet2"); } bool wallet2::has_testnet_option(const boost::program_options::variables_map& vm) @@ -484,11 +485,11 @@ std::pair<std::unique_ptr<wallet2>, password_container> wallet2::make_new(const } //---------------------------------------------------------------------------------------------------- -void wallet2::init(const std::string& daemon_address, uint64_t upper_transaction_size_limit) +bool wallet2::init(std::string daemon_address, uint64_t upper_transaction_size_limit) { m_upper_transaction_size_limit = upper_transaction_size_limit; - m_daemon_address = daemon_address; - m_node_rpc_proxy.init(m_daemon_address); + m_daemon_address = std::move(daemon_address); + return m_http_client.set_server(get_daemon_address()); } //---------------------------------------------------------------------------------------------------- bool wallet2::is_deterministic() const @@ -1169,7 +1170,7 @@ void wallet2::pull_blocks(uint64_t start_height, uint64_t &blocks_start_height, req.start_height = start_height; m_daemon_rpc_mutex.lock(); - bool r = net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/getblocks.bin", req, res, m_http_client, WALLET_RCP_CONNECTION_TIMEOUT); + bool r = net_utils::invoke_http_bin("/getblocks.bin", req, res, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "getblocks.bin"); THROW_WALLET_EXCEPTION_IF(res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "getblocks.bin"); @@ -1191,7 +1192,7 @@ void wallet2::pull_hashes(uint64_t start_height, uint64_t &blocks_start_height, req.start_height = start_height; m_daemon_rpc_mutex.lock(); - bool r = net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/gethashes.bin", req, res, m_http_client, WALLET_RCP_CONNECTION_TIMEOUT); + bool r = net_utils::invoke_http_bin("/gethashes.bin", req, res, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "gethashes.bin"); THROW_WALLET_EXCEPTION_IF(res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "gethashes.bin"); @@ -1353,7 +1354,7 @@ void wallet2::update_pool_state() cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req; cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res; m_daemon_rpc_mutex.lock(); - bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemon_address + "/get_transaction_pool", req, res, m_http_client, 200000); + bool r = epee::net_utils::invoke_http_json("/get_transaction_pool", req, res, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_transaction_pool"); THROW_WALLET_EXCEPTION_IF(res.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_transaction_pool"); @@ -1461,7 +1462,7 @@ void wallet2::update_pool_state() req.txs_hashes.push_back(it.id_hash); req.decode_as_json = false; m_daemon_rpc_mutex.lock(); - bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemon_address + "/gettransactions", req, res, m_http_client, 200000); + bool r = epee::net_utils::invoke_http_json("/gettransactions", req, res, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); if (r && res.status == CORE_RPC_STATUS_OK) { @@ -2262,15 +2263,7 @@ bool wallet2::check_connection(uint32_t *version, uint32_t timeout) if(!m_http_client.is_connected()) { - net_utils::http::url_content u; - net_utils::parse_url(m_daemon_address, u); - - if(!u.port) - { - u.port = m_testnet ? config::testnet::RPC_DEFAULT_PORT : config::RPC_DEFAULT_PORT; - } - - if (!m_http_client.connect(u.host, std::to_string(u.port), timeout)) + if (!m_http_client.connect(std::chrono::milliseconds(timeout))) return false; } @@ -2281,7 +2274,7 @@ bool wallet2::check_connection(uint32_t *version, uint32_t timeout) 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_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client); + bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client); if (!r || resp_t.result.status != CORE_RPC_STATUS_OK) *version = 0; else @@ -2602,7 +2595,7 @@ void wallet2::rescan_spent() COMMAND_RPC_IS_KEY_IMAGE_SPENT::response daemon_resp = AUTO_VAL_INIT(daemon_resp); req.key_images = key_images; m_daemon_rpc_mutex.lock(); - bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemon_address + "/is_key_image_spent", req, daemon_resp, m_http_client, 200000); + bool r = epee::net_utils::invoke_http_json("/is_key_image_spent", req, daemon_resp, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "is_key_image_spent"); THROW_WALLET_EXCEPTION_IF(daemon_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "is_key_image_spent"); @@ -2976,7 +2969,7 @@ void wallet2::commit_tx(pending_tx& ptx) req.do_not_relay = false; COMMAND_RPC_SEND_RAW_TX::response daemon_send_resp; m_daemon_rpc_mutex.lock(); - bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemon_address + "/sendrawtransaction", req, daemon_send_resp, m_http_client, 200000); + bool r = epee::net_utils::invoke_http_json("/sendrawtransaction", req, daemon_send_resp, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "sendrawtransaction"); THROW_WALLET_EXCEPTION_IF(daemon_send_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "sendrawtransaction"); @@ -3437,7 +3430,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>> 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_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client); + bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client); 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"); @@ -3564,7 +3557,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>> // get the keys for those m_daemon_rpc_mutex.lock(); - r = epee::net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/get_outs.bin", req, daemon_resp, m_http_client, 200000); + r = epee::net_utils::invoke_http_bin("/get_outs.bin", req, daemon_resp, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_outs.bin"); THROW_WALLET_EXCEPTION_IF(daemon_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_outs.bin"); @@ -4636,7 +4629,7 @@ std::vector<size_t> wallet2::select_available_outputs_from_histogram(uint64_t co req_t.params.min_count = count; req_t.params.max_count = 0; req_t.params.unlocked = unlocked; - bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client); + bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "select_available_unmixable_outputs"); THROW_WALLET_EXCEPTION_IF(resp_t.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_output_histogram"); @@ -4675,7 +4668,7 @@ uint64_t wallet2::get_num_rct_outputs() 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_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client); + bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client); 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"); @@ -4784,7 +4777,7 @@ uint64_t wallet2::get_daemon_blockchain_target_height(string &err) 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_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client); + bool ok = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client); m_daemon_rpc_mutex.unlock(); if (ok) { @@ -5097,7 +5090,7 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag } m_daemon_rpc_mutex.lock(); - bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemon_address + "/is_key_image_spent", req, daemon_resp, m_http_client, 200000); + bool r = epee::net_utils::invoke_http_json("/is_key_image_spent", req, daemon_resp, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "is_key_image_spent"); THROW_WALLET_EXCEPTION_IF(daemon_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "is_key_image_spent"); @@ -5407,7 +5400,7 @@ uint64_t wallet2::get_blockchain_height_by_date(uint16_t year, uint8_t month, ui height_mid, height_max }; - bool r = net_utils::invoke_http_bin_remote_command2(get_daemon_address() + "/getblocks_by_height.bin", req, res, m_http_client); + bool r = net_utils::invoke_http_bin("/getblocks_by_height.bin", req, res, m_http_client); if (!r || res.status != CORE_RPC_STATUS_OK) { std::ostringstream oss; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index e136c568c..91d4db47e 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -61,8 +61,6 @@ #undef MONERO_DEFAULT_LOG_CATEGORY #define MONERO_DEFAULT_LOG_CATEGORY "wallet.wallet2" -#define WALLET_RCP_CONNECTION_TIMEOUT 200000 - class Serialization_portability_wallet_Test; namespace tools @@ -96,6 +94,8 @@ namespace tools { friend class ::Serialization_portability_wallet_Test; public: + static constexpr const std::chrono::seconds rpc_timeout = std::chrono::minutes(3) + std::chrono::seconds(30); + enum RefreshType { RefreshFull, RefreshOptimizeCoinbase, @@ -107,7 +107,7 @@ namespace tools wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_node_rpc_proxy(m_http_client, m_daemon_rpc_mutex) {} public: - static const char* tr(const char* str);// { return i18n_translate(str, "cryptonote::simple_wallet"); } + static const char* tr(const char* str); static bool has_testnet_option(const boost::program_options::variables_map& vm); static void init_options(boost::program_options::options_description& desc_params); @@ -342,8 +342,8 @@ namespace tools // free block size. TODO: fix this so that it actually takes // into account the current median block size rather than // the minimum block size. - void init(const std::string& daemon_address = "http://localhost:8080", uint64_t upper_transaction_size_limit = 0); bool deinit(); + bool init(std::string daemon_address = "http://localhost:8080", uint64_t upper_transaction_size_limit = 0); void stop() { m_run.store(false, std::memory_order_relaxed); } @@ -1018,7 +1018,7 @@ namespace tools } m_daemon_rpc_mutex.lock(); - bool r = epee::net_utils::invoke_http_bin_remote_command2(m_daemon_address + "/getrandom_outs.bin", req, daemon_resp, m_http_client, 200000); + bool r = epee::net_utils::invoke_http_bin("/getrandom_outs.bin", req, daemon_resp, m_http_client, rpc_timeout); m_daemon_rpc_mutex.unlock(); THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "getrandom_outs.bin"); THROW_WALLET_EXCEPTION_IF(daemon_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "getrandom_outs.bin"); |