Fix race in C++ demo where num_txs_broadcasted ++s before =0s
[ldk-c-bindings] / lightning-c-bindings / src / lightning / chain / mod.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
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.
8
9 //! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
10
11 use std::str::FromStr;
12 use std::ffi::c_void;
13 use bitcoin::hashes::Hash;
14 use crate::c_types::*;
15
16 pub mod chaininterface;
17 pub mod chainmonitor;
18 pub mod channelmonitor;
19 pub mod transaction;
20 pub mod keysinterface;
21 mod onchaintx {
22
23 use std::str::FromStr;
24 use std::ffi::c_void;
25 use bitcoin::hashes::Hash;
26 use crate::c_types::*;
27
28 }
29 mod package {
30
31 use std::str::FromStr;
32 use std::ffi::c_void;
33 use bitcoin::hashes::Hash;
34 use crate::c_types::*;
35
36 }
37 /// An error when accessing the chain via [`Access`].
38 #[must_use]
39 #[derive(Clone)]
40 #[repr(C)]
41 pub enum AccessError {
42         /// The requested chain is unknown.
43         UnknownChain,
44         /// The requested transaction doesn't exist or hasn't confirmed.
45         UnknownTx,
46 }
47 use lightning::chain::AccessError as nativeAccessError;
48 impl AccessError {
49         #[allow(unused)]
50         pub(crate) fn to_native(&self) -> nativeAccessError {
51                 match self {
52                         AccessError::UnknownChain => nativeAccessError::UnknownChain,
53                         AccessError::UnknownTx => nativeAccessError::UnknownTx,
54                 }
55         }
56         #[allow(unused)]
57         pub(crate) fn into_native(self) -> nativeAccessError {
58                 match self {
59                         AccessError::UnknownChain => nativeAccessError::UnknownChain,
60                         AccessError::UnknownTx => nativeAccessError::UnknownTx,
61                 }
62         }
63         #[allow(unused)]
64         pub(crate) fn from_native(native: &nativeAccessError) -> Self {
65                 match native {
66                         nativeAccessError::UnknownChain => AccessError::UnknownChain,
67                         nativeAccessError::UnknownTx => AccessError::UnknownTx,
68                 }
69         }
70         #[allow(unused)]
71         pub(crate) fn native_into(native: nativeAccessError) -> Self {
72                 match native {
73                         nativeAccessError::UnknownChain => AccessError::UnknownChain,
74                         nativeAccessError::UnknownTx => AccessError::UnknownTx,
75                 }
76         }
77 }
78 /// Creates a copy of the AccessError
79 #[no_mangle]
80 pub extern "C" fn AccessError_clone(orig: &AccessError) -> AccessError {
81         orig.clone()
82 }
83 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
84 /// UTXOs.
85 #[repr(C)]
86 pub struct Access {
87         /// An opaque pointer which is passed to your function implementations as an argument.
88         /// This has no meaning in the LDK, and can be NULL or any other value.
89         pub this_arg: *mut c_void,
90         /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
91         /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
92         /// is unknown.
93         ///
94         /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
95         #[must_use]
96         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,
97         /// Frees any resources associated with this object given its this_arg pointer.
98         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
99         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
100 }
101 unsafe impl Send for Access {}
102 unsafe impl Sync for Access {}
103
104 use lightning::chain::Access as rustAccess;
105 impl rustAccess for Access {
106         fn get_utxo(&self, mut genesis_hash: &bitcoin::hash_types::BlockHash, mut short_channel_id: u64) -> Result<bitcoin::blockdata::transaction::TxOut, lightning::chain::AccessError> {
107                 let mut ret = (self.get_utxo)(self.this_arg, genesis_hash.as_inner(), short_channel_id);
108                 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() })};
109                 local_ret
110         }
111 }
112
113 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
114 // directly as a Deref trait in higher-level structs:
115 impl std::ops::Deref for Access {
116         type Target = Self;
117         fn deref(&self) -> &Self {
118                 self
119         }
120 }
121 /// Calls the free function if one is set
122 #[no_mangle]
123 pub extern "C" fn Access_free(this_ptr: Access) { }
124 impl Drop for Access {
125         fn drop(&mut self) {
126                 if let Some(f) = self.free {
127                         f(self.this_arg);
128                 }
129         }
130 }
131 /// The `Listen` trait is used to notify when blocks have been connected or disconnected from the
132 /// chain.
133 ///
134 /// Useful when needing to replay chain data upon startup or as new chain events occur. Clients
135 /// sourcing chain data using a block-oriented API should prefer this interface over [`Confirm`].
136 /// Such clients fetch the entire header chain whereas clients using [`Confirm`] only fetch headers
137 /// when needed.
138 #[repr(C)]
139 pub struct Listen {
140         /// An opaque pointer which is passed to your function implementations as an argument.
141         /// This has no meaning in the LDK, and can be NULL or any other value.
142         pub this_arg: *mut c_void,
143         /// Notifies the listener that a block was added at the given height.
144         pub block_connected: extern "C" fn (this_arg: *const c_void, block: crate::c_types::u8slice, height: u32),
145         /// Notifies the listener that a block was removed at the given height.
146         pub block_disconnected: extern "C" fn (this_arg: *const c_void, header: *const [u8; 80], height: u32),
147         /// Frees any resources associated with this object given its this_arg pointer.
148         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
149         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
150 }
151 unsafe impl Send for Listen {}
152 unsafe impl Sync for Listen {}
153
154 use lightning::chain::Listen as rustListen;
155 impl rustListen for Listen {
156         fn block_connected(&self, mut block: &bitcoin::blockdata::block::Block, mut height: u32) {
157                 let mut local_block = ::bitcoin::consensus::encode::serialize(block);
158                 (self.block_connected)(self.this_arg, crate::c_types::u8slice::from_slice(&local_block), height)
159         }
160         fn block_disconnected(&self, mut header: &bitcoin::blockdata::block::BlockHeader, mut height: u32) {
161                 let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s };
162                 (self.block_disconnected)(self.this_arg, &local_header, height)
163         }
164 }
165
166 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
167 // directly as a Deref trait in higher-level structs:
168 impl std::ops::Deref for Listen {
169         type Target = Self;
170         fn deref(&self) -> &Self {
171                 self
172         }
173 }
174 /// Calls the free function if one is set
175 #[no_mangle]
176 pub extern "C" fn Listen_free(this_ptr: Listen) { }
177 impl Drop for Listen {
178         fn drop(&mut self) {
179                 if let Some(f) = self.free {
180                         f(self.this_arg);
181                 }
182         }
183 }
184 /// The `Confirm` trait is used to notify when transactions have been confirmed on chain or
185 /// unconfirmed during a chain reorganization.
186 ///
187 /// Clients sourcing chain data using a transaction-oriented API should prefer this interface over
188 /// [`Listen`]. For instance, an Electrum client may implement [`Filter`] by subscribing to activity
189 /// related to registered transactions and outputs. Upon notification, it would pass along the
190 /// matching transactions using this interface.
191 ///
192 /// # Use
193 ///
194 /// The intended use is as follows:
195 /// - Call [`transactions_confirmed`] to process any on-chain activity of interest.
196 /// - Call [`transaction_unconfirmed`] to process any transaction returned by [`get_relevant_txids`]
197 ///   that has been reorganized out of the chain.
198 /// - Call [`best_block_updated`] whenever a new chain tip becomes available.
199 ///
200 /// # Order
201 ///
202 /// Clients must call these methods in chain order. Specifically:
203 /// - Transactions confirmed in a block must be given before transactions confirmed in a later
204 ///   block.
205 /// - Dependent transactions within the same block must be given in topological order, possibly in
206 ///   separate calls.
207 /// - Unconfirmed transactions must be given after the original confirmations and before any
208 ///   reconfirmation.
209 ///
210 /// See individual method documentation for further details.
211 ///
212 /// [`transactions_confirmed`]: Self::transactions_confirmed
213 /// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
214 /// [`best_block_updated`]: Self::best_block_updated
215 /// [`get_relevant_txids`]: Self::get_relevant_txids
216 #[repr(C)]
217 pub struct Confirm {
218         /// An opaque pointer which is passed to your function implementations as an argument.
219         /// This has no meaning in the LDK, and can be NULL or any other value.
220         pub this_arg: *mut c_void,
221         /// Processes transactions confirmed in a block with a given header and height.
222         ///
223         /// Should be called for any transactions registered by [`Filter::register_tx`] or any
224         /// transactions spending an output registered by [`Filter::register_output`]. Such transactions
225         /// appearing in the same block do not need to be included in the same call; instead, multiple
226         /// calls with additional transactions may be made so long as they are made in [chain order].
227         ///
228         /// May be called before or after [`best_block_updated`] for the corresponding block. However,
229         /// in the event of a chain reorganization, it must not be called with a `header` that is no
230         /// longer in the chain as of the last call to [`best_block_updated`].
231         ///
232         /// [chain order]: Confirm#Order
233         /// [`best_block_updated`]: Self::best_block_updated
234         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),
235         /// Processes a transaction that is no longer confirmed as result of a chain reorganization.
236         ///
237         /// Should be called for any transaction returned by [`get_relevant_txids`] if it has been
238         /// reorganized out of the best chain. Once called, the given transaction should not be returned
239         /// by [`get_relevant_txids`] unless it has been reconfirmed via [`transactions_confirmed`].
240         ///
241         /// [`get_relevant_txids`]: Self::get_relevant_txids
242         /// [`transactions_confirmed`]: Self::transactions_confirmed
243         pub transaction_unconfirmed: extern "C" fn (this_arg: *const c_void, txid: *const [u8; 32]),
244         /// Processes an update to the best header connected at the given height.
245         ///
246         /// Should be called when a new header is available but may be skipped for intermediary blocks
247         /// if they become available at the same time.
248         pub best_block_updated: extern "C" fn (this_arg: *const c_void, header: *const [u8; 80], height: u32),
249         /// Returns transactions that should be monitored for reorganization out of the chain.
250         ///
251         /// Should include any transactions passed to [`transactions_confirmed`] that have insufficient
252         /// confirmations to be safe from a chain reorganization. Should not include any transactions
253         /// passed to [`transaction_unconfirmed`] unless later reconfirmed.
254         ///
255         /// May be called to determine the subset of transactions that must still be monitored for
256         /// reorganization. Will be idempotent between calls but may change as a result of calls to the
257         /// other interface methods. Thus, this is useful to determine which transactions may need to be
258         /// given to [`transaction_unconfirmed`].
259         ///
260         /// [`transactions_confirmed`]: Self::transactions_confirmed
261         /// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
262         #[must_use]
263         pub get_relevant_txids: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_TxidZ,
264         /// Frees any resources associated with this object given its this_arg pointer.
265         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
266         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
267 }
268 unsafe impl Send for Confirm {}
269 unsafe impl Sync for Confirm {}
270
271 use lightning::chain::Confirm as rustConfirm;
272 impl rustConfirm for Confirm {
273         fn transactions_confirmed(&self, mut header: &bitcoin::blockdata::block::BlockHeader, mut txdata: &lightning::chain::transaction::TransactionData, mut height: u32) {
274                 let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s };
275                 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 }); };
276                 (self.transactions_confirmed)(self.this_arg, &local_header, local_txdata.into(), height)
277         }
278         fn transaction_unconfirmed(&self, mut txid: &bitcoin::hash_types::Txid) {
279                 (self.transaction_unconfirmed)(self.this_arg, txid.as_inner())
280         }
281         fn best_block_updated(&self, mut header: &bitcoin::blockdata::block::BlockHeader, mut height: u32) {
282                 let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s };
283                 (self.best_block_updated)(self.this_arg, &local_header, height)
284         }
285         fn get_relevant_txids(&self) -> Vec<bitcoin::hash_types::Txid> {
286                 let mut ret = (self.get_relevant_txids)(self.this_arg);
287                 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() }); };
288                 local_ret
289         }
290 }
291
292 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
293 // directly as a Deref trait in higher-level structs:
294 impl std::ops::Deref for Confirm {
295         type Target = Self;
296         fn deref(&self) -> &Self {
297                 self
298         }
299 }
300 /// Calls the free function if one is set
301 #[no_mangle]
302 pub extern "C" fn Confirm_free(this_ptr: Confirm) { }
303 impl Drop for Confirm {
304         fn drop(&mut self) {
305                 if let Some(f) = self.free {
306                         f(self.this_arg);
307                 }
308         }
309 }
310 /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
311 /// blocks are connected and disconnected.
312 ///
313 /// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
314 /// responsible for maintaining a set of monitors such that they can be updated accordingly as
315 /// channel state changes and HTLCs are resolved. See method documentation for specific
316 /// requirements.
317 ///
318 /// Implementations **must** ensure that updates are successfully applied and persisted upon method
319 /// completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
320 /// without taking any further action such as persisting the current state.
321 ///
322 /// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
323 /// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
324 /// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
325 /// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
326 /// multiple instances.
327 ///
328 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
329 /// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
330 /// [`PermanentFailure`]: channelmonitor::ChannelMonitorUpdateErr::PermanentFailure
331 #[repr(C)]
332 pub struct Watch {
333         /// An opaque pointer which is passed to your function implementations as an argument.
334         /// This has no meaning in the LDK, and can be NULL or any other value.
335         pub this_arg: *mut c_void,
336         /// Watches a channel identified by `funding_txo` using `monitor`.
337         ///
338         /// Implementations are responsible for watching the chain for the funding transaction along
339         /// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
340         /// calling [`block_connected`] and [`block_disconnected`] on the monitor.
341         ///
342         /// [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
343         /// [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
344         /// [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
345         #[must_use]
346         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,
347         /// Updates a channel identified by `funding_txo` by applying `update` to its monitor.
348         ///
349         /// Implementations must call [`update_monitor`] with the given update. See
350         /// [`ChannelMonitorUpdateErr`] for invariants around returning an error.
351         ///
352         /// [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
353         /// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
354         #[must_use]
355         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,
356         /// Returns any monitor events since the last call. Subsequent calls must only return new
357         /// events.
358         #[must_use]
359         pub release_pending_monitor_events: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_MonitorEventZ,
360         /// Frees any resources associated with this object given its this_arg pointer.
361         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
362         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
363 }
364 unsafe impl Send for Watch {}
365 unsafe impl Sync for Watch {}
366
367 use lightning::chain::Watch as rustWatch;
368 impl rustWatch<crate::lightning::chain::keysinterface::Sign> for Watch {
369         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> {
370                 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 });
371                 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() })};
372                 local_ret
373         }
374         fn update_channel(&self, mut funding_txo: lightning::chain::transaction::OutPoint, mut update: lightning::chain::channelmonitor::ChannelMonitorUpdate) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> {
375                 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 });
376                 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() })};
377                 local_ret
378         }
379         fn release_pending_monitor_events(&self) -> Vec<lightning::chain::channelmonitor::MonitorEvent> {
380                 let mut ret = (self.release_pending_monitor_events)(self.this_arg);
381                 let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { item.into_native() }); };
382                 local_ret
383         }
384 }
385
386 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
387 // directly as a Deref trait in higher-level structs:
388 impl std::ops::Deref for Watch {
389         type Target = Self;
390         fn deref(&self) -> &Self {
391                 self
392         }
393 }
394 /// Calls the free function if one is set
395 #[no_mangle]
396 pub extern "C" fn Watch_free(this_ptr: Watch) { }
397 impl Drop for Watch {
398         fn drop(&mut self) {
399                 if let Some(f) = self.free {
400                         f(self.this_arg);
401                 }
402         }
403 }
404 /// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
405 /// channels.
406 ///
407 /// This is useful in order to have a [`Watch`] implementation convey to a chain source which
408 /// transactions to be notified of. Notification may take the form of pre-filtering blocks or, in
409 /// the case of [BIP 157]/[BIP 158], only fetching a block if the compact filter matches. If
410 /// receiving full blocks from a chain source, any further filtering is unnecessary.
411 ///
412 /// After an output has been registered, subsequent block retrievals from the chain source must not
413 /// exclude any transactions matching the new criteria nor any in-block descendants of such
414 /// transactions.
415 ///
416 /// Note that use as part of a [`Watch`] implementation involves reentrancy. Therefore, the `Filter`
417 /// should not block on I/O. Implementations should instead queue the newly monitored data to be
418 /// processed later. Then, in order to block until the data has been processed, any [`Watch`]
419 /// invocation that has called the `Filter` must return [`TemporaryFailure`].
420 ///
421 /// [`TemporaryFailure`]: channelmonitor::ChannelMonitorUpdateErr::TemporaryFailure
422 /// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
423 /// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
424 #[repr(C)]
425 pub struct Filter {
426         /// An opaque pointer which is passed to your function implementations as an argument.
427         /// This has no meaning in the LDK, and can be NULL or any other value.
428         pub this_arg: *mut c_void,
429         /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
430         /// a spending condition.
431         pub register_tx: extern "C" fn (this_arg: *const c_void, txid: *const [u8; 32], script_pubkey: crate::c_types::u8slice),
432         /// Registers interest in spends of a transaction output.
433         ///
434         /// Optionally, when `output.block_hash` is set, should return any transaction spending the
435         /// output that is found in the corresponding block along with its index.
436         ///
437         /// This return value is useful for Electrum clients in order to supply in-block descendant
438         /// transactions which otherwise were not included. This is not necessary for other clients if
439         /// such descendant transactions were already included (e.g., when a BIP 157 client provides the
440         /// full block).
441         #[must_use]
442         pub register_output: extern "C" fn (this_arg: *const c_void, output: crate::lightning::chain::WatchedOutput) -> crate::c_types::derived::COption_C2Tuple_usizeTransactionZZ,
443         /// Frees any resources associated with this object given its this_arg pointer.
444         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
445         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
446 }
447 unsafe impl Send for Filter {}
448 unsafe impl Sync for Filter {}
449
450 use lightning::chain::Filter as rustFilter;
451 impl rustFilter for Filter {
452         fn register_tx(&self, mut txid: &bitcoin::hash_types::Txid, mut script_pubkey: &bitcoin::blockdata::script::Script) {
453                 (self.register_tx)(self.this_arg, txid.as_inner(), crate::c_types::u8slice::from_slice(&script_pubkey[..]))
454         }
455         fn register_output(&self, mut output: lightning::chain::WatchedOutput) -> Option<(usize, bitcoin::blockdata::transaction::Transaction)> {
456                 let mut ret = (self.register_output)(self.this_arg, crate::lightning::chain::WatchedOutput { inner: Box::into_raw(Box::new(output)), is_owned: true });
457                 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 };
458                 local_ret
459         }
460 }
461
462 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
463 // directly as a Deref trait in higher-level structs:
464 impl std::ops::Deref for Filter {
465         type Target = Self;
466         fn deref(&self) -> &Self {
467                 self
468         }
469 }
470 /// Calls the free function if one is set
471 #[no_mangle]
472 pub extern "C" fn Filter_free(this_ptr: Filter) { }
473 impl Drop for Filter {
474         fn drop(&mut self) {
475                 if let Some(f) = self.free {
476                         f(self.this_arg);
477                 }
478         }
479 }
480
481 use lightning::chain::WatchedOutput as nativeWatchedOutputImport;
482 type nativeWatchedOutput = nativeWatchedOutputImport;
483
484 /// A transaction output watched by a [`ChannelMonitor`] for spends on-chain.
485 ///
486 /// Used to convey to a [`Filter`] such an output with a given spending condition. Any transaction
487 /// spending the output must be given to [`ChannelMonitor::block_connected`] either directly or via
488 /// the return value of [`Filter::register_output`].
489 ///
490 /// If `block_hash` is `Some`, this indicates the output was created in the corresponding block and
491 /// may have been spent there. See [`Filter::register_output`] for details.
492 ///
493 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
494 /// [`ChannelMonitor::block_connected`]: channelmonitor::ChannelMonitor::block_connected
495 #[must_use]
496 #[repr(C)]
497 pub struct WatchedOutput {
498         /// A pointer to the opaque Rust object.
499
500         /// Nearly everywhere, inner must be non-null, however in places where
501         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
502         pub inner: *mut nativeWatchedOutput,
503         /// Indicates that this is the only struct which contains the same pointer.
504
505         /// Rust functions which take ownership of an object provided via an argument require
506         /// this to be true and invalidate the object pointed to by inner.
507         pub is_owned: bool,
508 }
509
510 impl Drop for WatchedOutput {
511         fn drop(&mut self) {
512                 if self.is_owned && !<*mut nativeWatchedOutput>::is_null(self.inner) {
513                         let _ = unsafe { Box::from_raw(self.inner) };
514                 }
515         }
516 }
517 /// Frees any resources used by the WatchedOutput, if is_owned is set and inner is non-NULL.
518 #[no_mangle]
519 pub extern "C" fn WatchedOutput_free(this_obj: WatchedOutput) { }
520 #[allow(unused)]
521 /// Used only if an object of this type is returned as a trait impl by a method
522 extern "C" fn WatchedOutput_free_void(this_ptr: *mut c_void) {
523         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeWatchedOutput); }
524 }
525 #[allow(unused)]
526 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
527 impl WatchedOutput {
528         pub(crate) fn take_inner(mut self) -> *mut nativeWatchedOutput {
529                 assert!(self.is_owned);
530                 let ret = self.inner;
531                 self.inner = std::ptr::null_mut();
532                 ret
533         }
534 }
535 /// First block where the transaction output may have been spent.
536 #[no_mangle]
537 pub extern "C" fn WatchedOutput_get_block_hash(this_ptr: &WatchedOutput) -> crate::c_types::ThirtyTwoBytes {
538         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.block_hash;
539         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() } } };
540         local_inner_val
541 }
542 /// First block where the transaction output may have been spent.
543 #[no_mangle]
544 pub extern "C" fn WatchedOutput_set_block_hash(this_ptr: &mut WatchedOutput, mut val: crate::c_types::ThirtyTwoBytes) {
545         let mut local_val = if val.data == [0; 32] { None } else { Some( { ::bitcoin::hash_types::BlockHash::from_slice(&val.data[..]).unwrap() }) };
546         unsafe { &mut *this_ptr.inner }.block_hash = local_val;
547 }
548 /// Outpoint identifying the transaction output.
549 #[no_mangle]
550 pub extern "C" fn WatchedOutput_get_outpoint(this_ptr: &WatchedOutput) -> crate::lightning::chain::transaction::OutPoint {
551         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.outpoint;
552         crate::lightning::chain::transaction::OutPoint { inner: unsafe { ( (&(*inner_val) as *const _) as *mut _) }, is_owned: false }
553 }
554 /// Outpoint identifying the transaction output.
555 #[no_mangle]
556 pub extern "C" fn WatchedOutput_set_outpoint(this_ptr: &mut WatchedOutput, mut val: crate::lightning::chain::transaction::OutPoint) {
557         unsafe { &mut *this_ptr.inner }.outpoint = *unsafe { Box::from_raw(val.take_inner()) };
558 }
559 /// Spending condition of the transaction output.
560 #[no_mangle]
561 pub extern "C" fn WatchedOutput_get_script_pubkey(this_ptr: &WatchedOutput) -> crate::c_types::u8slice {
562         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.script_pubkey;
563         crate::c_types::u8slice::from_slice(&inner_val[..])
564 }
565 /// Spending condition of the transaction output.
566 #[no_mangle]
567 pub extern "C" fn WatchedOutput_set_script_pubkey(this_ptr: &mut WatchedOutput, mut val: crate::c_types::derived::CVec_u8Z) {
568         unsafe { &mut *this_ptr.inner }.script_pubkey = ::bitcoin::blockdata::script::Script::from(val.into_rust());
569 }
570 /// Constructs a new WatchedOutput given each field
571 #[must_use]
572 #[no_mangle]
573 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 {
574         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() }) };
575         WatchedOutput { inner: Box::into_raw(Box::new(nativeWatchedOutput {
576                 block_hash: local_block_hash_arg,
577                 outpoint: *unsafe { Box::from_raw(outpoint_arg.take_inner()) },
578                 script_pubkey: ::bitcoin::blockdata::script::Script::from(script_pubkey_arg.into_rust()),
579         })), is_owned: true }
580 }