From 9d2cb4f36ca7220bb05217547b72710b870f6e57 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Sun, 10 Jul 2016 17:17:23 +0300 Subject: [PATCH] WalletListener functionality --- src/wallet/api/wallet.cpp | 89 +++++++++++++-- src/wallet/api/wallet.h | 21 +++- src/wallet/wallet2_api.h | 8 +- tests/libwallet_api_tests/main.cpp | 107 ++++++++++++------ .../libwallet_api_tests/scripts/send_funds.sh | 24 +++- 5 files changed, 196 insertions(+), 53 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 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 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 +#include +#include +#include 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 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 m_refreshEnabled; + std::atomic m_refreshThreadDone; + std::atomic 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 diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index df06d68ae..51f59def5 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -39,6 +39,8 @@ #include #include #include +#include +#include using namespace std; @@ -71,7 +73,7 @@ const std::string TESTNET_WALLET6_NAME = WALLETS_ROOT_DIR + "wallet_06.bin"; const char * TESTNET_WALLET_PASS = ""; -const std::string CURRENT_SRC_WALLET = TESTNET_WALLET1_NAME; +const std::string CURRENT_SRC_WALLET = TESTNET_WALLET3_NAME; const std::string CURRENT_DST_WALLET = TESTNET_WALLET6_NAME; const char * TESTNET_DAEMON_ADDRESS = "localhost:38081"; @@ -176,6 +178,7 @@ struct WalletTest2 : public testing::Test }; +/* TEST_F(WalletManagerTest, WalletManagerCreatesWallet) { @@ -235,7 +238,7 @@ TEST_F(WalletManagerTest, WalletManagerMovesWallet) } -/* + TEST_F(WalletManagerTest, WalletManagerChangesPassword) { Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); @@ -345,6 +348,8 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet4) } */ + +/* TEST_F(WalletManagerTest, WalletManagerFindsWallet) { std::vector wallets = wmgr->findWallets(WALLETS_ROOT_DIR); @@ -415,7 +420,7 @@ TEST_F(WalletTest1, WalletConvertsToString) } -/* + TEST_F(WalletTest1, WalletTransaction) { Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true); @@ -429,8 +434,10 @@ TEST_F(WalletTest1, WalletTransaction) wallet1->setDefaultMixin(1); ASSERT_TRUE(wallet1->defaultMixin() == 1); - Bitmonero::PendingTransaction * transaction = wallet1->createTransaction( - recepient_address, AMOUNT_10XMR); + Bitmonero::PendingTransaction * transaction = wallet1->createTransaction(recepient_address, + PAYMENT_ID_EMPTY, + AMOUNT_10XMR, + 1); ASSERT_TRUE(transaction->status() == Bitmonero::PendingTransaction::Status_Ok); wallet1->refresh(); @@ -440,7 +447,6 @@ TEST_F(WalletTest1, WalletTransaction) ASSERT_FALSE(wallet1->balance() == balance); ASSERT_TRUE(wmgr->closeWallet(wallet1)); } -*/ TEST_F(WalletTest1, WalletTransactionWithMixin) { @@ -564,7 +570,7 @@ TEST_F(WalletTest1, WalletTransactionAndHistory) Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(wallet4_addr, PAYMENT_ID_EMPTY, - AMOUNT_10XMR * 5, 0); + AMOUNT_10XMR * 5, 1); ASSERT_TRUE(tx->status() == Bitmonero::PendingTransaction::Status_Ok); ASSERT_TRUE(tx->commit()); @@ -627,6 +633,7 @@ TEST_F(WalletTest1, WalletTransactionWithPaymentId) ASSERT_TRUE(payment_id_in_history); } +*/ struct MyWalletListener : public Bitmonero::WalletListener { @@ -634,7 +641,8 @@ struct MyWalletListener : public Bitmonero::WalletListener Bitmonero::Wallet * wallet; uint64_t total_tx; uint64_t total_rx; - std::timed_mutex guard; + std::mutex mutex; + std::condition_variable cv; MyWalletListener(Bitmonero::Wallet * wallet) : total_tx(0), total_rx(0) @@ -645,79 +653,110 @@ struct MyWalletListener : public Bitmonero::WalletListener virtual void moneySpent(const string &txId, uint64_t amount) { - std::cout << "wallet: " << wallet->address() << " just spent money (" + std::cerr << "wallet: " << wallet->address() << "**** just spent money (" << txId << ", " << wallet->displayAmount(amount) << ")" << std::endl; total_tx += amount; - guard.unlock(); + cv.notify_one(); } virtual void moneyReceived(const string &txId, uint64_t amount) { - std::cout << "wallet: " << wallet->address() << " just received money (" + std::cout << "wallet: " << wallet->address() << "**** just received money (" << txId << ", " << wallet->displayAmount(amount) << ")" << std::endl; total_rx += amount; - guard.unlock(); + cv.notify_one(); } + + virtual void updated() + { + cv.notify_one(); + } + + virtual void refreshed() + { + std::cout << "Wallet refreshed"; + } + }; -/* + TEST_F(WalletTest2, WalletCallbackSent) { - Bitmonero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET3_NAME, TESTNET_WALLET_PASS, true); + Bitmonero::Wallet * wallet_src = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true); // make sure testnet daemon is running ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet_src->refresh()); MyWalletListener * wallet_src_listener = new MyWalletListener(wallet_src); + uint64_t balance = wallet_src->balance(); std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl; + Bitmonero::Wallet * wallet_dst = wmgr->openWallet(CURRENT_DST_WALLET, TESTNET_WALLET_PASS, true); + + uint64_t amount = AMOUNT_1XMR * 5; + std::cout << "** Sending " << Bitmonero::Wallet::displayAmount(amount) << " to " << wallet_dst->address(); - uint64_t amount = AMOUNT_10XMR * 5; - std::cout << "** Sending " << Bitmonero::Wallet::displayAmount(amount) << " to " << TESTNET_WALLET4_ADDRESS; - Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(TESTNET_WALLET4_ADDRESS, AMOUNT_1XMR * 5); + Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->address(), + PAYMENT_ID_EMPTY, + amount, 1); + std::cout << "** Committing transaction: " << Bitmonero::Wallet::displayAmount(tx->amount()) + << " with fee: " << Bitmonero::Wallet::displayAmount(tx->fee()); + ASSERT_TRUE(tx->status() == Bitmonero::PendingTransaction::Status_Ok); ASSERT_TRUE(tx->commit()); std::chrono::seconds wait_for = std::chrono::seconds(60*3); - - wallet_src_listener->guard.lock(); - wallet_src_listener->guard.try_lock_for(wait_for); - - ASSERT_TRUE(wallet_src_listener->total_tx != 0); + std::unique_lock lock (wallet_src_listener->mutex); + wallet_src_listener->cv.wait_for(lock, wait_for); + std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl; + ASSERT_TRUE(wallet_src->balance() < balance); + wmgr->closeWallet(wallet_src); + wmgr->closeWallet(wallet_dst); } -*/ -/* + TEST_F(WalletTest2, WalletCallbackReceived) { - Bitmonero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET3_NAME, TESTNET_WALLET_PASS, true); + Bitmonero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET5_NAME, TESTNET_WALLET_PASS, true); // make sure testnet daemon is running ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet_src->refresh()); std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl; - Bitmonero::Wallet * wallet_dst = wmgr->openWallet(TESTNET_WALLET4_NAME, TESTNET_WALLET_PASS, true); + Bitmonero::Wallet * wallet_dst = wmgr->openWallet(CURRENT_DST_WALLET, TESTNET_WALLET_PASS, true); ASSERT_TRUE(wallet_dst->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet_dst->refresh()); + uint64_t balance = wallet_dst->balance(); MyWalletListener * wallet_dst_listener = new MyWalletListener(wallet_dst); uint64_t amount = AMOUNT_1XMR * 5; - std::cout << "** Sending " << Bitmonero::Wallet::displayAmount(amount) << " to " << TESTNET_WALLET4_ADDRESS; - Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(TESTNET_WALLET4_ADDRESS, AMOUNT_1XMR * 5); + std::cout << "** Sending " << Bitmonero::Wallet::displayAmount(amount) << " to " << wallet_dst->address(); + Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->address(), + PAYMENT_ID_EMPTY, + amount, 1); + + std::cout << "** Committing transaction: " << Bitmonero::Wallet::displayAmount(tx->amount()) + << " with fee: " << Bitmonero::Wallet::displayAmount(tx->fee()); + ASSERT_TRUE(tx->status() == Bitmonero::PendingTransaction::Status_Ok); ASSERT_TRUE(tx->commit()); std::chrono::seconds wait_for = std::chrono::seconds(60*4); + std::unique_lock lock (wallet_dst_listener->mutex); + wallet_dst_listener->cv.wait_for(lock, wait_for); + std::cout << "** Balance: " << wallet_dst->displayAmount(wallet_src->balance()) << std::endl; + ASSERT_TRUE(wallet_dst->balance() > balance); - wallet_dst_listener->guard.lock(); - wallet_dst_listener->guard.try_lock_for(wait_for); - - ASSERT_TRUE(wallet_dst_listener->total_tx != 0); - + wmgr->closeWallet(wallet_src); + wmgr->closeWallet(wallet_dst); } -*/ + + + + + int main(int argc, char** argv) { diff --git a/tests/libwallet_api_tests/scripts/send_funds.sh b/tests/libwallet_api_tests/scripts/send_funds.sh index 306b06a40..437e4f240 100755 --- a/tests/libwallet_api_tests/scripts/send_funds.sh +++ b/tests/libwallet_api_tests/scripts/send_funds.sh @@ -12,11 +12,23 @@ function send_funds { } -send_funds 100 wallet_01.bin -send_funds 100 wallet_02.bin -send_funds 100 wallet_03.bin -send_funds 100 wallet_04.bin -send_funds 100 wallet_05.bin -send_funds 100 wallet_06.bin +function seed_wallets { + local amount=$1 + send_funds $amount wallet_01.bin + send_funds $amount wallet_02.bin + send_funds $amount wallet_03.bin + send_funds $amount wallet_04.bin + send_funds $amount wallet_05.bin + send_funds $amount wallet_06.bin +} + +seed_wallets 1 +seed_wallets 2 +seed_wallets 5 +seed_wallets 10 +seed_wallets 20 +seed_wallets 50 +seed_wallets 100 +