Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add context to ffi callbacks #6608

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ fn print_mr_values(block: &mut Block, print: bool) {

let mut kernel_mmr = KernelMmr::new(Vec::new());
for k in block.body.kernels() {
println!("k: {}", k);
kernel_mmr.push(k.hash().to_vec()).unwrap();
}

Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/blocks/pre_mine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,6 @@ mod test {
Vec<PublicKey>,
) {
let threshold_addresses_for_index = vec![
// This wil be public keys
TariAddress::from_base58(
"f4bYsv3sEMroDGKMMjhgm7cp1jDShdRWQzmV8wZiD6sJPpAEuezkiHtVhn7akK3YqswH5t3sUASW7rbvPSqMBDSCSp",
)
Expand Down Expand Up @@ -1404,6 +1403,7 @@ mod test {
);
}

#[ignore]
#[tokio::test]
async fn test_create_genesis_block_info() {
for network in [
Expand Down
127 changes: 72 additions & 55 deletions base_layer/wallet_ffi/src/callback_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
//! request_key is used to identify which request this callback references and a result of true means it was successful
//! and false that the process timed out and new one will be started

use std::{ops::Deref, sync::Arc};
use std::{ffi::c_void, ops::Deref, sync::Arc};

use log::*;
use minotari_wallet::{
Expand Down Expand Up @@ -65,29 +65,35 @@ use tokio::sync::{broadcast, watch};

use crate::ffi_basenode_state::TariBaseNodeState;

#[derive(Clone, Copy)]
pub struct Context(pub *mut c_void);

unsafe impl Send for Context {}

const LOG_TARGET: &str = "wallet::transaction_service::callback_handler";

pub struct CallbackHandler<TBackend>
where TBackend: TransactionBackend + 'static
{
callback_received_transaction: unsafe extern "C" fn(*mut InboundTransaction),
callback_received_transaction_reply: unsafe extern "C" fn(*mut CompletedTransaction),
callback_received_finalized_transaction: unsafe extern "C" fn(*mut CompletedTransaction),
callback_transaction_broadcast: unsafe extern "C" fn(*mut CompletedTransaction),
callback_transaction_mined: unsafe extern "C" fn(*mut CompletedTransaction),
callback_transaction_mined_unconfirmed: unsafe extern "C" fn(*mut CompletedTransaction, u64),
callback_faux_transaction_confirmed: unsafe extern "C" fn(*mut CompletedTransaction),
callback_faux_transaction_unconfirmed: unsafe extern "C" fn(*mut CompletedTransaction, u64),
callback_transaction_send_result: unsafe extern "C" fn(u64, *mut TransactionSendStatus),
callback_transaction_cancellation: unsafe extern "C" fn(*mut CompletedTransaction, u64),
callback_txo_validation_complete: unsafe extern "C" fn(u64, u64),
callback_contacts_liveness_data_updated: unsafe extern "C" fn(*mut ContactsLivenessData),
callback_balance_updated: unsafe extern "C" fn(*mut Balance),
callback_transaction_validation_complete: unsafe extern "C" fn(u64, u64),
callback_saf_messages_received: unsafe extern "C" fn(),
callback_connectivity_status: unsafe extern "C" fn(u64),
callback_wallet_scanned_height: unsafe extern "C" fn(u64),
callback_base_node_state: unsafe extern "C" fn(*mut TariBaseNodeState),
pub context: Context,
callback_received_transaction: unsafe extern "C" fn(context: *mut c_void, *mut InboundTransaction),
callback_received_transaction_reply: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_received_finalized_transaction: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_transaction_broadcast: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_transaction_mined: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_transaction_mined_unconfirmed: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction, u64),
callback_faux_transaction_confirmed: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_faux_transaction_unconfirmed: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction, u64),
callback_transaction_send_result: unsafe extern "C" fn(context: *mut c_void, u64, *mut TransactionSendStatus),
callback_transaction_cancellation: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction, u64),
callback_txo_validation_complete: unsafe extern "C" fn(context: *mut c_void, u64, u64),
callback_contacts_liveness_data_updated: unsafe extern "C" fn(context: *mut c_void, *mut ContactsLivenessData),
callback_balance_updated: unsafe extern "C" fn(context: *mut c_void, *mut Balance),
callback_transaction_validation_complete: unsafe extern "C" fn(context: *mut c_void, u64, u64),
callback_saf_messages_received: unsafe extern "C" fn(context: *mut c_void),
callback_connectivity_status: unsafe extern "C" fn(context: *mut c_void, u64),
callback_wallet_scanned_height: unsafe extern "C" fn(context: *mut c_void, u64),
callback_base_node_state: unsafe extern "C" fn(context: *mut c_void, *mut TariBaseNodeState),
db: TransactionDatabase<TBackend>,
base_node_service_event_stream: BaseNodeEventReceiver,
transaction_service_event_stream: TransactionEventReceiver,
Expand All @@ -106,7 +112,9 @@ impl<TBackend> CallbackHandler<TBackend>
where TBackend: TransactionBackend + 'static
{
#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_lines)]
pub fn new(
context: Context,
db: TransactionDatabase<TBackend>,
base_node_service_event_stream: BaseNodeEventReceiver,
transaction_service_event_stream: TransactionEventReceiver,
Expand All @@ -118,24 +126,32 @@ where TBackend: TransactionBackend + 'static
comms_address: TariAddress,
connectivity_status_watch: watch::Receiver<OnlineStatus>,
contacts_liveness_events: broadcast::Receiver<Arc<ContactsLivenessEvent>>,
callback_received_transaction: unsafe extern "C" fn(*mut InboundTransaction),
callback_received_transaction_reply: unsafe extern "C" fn(*mut CompletedTransaction),
callback_received_finalized_transaction: unsafe extern "C" fn(*mut CompletedTransaction),
callback_transaction_broadcast: unsafe extern "C" fn(*mut CompletedTransaction),
callback_transaction_mined: unsafe extern "C" fn(*mut CompletedTransaction),
callback_transaction_mined_unconfirmed: unsafe extern "C" fn(*mut CompletedTransaction, u64),
callback_faux_transaction_confirmed: unsafe extern "C" fn(*mut CompletedTransaction),
callback_faux_transaction_unconfirmed: unsafe extern "C" fn(*mut CompletedTransaction, u64),
callback_transaction_send_result: unsafe extern "C" fn(u64, *mut TransactionSendStatus),
callback_transaction_cancellation: unsafe extern "C" fn(*mut CompletedTransaction, u64),
callback_txo_validation_complete: unsafe extern "C" fn(u64, u64),
callback_contacts_liveness_data_updated: unsafe extern "C" fn(*mut ContactsLivenessData),
callback_balance_updated: unsafe extern "C" fn(*mut Balance),
callback_transaction_validation_complete: unsafe extern "C" fn(u64, u64),
callback_saf_messages_received: unsafe extern "C" fn(),
callback_connectivity_status: unsafe extern "C" fn(u64),
callback_wallet_scanned_height: unsafe extern "C" fn(u64),
callback_base_node_state: unsafe extern "C" fn(*mut TariBaseNodeState),
callback_received_transaction: unsafe extern "C" fn(context: *mut c_void, *mut InboundTransaction),
callback_received_transaction_reply: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_received_finalized_transaction: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_transaction_broadcast: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_transaction_mined: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_transaction_mined_unconfirmed: unsafe extern "C" fn(
context: *mut c_void,
*mut CompletedTransaction,
u64,
),
callback_faux_transaction_confirmed: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction),
callback_faux_transaction_unconfirmed: unsafe extern "C" fn(
context: *mut c_void,
*mut CompletedTransaction,
u64,
),
callback_transaction_send_result: unsafe extern "C" fn(context: *mut c_void, u64, *mut TransactionSendStatus),
callback_transaction_cancellation: unsafe extern "C" fn(context: *mut c_void, *mut CompletedTransaction, u64),
callback_txo_validation_complete: unsafe extern "C" fn(context: *mut c_void, u64, u64),
callback_contacts_liveness_data_updated: unsafe extern "C" fn(context: *mut c_void, *mut ContactsLivenessData),
callback_balance_updated: unsafe extern "C" fn(context: *mut c_void, *mut Balance),
callback_transaction_validation_complete: unsafe extern "C" fn(context: *mut c_void, u64, u64),
callback_saf_messages_received: unsafe extern "C" fn(context: *mut c_void),
callback_connectivity_status: unsafe extern "C" fn(context: *mut c_void, u64),
callback_wallet_scanned_height: unsafe extern "C" fn(context: *mut c_void, u64),
callback_base_node_state: unsafe extern "C" fn(context: *mut c_void, *mut TariBaseNodeState),
) -> Self {
info!(
target: LOG_TARGET,
Expand Down Expand Up @@ -207,6 +223,7 @@ where TBackend: TransactionBackend + 'static
);

Self {
context,
callback_received_transaction,
callback_received_transaction_reply,
callback_received_finalized_transaction,
Expand Down Expand Up @@ -440,7 +457,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(tx));
unsafe {
(self.callback_received_transaction)(boxing);
(self.callback_received_transaction)(self.context.0, boxing);
}
},
Err(e) => error!(
Expand All @@ -459,7 +476,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(tx));
unsafe {
(self.callback_received_transaction_reply)(boxing);
(self.callback_received_transaction_reply)(self.context.0, boxing);
}
},
Err(e) => error!(target: LOG_TARGET, "Error retrieving Completed Transaction: {:?}", e),
Expand All @@ -475,7 +492,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(tx));
unsafe {
(self.callback_received_finalized_transaction)(boxing);
(self.callback_received_finalized_transaction)(self.context.0, boxing);
}
},
Err(e) => error!(target: LOG_TARGET, "Error retrieving Completed Transaction: {:?}", e),
Expand All @@ -498,7 +515,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(balance));
unsafe {
(self.callback_balance_updated)(boxing);
(self.callback_balance_updated)(self.context.0, boxing);
}
}
},
Expand All @@ -516,7 +533,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(data));
unsafe {
(self.callback_contacts_liveness_data_updated)(boxing);
(self.callback_contacts_liveness_data_updated)(self.context.0, boxing);
}
}

