Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Jul 31, 2024
1 parent e805a11 commit f56d9de
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion base_layer/wallet_ffi/src/callback_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
32 changes: 32 additions & 0 deletions base_layer/wallet_ffi/src/callback_handler_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod test {
sqlite_db::TransactionServiceSqliteDatabase,
},
},
utxo_scanner_service::handle::UtxoScannerEvent,
};
use once_cell::sync::Lazy;
use rand::{rngs::OsRng, RngCore};
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)),
Expand All @@ -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,
Expand All @@ -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,
);

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
21 changes: 20 additions & 1 deletion base_layer/wallet_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5955,14 +5955,16 @@ 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),
w.base_node_service.get_event_stream(),
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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f56d9de

Please sign in to comment.