wallet2_api: added Wallet::daemonBlockChainHeight()

This commit is contained in:
Ilya Kitaev 2016-09-26 21:35:00 +03:00
parent 9de3ec3e2a
commit 7b4a85b309
7 changed files with 78 additions and 9 deletions

View file

@ -422,6 +422,21 @@ uint64_t WalletImpl::blockChainHeight() const
return m_wallet->get_blockchain_current_height();
}
uint64_t WalletImpl::daemonBlockChainHeight() const
{
std::string err;
uint64_t result = m_wallet->get_daemon_blockchain_height(err);
if (!err.empty()) {
LOG_ERROR(__FUNCTION__ << ": " << err);
m_errorString = err;
m_status = Status_Error;
} else {
m_status = Status_Ok;
m_errorString = "";
}
return result;
}
bool WalletImpl::refresh()
{
clearStatus();

View file

@ -76,6 +76,7 @@ public:
uint64_t balance() const;
uint64_t unlockedBalance() const;
uint64_t blockChainHeight() const;
uint64_t daemonBlockChainHeight() const;
bool refresh();
void refreshAsync();
void setAutoRefreshInterval(int seconds);
@ -106,8 +107,8 @@ private:
friend class TransactionHistoryImpl;
tools::wallet2 * m_wallet;
std::atomic<int> m_status;
std::string m_errorString;
mutable std::atomic<int> m_status;
mutable std::string m_errorString;
std::string m_password;
TransactionHistoryImpl * m_history;
bool m_trustedDaemon;

View file

@ -4050,7 +4050,35 @@ std::string wallet2::get_keys_file() const
std::string wallet2::get_daemon_address() const
{
return m_daemon_address;
return m_daemon_address;
}
uint64_t wallet2::get_daemon_blockchain_height(string &err)
{
// XXX: DRY violation. copy-pasted from simplewallet.cpp:get_daemon_blockchain_height()
// consider to move it from simplewallet to wallet2 ?
COMMAND_RPC_GET_HEIGHT::request req;
COMMAND_RPC_GET_HEIGHT::response res = boost::value_initialized<COMMAND_RPC_GET_HEIGHT::response>();
m_daemon_rpc_mutex.lock();
bool ok = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/getheight", req, res, m_http_client);
m_daemon_rpc_mutex.unlock();
// XXX: DRY violation. copy-pasted from simplewallet.cpp:interpret_rpc_response()
if (ok)
{
if (res.status == CORE_RPC_STATUS_BUSY)
{
err = "daemon is busy. Please try again later.";
}
else if (res.status != CORE_RPC_STATUS_OK)
{
err = res.status;
}
}
else
{
err = "possibly lost connection to daemon";
}
return res.height;
}
void wallet2::set_tx_note(const crypto::hash &txid, const std::string &note)

View file

@ -400,6 +400,7 @@ namespace tools
std::string get_wallet_file() const;
std::string get_keys_file() const;
std::string get_daemon_address() const;
uint64_t get_daemon_blockchain_height(std::string& err);
std::vector<size_t> select_available_outputs_from_histogram(uint64_t count, bool atleast, bool unlocked, bool trusted_daemon);
std::vector<size_t> select_available_outputs(const std::function<bool(const transfer_details &td)> &f);

View file

@ -236,11 +236,19 @@ struct Wallet
virtual uint64_t unlockedBalance() const = 0;
/**
* @brief getBlockChainHeight - returns current blockchain height
* @brief blockChainHeight - returns current blockchain height
* @return
*/
virtual uint64_t blockChainHeight() const = 0;
/**
* @brief daemonBlockChainHeight - returns daemon blockchain height
* @return 0 - in case error communicating with the daemon.
* status() will return Status_Error and errorString() will return verbose error description
*/
virtual uint64_t daemonBlockChainHeight() const = 0;
static std::string displayAmount(uint64_t amount);
static uint64_t amountFromString(const std::string &amount);
static uint64_t amountFromDouble(double amount);