X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fchain%2Fmod.rs;h=9c8b4f5c044f00b82239e33e446fbd01b1ac574e;hp=21a6b8ad78ba92654663f021c909342e670815eb;hb=ed6eb46f7b4e0d4c869ad08dffc600b4e93e2129;hpb=2c56d2b4709d39d5e96ec84e4ecb07f9ebed0387 diff --git a/lightning-c-bindings/src/lightning/chain/mod.rs b/lightning-c-bindings/src/lightning/chain/mod.rs index 21a6b8a..9c8b4f5 100644 --- a/lightning-c-bindings/src/lightning/chain/mod.rs +++ b/lightning-c-bindings/src/lightning/chain/mod.rs @@ -57,7 +57,7 @@ pub struct BestBlock { impl Drop for BestBlock { fn drop(&mut self) { if self.is_owned && !<*mut nativeBestBlock>::is_null(self.inner) { - let _ = unsafe { Box::from_raw(self.inner) }; + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; } } } @@ -70,11 +70,17 @@ extern "C" fn BestBlock_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBestBlock); } } #[allow(unused)] -/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl BestBlock { + pub(crate) fn get_native_ref(&self) -> &'static nativeBestBlock { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBestBlock { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy pub(crate) fn take_inner(mut self) -> *mut nativeBestBlock { assert!(self.is_owned); - let ret = self.inner; + let ret = ObjOps::untweak_ptr(self.inner); self.inner = std::ptr::null_mut(); ret } @@ -83,7 +89,7 @@ impl Clone for BestBlock { fn clone(&self) -> Self { Self { inner: if <*mut nativeBestBlock>::is_null(self.inner) { std::ptr::null_mut() } else { - Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } } @@ -104,7 +110,7 @@ pub extern "C" fn BestBlock_clone(orig: &BestBlock) -> BestBlock { #[no_mangle] pub extern "C" fn BestBlock_from_genesis(mut network: crate::bitcoin::network::Network) -> BestBlock { let mut ret = lightning::chain::BestBlock::from_genesis(network.into_bitcoin()); - BestBlock { inner: Box::into_raw(Box::new(ret)), is_owned: true } + BestBlock { inner: ObjOps::heap_alloc(ret), is_owned: true } } /// Returns a `BestBlock` as identified by the given block hash and height. @@ -112,14 +118,14 @@ pub extern "C" fn BestBlock_from_genesis(mut network: crate::bitcoin::network::N #[no_mangle] pub extern "C" fn BestBlock_new(mut block_hash: crate::c_types::ThirtyTwoBytes, mut height: u32) -> BestBlock { let mut ret = lightning::chain::BestBlock::new(::bitcoin::hash_types::BlockHash::from_slice(&block_hash.data[..]).unwrap(), height); - BestBlock { inner: Box::into_raw(Box::new(ret)), is_owned: true } + BestBlock { inner: ObjOps::heap_alloc(ret), is_owned: true } } /// Returns the best block hash. #[must_use] #[no_mangle] pub extern "C" fn BestBlock_block_hash(this_arg: &BestBlock) -> crate::c_types::ThirtyTwoBytes { - let mut ret = unsafe { &*this_arg.inner }.block_hash(); + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.block_hash(); crate::c_types::ThirtyTwoBytes { data: ret.into_inner() } } @@ -127,7 +133,7 @@ pub extern "C" fn BestBlock_block_hash(this_arg: &BestBlock) -> crate::c_types:: #[must_use] #[no_mangle] pub extern "C" fn BestBlock_height(this_arg: &BestBlock) -> u32 { - let mut ret = unsafe { &*this_arg.inner }.height(); + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.height(); ret } @@ -177,6 +183,14 @@ impl AccessError { pub extern "C" fn AccessError_clone(orig: &AccessError) -> AccessError { orig.clone() } +#[no_mangle] +/// Utility method to constructs a new UnknownChain-variant AccessError +pub extern "C" fn AccessError_unknown_chain() -> AccessError { + AccessError::UnknownChain} +#[no_mangle] +/// Utility method to constructs a new UnknownTx-variant AccessError +pub extern "C" fn AccessError_unknown_tx() -> AccessError { + AccessError::UnknownTx} /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and /// UTXOs. #[repr(C)] @@ -197,6 +211,14 @@ pub struct Access { } unsafe impl Send for Access {} unsafe impl Sync for Access {} +#[no_mangle] +pub(crate) extern "C" fn Access_clone_fields(orig: &Access) -> Access { + Access { + this_arg: orig.this_arg, + get_utxo: Clone::clone(&orig.get_utxo), + free: Clone::clone(&orig.free), + } +} use lightning::chain::Access as rustAccess; impl rustAccess for Access { @@ -247,6 +269,15 @@ pub struct Listen { } unsafe impl Send for Listen {} unsafe impl Sync for Listen {} +#[no_mangle] +pub(crate) extern "C" fn Listen_clone_fields(orig: &Listen) -> Listen { + Listen { + this_arg: orig.this_arg, + block_connected: Clone::clone(&orig.block_connected), + block_disconnected: Clone::clone(&orig.block_disconnected), + free: Clone::clone(&orig.free), + } +} use lightning::chain::Listen as rustListen; impl rustListen for Listen { @@ -364,6 +395,17 @@ pub struct Confirm { } unsafe impl Send for Confirm {} unsafe impl Sync for Confirm {} +#[no_mangle] +pub(crate) extern "C" fn Confirm_clone_fields(orig: &Confirm) -> Confirm { + Confirm { + this_arg: orig.this_arg, + transactions_confirmed: Clone::clone(&orig.transactions_confirmed), + transaction_unconfirmed: Clone::clone(&orig.transaction_unconfirmed), + best_block_updated: Clone::clone(&orig.best_block_updated), + get_relevant_txids: Clone::clone(&orig.get_relevant_txids), + free: Clone::clone(&orig.free), + } +} use lightning::chain::Confirm as rustConfirm; impl rustConfirm for Confirm { @@ -460,16 +502,26 @@ pub struct Watch { } unsafe impl Send for Watch {} unsafe impl Sync for Watch {} +#[no_mangle] +pub(crate) extern "C" fn Watch_clone_fields(orig: &Watch) -> Watch { + Watch { + this_arg: orig.this_arg, + watch_channel: Clone::clone(&orig.watch_channel), + update_channel: Clone::clone(&orig.update_channel), + release_pending_monitor_events: Clone::clone(&orig.release_pending_monitor_events), + free: Clone::clone(&orig.free), + } +} use lightning::chain::Watch as rustWatch; impl rustWatch for Watch { fn watch_channel(&self, mut funding_txo: lightning::chain::transaction::OutPoint, mut monitor: lightning::chain::channelmonitor::ChannelMonitor) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> { - let mut ret = (self.watch_channel)(self.this_arg, crate::lightning::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(funding_txo)), is_owned: true }, crate::lightning::chain::channelmonitor::ChannelMonitor { inner: Box::into_raw(Box::new(monitor)), is_owned: true }); + let mut ret = (self.watch_channel)(self.this_arg, crate::lightning::chain::transaction::OutPoint { inner: ObjOps::heap_alloc(funding_txo), is_owned: true }, crate::lightning::chain::channelmonitor::ChannelMonitor { inner: ObjOps::heap_alloc(monitor), is_owned: true }); let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })}; local_ret } fn update_channel(&self, mut funding_txo: lightning::chain::transaction::OutPoint, mut update: lightning::chain::channelmonitor::ChannelMonitorUpdate) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> { - let mut ret = (self.update_channel)(self.this_arg, crate::lightning::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(funding_txo)), is_owned: true }, crate::lightning::chain::channelmonitor::ChannelMonitorUpdate { inner: Box::into_raw(Box::new(update)), is_owned: true }); + let mut ret = (self.update_channel)(self.this_arg, crate::lightning::chain::transaction::OutPoint { inner: ObjOps::heap_alloc(funding_txo), is_owned: true }, crate::lightning::chain::channelmonitor::ChannelMonitorUpdate { inner: ObjOps::heap_alloc(update), is_owned: true }); let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })}; local_ret } @@ -543,6 +595,15 @@ pub struct Filter { } unsafe impl Send for Filter {} unsafe impl Sync for Filter {} +#[no_mangle] +pub(crate) extern "C" fn Filter_clone_fields(orig: &Filter) -> Filter { + Filter { + this_arg: orig.this_arg, + register_tx: Clone::clone(&orig.register_tx), + register_output: Clone::clone(&orig.register_output), + free: Clone::clone(&orig.free), + } +} use lightning::chain::Filter as rustFilter; impl rustFilter for Filter { @@ -550,7 +611,7 @@ impl rustFilter for Filter { (self.register_tx)(self.this_arg, txid.as_inner(), crate::c_types::u8slice::from_slice(&script_pubkey[..])) } fn register_output(&self, mut output: lightning::chain::WatchedOutput) -> Option<(usize, bitcoin::blockdata::transaction::Transaction)> { - let mut ret = (self.register_output)(self.this_arg, crate::lightning::chain::WatchedOutput { inner: Box::into_raw(Box::new(output)), is_owned: true }); + let mut ret = (self.register_output)(self.this_arg, crate::lightning::chain::WatchedOutput { inner: ObjOps::heap_alloc(output), is_owned: true }); let mut local_ret = if ret.is_some() { Some( { let (mut orig_ret_0_0, mut orig_ret_0_1) = ret.take().to_rust(); let mut local_ret_0 = (orig_ret_0_0, orig_ret_0_1.into_bitcoin()); local_ret_0 }) } else { None }; local_ret } @@ -607,7 +668,7 @@ pub struct WatchedOutput { impl Drop for WatchedOutput { fn drop(&mut self) { if self.is_owned && !<*mut nativeWatchedOutput>::is_null(self.inner) { - let _ = unsafe { Box::from_raw(self.inner) }; + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; } } } @@ -620,66 +681,76 @@ extern "C" fn WatchedOutput_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeWatchedOutput); } } #[allow(unused)] -/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl WatchedOutput { + pub(crate) fn get_native_ref(&self) -> &'static nativeWatchedOutput { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeWatchedOutput { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy pub(crate) fn take_inner(mut self) -> *mut nativeWatchedOutput { assert!(self.is_owned); - let ret = self.inner; + let ret = ObjOps::untweak_ptr(self.inner); self.inner = std::ptr::null_mut(); ret } } /// First block where the transaction output may have been spent. +/// +/// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None #[no_mangle] pub extern "C" fn WatchedOutput_get_block_hash(this_ptr: &WatchedOutput) -> crate::c_types::ThirtyTwoBytes { - let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.block_hash; + let mut inner_val = &mut this_ptr.get_native_mut_ref().block_hash; let mut local_inner_val = if inner_val.is_none() { crate::c_types::ThirtyTwoBytes::null() } else { { crate::c_types::ThirtyTwoBytes { data: (inner_val.unwrap()).into_inner() } } }; local_inner_val } /// First block where the transaction output may have been spent. +/// +/// Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None #[no_mangle] pub extern "C" fn WatchedOutput_set_block_hash(this_ptr: &mut WatchedOutput, mut val: crate::c_types::ThirtyTwoBytes) { let mut local_val = if val.data == [0; 32] { None } else { Some( { ::bitcoin::hash_types::BlockHash::from_slice(&val.data[..]).unwrap() }) }; - unsafe { &mut *this_ptr.inner }.block_hash = local_val; + unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.block_hash = local_val; } /// Outpoint identifying the transaction output. #[no_mangle] pub extern "C" fn WatchedOutput_get_outpoint(this_ptr: &WatchedOutput) -> crate::lightning::chain::transaction::OutPoint { - let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.outpoint; - crate::lightning::chain::transaction::OutPoint { inner: unsafe { ( (&(*inner_val) as *const _) as *mut _) }, is_owned: false } + let mut inner_val = &mut this_ptr.get_native_mut_ref().outpoint; + crate::lightning::chain::transaction::OutPoint { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const _) as *mut _) }, is_owned: false } } /// Outpoint identifying the transaction output. #[no_mangle] pub extern "C" fn WatchedOutput_set_outpoint(this_ptr: &mut WatchedOutput, mut val: crate::lightning::chain::transaction::OutPoint) { - unsafe { &mut *this_ptr.inner }.outpoint = *unsafe { Box::from_raw(val.take_inner()) }; + unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.outpoint = *unsafe { Box::from_raw(val.take_inner()) }; } /// Spending condition of the transaction output. #[no_mangle] pub extern "C" fn WatchedOutput_get_script_pubkey(this_ptr: &WatchedOutput) -> crate::c_types::u8slice { - let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.script_pubkey; + let mut inner_val = &mut this_ptr.get_native_mut_ref().script_pubkey; crate::c_types::u8slice::from_slice(&inner_val[..]) } /// Spending condition of the transaction output. #[no_mangle] pub extern "C" fn WatchedOutput_set_script_pubkey(this_ptr: &mut WatchedOutput, mut val: crate::c_types::derived::CVec_u8Z) { - unsafe { &mut *this_ptr.inner }.script_pubkey = ::bitcoin::blockdata::script::Script::from(val.into_rust()); + unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.script_pubkey = ::bitcoin::blockdata::script::Script::from(val.into_rust()); } /// Constructs a new WatchedOutput given each field #[must_use] #[no_mangle] pub extern "C" fn WatchedOutput_new(mut block_hash_arg: crate::c_types::ThirtyTwoBytes, mut outpoint_arg: crate::lightning::chain::transaction::OutPoint, mut script_pubkey_arg: crate::c_types::derived::CVec_u8Z) -> WatchedOutput { let mut local_block_hash_arg = if block_hash_arg.data == [0; 32] { None } else { Some( { ::bitcoin::hash_types::BlockHash::from_slice(&block_hash_arg.data[..]).unwrap() }) }; - WatchedOutput { inner: Box::into_raw(Box::new(nativeWatchedOutput { + WatchedOutput { inner: ObjOps::heap_alloc(nativeWatchedOutput { block_hash: local_block_hash_arg, outpoint: *unsafe { Box::from_raw(outpoint_arg.take_inner()) }, script_pubkey: ::bitcoin::blockdata::script::Script::from(script_pubkey_arg.into_rust()), - })), is_owned: true } + }), is_owned: true } } impl Clone for WatchedOutput { fn clone(&self) -> Self { Self { inner: if <*mut nativeWatchedOutput>::is_null(self.inner) { std::ptr::null_mut() } else { - Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } } @@ -701,6 +772,6 @@ pub extern "C" fn WatchedOutput_hash(o: &WatchedOutput) -> u64 { // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core #[allow(deprecated)] let mut hasher = core::hash::SipHasher::new(); - std::hash::Hash::hash(unsafe { &*o.inner }, &mut hasher); + std::hash::Hash::hash(o.get_native_ref(), &mut hasher); std::hash::Hasher::finish(&hasher) }