Permalink
Browse files

keep using existing daemon connection instead of reconnecting every time

  • Loading branch information...
miltonf committed Jan 16, 2019
1 parent b6fcc56 commit e7b91de90db5a6b7913179dab0624f8468f8276a
Showing with 35 additions and 39 deletions.
  1. +7 −7 src/wallet/api/wallet2_api.h
  2. +19 −25 src/wallet/api/wallet_manager.cpp
  3. +9 −7 src/wallet/api/wallet_manager.h
@@ -1039,25 +1039,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;
@@ -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,82 +184,85 @@ 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)
*version = resp_t.result.version;
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;
}
@@ -30,6 +30,7 @@


#include "wallet/api/wallet2_api.h"
#include "net/http_client.h"
#include <string>

namespace Monero {
@@ -69,13 +70,13 @@ class WalletManagerImpl : public WalletManager
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 @@ class WalletManagerImpl : public WalletManager
WalletManagerImpl() {}
friend struct WalletManagerFactory;
std::string m_daemonAddress;
epee::net_utils::http::http_simple_client m_http_client;
std::string m_errorString;
};

0 comments on commit e7b91de

Please sign in to comment.