Merge pull request #3576

f82c10dc WalletManagerImpl: reuse existing connection to daemon instead of reconnectivng every time (stoffu)
This commit is contained in:
Riccardo Spagni 2018-04-21 21:58:10 +02:00
commit a3a617f35f
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
3 changed files with 35 additions and 39 deletions

View file

@ -956,25 +956,25 @@ struct WalletManager
virtual void setDaemonAddress(const std::string &address) = 0;
//! returns whether the daemon can be reached, and its version number
virtual bool connected(uint32_t *version = NULL) const = 0;
virtual bool connected(uint32_t *version = NULL) = 0;
//! returns current blockchain height
virtual uint64_t blockchainHeight() const = 0;
virtual uint64_t blockchainHeight() = 0;
//! returns current blockchain target height
virtual uint64_t blockchainTargetHeight() const = 0;
virtual uint64_t blockchainTargetHeight() = 0;
//! returns current network difficulty
virtual uint64_t networkDifficulty() const = 0;
virtual uint64_t networkDifficulty() = 0;
//! returns current mining hash rate (0 if not mining)
virtual double miningHashRate() const = 0;
virtual double miningHashRate() = 0;
//! returns current block target
virtual uint64_t blockTarget() const = 0;
virtual uint64_t blockTarget() = 0;
//! returns true iff mining
virtual bool isMining() const = 0;
virtual bool isMining() = 0;
//! starts mining with the set number of threads
virtual bool startMining(const std::string &address, uint32_t threads = 1, bool background_mining = false, bool ignore_battery = true) = 0;

View file

@ -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;
}

View file

@ -30,6 +30,7 @@
#include "wallet/api/wallet2_api.h"
#include "net/http_client.h"
#include <string>
namespace Monero {
@ -69,13 +70,13 @@ public:
std::vector<std::string> findWallets(const std::string &path);
std::string errorString() const;
void setDaemonAddress(const std::string &address);
bool connected(uint32_t *version = NULL) const;
uint64_t blockchainHeight() const;
uint64_t blockchainTargetHeight() const;
uint64_t networkDifficulty() const;
double miningHashRate() const;
uint64_t blockTarget() const;
bool isMining() const;
bool connected(uint32_t *version = NULL);
uint64_t blockchainHeight();
uint64_t blockchainTargetHeight();
uint64_t networkDifficulty();
double miningHashRate();
uint64_t blockTarget();
bool isMining();
bool startMining(const std::string &address, uint32_t threads = 1, bool background_mining = false, bool ignore_battery = true);
bool stopMining();
std::string resolveOpenAlias(const std::string &address, bool &dnssec_valid) const;
@ -84,6 +85,7 @@ private:
WalletManagerImpl() {}
friend struct WalletManagerFactory;
std::string m_daemonAddress;
epee::net_utils::http::http_simple_client m_http_client;
std::string m_errorString;
};