From 7b4a85b309aa0fb864d63a2fd6f1ae062aaa71dd Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Mon, 26 Sep 2016 21:35:00 +0300 Subject: [PATCH] wallet2_api: added Wallet::daemonBlockChainHeight() --- src/wallet/api/wallet.cpp | 15 ++++++++++ src/wallet/api/wallet.h | 5 ++-- src/wallet/wallet2.cpp | 30 ++++++++++++++++++- src/wallet/wallet2.h | 1 + src/wallet/wallet2_api.h | 10 ++++++- tests/libwallet_api_tests/main.cpp | 24 +++++++++++++-- .../libwallet_api_tests/scripts/send_funds.sh | 2 -- 7 files changed, 78 insertions(+), 9 deletions(-) diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index eef956e80..986deca1f 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -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(); diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 1a34a04fd..03801edac 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -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 m_status; - std::string m_errorString; + mutable std::atomic m_status; + mutable std::string m_errorString; std::string m_password; TransactionHistoryImpl * m_history; bool m_trustedDaemon; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 3d4f93aff..07b305b4c 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -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(); + 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 ¬e) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 89b613d34..dd7cd19dc 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -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 select_available_outputs_from_histogram(uint64_t count, bool atleast, bool unlocked, bool trusted_daemon); std::vector select_available_outputs(const std::function &f); diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 08f010b32..f424f7258 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -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); diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index af5549426..7cba5b8b0 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -489,7 +489,7 @@ TEST_F(WalletTest1, WalletShowsBalance) 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); 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) { @@ -942,7 +960,7 @@ TEST_F(WalletTest2, WalletCallbackNewBlock) // make sure testnet daemon is running ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet_src->refresh()); - uint64_t bc1 = wallet_src->blockChainHeight(); + uint64_t bc1 = wallet_src->currentBlockChainHeight(); std::cout << "** Block height: " << bc1 << std::endl; @@ -956,7 +974,7 @@ TEST_F(WalletTest2, WalletCallbackNewBlock) std::cerr << "TEST: newblock lock acquired...\n"; ASSERT_TRUE(wallet_listener->newblock_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; ASSERT_TRUE(bc2 > bc1); wmgr->closeWallet(wallet_src); diff --git a/tests/libwallet_api_tests/scripts/send_funds.sh b/tests/libwallet_api_tests/scripts/send_funds.sh index b7f282b71..3ce923353 100755 --- a/tests/libwallet_api_tests/scripts/send_funds.sh +++ b/tests/libwallet_api_tests/scripts/send_funds.sh @@ -1,7 +1,5 @@ #!/bin/bash - - function send_funds { local amount=$1 local dest=$(cat "$2.address.txt")