From 00040df4129d122db492e2a19b2f13287c1f74b0 Mon Sep 17 00:00:00 2001 From: woodser Date: Sat, 9 May 2020 07:57:11 -0400 Subject: [PATCH] combine monero_sync_listener and monero_wallet_listener --- src/wallet/monero_wallet.h | 23 +++++++---------------- src/wallet/monero_wallet_core.cpp | 27 ++++++--------------------- src/wallet/monero_wallet_core.h | 4 ++-- test/sample_code.cpp | 2 +- 4 files changed, 16 insertions(+), 40 deletions(-) diff --git a/src/wallet/monero_wallet.h b/src/wallet/monero_wallet.h index 661c873..e02e6cc 100644 --- a/src/wallet/monero_wallet.h +++ b/src/wallet/monero_wallet.h @@ -65,11 +65,9 @@ namespace monero { // --------------------------------- LISTENERS ------------------------------ /** - * Receives progress notifications as a wallet is synchronized. - * - * TODO: make these interfaces and provide default implementation + * Interface to receive wallet notifications.. */ - class monero_sync_listener { + class monero_wallet_listener { public: /** @@ -82,13 +80,6 @@ namespace monero { * @param message - human-readable description of the current progress */ virtual void on_sync_progress(uint64_t height, uint64_t start_height, uint64_t end_height, double percent_done, const string& message) {} - }; - - /** - * Receives notifications as a wallet is updated. - */ - class monero_wallet_listener : public monero_sync_listener { - public: /** * Invoked when a new block is processed. @@ -419,10 +410,10 @@ namespace monero { /** * Synchronize the wallet with the daemon as a one-time synchronous process. * - * @param listener is invoked as sync progress is made + * @param listener - listener to receive notifications during synchronization * @return the sync result */ - virtual monero_sync_result sync(monero_sync_listener& listener) { + virtual monero_sync_result sync(monero_wallet_listener& listener) { throw runtime_error("sync() not supported"); } @@ -439,11 +430,11 @@ namespace monero { /** * Synchronizes the wallet with the blockchain. * - * @param start_height is the start height to sync from (ignored if less than last processed block) - * @param listener is invoked as sync progress is made + * @param start_height - start height to sync from (ignored if less than last processed block) + * @param listener - listener to receive notifications during synchronization * @return the sync result */ - virtual monero_sync_result sync(uint64_t start_height, monero_sync_listener& listener) { + virtual monero_sync_result sync(uint64_t start_height, monero_wallet_listener& listener) { throw runtime_error("sync() not supported"); } diff --git a/src/wallet/monero_wallet_core.cpp b/src/wallet/monero_wallet_core.cpp index e1a262f..760c593 100644 --- a/src/wallet/monero_wallet_core.cpp +++ b/src/wallet/monero_wallet_core.cpp @@ -809,19 +809,6 @@ namespace monero { boost::optional m_sync_end_height; }; - /** - * Wraps a sync listener as a general wallet listener. - */ - struct sync_listener_wrapper : public monero_wallet_listener { - public: - sync_listener_wrapper(monero_sync_listener& listener) : listener(listener) { } - virtual void on_sync_progress(uint64_t height, uint64_t start_height, uint64_t end_height, double percent_done, const string& message) { - listener.on_sync_progress(height, start_height, end_height, percent_done, message); - } - private: - monero_sync_listener& listener; - }; - // --------------------------- STATIC WALLET UTILS -------------------------- bool monero_wallet_core::wallet_exists(const string& path) { @@ -1201,19 +1188,18 @@ namespace monero { return lock_and_sync(); } - monero_sync_result monero_wallet_core::sync(monero_sync_listener& listener) { + monero_sync_result monero_wallet_core::sync(monero_wallet_listener& listener) { MTRACE("sync(listener)"); if (!m_is_connected) throw runtime_error("Wallet is not connected to daemon"); // wrap and register sync listener as wallet listener - sync_listener_wrapper listener_wrapped = sync_listener_wrapper(listener); - add_listener(listener_wrapped); + add_listener(listener); // sync wallet monero_sync_result result = lock_and_sync(boost::none); // unregister sync listener - remove_listener(listener_wrapped); + remove_listener(listener); // return sync result return result; @@ -1225,19 +1211,18 @@ namespace monero { return lock_and_sync(start_height); } - monero_sync_result monero_wallet_core::sync(uint64_t start_height, monero_sync_listener& listener) { + monero_sync_result monero_wallet_core::sync(uint64_t start_height, monero_wallet_listener& listener) { MTRACE("sync(" << start_height << ", listener)"); if (!m_is_connected) throw runtime_error("Wallet is not connected to daemon"); // wrap and register sync listener as wallet listener - sync_listener_wrapper listener_wrapped = sync_listener_wrapper(listener); - add_listener(listener_wrapped); + add_listener(listener); // sync wallet monero_sync_result result = lock_and_sync(start_height); // unregister sync listener - remove_listener(listener_wrapped); + remove_listener(listener); // return sync result return result; diff --git a/src/wallet/monero_wallet_core.h b/src/wallet/monero_wallet_core.h index 39db95d..8c304eb 100644 --- a/src/wallet/monero_wallet_core.h +++ b/src/wallet/monero_wallet_core.h @@ -202,9 +202,9 @@ namespace monero { void remove_listener(monero_wallet_listener& listener); set get_listeners(); monero_sync_result sync(); - monero_sync_result sync(monero_sync_listener& listener); + monero_sync_result sync(monero_wallet_listener& listener); monero_sync_result sync(uint64_t start_height); - monero_sync_result sync(uint64_t start_height, monero_sync_listener& listener); + monero_sync_result sync(uint64_t start_height, monero_wallet_listener& listener); void start_syncing(); void stop_syncing(); void rescan_spent(); diff --git a/test/sample_code.cpp b/test/sample_code.cpp index f685380..519219f 100644 --- a/test/sample_code.cpp +++ b/test/sample_code.cpp @@ -32,7 +32,7 @@ int main(int argc, const char* argv[]) { ); // synchronize the wallet and receive progress notifications - struct : monero_sync_listener { + struct : monero_wallet_listener { void on_sync_progress(uint64_t height, uint64_t start_height, uint64_t end_height, double percent_done, const string& message) { // feed a progress bar? }