Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / chain / chainmonitor.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 //! Logic to connect off-chain channel management with on-chain transaction monitoring.
10 //!
11 //! [`ChainMonitor`] is an implementation of [`chain::Watch`] used both to process blocks and to
12 //! update [`ChannelMonitor`]s accordingly. If any on-chain events need further processing, it will
13 //! make those available as [`MonitorEvent`]s to be consumed.
14 //!
15 //! [`ChainMonitor`] is parameterized by an optional chain source, which must implement the
16 //! [`chain::Filter`] trait. This provides a mechanism to signal new relevant outputs back to light
17 //! clients, such that transactions spending those outputs are included in block data.
18 //!
19 //! [`ChainMonitor`] may be used directly to monitor channels locally or as a part of a distributed
20 //! setup to monitor channels remotely. In the latter case, a custom [`chain::Watch`] implementation
21 //! would be responsible for routing each update to a remote server and for retrieving monitor
22 //! events. The remote server would make use of [`ChainMonitor`] for block processing and for
23 //! servicing [`ChannelMonitor`] updates from the client.
24
25 use alloc::str::FromStr;
26 use core::ffi::c_void;
27 use core::convert::Infallible;
28 use bitcoin::hashes::Hash;
29 use crate::c_types::*;
30 #[cfg(feature="no-std")]
31 use alloc::{vec::Vec, boxed::Box};
32
33
34 use lightning::chain::chainmonitor::MonitorUpdateId as nativeMonitorUpdateIdImport;
35 pub(crate) type nativeMonitorUpdateId = nativeMonitorUpdateIdImport;
36
37 /// An opaque identifier describing a specific [`Persist`] method call.
38 #[must_use]
39 #[repr(C)]
40 pub struct MonitorUpdateId {
41         /// A pointer to the opaque Rust object.
42
43         /// Nearly everywhere, inner must be non-null, however in places where
44         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
45         pub inner: *mut nativeMonitorUpdateId,
46         /// Indicates that this is the only struct which contains the same pointer.
47
48         /// Rust functions which take ownership of an object provided via an argument require
49         /// this to be true and invalidate the object pointed to by inner.
50         pub is_owned: bool,
51 }
52
53 impl Drop for MonitorUpdateId {
54         fn drop(&mut self) {
55                 if self.is_owned && !<*mut nativeMonitorUpdateId>::is_null(self.inner) {
56                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
57                 }
58         }
59 }
60 /// Frees any resources used by the MonitorUpdateId, if is_owned is set and inner is non-NULL.
61 #[no_mangle]
62 pub extern "C" fn MonitorUpdateId_free(this_obj: MonitorUpdateId) { }
63 #[allow(unused)]
64 /// Used only if an object of this type is returned as a trait impl by a method
65 pub(crate) extern "C" fn MonitorUpdateId_free_void(this_ptr: *mut c_void) {
66         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMonitorUpdateId); }
67 }
68 #[allow(unused)]
69 impl MonitorUpdateId {
70         pub(crate) fn get_native_ref(&self) -> &'static nativeMonitorUpdateId {
71                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
72         }
73         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMonitorUpdateId {
74                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
75         }
76         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
77         pub(crate) fn take_inner(mut self) -> *mut nativeMonitorUpdateId {
78                 assert!(self.is_owned);
79                 let ret = ObjOps::untweak_ptr(self.inner);
80                 self.inner = core::ptr::null_mut();
81                 ret
82         }
83 }
84 impl Clone for MonitorUpdateId {
85         fn clone(&self) -> Self {
86                 Self {
87                         inner: if <*mut nativeMonitorUpdateId>::is_null(self.inner) { core::ptr::null_mut() } else {
88                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
89                         is_owned: true,
90                 }
91         }
92 }
93 #[allow(unused)]
94 /// Used only if an object of this type is returned as a trait impl by a method
95 pub(crate) extern "C" fn MonitorUpdateId_clone_void(this_ptr: *const c_void) -> *mut c_void {
96         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeMonitorUpdateId)).clone() })) as *mut c_void
97 }
98 #[no_mangle]
99 /// Creates a copy of the MonitorUpdateId
100 pub extern "C" fn MonitorUpdateId_clone(orig: &MonitorUpdateId) -> MonitorUpdateId {
101         orig.clone()
102 }
103 /// Checks if two MonitorUpdateIds contain equal inner contents.
104 #[no_mangle]
105 pub extern "C" fn MonitorUpdateId_hash(o: &MonitorUpdateId) -> u64 {
106         if o.inner.is_null() { return 0; }
107         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
108         #[allow(deprecated)]
109         let mut hasher = core::hash::SipHasher::new();
110         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
111         core::hash::Hasher::finish(&hasher)
112 }
113 /// Checks if two MonitorUpdateIds contain equal inner contents.
114 /// This ignores pointers and is_owned flags and looks at the values in fields.
115 /// Two objects with NULL inner values will be considered "equal" here.
116 #[no_mangle]
117 pub extern "C" fn MonitorUpdateId_eq(a: &MonitorUpdateId, b: &MonitorUpdateId) -> bool {
118         if a.inner == b.inner { return true; }
119         if a.inner.is_null() || b.inner.is_null() { return false; }
120         if a.get_native_ref() == b.get_native_ref() { true } else { false }
121 }
122 /// `Persist` defines behavior for persisting channel monitors: this could mean
123 /// writing once to disk, and/or uploading to one or more backup services.
124 ///
125 /// Each method can return three possible values:
126 ///  * If persistence (including any relevant `fsync()` calls) happens immediately, the
127 ///    implementation should return `Ok(())`, indicating normal channel operation should continue.
128 ///  * If persistence happens asynchronously, implementations should first ensure the
129 ///    [`ChannelMonitor`] or [`ChannelMonitorUpdate`] are written durably to disk, and then return
130 ///    `Err(ChannelMonitorUpdateErr::TemporaryFailure)` while the update continues in the
131 ///    background. Once the update completes, [`ChainMonitor::channel_monitor_updated`] should be
132 ///    called with the corresponding [`MonitorUpdateId`].
133 ///
134 ///    Note that unlike the direct [`chain::Watch`] interface,
135 ///    [`ChainMonitor::channel_monitor_updated`] must be called once for *each* update which occurs.
136 ///
137 ///  * If persistence fails for some reason, implementations should return
138 ///    `Err(ChannelMonitorUpdateErr::PermanentFailure)`, in which case the channel will likely be
139 ///    closed without broadcasting the latest state. See
140 ///    [`ChannelMonitorUpdateErr::PermanentFailure`] for more details.
141 #[repr(C)]
142 pub struct Persist {
143         /// An opaque pointer which is passed to your function implementations as an argument.
144         /// This has no meaning in the LDK, and can be NULL or any other value.
145         pub this_arg: *mut c_void,
146         /// Persist a new channel's data in response to a [`chain::Watch::watch_channel`] call. This is
147         /// called by [`ChannelManager`] for new channels, or may be called directly, e.g. on startup.
148         ///
149         /// The data can be stored any way you want, but the identifier provided by LDK is the
150         /// channel's outpoint (and it is up to you to maintain a correct mapping between the outpoint
151         /// and the stored channel data). Note that you **must** persist every new monitor to disk.
152         ///
153         /// The `update_id` is used to identify this call to [`ChainMonitor::channel_monitor_updated`],
154         /// if you return [`ChannelMonitorUpdateErr::TemporaryFailure`].
155         ///
156         /// See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`
157         /// and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
158         ///
159         /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
160         /// [`Writeable::write`]: crate::util::ser::Writeable::write
161         #[must_use]
162         pub persist_new_channel: extern "C" fn (this_arg: *const c_void, channel_id: crate::lightning::chain::transaction::OutPoint, data: &crate::lightning::chain::channelmonitor::ChannelMonitor, update_id: crate::lightning::chain::chainmonitor::MonitorUpdateId) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
163         /// Update one channel's data. The provided [`ChannelMonitor`] has already applied the given
164         /// update.
165         ///
166         /// Note that on every update, you **must** persist either the [`ChannelMonitorUpdate`] or the
167         /// updated monitor itself to disk/backups. See the [`Persist`] trait documentation for more
168         /// details.
169         ///
170         /// During blockchain synchronization operations, this may be called with no
171         /// [`ChannelMonitorUpdate`], in which case the full [`ChannelMonitor`] needs to be persisted.
172         /// Note that after the full [`ChannelMonitor`] is persisted any previous
173         /// [`ChannelMonitorUpdate`]s which were persisted should be discarded - they can no longer be
174         /// applied to the persisted [`ChannelMonitor`] as they were already applied.
175         ///
176         /// If an implementer chooses to persist the updates only, they need to make
177         /// sure that all the updates are applied to the `ChannelMonitors` *before*
178         /// the set of channel monitors is given to the `ChannelManager`
179         /// deserialization routine. See [`ChannelMonitor::update_monitor`] for
180         /// applying a monitor update to a monitor. If full `ChannelMonitors` are
181         /// persisted, then there is no need to persist individual updates.
182         ///
183         /// Note that there could be a performance tradeoff between persisting complete
184         /// channel monitors on every update vs. persisting only updates and applying
185         /// them in batches. The size of each monitor grows `O(number of state updates)`
186         /// whereas updates are small and `O(1)`.
187         ///
188         /// The `update_id` is used to identify this call to [`ChainMonitor::channel_monitor_updated`],
189         /// if you return [`ChannelMonitorUpdateErr::TemporaryFailure`].
190         ///
191         /// See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`,
192         /// [`Writeable::write`] on [`ChannelMonitorUpdate`] for writing out an update, and
193         /// [`ChannelMonitorUpdateErr`] for requirements when returning errors.
194         ///
195         /// [`Writeable::write`]: crate::util::ser::Writeable::write
196         ///
197         /// Note that update (or a relevant inner pointer) may be NULL or all-0s to represent None
198         #[must_use]
199         pub update_persisted_channel: extern "C" fn (this_arg: *const c_void, channel_id: crate::lightning::chain::transaction::OutPoint, update: &crate::lightning::chain::channelmonitor::ChannelMonitorUpdate, data: &crate::lightning::chain::channelmonitor::ChannelMonitor, update_id: crate::lightning::chain::chainmonitor::MonitorUpdateId) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
200         /// Frees any resources associated with this object given its this_arg pointer.
201         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
202         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
203 }
204 unsafe impl Send for Persist {}
205 unsafe impl Sync for Persist {}
206 #[no_mangle]
207 pub(crate) extern "C" fn Persist_clone_fields(orig: &Persist) -> Persist {
208         Persist {
209                 this_arg: orig.this_arg,
210                 persist_new_channel: Clone::clone(&orig.persist_new_channel),
211                 update_persisted_channel: Clone::clone(&orig.update_persisted_channel),
212                 free: Clone::clone(&orig.free),
213         }
214 }
215
216 use lightning::chain::chainmonitor::Persist as rustPersist;
217 impl rustPersist<crate::lightning::chain::keysinterface::Sign> for Persist {
218         fn persist_new_channel(&self, mut channel_id: lightning::chain::transaction::OutPoint, mut data: &lightning::chain::channelmonitor::ChannelMonitor<crate::lightning::chain::keysinterface::Sign>, mut update_id: lightning::chain::chainmonitor::MonitorUpdateId) -> Result<(), lightning::chain::ChannelMonitorUpdateErr> {
219                 let mut ret = (self.persist_new_channel)(self.this_arg, crate::lightning::chain::transaction::OutPoint { inner: ObjOps::heap_alloc(channel_id), is_owned: true }, &crate::lightning::chain::channelmonitor::ChannelMonitor { inner: unsafe { ObjOps::nonnull_ptr_to_inner((data as *const lightning::chain::channelmonitor::ChannelMonitor<_, >) as *mut _) }, is_owned: false }, crate::lightning::chain::chainmonitor::MonitorUpdateId { inner: ObjOps::heap_alloc(update_id), is_owned: true });
220                 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() })};
221                 local_ret
222         }
223         fn update_persisted_channel(&self, mut channel_id: lightning::chain::transaction::OutPoint, mut update: &Option<lightning::chain::channelmonitor::ChannelMonitorUpdate>, mut data: &lightning::chain::channelmonitor::ChannelMonitor<crate::lightning::chain::keysinterface::Sign>, mut update_id: lightning::chain::chainmonitor::MonitorUpdateId) -> Result<(), lightning::chain::ChannelMonitorUpdateErr> {
224                 let mut local_update = &crate::lightning::chain::channelmonitor::ChannelMonitorUpdate { inner: unsafe { (if update.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (update.as_ref().unwrap()) }) } as *const lightning::chain::channelmonitor::ChannelMonitorUpdate<>) as *mut _ }, is_owned: false };
225                 let mut ret = (self.update_persisted_channel)(self.this_arg, crate::lightning::chain::transaction::OutPoint { inner: ObjOps::heap_alloc(channel_id), is_owned: true }, local_update, &crate::lightning::chain::channelmonitor::ChannelMonitor { inner: unsafe { ObjOps::nonnull_ptr_to_inner((data as *const lightning::chain::channelmonitor::ChannelMonitor<_, >) as *mut _) }, is_owned: false }, crate::lightning::chain::chainmonitor::MonitorUpdateId { inner: ObjOps::heap_alloc(update_id), is_owned: true });
226                 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() })};
227                 local_ret
228         }
229 }
230
231 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
232 // directly as a Deref trait in higher-level structs:
233 impl core::ops::Deref for Persist {
234         type Target = Self;
235         fn deref(&self) -> &Self {
236                 self
237         }
238 }
239 /// Calls the free function if one is set
240 #[no_mangle]
241 pub extern "C" fn Persist_free(this_ptr: Persist) { }
242 impl Drop for Persist {
243         fn drop(&mut self) {
244                 if let Some(f) = self.free {
245                         f(self.this_arg);
246                 }
247         }
248 }
249
250 use lightning::chain::chainmonitor::LockedChannelMonitor as nativeLockedChannelMonitorImport;
251 pub(crate) type nativeLockedChannelMonitor = nativeLockedChannelMonitorImport<'static, crate::lightning::chain::keysinterface::Sign>;
252
253 /// A read-only reference to a current ChannelMonitor.
254 ///
255 /// Note that this holds a mutex in [`ChainMonitor`] and may block other events until it is
256 /// released.
257 #[must_use]
258 #[repr(C)]
259 pub struct LockedChannelMonitor {
260         /// A pointer to the opaque Rust object.
261
262         /// Nearly everywhere, inner must be non-null, however in places where
263         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
264         pub inner: *mut nativeLockedChannelMonitor,
265         /// Indicates that this is the only struct which contains the same pointer.
266
267         /// Rust functions which take ownership of an object provided via an argument require
268         /// this to be true and invalidate the object pointed to by inner.
269         pub is_owned: bool,
270 }
271
272 impl Drop for LockedChannelMonitor {
273         fn drop(&mut self) {
274                 if self.is_owned && !<*mut nativeLockedChannelMonitor>::is_null(self.inner) {
275                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
276                 }
277         }
278 }
279 /// Frees any resources used by the LockedChannelMonitor, if is_owned is set and inner is non-NULL.
280 #[no_mangle]
281 pub extern "C" fn LockedChannelMonitor_free(this_obj: LockedChannelMonitor) { }
282 #[allow(unused)]
283 /// Used only if an object of this type is returned as a trait impl by a method
284 pub(crate) extern "C" fn LockedChannelMonitor_free_void(this_ptr: *mut c_void) {
285         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeLockedChannelMonitor); }
286 }
287 #[allow(unused)]
288 impl LockedChannelMonitor {
289         pub(crate) fn get_native_ref(&self) -> &'static nativeLockedChannelMonitor {
290                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
291         }
292         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeLockedChannelMonitor {
293                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
294         }
295         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
296         pub(crate) fn take_inner(mut self) -> *mut nativeLockedChannelMonitor {
297                 assert!(self.is_owned);
298                 let ret = ObjOps::untweak_ptr(self.inner);
299                 self.inner = core::ptr::null_mut();
300                 ret
301         }
302 }
303
304 use lightning::chain::chainmonitor::ChainMonitor as nativeChainMonitorImport;
305 pub(crate) type nativeChainMonitor = nativeChainMonitorImport<crate::lightning::chain::keysinterface::Sign, crate::lightning::chain::Filter, crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::chain::chaininterface::FeeEstimator, crate::lightning::util::logger::Logger, crate::lightning::chain::chainmonitor::Persist>;
306
307 /// An implementation of [`chain::Watch`] for monitoring channels.
308 ///
309 /// Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
310 /// [`chain::Watch`]. May be used in conjunction with [`ChannelManager`] to monitor channels locally
311 /// or used independently to monitor channels remotely. See the [module-level documentation] for
312 /// details.
313 ///
314 /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
315 /// [module-level documentation]: crate::chain::chainmonitor
316 #[must_use]
317 #[repr(C)]
318 pub struct ChainMonitor {
319         /// A pointer to the opaque Rust object.
320
321         /// Nearly everywhere, inner must be non-null, however in places where
322         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
323         pub inner: *mut nativeChainMonitor,
324         /// Indicates that this is the only struct which contains the same pointer.
325
326         /// Rust functions which take ownership of an object provided via an argument require
327         /// this to be true and invalidate the object pointed to by inner.
328         pub is_owned: bool,
329 }
330
331 impl Drop for ChainMonitor {
332         fn drop(&mut self) {
333                 if self.is_owned && !<*mut nativeChainMonitor>::is_null(self.inner) {
334                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
335                 }
336         }
337 }
338 /// Frees any resources used by the ChainMonitor, if is_owned is set and inner is non-NULL.
339 #[no_mangle]
340 pub extern "C" fn ChainMonitor_free(this_obj: ChainMonitor) { }
341 #[allow(unused)]
342 /// Used only if an object of this type is returned as a trait impl by a method
343 pub(crate) extern "C" fn ChainMonitor_free_void(this_ptr: *mut c_void) {
344         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChainMonitor); }
345 }
346 #[allow(unused)]
347 impl ChainMonitor {
348         pub(crate) fn get_native_ref(&self) -> &'static nativeChainMonitor {
349                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
350         }
351         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeChainMonitor {
352                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
353         }
354         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
355         pub(crate) fn take_inner(mut self) -> *mut nativeChainMonitor {
356                 assert!(self.is_owned);
357                 let ret = ObjOps::untweak_ptr(self.inner);
358                 self.inner = core::ptr::null_mut();
359                 ret
360         }
361 }
362 /// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
363 ///
364 /// When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
365 /// will call back to it indicating transactions and outputs of interest. This allows clients to
366 /// pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
367 /// always need to fetch full blocks absent another means for determining which blocks contain
368 /// transactions relevant to the watched channels.
369 #[must_use]
370 #[no_mangle]
371 pub extern "C" fn ChainMonitor_new(mut chain_source: crate::c_types::derived::COption_FilterZ, mut broadcaster: crate::lightning::chain::chaininterface::BroadcasterInterface, mut logger: crate::lightning::util::logger::Logger, mut feeest: crate::lightning::chain::chaininterface::FeeEstimator, mut persister: crate::lightning::chain::chainmonitor::Persist) -> ChainMonitor {
372         let mut local_chain_source = { /* chain_source*/ let chain_source_opt = chain_source; { } if chain_source_opt.is_none() { None } else { Some({ chain_source_opt.take() }) } };
373         let mut ret = lightning::chain::chainmonitor::ChainMonitor::new(local_chain_source, broadcaster, logger, feeest, persister);
374         ChainMonitor { inner: ObjOps::heap_alloc(ret), is_owned: true }
375 }
376
377 /// Gets the balances in the contained [`ChannelMonitor`]s which are claimable on-chain or
378 /// claims which are awaiting confirmation.
379 ///
380 /// Includes the balances from each [`ChannelMonitor`] *except* those included in
381 /// `ignored_channels`, allowing you to filter out balances from channels which are still open
382 /// (and whose balance should likely be pulled from the [`ChannelDetails`]).
383 ///
384 /// See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
385 /// inclusion in the return value.
386 #[must_use]
387 #[no_mangle]
388 pub extern "C" fn ChainMonitor_get_claimable_balances(this_arg: &ChainMonitor, mut ignored_channels: crate::c_types::derived::CVec_ChannelDetailsZ) -> crate::c_types::derived::CVec_BalanceZ {
389         let mut local_ignored_channels = Vec::new(); for mut item in ignored_channels.as_slice().iter() { local_ignored_channels.push( { item.get_native_ref() }); };
390         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_claimable_balances(&local_ignored_channels[..]);
391         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::chain::channelmonitor::Balance::native_into(item) }); };
392         local_ret.into()
393 }
394
395 /// Gets the [`LockedChannelMonitor`] for a given funding outpoint, returning an `Err` if no
396 /// such [`ChannelMonitor`] is currently being monitored for.
397 ///
398 /// Note that the result holds a mutex over our monitor set, and should not be held
399 /// indefinitely.
400 #[must_use]
401 #[no_mangle]
402 pub extern "C" fn ChainMonitor_get_monitor(this_arg: &ChainMonitor, mut funding_txo: crate::lightning::chain::transaction::OutPoint) -> crate::c_types::derived::CResult_LockedChannelMonitorNoneZ {
403         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_monitor(*unsafe { Box::from_raw(funding_txo.take_inner()) });
404         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::chain::chainmonitor::LockedChannelMonitor { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
405         local_ret
406 }
407
408 /// Lists the funding outpoint of each [`ChannelMonitor`] being monitored.
409 ///
410 /// Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
411 /// monitoring for on-chain state resolutions.
412 #[must_use]
413 #[no_mangle]
414 pub extern "C" fn ChainMonitor_list_monitors(this_arg: &ChainMonitor) -> crate::c_types::derived::CVec_OutPointZ {
415         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.list_monitors();
416         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::chain::transaction::OutPoint { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
417         local_ret.into()
418 }
419
420 /// Indicates the persistence of a [`ChannelMonitor`] has completed after
421 /// [`ChannelMonitorUpdateErr::TemporaryFailure`] was returned from an update operation.
422 ///
423 /// Thus, the anticipated use is, at a high level:
424 ///  1) This [`ChainMonitor`] calls [`Persist::update_persisted_channel`] which stores the
425 ///     update to disk and begins updating any remote (e.g. watchtower/backup) copies,
426 ///     returning [`ChannelMonitorUpdateErr::TemporaryFailure`],
427 ///  2) once all remote copies are updated, you call this function with the
428 ///     `completed_update_id` that completed, and once all pending updates have completed the
429 ///     channel will be re-enabled.
430 ///
431 /// Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
432 /// registered [`ChannelMonitor`]s.
433 #[must_use]
434 #[no_mangle]
435 pub extern "C" fn ChainMonitor_channel_monitor_updated(this_arg: &ChainMonitor, mut funding_txo: crate::lightning::chain::transaction::OutPoint, mut completed_update_id: crate::lightning::chain::chainmonitor::MonitorUpdateId) -> crate::c_types::derived::CResult_NoneAPIErrorZ {
436         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.channel_monitor_updated(*unsafe { Box::from_raw(funding_txo.take_inner()) }, *unsafe { Box::from_raw(completed_update_id.take_inner()) });
437         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::util::errors::APIError::native_into(e) }).into() };
438         local_ret
439 }
440
441 impl From<nativeChainMonitor> for crate::lightning::chain::Listen {
442         fn from(obj: nativeChainMonitor) -> Self {
443                 let mut rust_obj = ChainMonitor { inner: ObjOps::heap_alloc(obj), is_owned: true };
444                 let mut ret = ChainMonitor_as_Listen(&rust_obj);
445                 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
446                 rust_obj.inner = core::ptr::null_mut();
447                 ret.free = Some(ChainMonitor_free_void);
448                 ret
449         }
450 }
451 /// Constructs a new Listen which calls the relevant methods on this_arg.
452 /// This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
453 #[no_mangle]
454 pub extern "C" fn ChainMonitor_as_Listen(this_arg: &ChainMonitor) -> crate::lightning::chain::Listen {
455         crate::lightning::chain::Listen {
456                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
457                 free: None,
458                 block_connected: ChainMonitor_Listen_block_connected,
459                 block_disconnected: ChainMonitor_Listen_block_disconnected,
460         }
461 }
462
463 extern "C" fn ChainMonitor_Listen_block_connected(this_arg: *const c_void, mut block: crate::c_types::u8slice, mut height: u32) {
464         <nativeChainMonitor as lightning::chain::Listen<>>::block_connected(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, &::bitcoin::consensus::encode::deserialize(block.to_slice()).unwrap(), height)
465 }
466 extern "C" fn ChainMonitor_Listen_block_disconnected(this_arg: *const c_void, header: *const [u8; 80], mut height: u32) {
467         <nativeChainMonitor as lightning::chain::Listen<>>::block_disconnected(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, &::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), height)
468 }
469
470 impl From<nativeChainMonitor> for crate::lightning::chain::Confirm {
471         fn from(obj: nativeChainMonitor) -> Self {
472                 let mut rust_obj = ChainMonitor { inner: ObjOps::heap_alloc(obj), is_owned: true };
473                 let mut ret = ChainMonitor_as_Confirm(&rust_obj);
474                 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
475                 rust_obj.inner = core::ptr::null_mut();
476                 ret.free = Some(ChainMonitor_free_void);
477                 ret
478         }
479 }
480 /// Constructs a new Confirm which calls the relevant methods on this_arg.
481 /// This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
482 #[no_mangle]
483 pub extern "C" fn ChainMonitor_as_Confirm(this_arg: &ChainMonitor) -> crate::lightning::chain::Confirm {
484         crate::lightning::chain::Confirm {
485                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
486                 free: None,
487                 transactions_confirmed: ChainMonitor_Confirm_transactions_confirmed,
488                 transaction_unconfirmed: ChainMonitor_Confirm_transaction_unconfirmed,
489                 best_block_updated: ChainMonitor_Confirm_best_block_updated,
490                 get_relevant_txids: ChainMonitor_Confirm_get_relevant_txids,
491         }
492 }
493
494 extern "C" fn ChainMonitor_Confirm_transactions_confirmed(this_arg: *const c_void, header: *const [u8; 80], mut txdata: crate::c_types::derived::CVec_C2Tuple_usizeTransactionZZ, mut height: u32) {
495         let mut local_txdata = Vec::new(); for mut item in txdata.into_rust().drain(..) { local_txdata.push( { let (mut orig_txdata_0_0, mut orig_txdata_0_1) = item.to_rust(); let mut local_txdata_0 = (orig_txdata_0_0, orig_txdata_0_1.into_bitcoin()); local_txdata_0 }); };
496         <nativeChainMonitor as lightning::chain::Confirm<>>::transactions_confirmed(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, &::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), &local_txdata.iter().map(|(a, b)| (*a, b)).collect::<Vec<_>>()[..], height)
497 }
498 extern "C" fn ChainMonitor_Confirm_transaction_unconfirmed(this_arg: *const c_void, txid: *const [u8; 32]) {
499         <nativeChainMonitor as lightning::chain::Confirm<>>::transaction_unconfirmed(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, &::bitcoin::hash_types::Txid::from_slice(&unsafe { &*txid }[..]).unwrap())
500 }
501 extern "C" fn ChainMonitor_Confirm_best_block_updated(this_arg: *const c_void, header: *const [u8; 80], mut height: u32) {
502         <nativeChainMonitor as lightning::chain::Confirm<>>::best_block_updated(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, &::bitcoin::consensus::encode::deserialize(unsafe { &*header }).unwrap(), height)
503 }
504 #[must_use]
505 extern "C" fn ChainMonitor_Confirm_get_relevant_txids(this_arg: *const c_void) -> crate::c_types::derived::CVec_TxidZ {
506         let mut ret = <nativeChainMonitor as lightning::chain::Confirm<>>::get_relevant_txids(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, );
507         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::c_types::ThirtyTwoBytes { data: item.into_inner() } }); };
508         local_ret.into()
509 }
510
511 impl From<nativeChainMonitor> for crate::lightning::chain::Watch {
512         fn from(obj: nativeChainMonitor) -> Self {
513                 let mut rust_obj = ChainMonitor { inner: ObjOps::heap_alloc(obj), is_owned: true };
514                 let mut ret = ChainMonitor_as_Watch(&rust_obj);
515                 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
516                 rust_obj.inner = core::ptr::null_mut();
517                 ret.free = Some(ChainMonitor_free_void);
518                 ret
519         }
520 }
521 /// Constructs a new Watch which calls the relevant methods on this_arg.
522 /// This copies the `inner` pointer in this_arg and thus the returned Watch must be freed before this_arg is
523 #[no_mangle]
524 pub extern "C" fn ChainMonitor_as_Watch(this_arg: &ChainMonitor) -> crate::lightning::chain::Watch {
525         crate::lightning::chain::Watch {
526                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
527                 free: None,
528                 watch_channel: ChainMonitor_Watch_watch_channel,
529                 update_channel: ChainMonitor_Watch_update_channel,
530                 release_pending_monitor_events: ChainMonitor_Watch_release_pending_monitor_events,
531         }
532 }
533
534 #[must_use]
535 extern "C" fn ChainMonitor_Watch_watch_channel(this_arg: *const c_void, mut funding_outpoint: crate::lightning::chain::transaction::OutPoint, mut monitor: crate::lightning::chain::channelmonitor::ChannelMonitor) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ {
536         let mut ret = <nativeChainMonitor as lightning::chain::Watch<_>>::watch_channel(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, *unsafe { Box::from_raw(funding_outpoint.take_inner()) }, *unsafe { Box::from_raw(monitor.take_inner()) });
537         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::chain::ChannelMonitorUpdateErr::native_into(e) }).into() };
538         local_ret
539 }
540 #[must_use]
541 extern "C" fn ChainMonitor_Watch_update_channel(this_arg: *const c_void, mut funding_txo: crate::lightning::chain::transaction::OutPoint, mut update: crate::lightning::chain::channelmonitor::ChannelMonitorUpdate) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ {
542         let mut ret = <nativeChainMonitor as lightning::chain::Watch<_>>::update_channel(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, *unsafe { Box::from_raw(funding_txo.take_inner()) }, *unsafe { Box::from_raw(update.take_inner()) });
543         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::chain::ChannelMonitorUpdateErr::native_into(e) }).into() };
544         local_ret
545 }
546 #[must_use]
547 extern "C" fn ChainMonitor_Watch_release_pending_monitor_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MonitorEventZ {
548         let mut ret = <nativeChainMonitor as lightning::chain::Watch<_>>::release_pending_monitor_events(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, );
549         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::chain::channelmonitor::MonitorEvent::native_into(item) }); };
550         local_ret.into()
551 }
552
553 impl From<nativeChainMonitor> for crate::lightning::util::events::EventsProvider {
554         fn from(obj: nativeChainMonitor) -> Self {
555                 let mut rust_obj = ChainMonitor { inner: ObjOps::heap_alloc(obj), is_owned: true };
556                 let mut ret = ChainMonitor_as_EventsProvider(&rust_obj);
557                 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
558                 rust_obj.inner = core::ptr::null_mut();
559                 ret.free = Some(ChainMonitor_free_void);
560                 ret
561         }
562 }
563 /// Constructs a new EventsProvider which calls the relevant methods on this_arg.
564 /// This copies the `inner` pointer in this_arg and thus the returned EventsProvider must be freed before this_arg is
565 #[no_mangle]
566 pub extern "C" fn ChainMonitor_as_EventsProvider(this_arg: &ChainMonitor) -> crate::lightning::util::events::EventsProvider {
567         crate::lightning::util::events::EventsProvider {
568                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
569                 free: None,
570                 process_pending_events: ChainMonitor_EventsProvider_process_pending_events,
571         }
572 }
573
574 extern "C" fn ChainMonitor_EventsProvider_process_pending_events(this_arg: *const c_void, mut handler: crate::lightning::util::events::EventHandler) {
575         <nativeChainMonitor as lightning::util::events::EventsProvider<>>::process_pending_events(unsafe { &mut *(this_arg as *mut nativeChainMonitor) }, handler)
576 }
577