mirror of
https://github.com/monero-project/monero.git
synced 2025-05-19 21:40:23 -04:00
WalletManagerImpl: reuse existing connection to daemon instead of reconnectivng every time
This commit is contained in:
parent
8361d60aef
commit
f82c10dc96
3 changed files with 35 additions and 39 deletions
|
@ -47,15 +47,6 @@ 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, boost::none) && epee::net_utils::invoke_http_json(path, request, response, client);
|
||||
}
|
||||
}
|
||||
|
||||
namespace Monero {
|
||||
|
||||
Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password,
|
||||
|
@ -193,16 +184,19 @@ std::string WalletManagerImpl::errorString() const
|
|||
void WalletManagerImpl::setDaemonAddress(const std::string &address)
|
||||
{
|
||||
m_daemonAddress = address;
|
||||
if(m_http_client.is_connected())
|
||||
m_http_client.disconnect();
|
||||
m_http_client.set_server(address, boost::none);
|
||||
}
|
||||
|
||||
bool WalletManagerImpl::connected(uint32_t *version) const
|
||||
bool WalletManagerImpl::connected(uint32_t *version)
|
||||
{
|
||||
epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_VERSION::request> req_t = AUTO_VAL_INIT(req_t);
|
||||
epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_VERSION::response, std::string> 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";
|
||||
if (!connect_and_invoke(m_daemonAddress, "/json_rpc", req_t, resp_t))
|
||||
if (!epee::net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client))
|
||||
return false;
|
||||
|
||||
if (version)
|
||||
|
@ -210,65 +204,65 @@ bool WalletManagerImpl::connected(uint32_t *version) const
|
|||
return true;
|
||||
}
|
||||
|
||||
uint64_t WalletManagerImpl::blockchainHeight() const
|
||||
uint64_t WalletManagerImpl::blockchainHeight()
|
||||
{
|
||||
cryptonote::COMMAND_RPC_GET_INFO::request ireq;
|
||||
cryptonote::COMMAND_RPC_GET_INFO::response ires;
|
||||
|
||||
if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires))
|
||||
if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client))
|
||||
return 0;
|
||||
return ires.height;
|
||||
}
|
||||
|
||||
uint64_t WalletManagerImpl::blockchainTargetHeight() const
|
||||
uint64_t WalletManagerImpl::blockchainTargetHeight()
|
||||
{
|
||||
cryptonote::COMMAND_RPC_GET_INFO::request ireq;
|
||||
cryptonote::COMMAND_RPC_GET_INFO::response ires;
|
||||
|
||||
if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires))
|
||||
if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client))
|
||||
return 0;
|
||||
return ires.target_height >= ires.height ? ires.target_height : ires.height;
|
||||
}
|
||||
|
||||
uint64_t WalletManagerImpl::networkDifficulty() const
|
||||
uint64_t WalletManagerImpl::networkDifficulty()
|
||||
{
|
||||
cryptonote::COMMAND_RPC_GET_INFO::request ireq;
|
||||
cryptonote::COMMAND_RPC_GET_INFO::response ires;
|
||||
|
||||
if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires))
|
||||
if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client))
|
||||
return 0;
|
||||
return ires.difficulty;
|
||||
}
|
||||
|
||||
double WalletManagerImpl::miningHashRate() const
|
||||
double WalletManagerImpl::miningHashRate()
|
||||
{
|
||||
cryptonote::COMMAND_RPC_MINING_STATUS::request mreq;
|
||||
cryptonote::COMMAND_RPC_MINING_STATUS::response mres;
|
||||
|
||||
epee::net_utils::http::http_simple_client http_client;
|
||||
if (!connect_and_invoke(m_daemonAddress, "/mining_status", mreq, mres))
|
||||
if (!epee::net_utils::invoke_http_json("/mining_status", mreq, mres, m_http_client))
|
||||
return 0.0;
|
||||
if (!mres.active)
|
||||
return 0.0;
|
||||
return mres.speed;
|
||||
}
|
||||
|
||||
uint64_t WalletManagerImpl::blockTarget() const
|
||||
uint64_t WalletManagerImpl::blockTarget()
|
||||
{
|
||||
cryptonote::COMMAND_RPC_GET_INFO::request ireq;
|
||||
cryptonote::COMMAND_RPC_GET_INFO::response ires;
|
||||
|
||||
if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires))
|
||||
if (!epee::net_utils::invoke_http_json("/getinfo", ireq, ires, m_http_client))
|
||||
return 0;
|
||||
return ires.target;
|
||||
}
|
||||
|
||||
bool WalletManagerImpl::isMining() const
|
||||
bool WalletManagerImpl::isMining()
|
||||
{
|
||||
cryptonote::COMMAND_RPC_MINING_STATUS::request mreq;
|
||||
cryptonote::COMMAND_RPC_MINING_STATUS::response mres;
|
||||
|
||||
if (!connect_and_invoke(m_daemonAddress, "/mining_status", mreq, mres))
|
||||
if (!epee::net_utils::invoke_http_json("/mining_status", mreq, mres, m_http_client))
|
||||
return false;
|
||||
return mres.active;
|
||||
}
|
||||
|
@ -283,7 +277,7 @@ bool WalletManagerImpl::startMining(const std::string &address, uint32_t threads
|
|||
mreq.ignore_battery = ignore_battery;
|
||||
mreq.do_background_mining = background_mining;
|
||||
|
||||
if (!connect_and_invoke(m_daemonAddress, "/start_mining", mreq, mres))
|
||||
if (!epee::net_utils::invoke_http_json("/start_mining", mreq, mres, m_http_client))
|
||||
return false;
|
||||
return mres.status == CORE_RPC_STATUS_OK;
|
||||
}
|
||||
|
@ -293,7 +287,7 @@ bool WalletManagerImpl::stopMining()
|
|||
cryptonote::COMMAND_RPC_STOP_MINING::request mreq;
|
||||
cryptonote::COMMAND_RPC_STOP_MINING::response mres;
|
||||
|
||||
if (!connect_and_invoke(m_daemonAddress, "/stop_mining", mreq, mres))
|
||||
if (!epee::net_utils::invoke_http_json("/stop_mining", mreq, mres, m_http_client))
|
||||
return false;
|
||||
return mres.status == CORE_RPC_STATUS_OK;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue