diff options
author | Riccardo Spagni <ric@spagni.net> | 2016-09-29 22:13:49 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2016-09-29 22:13:49 +0200 |
commit | f195a447ccf1a26eba8aaf7560e6bfc902db6f77 (patch) | |
tree | ee36fd1a595b1d0df170f805e2a0b6a4d2d90019 /src/wallet | |
parent | Merge pull request #1137 (diff) | |
parent | wallet2: wallet2::get_daemon_blockchain_height() clean error message on (diff) | |
download | monero-f195a447ccf1a26eba8aaf7560e6bfc902db6f77.tar.xz |
Merge pull request #1136
2dacb19 wallet2: wallet2::get_daemon_blockchain_height() clean error message on success (Ilya Kitaev)
25e5efc libwallet_api: Wallet::setAutoRefreshInterval sanity check (Ilya Kitaev)
a668820 libwallet_api: explicitly return 0 in Wallet::daemonBlockChainHeight() on error (Ilya Kitaev)
aef92f2 libwallet_api: tests: fixed WalletCallbackReceived test (Ilya Kitaev)
15c0882 libwallet_api: tests: test fixed according implementation (Ilya Kitaev)
a7882da libwallet_api: tests: compilation errors fixed (Ilya Kitaev)
cda4cb9 formatting: 2-spaces indentation (Ilya Kitaev)
545a48f formatting: 2-spaces indentation (Ilya Kitaev)
3079c57 wallet2_api: milliseconds resolution for auto-refresh interval (Ilya Kitaev)
7b4a85b wallet2_api: added Wallet::daemonBlockChainHeight() (Ilya Kitaev)
9de3ec3 libwallet_api: Wallet::blockChainHeight, WalletListener::newBlock (Ilya Kitaev)
Diffstat (limited to 'src/wallet')
-rw-r--r-- | src/wallet/api/wallet.cpp | 49 | ||||
-rw-r--r-- | src/wallet/api/wallet.h | 10 | ||||
-rw-r--r-- | src/wallet/wallet2.cpp | 32 | ||||
-rw-r--r-- | src/wallet/wallet2.h | 1 | ||||
-rw-r--r-- | src/wallet/wallet2_api.h | 48 |
5 files changed, 123 insertions, 17 deletions
diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 49ccceb13..eefb49e95 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -46,7 +46,9 @@ namespace Bitmonero { namespace { // copy-pasted from simplewallet static const size_t DEFAULT_MIXIN = 4; - static const int DEFAULT_REFRESH_INTERVAL_SECONDS = 10; + static const int DEFAULT_REFRESH_INTERVAL_MILLIS = 1000 * 10; + // limit maximum refresh interval as one minute + static const int MAX_REFRESH_INTERVAL_MILLIS = 1000 * 60 * 1; } struct Wallet2CallbackImpl : public tools::i_wallet2_callback @@ -75,8 +77,12 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback virtual void on_new_block(uint64_t height, const cryptonote::block& block) { - // TODO; LOG_PRINT_L3(__FUNCTION__ << ": new block. height: " << height); + + if (m_listener) { + m_listener->newBlock(height); + // m_listener->updated(); + } } virtual void on_money_received(uint64_t height, const cryptonote::transaction& tx, uint64_t amount) @@ -170,7 +176,7 @@ WalletImpl::WalletImpl(bool testnet) m_refreshThreadDone = false; m_refreshEnabled = false; - m_refreshIntervalSeconds = DEFAULT_REFRESH_INTERVAL_SECONDS; + m_refreshIntervalMillis = DEFAULT_REFRESH_INTERVAL_MILLIS; m_refreshThread = boost::thread([this] () { this->refreshThreadFunc(); @@ -413,6 +419,27 @@ uint64_t WalletImpl::unlockedBalance() const return m_wallet->unlocked_balance(); } +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); + result = 0; + m_errorString = err; + m_status = Status_Error; + + } else { + m_status = Status_Ok; + m_errorString = ""; + } + return result; +} bool WalletImpl::refresh() { @@ -428,14 +455,20 @@ void WalletImpl::refreshAsync() m_refreshCV.notify_one(); } -void WalletImpl::setAutoRefreshInterval(int seconds) +void WalletImpl::setAutoRefreshInterval(int millis) { - m_refreshIntervalSeconds = seconds; + if (millis > MAX_REFRESH_INTERVAL_MILLIS) { + LOG_ERROR(__FUNCTION__<< ": invalid refresh interval " << millis + << " ms, maximum allowed is " << MAX_REFRESH_INTERVAL_MILLIS << " ms"); + m_refreshIntervalMillis = MAX_REFRESH_INTERVAL_MILLIS; + } else { + m_refreshIntervalMillis = millis; + } } int WalletImpl::autoRefreshInterval() const { - return m_refreshIntervalSeconds; + return m_refreshIntervalMillis; } // TODO: @@ -652,8 +685,8 @@ void WalletImpl::refreshThreadFunc() LOG_PRINT_L3(__FUNCTION__ << ": waiting for refresh..."); // if auto refresh enabled, we wait for the "m_refreshIntervalSeconds" interval. // if not - we wait forever - if (m_refreshIntervalSeconds > 0) { - boost::posix_time::milliseconds wait_for_ms(m_refreshIntervalSeconds * 1000); + if (m_refreshIntervalMillis > 0) { + boost::posix_time::milliseconds wait_for_ms(m_refreshIntervalMillis); m_refreshCV.timed_wait(lock, wait_for_ms); } else { m_refreshCV.wait(lock); diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 11880d555..d97a8f3b3 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -75,9 +75,11 @@ public: bool trustedDaemon() const; uint64_t balance() const; uint64_t unlockedBalance() const; + uint64_t blockChainHeight() const; + uint64_t daemonBlockChainHeight() const; bool refresh(); void refreshAsync(); - void setAutoRefreshInterval(int seconds); + void setAutoRefreshInterval(int millis); int autoRefreshInterval() const; @@ -105,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; @@ -116,7 +118,7 @@ private: // multi-threaded refresh stuff std::atomic<bool> m_refreshEnabled; std::atomic<bool> m_refreshThreadDone; - std::atomic<int> m_refreshIntervalSeconds; + std::atomic<int> m_refreshIntervalMillis; // synchronizing refresh loop; boost::mutex m_refreshMutex; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 3d4f93aff..ed4ab93de 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -4053,6 +4053,38 @@ std::string wallet2::get_daemon_address() const 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 // success, cleaning up error message + { + err = ""; + } + } + else + { + err = "possibly lost connection to daemon"; + } + return res.height; +} + void wallet2::set_tx_note(const crypto::hash &txid, const std::string ¬e) { m_tx_notes[txid] = note; 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<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); diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index 2d2877856..08e2ae16b 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -114,11 +114,35 @@ struct TransactionHistory struct WalletListener { virtual ~WalletListener() = 0; + /** + * @brief moneySpent - called when money spent + * @param txId - transaction id + * @param amount - amount + */ virtual void moneySpent(const std::string &txId, uint64_t amount) = 0; + + /** + * @brief moneyReceived - called when money received + * @param txId - transaction id + * @param amount - amount + */ virtual void moneyReceived(const std::string &txId, uint64_t amount) = 0; - // generic callback, called when any event (sent/received/block reveived/etc) happened with the wallet; + + /** + * @brief newBlock - called when new block received + * @param height - block height + */ + virtual void newBlock(uint64_t height) = 0; + + /** + * @brief updated - generic callback, called when any event (sent/received/block reveived/etc) happened with the wallet; + */ virtual void updated() = 0; - // called when wallet refreshed by background thread or explicitly called be calling "refresh" synchronously + + + /** + * @brief refreshed - called when wallet refreshed by background thread or explicitly refreshed by calling "refresh" synchronously + */ virtual void refreshed() = 0; }; @@ -211,6 +235,20 @@ struct Wallet virtual uint64_t balance() const = 0; virtual uint64_t unlockedBalance() const = 0; + /** + * @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); @@ -231,12 +269,12 @@ struct Wallet /** * @brief setAutoRefreshInterval - setup interval for automatic refresh. - * @param seconds - interval in seconds. if zero or less than zero - automatic refresh disabled; + * @param seconds - interval in millis. if zero or less than zero - automatic refresh disabled; */ - virtual void setAutoRefreshInterval(int seconds) = 0; + virtual void setAutoRefreshInterval(int millis) = 0; /** - * @brief autoRefreshInterval - returns automatic refresh interval in seconds + * @brief autoRefreshInterval - returns automatic refresh interval in millis * @return */ virtual int autoRefreshInterval() const = 0; |