Expand All @@ -527,7 +544,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(status));
unsafe {
(self.callback_transaction_send_result)(tx_id.as_u64(), boxing);
(self.callback_transaction_send_result)(self.context.0, tx_id.as_u64(), boxing);
}
}

Expand Down Expand Up @@ -559,7 +576,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(tx));
unsafe {
(self.callback_transaction_cancellation)(boxing, reason);
(self.callback_transaction_cancellation)(self.context.0, boxing, reason);
}
},
}
Expand All @@ -574,7 +591,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(tx));
unsafe {
(self.callback_transaction_broadcast)(boxing);
(self.callback_transaction_broadcast)(self.context.0, boxing);
}
},
Err(e) => error!(target: LOG_TARGET, "Error retrieving Completed Transaction: {:?}", e),
Expand All @@ -590,7 +607,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(tx));
unsafe {
(self.callback_transaction_mined)(boxing);
(self.callback_transaction_mined)(self.context.0, boxing);
}
},
Err(e) => error!(target: LOG_TARGET, "Error retrieving Completed Transaction: {:?}", e),
Expand All @@ -606,7 +623,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(tx));
unsafe {
(self.callback_transaction_mined_unconfirmed)(boxing, confirmations);
(self.callback_transaction_mined_unconfirmed)(self.context.0, boxing, confirmations);
}
},
Err(e) => error!(target: LOG_TARGET, "Error retrieving Completed Transaction: {:?}", e),
Expand All @@ -622,7 +639,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(tx));
unsafe {
(self.callback_faux_transaction_confirmed)(boxing);
(self.callback_faux_transaction_confirmed)(self.context.0, boxing);
}
},
Err(e) => error!(target: LOG_TARGET, "Error retrieving Completed Transaction: {:?}", e),
Expand All @@ -638,7 +655,7 @@ where TBackend: TransactionBackend + 'static
);
let boxing = Box::into_raw(Box::new(tx));
unsafe {
(self.callback_faux_transaction_unconfirmed)(boxing, confirmations);
(self.callback_faux_transaction_unconfirmed)(self.context.0, boxing, confirmations);
}
},
Err(e) => error!(target: LOG_TARGET, "Error retrieving Completed Transaction: {:?}", e),
Expand All @@ -651,7 +668,7 @@ where TBackend: TransactionBackend + 'static
"Calling Transaction Validation Complete callback function for Request Key: {}", request_key,
);
unsafe {
(self.callback_transaction_validation_complete)(request_key, success);
(self.callback_transaction_validation_complete)(self.context.0, request_key, success);
}
}

