diff options
author | Ilya Kitaev <mbg033@gmail.com> | 2016-07-10 17:17:23 +0300 |
---|---|---|
committer | Ilya Kitaev <mbg033@gmail.com> | 2016-07-18 23:02:47 +0300 |
commit | 9d2cb4f36ca7220bb05217547b72710b870f6e57 (patch) | |
tree | e0ced3eede4406ef4f4a020c0dbc09764c2cc62e /src/wallet | |
parent | hack to successfull linking for MSYS2 (diff) | |
download | monero-9d2cb4f36ca7220bb05217547b72710b870f6e57.tar.xz |
WalletListener functionality
Diffstat (limited to '')
-rw-r--r-- | src/wallet/api/wallet.cpp | 89 | ||||
-rw-r--r-- | src/wallet/api/wallet.h | 21 | ||||
-rw-r--r-- | src/wallet/wallet2_api.h | 8 |
3 files changed, 105 insertions, 13 deletions
diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 3f2ea50bd..f71cbd85b 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -46,6 +46,7 @@ namespace Bitmonero { namespace { // copy-pasted from simplewallet static const size_t DEFAULT_MIXIN = 4; + static const int DEFAULT_REFRESH_INTERVAL_SECONDS = 10; } struct Wallet2CallbackImpl : public tools::i_wallet2_callback @@ -75,7 +76,7 @@ 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); + LOG_PRINT_L0(__FUNCTION__ << ": new block. height: " << height); } virtual void on_money_received(uint64_t height, const cryptonote::transaction& tx, size_t out_index) @@ -84,11 +85,12 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback std::string tx_hash = epee::string_tools::pod_to_hex(get_transaction_hash(tx)); uint64_t amount = tx.vout[out_index].amount; - LOG_PRINT_L3(__FUNCTION__ << ": money received. height: " << height + LOG_PRINT_L0(__FUNCTION__ << ": money received. height: " << height << ", tx: " << tx_hash << ", amount: " << print_money(amount)); if (m_listener) { m_listener->moneyReceived(tx_hash, amount); + m_listener->updated(); } } @@ -98,11 +100,12 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback // TODO; std::string tx_hash = epee::string_tools::pod_to_hex(get_transaction_hash(spend_tx)); uint64_t amount = in_tx.vout[out_index].amount; - LOG_PRINT_L3(__FUNCTION__ << ": money spent. height: " << height + LOG_PRINT_L0(__FUNCTION__ << ": money spent. height: " << height << ", tx: " << tx_hash << ", amount: " << print_money(amount)); if (m_listener) { m_listener->moneySpent(tx_hash, amount); + m_listener->updated(); } } @@ -159,13 +162,22 @@ WalletImpl::WalletImpl(bool testnet) m_wallet = new tools::wallet2(testnet); m_history = new TransactionHistoryImpl(this); m_wallet2Callback = new Wallet2CallbackImpl; + m_wallet->callback(m_wallet2Callback); + m_refreshThreadDone = false; + m_refreshEnabled = false; + m_refreshIntervalSeconds = DEFAULT_REFRESH_INTERVAL_SECONDS; + m_refreshThread = std::thread([this] () { + this->refreshThreadFunc(); + }); + } WalletImpl::~WalletImpl() { - delete m_wallet2Callback; + stopRefresh(); delete m_history; delete m_wallet; + delete m_wallet2Callback; } bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) @@ -359,6 +371,7 @@ bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transact if (Utils::isAddressLocal(daemon_address)) { this->setTrustedDaemon(true); } + startRefresh(); } catch (const std::exception &e) { LOG_ERROR("Error initializing wallet: " << e.what()); @@ -383,15 +396,17 @@ uint64_t WalletImpl::unlockedBalance() const bool WalletImpl::refresh() { clearStatus(); - try { - m_wallet->refresh(); - } catch (const std::exception &e) { - m_status = Status_Error; - m_errorString = e.what(); - } + doRefresh(); return m_status == Status_Ok; } +void WalletImpl::refreshAsync() +{ + LOG_PRINT_L3(__FUNCTION__ << ": Refreshing asyncronously.."); + clearStatus(); + m_refreshCV.notify_one(); +} + // TODO: // 1 - properly handle payment id (add another menthod with explicit 'payment_id' param) // 2 - check / design how "Transaction" can be single interface @@ -574,6 +589,8 @@ bool WalletImpl::connectToDaemon() m_status = result ? Status_Ok : Status_Error; if (!result) { m_errorString = "Error connecting to daemon at " + m_wallet->get_daemon_address(); + } else { + // start refreshing here } return result; } @@ -594,5 +611,57 @@ void WalletImpl::clearStatus() m_errorString.clear(); } +void WalletImpl::refreshThreadFunc() +{ + LOG_PRINT_L3(__FUNCTION__ << ": starting refresh thread"); + + while (true) { + std::unique_lock<std::mutex> lock(m_refreshMutex); + if (m_refreshThreadDone) { + break; + } + m_refreshCV.wait_for(lock, std::chrono::seconds(m_refreshIntervalSeconds)); + if (m_refreshEnabled && m_status == Status_Ok) { + doRefresh(); + } + } + LOG_PRINT_L3(__FUNCTION__ << ": refresh thread stopped"); +} + +void WalletImpl::doRefresh() +{ + // synchronizing async and sync refresh calls + std::lock_guard<std::mutex> guarg(m_refreshMutex2); + try { + m_wallet->refresh(); + if (m_walletListener) { + m_walletListener->refreshed(); + } + } catch (const std::exception &e) { + m_status = Status_Error; + m_errorString = e.what(); + } +} + +// supposed to be called from ctor only +void WalletImpl::startRefresh() +{ + if (!m_refreshEnabled) { + m_refreshEnabled = true; + m_refreshCV.notify_one(); + } +} + + +// supposed to be called from dtor only +void WalletImpl::stopRefresh() +{ + if (!m_refreshThreadDone) { + m_refreshEnabled = false; + m_refreshThreadDone = true; + m_refreshThread.join(); + } +} + } // namespace diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 0896f51ee..e9da05347 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -35,6 +35,9 @@ #include "wallet/wallet2.h" #include <string> +#include <thread> +#include <mutex> +#include <condition_variable> namespace Bitmonero { @@ -71,7 +74,7 @@ public: uint64_t balance() const; uint64_t unlockedBalance() const; bool refresh(); - + void refreshAsync(); PendingTransaction * createTransaction(const std::string &dst_addr, const std::string &payment_id, uint64_t amount, uint32_t mixin_count, PendingTransaction::Priority priority = PendingTransaction::Priority_Low); @@ -84,19 +87,33 @@ public: private: void clearStatus(); + void refreshThreadFunc(); + void doRefresh(); + void startRefresh(); + void stopRefresh(); private: friend class PendingTransactionImpl; friend class TransactionHistoryImpl; tools::wallet2 * m_wallet; - int m_status; + std::atomic<int> m_status; std::string m_errorString; std::string m_password; TransactionHistoryImpl * m_history; bool m_trustedDaemon; WalletListener * m_walletListener; Wallet2CallbackImpl * m_wallet2Callback; + + // multi-threaded refresh stuff + std::atomic<bool> m_refreshEnabled; + std::atomic<bool> m_refreshThreadDone; + std::atomic<int> m_refreshIntervalSeconds; + std::mutex m_refreshMutex; + std::mutex m_refreshMutex2; + std::condition_variable m_refreshCV; + std::thread m_refreshThread; + }; diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index fb3f9a648..a51d38a71 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -116,7 +116,11 @@ struct WalletListener virtual ~WalletListener() = 0; virtual void moneySpent(const std::string &txId, uint64_t amount) = 0; virtual void moneyReceived(const std::string &txId, uint64_t amount) = 0; - // TODO: on_skip_transaction; + // generic callback, called when any event happened with the wallet; + virtual void updated() = 0; + // called when wallet refreshed by background thread or explicitly + virtual void refreshed() = 0; + }; @@ -188,6 +192,8 @@ struct Wallet // TODO? // virtual uint64_t unlockedDustBalance() const = 0; virtual bool refresh() = 0; + + virtual void refreshAsync() = 0; /*! * \brief createTransaction creates transaction. if dst_addr is an integrated address, payment_id is ignored * \param dst_addr destination address as string |