1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
11 use std::str::FromStr;
13 use bitcoin::hashes::Hash;
14 use crate::c_types::*;
16 pub mod chaininterface;
18 pub mod channelmonitor;
20 pub mod keysinterface;
23 use std::str::FromStr;
25 use bitcoin::hashes::Hash;
26 use crate::c_types::*;
31 use std::str::FromStr;
33 use bitcoin::hashes::Hash;
34 use crate::c_types::*;
38 use lightning::chain::BestBlock as nativeBestBlockImport;
39 type nativeBestBlock = nativeBestBlockImport;
41 /// The best known block as identified by its hash and height.
44 pub struct BestBlock {
45 /// A pointer to the opaque Rust object.
47 /// Nearly everywhere, inner must be non-null, however in places where
48 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
49 pub inner: *mut nativeBestBlock,
50 /// Indicates that this is the only struct which contains the same pointer.
52 /// Rust functions which take ownership of an object provided via an argument require
53 /// this to be true and invalidate the object pointed to by inner.
57 impl Drop for BestBlock {
59 if self.is_owned && !<*mut nativeBestBlock>::is_null(self.inner) {
60 let _ = unsafe { Box::from_raw(self.inner) };
64 /// Frees any resources used by the BestBlock, if is_owned is set and inner is non-NULL.
66 pub extern "C" fn BestBlock_free(this_obj: BestBlock) { }
68 /// Used only if an object of this type is returned as a trait impl by a method
69 extern "C" fn BestBlock_free_void(this_ptr: *mut c_void) {
70 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBestBlock); }
73 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
75 pub(crate) fn take_inner(mut self) -> *mut nativeBestBlock {
76 assert!(self.is_owned);
78 self.inner = std::ptr::null_mut();
82 impl Clone for BestBlock {
83 fn clone(&self) -> Self {
85 inner: if <*mut nativeBestBlock>::is_null(self.inner) { std::ptr::null_mut() } else {
86 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
92 /// Used only if an object of this type is returned as a trait impl by a method
93 pub(crate) extern "C" fn BestBlock_clone_void(this_ptr: *const c_void) -> *mut c_void {
94 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBestBlock)).clone() })) as *mut c_void
97 /// Creates a copy of the BestBlock
98 pub extern "C" fn BestBlock_clone(orig: &BestBlock) -> BestBlock {
101 /// Constructs a `BestBlock` that represents the genesis block at height 0 of the given
105 pub extern "C" fn BestBlock_from_genesis(mut network: crate::bitcoin::network::Network) -> BestBlock {
106 let mut ret = lightning::chain::BestBlock::from_genesis(network.into_bitcoin());
107 BestBlock { inner: Box::into_raw(Box::new(ret)), is_owned: true }
110 /// Returns a `BestBlock` as identified by the given block hash and height.
113 pub extern "C" fn BestBlock_new(mut block_hash: crate::c_types::ThirtyTwoBytes, mut height: u32) -> BestBlock {
114 let mut ret = lightning::chain::BestBlock::new(::bitcoin::hash_types::BlockHash::from_slice(&block_hash.data[..]).unwrap(), height);
115 BestBlock { inner: Box::into_raw(Box::new(ret)), is_owned: true }
118 /// Returns the best block hash.
121 pub extern "C" fn BestBlock_block_hash(this_arg: &BestBlock) -> crate::c_types::ThirtyTwoBytes {
122 let mut ret = unsafe { &*this_arg.inner }.block_hash();
123 crate::c_types::ThirtyTwoBytes { data: ret.into_inner() }
126 /// Returns the best block height.
129 pub extern "C" fn BestBlock_height(this_arg: &BestBlock) -> u32 {
130 let mut ret = unsafe { &*this_arg.inner }.height();
134 /// An error when accessing the chain via [`Access`].
138 pub enum AccessError {
139 /// The requested chain is unknown.
141 /// The requested transaction doesn't exist or hasn't confirmed.
144 use lightning::chain::AccessError as nativeAccessError;
147 pub(crate) fn to_native(&self) -> nativeAccessError {
149 AccessError::UnknownChain => nativeAccessError::UnknownChain,
150 AccessError::UnknownTx => nativeAccessError::UnknownTx,
154 pub(crate) fn into_native(self) -> nativeAccessError {
156 AccessError::UnknownChain => nativeAccessError::UnknownChain,
157 AccessError::UnknownTx => nativeAccessError::UnknownTx,
161 pub(crate) fn from_native(native: &nativeAccessError) -> Self {
163 nativeAccessError::UnknownChain => AccessError::UnknownChain,
164 nativeAccessError::UnknownTx => AccessError::UnknownTx,
168 pub(crate) fn native_into(native: nativeAccessError) -> Self {
170 nativeAccessError::UnknownChain => AccessError::UnknownChain,
171 nativeAccessError::UnknownTx => AccessError::UnknownTx,
175 /// Creates a copy of the AccessError
177 pub extern "C" fn AccessError_clone(orig: &AccessError) -> AccessError {
180 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
184 /// An opaque pointer which is passed to your function implementations as an argument.
185 /// This has no meaning in the LDK, and can be NULL or any other value.
186 pub this_arg: *mut c_void,
187 /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
188 /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
191 /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
193 pub get_utxo: extern "C" fn (this_arg: *const c_void, genesis_hash: *const [u8; 32], short_channel_id: u64) -> crate::c_types::derived::CResult_TxOutAccessErrorZ,
194 /// Frees any resources associated with this object given its this_arg pointer.
195 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
196 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
198 unsafe impl Send for Access {}
199 unsafe impl Sync for Access {}
201 use lightning::chain::Access as rustAccess;
202 impl rustAccess for Access {
203 fn get_utxo(&self, mut genesis_hash: &bitcoin::hash_types::BlockHash, mut short_channel_id: u64) -> Result<bitcoin::blockdata::transaction::TxOut, lightning::chain::AccessError> {
204 let mut ret = (self.get_utxo)(self.this_arg, genesis_hash.as_inner(), short_channel_id);
205 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_rust() }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })};
210 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
211 // directly as a Deref trait in higher-level structs:
212 impl std::ops::Deref for Access {
214 fn deref(&self) -> &Self {
218 /// Calls the free function if one is set
220 pub extern "C" fn Access_free(this_ptr: Access) { }
221 impl Drop for Access {
223 if let Some(f) = self.free {
228 /// The `Listen` trait is used to notify when blocks have been connected or disconnected from the
231 /// Useful when needing to replay chain data upon startup or as new chain events occur. Clients
232 /// sourcing chain data using a block-oriented API should prefer this interface over [`Confirm`].
233 /// Such clients fetch the entire header chain whereas clients using [`Confirm`] only fetch headers
237 /// An opaque pointer which is passed to your function implementations as an argument.
238 /// This has no meaning in the LDK, and can be NULL or any other value.
239 pub this_arg: *mut c_void,
240 /// Notifies the listener that a block was added at the given height.
241 pub block_connected: extern "C" fn (this_arg: *const c_void, block: crate::c_types::u8slice, height: u32),
242 /// Notifies the listener that a block was removed at the given height.
243 pub block_disconnected: extern "C" fn (this_arg: *const c_void, header: *const [u8; 80], height: u32),
244 /// Frees any resources associated with this object given its this_arg pointer.
245 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
246 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
248 unsafe impl Send for Listen {}
249 unsafe impl Sync for Listen {}
251 use lightning::chain::Listen as rustListen;
252 impl rustListen for Listen {
253 fn block_connected(&self, mut block: &bitcoin::blockdata::block::Block, mut height: u32) {
254 let mut local_block = ::bitcoin::consensus::encode::serialize(block);
255 (self.block_connected)(self.this_arg, crate::c_types::u8slice::from_slice(&local_block), height)
257 fn block_disconnected(&self, mut header: &bitcoin::blockdata::block::BlockHeader, mut height: u32) {
258 let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s };
259 (self.block_disconnected)(self.this_arg, &local_header, height)
263 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
264 // directly as a Deref trait in higher-level structs:
265 impl std::ops::Deref for Listen {
267 fn deref(&self) -> &Self {
271 /// Calls the free function if one is set
273 pub extern "C" fn Listen_free(this_ptr: Listen) { }
274 impl Drop for Listen {
276 if let Some(f) = self.free {
281 /// The `Confirm` trait is used to notify when transactions have been confirmed on chain or
282 /// unconfirmed during a chain reorganization.
284 /// Clients sourcing chain data using a transaction-oriented API should prefer this interface over
285 /// [`Listen`]. For instance, an Electrum client may implement [`Filter`] by subscribing to activity
286 /// related to registered transactions and outputs. Upon notification, it would pass along the
287 /// matching transactions using this interface.
291 /// The intended use is as follows:
292 /// - Call [`transactions_confirmed`] to process any on-chain activity of interest.
293 /// - Call [`transaction_unconfirmed`] to process any transaction returned by [`get_relevant_txids`]
294 /// that has been reorganized out of the chain.
295 /// - Call [`best_block_updated`] whenever a new chain tip becomes available.
299 /// Clients must call these methods in chain order. Specifically:
300 /// - Transactions confirmed in a block must be given before transactions confirmed in a later
302 /// - Dependent transactions within the same block must be given in topological order, possibly in
304 /// - Unconfirmed transactions must be given after the original confirmations and before any
307 /// See individual method documentation for further details.
309 /// [`transactions_confirmed`]: Self::transactions_confirmed
310 /// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
311 /// [`best_block_updated`]: Self::best_block_updated
312 /// [`get_relevant_txids`]: Self::get_relevant_txids
315 /// An opaque pointer which is passed to your function implementations as an argument.
316 /// This has no meaning in the LDK, and can be NULL or any other value.
317 pub this_arg: *mut c_void,
318 /// Processes transactions confirmed in a block with a given header and height.
320 /// Should be called for any transactions registered by [`Filter::register_tx`] or any
321 /// transactions spending an output registered by [`Filter::register_output`]. Such transactions
322 /// appearing in the same block do not need to be included in the same call; instead, multiple
323 /// calls with additional transactions may be made so long as they are made in [chain order].
325 /// May be called before or after [`best_block_updated`] for the corresponding block. However,
326 /// in the event of a chain reorganization, it must not be called with a `header` that is no
327 /// longer in the chain as of the last call to [`best_block_updated`].
329 /// [chain order]: Confirm#Order
330 /// [`best_block_updated`]: Self::best_block_updated
331 pub transactions_confirmed: extern "C" fn (this_arg: *const c_void, header: *const [u8; 80], txdata: crate::c_types::derived::CVec_C2Tuple_usizeTransactionZZ, height: u32),
332 /// Processes a transaction that is no longer confirmed as result of a chain reorganization.
334 /// Should be called for any transaction returned by [`get_relevant_txids`] if it has been
335 /// reorganized out of the best chain. Once called, the given transaction should not be returned
336 /// by [`get_relevant_txids`] unless it has been reconfirmed via [`transactions_confirmed`].
338 /// [`get_relevant_txids`]: Self::get_relevant_txids
339 /// [`transactions_confirmed`]: Self::transactions_confirmed
340 pub transaction_unconfirmed: extern "C" fn (this_arg: *const c_void, txid: *const [u8; 32]),
341 /// Processes an update to the best header connected at the given height.
343 /// Should be called when a new header is available but may be skipped for intermediary blocks
344 /// if they become available at the same time.
345 pub best_block_updated: extern "C" fn (this_arg: *const c_void, header: *const [u8; 80], height: u32),
346 /// Returns transactions that should be monitored for reorganization out of the chain.
348 /// Should include any transactions passed to [`transactions_confirmed`] that have insufficient
349 /// confirmations to be safe from a chain reorganization. Should not include any transactions
350 /// passed to [`transaction_unconfirmed`] unless later reconfirmed.
352 /// May be called to determine the subset of transactions that must still be monitored for
353 /// reorganization. Will be idempotent between calls but may change as a result of calls to the
354 /// other interface methods. Thus, this is useful to determine which transactions may need to be
355 /// given to [`transaction_unconfirmed`].
357 /// [`transactions_confirmed`]: Self::transactions_confirmed
358 /// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
360 pub get_relevant_txids: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_TxidZ,
361 /// Frees any resources associated with this object given its this_arg pointer.
362 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
363 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
365 unsafe impl Send for Confirm {}
366 unsafe impl Sync for Confirm {}
368 use lightning::chain::Confirm as rustConfirm;
369 impl rustConfirm for Confirm {
370 fn transactions_confirmed(&self, mut header: &bitcoin::blockdata::block::BlockHeader, mut txdata: &lightning::chain::transaction::TransactionData, mut height: u32) {
371 let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s };
372 let mut local_txdata = Vec::new(); for item in txdata.iter() { local_txdata.push( { let (mut orig_txdata_0_0, mut orig_txdata_0_1) = item; let mut local_txdata_0 = (orig_txdata_0_0, crate::c_types::Transaction::from_bitcoin(&orig_txdata_0_1)).into(); local_txdata_0 }); };
373 (self.transactions_confirmed)(self.this_arg, &local_header, local_txdata.into(), height)
375 fn transaction_unconfirmed(&self, mut txid: &bitcoin::hash_types::Txid) {
376 (self.transaction_unconfirmed)(self.this_arg, txid.as_inner())
378 fn best_block_updated(&self, mut header: &bitcoin::blockdata::block::BlockHeader, mut height: u32) {
379 let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s };
380 (self.best_block_updated)(self.this_arg, &local_header, height)
382 fn get_relevant_txids(&self) -> Vec<bitcoin::hash_types::Txid> {
383 let mut ret = (self.get_relevant_txids)(self.this_arg);
384 let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { ::bitcoin::hash_types::Txid::from_slice(&item.data[..]).unwrap() }); };
389 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
390 // directly as a Deref trait in higher-level structs:
391 impl std::ops::Deref for Confirm {
393 fn deref(&self) -> &Self {
397 /// Calls the free function if one is set
399 pub extern "C" fn Confirm_free(this_ptr: Confirm) { }
400 impl Drop for Confirm {
402 if let Some(f) = self.free {
407 /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
408 /// blocks are connected and disconnected.
410 /// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
411 /// responsible for maintaining a set of monitors such that they can be updated accordingly as
412 /// channel state changes and HTLCs are resolved. See method documentation for specific
415 /// Implementations **must** ensure that updates are successfully applied and persisted upon method
416 /// completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
417 /// without taking any further action such as persisting the current state.
419 /// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
420 /// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
421 /// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
422 /// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
423 /// multiple instances.
425 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
426 /// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
427 /// [`PermanentFailure`]: channelmonitor::ChannelMonitorUpdateErr::PermanentFailure
430 /// An opaque pointer which is passed to your function implementations as an argument.
431 /// This has no meaning in the LDK, and can be NULL or any other value.
432 pub this_arg: *mut c_void,
433 /// Watches a channel identified by `funding_txo` using `monitor`.
435 /// Implementations are responsible for watching the chain for the funding transaction along
436 /// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
437 /// calling [`block_connected`] and [`block_disconnected`] on the monitor.
439 /// [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
440 /// [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
441 /// [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
443 pub watch_channel: extern "C" fn (this_arg: *const c_void, funding_txo: crate::lightning::chain::transaction::OutPoint, monitor: crate::lightning::chain::channelmonitor::ChannelMonitor) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
444 /// Updates a channel identified by `funding_txo` by applying `update` to its monitor.
446 /// Implementations must call [`update_monitor`] with the given update. See
447 /// [`ChannelMonitorUpdateErr`] for invariants around returning an error.
449 /// [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
450 /// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
452 pub update_channel: extern "C" fn (this_arg: *const c_void, funding_txo: crate::lightning::chain::transaction::OutPoint, update: crate::lightning::chain::channelmonitor::ChannelMonitorUpdate) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
453 /// Returns any monitor events since the last call. Subsequent calls must only return new
456 pub release_pending_monitor_events: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_MonitorEventZ,
457 /// Frees any resources associated with this object given its this_arg pointer.
458 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
459 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
461 unsafe impl Send for Watch {}
462 unsafe impl Sync for Watch {}
464 use lightning::chain::Watch as rustWatch;
465 impl rustWatch<crate::lightning::chain::keysinterface::Sign> for Watch {
466 fn watch_channel(&self, mut funding_txo: lightning::chain::transaction::OutPoint, mut monitor: lightning::chain::channelmonitor::ChannelMonitor<crate::lightning::chain::keysinterface::Sign>) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> {
467 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 });
468 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() })};
471 fn update_channel(&self, mut funding_txo: lightning::chain::transaction::OutPoint, mut update: lightning::chain::channelmonitor::ChannelMonitorUpdate) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> {
472 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 });
473 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() })};
476 fn release_pending_monitor_events(&self) -> Vec<lightning::chain::channelmonitor::MonitorEvent> {
477 let mut ret = (self.release_pending_monitor_events)(self.this_arg);
478 let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { item.into_native() }); };
483 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
484 // directly as a Deref trait in higher-level structs:
485 impl std::ops::Deref for Watch {
487 fn deref(&self) -> &Self {
491 /// Calls the free function if one is set
493 pub extern "C" fn Watch_free(this_ptr: Watch) { }
494 impl Drop for Watch {
496 if let Some(f) = self.free {
501 /// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
504 /// This is useful in order to have a [`Watch`] implementation convey to a chain source which
505 /// transactions to be notified of. Notification may take the form of pre-filtering blocks or, in
506 /// the case of [BIP 157]/[BIP 158], only fetching a block if the compact filter matches. If
507 /// receiving full blocks from a chain source, any further filtering is unnecessary.
509 /// After an output has been registered, subsequent block retrievals from the chain source must not
510 /// exclude any transactions matching the new criteria nor any in-block descendants of such
513 /// Note that use as part of a [`Watch`] implementation involves reentrancy. Therefore, the `Filter`
514 /// should not block on I/O. Implementations should instead queue the newly monitored data to be
515 /// processed later. Then, in order to block until the data has been processed, any [`Watch`]
516 /// invocation that has called the `Filter` must return [`TemporaryFailure`].
518 /// [`TemporaryFailure`]: channelmonitor::ChannelMonitorUpdateErr::TemporaryFailure
519 /// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
520 /// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
523 /// An opaque pointer which is passed to your function implementations as an argument.
524 /// This has no meaning in the LDK, and can be NULL or any other value.
525 pub this_arg: *mut c_void,
526 /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
527 /// a spending condition.
528 pub register_tx: extern "C" fn (this_arg: *const c_void, txid: *const [u8; 32], script_pubkey: crate::c_types::u8slice),
529 /// Registers interest in spends of a transaction output.
531 /// Optionally, when `output.block_hash` is set, should return any transaction spending the
532 /// output that is found in the corresponding block along with its index.
534 /// This return value is useful for Electrum clients in order to supply in-block descendant
535 /// transactions which otherwise were not included. This is not necessary for other clients if
536 /// such descendant transactions were already included (e.g., when a BIP 157 client provides the
539 pub register_output: extern "C" fn (this_arg: *const c_void, output: crate::lightning::chain::WatchedOutput) -> crate::c_types::derived::COption_C2Tuple_usizeTransactionZZ,
540 /// Frees any resources associated with this object given its this_arg pointer.
541 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
542 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
544 unsafe impl Send for Filter {}
545 unsafe impl Sync for Filter {}
547 use lightning::chain::Filter as rustFilter;
548 impl rustFilter for Filter {
549 fn register_tx(&self, mut txid: &bitcoin::hash_types::Txid, mut script_pubkey: &bitcoin::blockdata::script::Script) {
550 (self.register_tx)(self.this_arg, txid.as_inner(), crate::c_types::u8slice::from_slice(&script_pubkey[..]))
552 fn register_output(&self, mut output: lightning::chain::WatchedOutput) -> Option<(usize, bitcoin::blockdata::transaction::Transaction)> {
553 let mut ret = (self.register_output)(self.this_arg, crate::lightning::chain::WatchedOutput { inner: Box::into_raw(Box::new(output)), is_owned: true });
554 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 };
559 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
560 // directly as a Deref trait in higher-level structs:
561 impl std::ops::Deref for Filter {
563 fn deref(&self) -> &Self {
567 /// Calls the free function if one is set
569 pub extern "C" fn Filter_free(this_ptr: Filter) { }
570 impl Drop for Filter {
572 if let Some(f) = self.free {
578 use lightning::chain::WatchedOutput as nativeWatchedOutputImport;
579 type nativeWatchedOutput = nativeWatchedOutputImport;
581 /// A transaction output watched by a [`ChannelMonitor`] for spends on-chain.
583 /// Used to convey to a [`Filter`] such an output with a given spending condition. Any transaction
584 /// spending the output must be given to [`ChannelMonitor::block_connected`] either directly or via
585 /// the return value of [`Filter::register_output`].
587 /// If `block_hash` is `Some`, this indicates the output was created in the corresponding block and
588 /// may have been spent there. See [`Filter::register_output`] for details.
590 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
591 /// [`ChannelMonitor::block_connected`]: channelmonitor::ChannelMonitor::block_connected
594 pub struct WatchedOutput {
595 /// A pointer to the opaque Rust object.
597 /// Nearly everywhere, inner must be non-null, however in places where
598 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
599 pub inner: *mut nativeWatchedOutput,
600 /// Indicates that this is the only struct which contains the same pointer.
602 /// Rust functions which take ownership of an object provided via an argument require
603 /// this to be true and invalidate the object pointed to by inner.
607 impl Drop for WatchedOutput {
609 if self.is_owned && !<*mut nativeWatchedOutput>::is_null(self.inner) {
610 let _ = unsafe { Box::from_raw(self.inner) };
614 /// Frees any resources used by the WatchedOutput, if is_owned is set and inner is non-NULL.
616 pub extern "C" fn WatchedOutput_free(this_obj: WatchedOutput) { }
618 /// Used only if an object of this type is returned as a trait impl by a method
619 extern "C" fn WatchedOutput_free_void(this_ptr: *mut c_void) {
620 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeWatchedOutput); }
623 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
625 pub(crate) fn take_inner(mut self) -> *mut nativeWatchedOutput {
626 assert!(self.is_owned);
627 let ret = self.inner;
628 self.inner = std::ptr::null_mut();
632 /// First block where the transaction output may have been spent.
634 pub extern "C" fn WatchedOutput_get_block_hash(this_ptr: &WatchedOutput) -> crate::c_types::ThirtyTwoBytes {
635 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.block_hash;
636 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() } } };
639 /// First block where the transaction output may have been spent.
641 pub extern "C" fn WatchedOutput_set_block_hash(this_ptr: &mut WatchedOutput, mut val: crate::c_types::ThirtyTwoBytes) {
642 let mut local_val = if val.data == [0; 32] { None } else { Some( { ::bitcoin::hash_types::BlockHash::from_slice(&val.data[..]).unwrap() }) };
643 unsafe { &mut *this_ptr.inner }.block_hash = local_val;
645 /// Outpoint identifying the transaction output.
647 pub extern "C" fn WatchedOutput_get_outpoint(this_ptr: &WatchedOutput) -> crate::lightning::chain::transaction::OutPoint {
648 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.outpoint;
649 crate::lightning::chain::transaction::OutPoint { inner: unsafe { ( (&(*inner_val) as *const _) as *mut _) }, is_owned: false }
651 /// Outpoint identifying the transaction output.
653 pub extern "C" fn WatchedOutput_set_outpoint(this_ptr: &mut WatchedOutput, mut val: crate::lightning::chain::transaction::OutPoint) {
654 unsafe { &mut *this_ptr.inner }.outpoint = *unsafe { Box::from_raw(val.take_inner()) };
656 /// Spending condition of the transaction output.
658 pub extern "C" fn WatchedOutput_get_script_pubkey(this_ptr: &WatchedOutput) -> crate::c_types::u8slice {
659 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.script_pubkey;
660 crate::c_types::u8slice::from_slice(&inner_val[..])
662 /// Spending condition of the transaction output.
664 pub extern "C" fn WatchedOutput_set_script_pubkey(this_ptr: &mut WatchedOutput, mut val: crate::c_types::derived::CVec_u8Z) {
665 unsafe { &mut *this_ptr.inner }.script_pubkey = ::bitcoin::blockdata::script::Script::from(val.into_rust());
667 /// Constructs a new WatchedOutput given each field
670 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 {
671 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() }) };
672 WatchedOutput { inner: Box::into_raw(Box::new(nativeWatchedOutput {
673 block_hash: local_block_hash_arg,
674 outpoint: *unsafe { Box::from_raw(outpoint_arg.take_inner()) },
675 script_pubkey: ::bitcoin::blockdata::script::Script::from(script_pubkey_arg.into_rust()),
676 })), is_owned: true }
678 impl Clone for WatchedOutput {
679 fn clone(&self) -> Self {
681 inner: if <*mut nativeWatchedOutput>::is_null(self.inner) { std::ptr::null_mut() } else {
682 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
688 /// Used only if an object of this type is returned as a trait impl by a method
689 pub(crate) extern "C" fn WatchedOutput_clone_void(this_ptr: *const c_void) -> *mut c_void {
690 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeWatchedOutput)).clone() })) as *mut c_void
693 /// Creates a copy of the WatchedOutput
694 pub extern "C" fn WatchedOutput_clone(orig: &WatchedOutput) -> WatchedOutput {
697 /// Checks if two WatchedOutputs contain equal inner contents.
699 pub extern "C" fn WatchedOutput_hash(o: &WatchedOutput) -> u64 {
700 if o.inner.is_null() { return 0; }
701 // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
703 let mut hasher = core::hash::SipHasher::new();
704 std::hash::Hash::hash(unsafe { &*o.inner }, &mut hasher);
705 std::hash::Hasher::finish(&hasher)