From f56d9deae998f7c6fbc25a098305b8e1f2149820 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Wed, 31 Jul 2024 17:00:22 +0200 Subject: [PATCH] add test --- base_layer/wallet_ffi/src/callback_handler.rs | 2 +- .../wallet_ffi/src/callback_handler_tests.rs | 32 +++++++++++++++++++ base_layer/wallet_ffi/src/lib.rs | 21 +++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/base_layer/wallet_ffi/src/callback_handler.rs b/base_layer/wallet_ffi/src/callback_handler.rs index eca2b154e1..b25d12b05c 100644 --- a/base_layer/wallet_ffi/src/callback_handler.rs +++ b/base_layer/wallet_ffi/src/callback_handler.rs @@ -55,7 +55,7 @@ use minotari_wallet::{ models::{CompletedTransaction, InboundTransaction}, }, }, - utxo_scanner_service::handle::{UtxoScannerEvent, UtxoScannerHandle}, + utxo_scanner_service::handle::UtxoScannerEvent, }; use tari_common_types::{tari_address::TariAddress, transaction::TxId, types::BlockHash}; use tari_comms_dht::event::{DhtEvent, DhtEventReceiver}; diff --git a/base_layer/wallet_ffi/src/callback_handler_tests.rs b/base_layer/wallet_ffi/src/callback_handler_tests.rs index 337a07ce86..a381137a0c 100644 --- a/base_layer/wallet_ffi/src/callback_handler_tests.rs +++ b/base_layer/wallet_ffi/src/callback_handler_tests.rs @@ -28,6 +28,7 @@ mod test { sqlite_db::TransactionServiceSqliteDatabase, }, }, + utxo_scanner_service::handle::UtxoScannerEvent, }; use once_cell::sync::Lazy; use rand::{rngs::OsRng, RngCore}; @@ -92,6 +93,7 @@ mod test { pub callback_transaction_validation_complete: u32, pub saf_messages_received: bool, pub connectivity_status_callback_called: u64, + pub wallet_scanner_height_callback_called: u64, pub base_node_state_changed_callback_invoked: bool, } @@ -121,6 +123,7 @@ mod test { tx_cancellation_callback_called_outbound: false, saf_messages_received: false, connectivity_status_callback_called: 0, + wallet_scanner_height_callback_called: 0, base_node_state_changed_callback_invoked: false, } } @@ -255,6 +258,12 @@ mod test { drop(lock); } + unsafe extern "C" fn wallet_scanner_height_callback(height: u64) { + let mut lock = CALLBACK_STATE.lock().unwrap(); + lock.wallet_scanner_height_callback_called += height; + drop(lock); + } + unsafe extern "C" fn base_node_state_changed_callback(state: *mut TariBaseNodeState) { let mut lock = CALLBACK_STATE.lock().unwrap(); lock.base_node_state_changed_callback_invoked = true; @@ -466,6 +475,8 @@ mod test { let (connectivity_tx, connectivity_rx) = watch::channel(OnlineStatus::Offline); let (contacts_liveness_events_sender, _) = broadcast::channel(250); let contacts_liveness_events = contacts_liveness_events_sender.subscribe(); + let (utxo_scanner_events_sender, _) = broadcast::channel(250); + let utxo_scanner_events = utxo_scanner_events_sender.subscribe(); let comms_address = TariAddress::new_dual_address_with_default_features( PublicKey::from_secret_key(&PrivateKey::random(&mut OsRng)), PublicKey::from_secret_key(&PrivateKey::random(&mut OsRng)), @@ -478,6 +489,7 @@ mod test { transaction_event_receiver, oms_event_receiver, oms_handle, + utxo_scanner_events, dht_event_receiver, shutdown_signal.to_signal(), comms_address, @@ -499,6 +511,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanner_height_callback, base_node_state_changed_callback, ); @@ -843,6 +856,24 @@ mod test { thread::sleep(Duration::from_secs(10)); + utxo_scanner_events_sender + .send(UtxoScannerEvent::Progress { + current_height: 500, + tip_height: 600, + }) + .unwrap(); + + thread::sleep(Duration::from_secs(2)); + utxo_scanner_events_sender + .send(UtxoScannerEvent::Completed { + final_height: 600, + num_recovered: 0, + value_recovered: 0.into(), + time_taken: Duration::from_secs(0), + }) + .unwrap(); + + thread::sleep(Duration::from_secs(2)); let lock = CALLBACK_STATE.lock().unwrap(); assert!(lock.received_tx_callback_called); assert!(lock.received_tx_reply_callback_called); @@ -867,6 +898,7 @@ mod test { assert_eq!(lock.callback_balance_updated, 7); assert_eq!(lock.callback_transaction_validation_complete, 13); assert_eq!(lock.connectivity_status_callback_called, 7); + assert_eq!(lock.wallet_scanner_height_callback_called, 1100); drop(lock); } diff --git a/base_layer/wallet_ffi/src/lib.rs b/base_layer/wallet_ffi/src/lib.rs index 4fd0d09baa..7832de9ab5 100644 --- a/base_layer/wallet_ffi/src/lib.rs +++ b/base_layer/wallet_ffi/src/lib.rs @@ -5955,6 +5955,8 @@ pub unsafe extern "C" fn wallet_create( }, }; + let mut utxo_scanner = w.utxo_scanner_service.clone(); + // Start Callback Handler let callback_handler = CallbackHandler::new( TransactionDatabase::new(transaction_backend), @@ -5962,7 +5964,7 @@ pub unsafe extern "C" fn wallet_create( w.transaction_service.get_event_stream(), w.output_manager_service.get_event_stream(), w.output_manager_service.clone(), - w.utxo_scanner_service.get_event_receiver(), + utxo_scanner.get_event_receiver(), w.dht_service.subscribe_dht_events(), w.comms.shutdown_signal(), wallet_address, @@ -9470,6 +9472,10 @@ mod test { // assert!(true); //optimized out by compiler } + unsafe extern "C" fn wallet_scanned_height_callback(_height: u64) { + // assert!(true); //optimized out by compiler + } + unsafe extern "C" fn base_node_state_callback(_state: *mut TariBaseNodeState) { // assert!(true); //optimized out by compiler } @@ -10156,6 +10162,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -10202,6 +10209,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -10317,6 +10325,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -10543,6 +10552,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -10609,6 +10619,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -10687,6 +10698,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -10863,6 +10875,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -11000,6 +11013,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -11217,6 +11231,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -11442,6 +11457,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -11701,6 +11717,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -12078,6 +12095,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr, @@ -12141,6 +12159,7 @@ mod test { transaction_validation_complete_callback, saf_messages_received_callback, connectivity_status_callback, + wallet_scanned_height_callback, base_node_state_callback, recovery_in_progress_ptr, error_ptr,