X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Fchain%2Fmod.rs;h=fb72263f39c45ee5dcc6110d4f50384d3bb75712;hb=7caadd446bd497cfd47e753fa8a931cb49d5b6d6;hp=b7c6bd78e1359d56640ff3028eff010c8b0b798e;hpb=ee68ffa5fdfcfa8ee7fe70512d9e079d621083bc;p=rust-lightning diff --git a/lightning-c-bindings/src/chain/mod.rs b/lightning-c-bindings/src/chain/mod.rs index b7c6bd78..fb72263f 100644 --- a/lightning-c-bindings/src/chain/mod.rs +++ b/lightning-c-bindings/src/chain/mod.rs @@ -100,6 +100,50 @@ impl Drop for Access { } } } +/// The `Listen` trait is used to be notified of when blocks have been connected or disconnected +/// from the chain. +/// +/// Useful when needing to replay chain data upon startup or as new chain events occur. +#[repr(C)] +pub struct Listen { + pub this_arg: *mut c_void, + /// Notifies the listener that a block was added at the given height. + pub block_connected: extern "C" fn (this_arg: *const c_void, block: crate::c_types::u8slice, height: u32), + /// Notifies the listener that a block was removed at the given height. + pub block_disconnected: extern "C" fn (this_arg: *const c_void, header: *const [u8; 80], height: u32), + pub free: Option, +} + +use lightning::chain::Listen as rustListen; +impl rustListen for Listen { + fn block_connected(&self, block: &bitcoin::blockdata::block::Block, height: u32) { + let mut local_block = ::bitcoin::consensus::encode::serialize(block); + (self.block_connected)(self.this_arg, crate::c_types::u8slice::from_slice(&local_block), height) + } + fn block_disconnected(&self, header: &bitcoin::blockdata::block::BlockHeader, height: u32) { + let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s }; + (self.block_disconnected)(self.this_arg, &local_header, height) + } +} + +// We're essentially a pointer already, or at least a set of pointers, so allow us to be used +// directly as a Deref trait in higher-level structs: +impl std::ops::Deref for Listen { + type Target = Self; + fn deref(&self) -> &Self { + self + } +} +/// Calls the free function if one is set +#[no_mangle] +pub extern "C" fn Listen_free(this_ptr: Listen) { } +impl Drop for Listen { + fn drop(&mut self) { + if let Some(f) = self.free { + f(self.this_arg); + } + } +} /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as /// blocks are connected and disconnected. /// @@ -154,9 +198,8 @@ unsafe impl Send for Watch {} unsafe impl Sync for Watch {} use lightning::chain::Watch as rustWatch; -impl rustWatch for Watch { - type Keys = crate::chain::keysinterface::ChannelKeys; - fn watch_channel(&self, funding_txo: lightning::chain::transaction::OutPoint, monitor: lightning::chain::channelmonitor::ChannelMonitor) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> { +impl rustWatch for Watch { + fn watch_channel(&self, funding_txo: lightning::chain::transaction::OutPoint, monitor: lightning::chain::channelmonitor::ChannelMonitor) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> { let mut ret = (self.watch_channel)(self.this_arg, crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(funding_txo)), is_owned: true }, crate::chain::channelmonitor::ChannelMonitor { inner: Box::into_raw(Box::new(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