Expand All @@ -664,14 +681,14 @@ where TBackend: TransactionBackend + 'static
);

unsafe {
(self.callback_txo_validation_complete)(request_key, success);
(self.callback_txo_validation_complete)(self.context.0, request_key, success);
}
}

fn saf_messages_received_event(&mut self) {
debug!(target: LOG_TARGET, "Calling SAF Messages Received callback function");
unsafe {
(self.callback_saf_messages_received)();
(self.callback_saf_messages_received)(self.context.0);
}
}

Expand All @@ -681,7 +698,7 @@ where TBackend: TransactionBackend + 'static
"Calling Connectivity Status changed callback function"
);
unsafe {
(self.callback_connectivity_status)(status as u64);
(self.callback_connectivity_status)(self.context.0, status as u64);
}
}

Expand All @@ -691,7 +708,7 @@ where TBackend: TransactionBackend + 'static
"Calling Scanned height changed callback function"
);
unsafe {
(self.callback_wallet_scanned_height)(height);
(self.callback_wallet_scanned_height)(self.context.0, height);
}
}

Expand Down Expand Up @@ -727,7 +744,7 @@ where TBackend: TransactionBackend + 'static
};

unsafe {
(self.callback_base_node_state)(Box::into_raw(Box::new(state)));
(self.callback_base_node_state)(self.context.0, Box::into_raw(Box::new(state)));
}
}
}
Loading
Loading