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(); 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() bool WalletImpl::refresh()
{ {
clearStatus(); clearStatus();

View File

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

View File

@ -4050,7 +4050,35 @@ std::string wallet2::get_keys_file() const
std::string wallet2::get_daemon_address() 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) 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_wallet_file() const;
std::string get_keys_file() const; std::string get_keys_file() const;
std::string get_daemon_address() 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_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); 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; virtual uint64_t unlockedBalance() const = 0;
/** /**
* @brief getBlockChainHeight - returns current blockchain height * @brief blockChainHeight - returns current blockchain height
* @return * @return
*/ */
virtual uint64_t blockChainHeight() const = 0; 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 std::string displayAmount(uint64_t amount);
static uint64_t amountFromString(const std::string &amount); static uint64_t amountFromString(const std::string &amount);
static uint64_t amountFromDouble(double amount); static uint64_t amountFromDouble(double amount);

View File

@ -489,7 +489,7 @@ TEST_F(WalletTest1, WalletShowsBalance)
ASSERT_TRUE(wmgr->closeWallet(wallet2)); ASSERT_TRUE(wmgr->closeWallet(wallet2));
} }
TEST_F(WalletTest1, WalletReturnsBlockHeight) TEST_F(WalletTest1, WalletReturnsCurrentBlockHeight)
{ {
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true); Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
ASSERT_TRUE(wallet1->blockChainHeight() > 0); ASSERT_TRUE(wallet1->blockChainHeight() > 0);
@ -497,6 +497,24 @@ TEST_F(WalletTest1, WalletReturnsBlockHeight)
} }
TEST_F(WalletTest1, WalletReturnsDaemonBlockHeight)
{
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
// wallet not connected to daemon
ASSERT_TRUE(wallet1->daemonBlockChainHeight() == 0);
ASSERT_TRUE(wallet1->status() != Bitmonero::Wallet::Status_Ok);
ASSERT_FALSE(wallet1->errorString().empty());
wmgr->closeWallet(wallet1);
wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
// wallet connected to daemon
wallet1->init(TESTNET_DAEMON_ADDRESS, 0);
ASSERT_TRUE(wallet1->daemonBlockChainHeight() > 0);
std::cout << "daemonBlockChainHeight: " << wallet1->daemonBlockChainHeight() << std::endl;
wmgr->closeWallet(wallet1);
}
TEST_F(WalletTest1, WalletRefresh) TEST_F(WalletTest1, WalletRefresh)
{ {
@ -942,7 +960,7 @@ TEST_F(WalletTest2, WalletCallbackNewBlock)
// make sure testnet daemon is running // make sure testnet daemon is running
ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0));
ASSERT_TRUE(wallet_src->refresh()); ASSERT_TRUE(wallet_src->refresh());
uint64_t bc1 = wallet_src->blockChainHeight(); uint64_t bc1 = wallet_src->currentBlockChainHeight();
std::cout << "** Block height: " << bc1 << std::endl; std::cout << "** Block height: " << bc1 << std::endl;
@ -956,7 +974,7 @@ TEST_F(WalletTest2, WalletCallbackNewBlock)
std::cerr << "TEST: newblock lock acquired...\n"; std::cerr << "TEST: newblock lock acquired...\n";
ASSERT_TRUE(wallet_listener->newblock_triggered); ASSERT_TRUE(wallet_listener->newblock_triggered);
ASSERT_TRUE(wallet_listener->update_triggered); ASSERT_TRUE(wallet_listener->update_triggered);
uint64_t bc2 = wallet_src->blockChainHeight(); uint64_t bc2 = wallet_src->currentBlockChainHeight();
std::cout << "** Block height: " << bc2 << std::endl; std::cout << "** Block height: " << bc2 << std::endl;
ASSERT_TRUE(bc2 > bc1); ASSERT_TRUE(bc2 > bc1);
wmgr->closeWallet(wallet_src); wmgr->closeWallet(wallet_src);

View File

@ -1,7 +1,5 @@
#!/bin/bash #!/bin/bash
function send_funds { function send_funds {
local amount=$1 local amount=$1
local dest=$(cat "$2.address.txt") local dest=$(cat "$2.address.txt")