mirror of
https://github.com/wownero/wownero-cpp.git
synced 2026-10-17 15:21:30 +02:00
combine monero_sync_listener and monero_wallet_listener
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
@@ -809,19 +809,6 @@ namespace monero {
|
||||
boost::optional<uint64_t> 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;
|
||||
|
||||
@@ -202,9 +202,9 @@ namespace monero {
|
||||
void remove_listener(monero_wallet_listener& listener);
|
||||
set<monero_wallet_listener*> 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();
|
||||
|
||||
@@ -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?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user