cd6507acffb8634b9d4d587f9c5e533ece41fc44
[rust-lightning] / lightning-c-bindings / src / ln / channelmonitor.rs
1 //! The logic to monitor for on-chain transactions and create the relevant claim responses lives
2 //! here.
3 //!
4 //! ChannelMonitor objects are generated by ChannelManager in response to relevant
5 //! messages/actions, and MUST be persisted to disk (and, preferably, remotely) before progress can
6 //! be made in responding to certain messages, see ManyChannelMonitor for more.
7 //!
8 //! Note that ChannelMonitors are an important part of the lightning trust model and a copy of the
9 //! latest ChannelMonitor must always be actively monitoring for chain updates (and no out-of-date
10 //! ChannelMonitors should do so). Thus, if you're building rust-lightning into an HSM or other
11 //! security-domain-separated system design, you should consider having multiple paths for
12 //! ChannelMonitors to get out of the HSM and onto monitoring devices.
13
14 use std::ffi::c_void;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17
18
19 use lightning::ln::channelmonitor::ChannelMonitorUpdate as nativeChannelMonitorUpdateImport;
20 type nativeChannelMonitorUpdate = nativeChannelMonitorUpdateImport;
21
22 /// An update generated by the underlying Channel itself which contains some new information the
23 /// ChannelMonitor should be made aware of.
24 #[must_use]
25 #[repr(C)]
26 pub struct ChannelMonitorUpdate {
27         /// Nearly everyhwere, inner must be non-null, however in places where
28         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
29         pub inner: *mut nativeChannelMonitorUpdate,
30         pub is_owned: bool,
31 }
32
33 impl Drop for ChannelMonitorUpdate {
34         fn drop(&mut self) {
35                 if self.is_owned && !self.inner.is_null() {
36                         let _ = unsafe { Box::from_raw(self.inner) };
37                 }
38         }
39 }
40 #[no_mangle]
41 pub extern "C" fn ChannelMonitorUpdate_free(this_ptr: ChannelMonitorUpdate) { }
42 #[allow(unused)]
43 /// Used only if an object of this type is returned as a trait impl by a method
44 extern "C" fn ChannelMonitorUpdate_free_void(this_ptr: *mut c_void) {
45         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelMonitorUpdate); }
46 }
47 #[allow(unused)]
48 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
49 impl ChannelMonitorUpdate {
50         pub(crate) fn take_ptr(mut self) -> *mut nativeChannelMonitorUpdate {
51                 assert!(self.is_owned);
52                 let ret = self.inner;
53                 self.inner = std::ptr::null_mut();
54                 ret
55         }
56 }
57 impl Clone for ChannelMonitorUpdate {
58         fn clone(&self) -> Self {
59                 Self {
60                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
61                         is_owned: true,
62                 }
63         }
64 }
65 #[allow(unused)]
66 /// Used only if an object of this type is returned as a trait impl by a method
67 pub(crate) extern "C" fn ChannelMonitorUpdate_clone_void(this_ptr: *const c_void) -> *mut c_void {
68         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelMonitorUpdate)).clone() })) as *mut c_void
69 }
70 /// The sequence number of this update. Updates *must* be replayed in-order according to this
71 /// sequence number (and updates may panic if they are not). The update_id values are strictly
72 /// increasing and increase by one for each new update.
73 ///
74 /// This sequence number is also used to track up to which points updates which returned
75 /// ChannelMonitorUpdateErr::TemporaryFailure have been applied to all copies of a given
76 /// ChannelMonitor when ChannelManager::channel_monitor_updated is called.
77 #[no_mangle]
78 pub extern "C" fn ChannelMonitorUpdate_get_update_id(this_ptr: &ChannelMonitorUpdate) -> u64 {
79         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.update_id;
80         (*inner_val)
81 }
82 /// The sequence number of this update. Updates *must* be replayed in-order according to this
83 /// sequence number (and updates may panic if they are not). The update_id values are strictly
84 /// increasing and increase by one for each new update.
85 ///
86 /// This sequence number is also used to track up to which points updates which returned
87 /// ChannelMonitorUpdateErr::TemporaryFailure have been applied to all copies of a given
88 /// ChannelMonitor when ChannelManager::channel_monitor_updated is called.
89 #[no_mangle]
90 pub extern "C" fn ChannelMonitorUpdate_set_update_id(this_ptr: &mut ChannelMonitorUpdate, mut val: u64) {
91         unsafe { &mut *this_ptr.inner }.update_id = val;
92 }
93 #[no_mangle]
94 pub extern "C" fn ChannelMonitorUpdate_write(obj: *const ChannelMonitorUpdate) -> crate::c_types::derived::CVec_u8Z {
95         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
96 }
97 #[no_mangle]
98 pub extern "C" fn ChannelMonitorUpdate_read(ser: crate::c_types::u8slice) -> ChannelMonitorUpdate {
99         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
100                 ChannelMonitorUpdate { inner: Box::into_raw(Box::new(res)), is_owned: true }
101         } else {
102                 ChannelMonitorUpdate { inner: std::ptr::null_mut(), is_owned: true }
103         }
104 }
105 /// An error enum representing a failure to persist a channel monitor update.
106 #[must_use]
107 #[derive(Clone)]
108 #[repr(C)]
109 pub enum ChannelMonitorUpdateErr {
110         /// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
111         /// our state failed, but is expected to succeed at some point in the future).
112         ///
113         /// Such a failure will \"freeze\" a channel, preventing us from revoking old states or
114         /// submitting new commitment transactions to the remote party. Once the update(s) which failed
115         /// have been successfully applied, ChannelManager::channel_monitor_updated can be used to
116         /// restore the channel to an operational state.
117         ///
118         /// Note that a given ChannelManager will *never* re-generate a given ChannelMonitorUpdate. If
119         /// you return a TemporaryFailure you must ensure that it is written to disk safely before
120         /// writing out the latest ChannelManager state.
121         ///
122         /// Even when a channel has been \"frozen\" updates to the ChannelMonitor can continue to occur
123         /// (eg if an inbound HTLC which we forwarded was claimed upstream resulting in us attempting
124         /// to claim it on this channel) and those updates must be applied wherever they can be. At
125         /// least one such updated ChannelMonitor must be persisted otherwise PermanentFailure should
126         /// be returned to get things on-chain ASAP using only the in-memory copy. Obviously updates to
127         /// the channel which would invalidate previous ChannelMonitors are not made when a channel has
128         /// been \"frozen\".
129         ///
130         /// Note that even if updates made after TemporaryFailure succeed you must still call
131         /// channel_monitor_updated to ensure you have the latest monitor and re-enable normal channel
132         /// operation.
133         ///
134         /// Note that the update being processed here will not be replayed for you when you call
135         /// ChannelManager::channel_monitor_updated, so you must store the update itself along
136         /// with the persisted ChannelMonitor on your own local disk prior to returning a
137         /// TemporaryFailure. You may, of course, employ a journaling approach, storing only the
138         /// ChannelMonitorUpdate on disk without updating the monitor itself, replaying the journal at
139         /// reload-time.
140         ///
141         /// For deployments where a copy of ChannelMonitors and other local state are backed up in a
142         /// remote location (with local copies persisted immediately), it is anticipated that all
143         /// updates will return TemporaryFailure until the remote copies could be updated.
144         TemporaryFailure,
145         /// Used to indicate no further channel monitor updates will be allowed (eg we've moved on to a
146         /// different watchtower and cannot update with all watchtowers that were previously informed
147         /// of this channel). This will force-close the channel in question (which will generate one
148         /// final ChannelMonitorUpdate which must be delivered to at least one ChannelMonitor copy).
149         ///
150         /// Should also be used to indicate a failure to update the local persisted copy of the channel
151         /// monitor.
152         PermanentFailure,
153 }
154 use lightning::ln::channelmonitor::ChannelMonitorUpdateErr as nativeChannelMonitorUpdateErr;
155 impl ChannelMonitorUpdateErr {
156         #[allow(unused)]
157         pub(crate) fn to_native(&self) -> nativeChannelMonitorUpdateErr {
158                 match self {
159                         ChannelMonitorUpdateErr::TemporaryFailure => nativeChannelMonitorUpdateErr::TemporaryFailure,
160                         ChannelMonitorUpdateErr::PermanentFailure => nativeChannelMonitorUpdateErr::PermanentFailure,
161                 }
162         }
163         #[allow(unused)]
164         pub(crate) fn into_native(self) -> nativeChannelMonitorUpdateErr {
165                 match self {
166                         ChannelMonitorUpdateErr::TemporaryFailure => nativeChannelMonitorUpdateErr::TemporaryFailure,
167                         ChannelMonitorUpdateErr::PermanentFailure => nativeChannelMonitorUpdateErr::PermanentFailure,
168                 }
169         }
170         #[allow(unused)]
171         pub(crate) fn from_native(native: &nativeChannelMonitorUpdateErr) -> Self {
172                 match native {
173                         nativeChannelMonitorUpdateErr::TemporaryFailure => ChannelMonitorUpdateErr::TemporaryFailure,
174                         nativeChannelMonitorUpdateErr::PermanentFailure => ChannelMonitorUpdateErr::PermanentFailure,
175                 }
176         }
177         #[allow(unused)]
178         pub(crate) fn native_into(native: nativeChannelMonitorUpdateErr) -> Self {
179                 match native {
180                         nativeChannelMonitorUpdateErr::TemporaryFailure => ChannelMonitorUpdateErr::TemporaryFailure,
181                         nativeChannelMonitorUpdateErr::PermanentFailure => ChannelMonitorUpdateErr::PermanentFailure,
182                 }
183         }
184 }
185
186 use lightning::ln::channelmonitor::MonitorUpdateError as nativeMonitorUpdateErrorImport;
187 type nativeMonitorUpdateError = nativeMonitorUpdateErrorImport;
188
189 /// General Err type for ChannelMonitor actions. Generally, this implies that the data provided is
190 /// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::update_monitor this
191 /// means you tried to update a monitor for a different channel or the ChannelMonitorUpdate was
192 /// corrupted.
193 /// Contains a human-readable error message.
194 #[must_use]
195 #[repr(C)]
196 pub struct MonitorUpdateError {
197         /// Nearly everyhwere, inner must be non-null, however in places where
198         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
199         pub inner: *mut nativeMonitorUpdateError,
200         pub is_owned: bool,
201 }
202
203 impl Drop for MonitorUpdateError {
204         fn drop(&mut self) {
205                 if self.is_owned && !self.inner.is_null() {
206                         let _ = unsafe { Box::from_raw(self.inner) };
207                 }
208         }
209 }
210 #[no_mangle]
211 pub extern "C" fn MonitorUpdateError_free(this_ptr: MonitorUpdateError) { }
212 #[allow(unused)]
213 /// Used only if an object of this type is returned as a trait impl by a method
214 extern "C" fn MonitorUpdateError_free_void(this_ptr: *mut c_void) {
215         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMonitorUpdateError); }
216 }
217 #[allow(unused)]
218 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
219 impl MonitorUpdateError {
220         pub(crate) fn take_ptr(mut self) -> *mut nativeMonitorUpdateError {
221                 assert!(self.is_owned);
222                 let ret = self.inner;
223                 self.inner = std::ptr::null_mut();
224                 ret
225         }
226 }
227
228 use lightning::ln::channelmonitor::MonitorEvent as nativeMonitorEventImport;
229 type nativeMonitorEvent = nativeMonitorEventImport;
230
231 /// An event to be processed by the ChannelManager.
232 #[must_use]
233 #[repr(C)]
234 pub struct MonitorEvent {
235         /// Nearly everyhwere, inner must be non-null, however in places where
236         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
237         pub inner: *mut nativeMonitorEvent,
238         pub is_owned: bool,
239 }
240
241 impl Drop for MonitorEvent {
242         fn drop(&mut self) {
243                 if self.is_owned && !self.inner.is_null() {
244                         let _ = unsafe { Box::from_raw(self.inner) };
245                 }
246         }
247 }
248 #[no_mangle]
249 pub extern "C" fn MonitorEvent_free(this_ptr: MonitorEvent) { }
250 #[allow(unused)]
251 /// Used only if an object of this type is returned as a trait impl by a method
252 extern "C" fn MonitorEvent_free_void(this_ptr: *mut c_void) {
253         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMonitorEvent); }
254 }
255 #[allow(unused)]
256 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
257 impl MonitorEvent {
258         pub(crate) fn take_ptr(mut self) -> *mut nativeMonitorEvent {
259                 assert!(self.is_owned);
260                 let ret = self.inner;
261                 self.inner = std::ptr::null_mut();
262                 ret
263         }
264 }
265
266 use lightning::ln::channelmonitor::HTLCUpdate as nativeHTLCUpdateImport;
267 type nativeHTLCUpdate = nativeHTLCUpdateImport;
268
269 /// Simple structure send back by ManyChannelMonitor in case of HTLC detected onchain from a
270 /// forward channel and from which info are needed to update HTLC in a backward channel.
271 #[must_use]
272 #[repr(C)]
273 pub struct HTLCUpdate {
274         /// Nearly everyhwere, inner must be non-null, however in places where
275         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
276         pub inner: *mut nativeHTLCUpdate,
277         pub is_owned: bool,
278 }
279
280 impl Drop for HTLCUpdate {
281         fn drop(&mut self) {
282                 if self.is_owned && !self.inner.is_null() {
283                         let _ = unsafe { Box::from_raw(self.inner) };
284                 }
285         }
286 }
287 #[no_mangle]
288 pub extern "C" fn HTLCUpdate_free(this_ptr: HTLCUpdate) { }
289 #[allow(unused)]
290 /// Used only if an object of this type is returned as a trait impl by a method
291 extern "C" fn HTLCUpdate_free_void(this_ptr: *mut c_void) {
292         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHTLCUpdate); }
293 }
294 #[allow(unused)]
295 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
296 impl HTLCUpdate {
297         pub(crate) fn take_ptr(mut self) -> *mut nativeHTLCUpdate {
298                 assert!(self.is_owned);
299                 let ret = self.inner;
300                 self.inner = std::ptr::null_mut();
301                 ret
302         }
303 }
304 impl Clone for HTLCUpdate {
305         fn clone(&self) -> Self {
306                 Self {
307                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
308                         is_owned: true,
309                 }
310         }
311 }
312 #[allow(unused)]
313 /// Used only if an object of this type is returned as a trait impl by a method
314 pub(crate) extern "C" fn HTLCUpdate_clone_void(this_ptr: *const c_void) -> *mut c_void {
315         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHTLCUpdate)).clone() })) as *mut c_void
316 }
317 #[no_mangle]
318 pub extern "C" fn HTLCUpdate_write(obj: *const HTLCUpdate) -> crate::c_types::derived::CVec_u8Z {
319         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
320 }
321 #[no_mangle]
322 pub extern "C" fn HTLCUpdate_read(ser: crate::c_types::u8slice) -> HTLCUpdate {
323         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
324                 HTLCUpdate { inner: Box::into_raw(Box::new(res)), is_owned: true }
325         } else {
326                 HTLCUpdate { inner: std::ptr::null_mut(), is_owned: true }
327         }
328 }
329
330 use lightning::ln::channelmonitor::ChannelMonitor as nativeChannelMonitorImport;
331 type nativeChannelMonitor = nativeChannelMonitorImport<crate::chain::keysinterface::ChannelKeys>;
332
333 /// A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
334 /// on-chain transactions to ensure no loss of funds occurs.
335 ///
336 /// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
337 /// information and are actively monitoring the chain.
338 ///
339 /// Pending Events or updated HTLCs which have not yet been read out by
340 /// get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
341 /// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
342 /// gotten are fully handled before re-serializing the new state.
343 #[must_use]
344 #[repr(C)]
345 pub struct ChannelMonitor {
346         /// Nearly everyhwere, inner must be non-null, however in places where
347         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
348         pub inner: *mut nativeChannelMonitor,
349         pub is_owned: bool,
350 }
351
352 impl Drop for ChannelMonitor {
353         fn drop(&mut self) {
354                 if self.is_owned && !self.inner.is_null() {
355                         let _ = unsafe { Box::from_raw(self.inner) };
356                 }
357         }
358 }
359 #[no_mangle]
360 pub extern "C" fn ChannelMonitor_free(this_ptr: ChannelMonitor) { }
361 #[allow(unused)]
362 /// Used only if an object of this type is returned as a trait impl by a method
363 extern "C" fn ChannelMonitor_free_void(this_ptr: *mut c_void) {
364         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelMonitor); }
365 }
366 #[allow(unused)]
367 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
368 impl ChannelMonitor {
369         pub(crate) fn take_ptr(mut self) -> *mut nativeChannelMonitor {
370                 assert!(self.is_owned);
371                 let ret = self.inner;
372                 self.inner = std::ptr::null_mut();
373                 ret
374         }
375 }
376 /// Simple trait indicating ability to track a set of ChannelMonitors and multiplex events between
377 /// them. Generally should be implemented by keeping a local SimpleManyChannelMonitor and passing
378 /// events to it, while also taking any add/update_monitor events and passing them to some remote
379 /// server(s).
380 ///
381 /// In general, you must always have at least one local copy in memory, which must never fail to
382 /// update (as it is responsible for broadcasting the latest state in case the channel is closed),
383 /// and then persist it to various on-disk locations. If, for some reason, the in-memory copy fails
384 /// to update (eg out-of-memory or some other condition), you must immediately shut down without
385 /// taking any further action such as writing the current state to disk. This should likely be
386 /// accomplished via panic!() or abort().
387 ///
388 /// Note that any updates to a channel's monitor *must* be applied to each instance of the
389 /// channel's monitor everywhere (including remote watchtowers) *before* this function returns. If
390 /// an update occurs and a remote watchtower is left with old state, it may broadcast transactions
391 /// which we have revoked, allowing our counterparty to claim all funds in the channel!
392 ///
393 /// User needs to notify implementors of ManyChannelMonitor when a new block is connected or
394 /// disconnected using their `block_connected` and `block_disconnected` methods. However, rather
395 /// than calling these methods directly, the user should register implementors as listeners to the
396 /// BlockNotifier and call the BlockNotifier's `block_(dis)connected` methods, which will notify
397 /// all registered listeners in one go.
398 #[repr(C)]
399 pub struct ManyChannelMonitor {
400         pub this_arg: *mut c_void,
401         /// Adds a monitor for the given `funding_txo`.
402         ///
403         /// Implementer must also ensure that the funding_txo txid *and* outpoint are registered with
404         /// any relevant ChainWatchInterfaces such that the provided monitor receives block_connected
405         /// callbacks with the funding transaction, or any spends of it.
406         ///
407         /// Further, the implementer must also ensure that each output returned in
408         /// monitor.get_outputs_to_watch() is registered to ensure that the provided monitor learns about
409         /// any spends of any of the outputs.
410         ///
411         /// Any spends of outputs which should have been registered which aren't passed to
412         /// ChannelMonitors via block_connected may result in FUNDS LOSS.
413         #[must_use]
414         pub add_monitor: extern "C" fn (this_arg: *const c_void, funding_txo: crate::chain::transaction::OutPoint, monitor: crate::ln::channelmonitor::ChannelMonitor) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
415         /// Updates a monitor for the given `funding_txo`.
416         ///
417         /// Implementer must also ensure that the funding_txo txid *and* outpoint are registered with
418         /// any relevant ChainWatchInterfaces such that the provided monitor receives block_connected
419         /// callbacks with the funding transaction, or any spends of it.
420         ///
421         /// Further, the implementer must also ensure that each output returned in
422         /// monitor.get_watch_outputs() is registered to ensure that the provided monitor learns about
423         /// any spends of any of the outputs.
424         ///
425         /// Any spends of outputs which should have been registered which aren't passed to
426         /// ChannelMonitors via block_connected may result in FUNDS LOSS.
427         #[must_use]
428         pub update_monitor: extern "C" fn (this_arg: *const c_void, funding_txo: crate::chain::transaction::OutPoint, monitor: crate::ln::channelmonitor::ChannelMonitorUpdate) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
429         /// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
430         /// with success or failure.
431         ///
432         /// You should probably just call through to
433         /// ChannelMonitor::get_and_clear_pending_monitor_events() for each ChannelMonitor and return
434         /// the full list.
435         #[must_use]
436         pub get_and_clear_pending_monitor_events: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_MonitorEventZ,
437         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
438 }
439 unsafe impl Send for ManyChannelMonitor {}
440 unsafe impl Sync for ManyChannelMonitor {}
441
442 use lightning::ln::channelmonitor::ManyChannelMonitor as rustManyChannelMonitor;
443 impl rustManyChannelMonitor for ManyChannelMonitor {
444         type Keys = crate::chain::keysinterface::ChannelKeys;
445         fn add_monitor(&self, funding_txo: lightning::chain::transaction::OutPoint, monitor: lightning::ln::channelmonitor::ChannelMonitor<Self::Keys>) -> Result<(), lightning::ln::channelmonitor::ChannelMonitorUpdateErr> {
446                 let mut ret = (self.add_monitor)(self.this_arg, crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(funding_txo)), is_owned: true }, crate::ln::channelmonitor::ChannelMonitor { inner: Box::into_raw(Box::new(monitor)), is_owned: true });
447                 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(ret.contents.result.take_ptr()) })*/ }), false => Err( { (*unsafe { Box::from_raw(ret.contents.err.take_ptr()) }).into_native() })};
448                 local_ret
449         }
450         fn update_monitor(&self, funding_txo: lightning::chain::transaction::OutPoint, monitor: lightning::ln::channelmonitor::ChannelMonitorUpdate) -> Result<(), lightning::ln::channelmonitor::ChannelMonitorUpdateErr> {
451                 let mut ret = (self.update_monitor)(self.this_arg, crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(funding_txo)), is_owned: true }, crate::ln::channelmonitor::ChannelMonitorUpdate { inner: Box::into_raw(Box::new(monitor)), is_owned: true });
452                 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(ret.contents.result.take_ptr()) })*/ }), false => Err( { (*unsafe { Box::from_raw(ret.contents.err.take_ptr()) }).into_native() })};
453                 local_ret
454         }
455         fn get_and_clear_pending_monitor_events(&self) -> Vec<lightning::ln::channelmonitor::MonitorEvent> {
456                 let mut ret = (self.get_and_clear_pending_monitor_events)(self.this_arg);
457                 let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { *unsafe { Box::from_raw(item.take_ptr()) } }); };
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 ManyChannelMonitor {
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 ManyChannelMonitor_free(this_ptr: ManyChannelMonitor) { }
473 impl Drop for ManyChannelMonitor {
474         fn drop(&mut self) {
475                 if let Some(f) = self.free {
476                         f(self.this_arg);
477                 }
478         }
479 }
480 /// Updates a ChannelMonitor on the basis of some new information provided by the Channel
481 /// itself.
482 ///
483 /// panics if the given update is not the next update by update_id.
484 #[must_use]
485 #[no_mangle]
486 pub extern "C" fn ChannelMonitor_update_monitor(this_arg: &mut ChannelMonitor, mut updates: crate::ln::channelmonitor::ChannelMonitorUpdate, broadcaster: &crate::chain::chaininterface::BroadcasterInterface, logger: &crate::util::logger::Logger) -> crate::c_types::derived::CResult_NoneMonitorUpdateErrorZ {
487         let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeChannelMonitor)) }.update_monitor(*unsafe { Box::from_raw(updates.take_ptr()) }, broadcaster, logger);
488         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::channelmonitor::MonitorUpdateError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) };
489         local_ret
490 }
491
492 /// Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
493 /// ChannelMonitor.
494 #[must_use]
495 #[no_mangle]
496 pub extern "C" fn ChannelMonitor_get_latest_update_id(this_arg: &ChannelMonitor) -> u64 {
497         let mut ret = unsafe { &*this_arg.inner }.get_latest_update_id();
498         ret
499 }
500
501 /// Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
502 #[must_use]
503 #[no_mangle]
504 pub extern "C" fn ChannelMonitor_get_funding_txo(this_arg: &ChannelMonitor) -> crate::c_types::derived::C2Tuple_OutPointScriptZ {
505         let mut ret = unsafe { &*this_arg.inner }.get_funding_txo();
506         let (ref orig_ret_0, ref orig_ret_1) = ret; let mut local_ret = (crate::chain::transaction::OutPoint { inner: unsafe { ( (&(*orig_ret_0) as *const _) as *mut _) }, is_owned: false }, orig_ret_1.clone().into_bytes().into()).into();
507         local_ret
508 }
509
510 /// Get the list of HTLCs who's status has been updated on chain. This should be called by
511 /// ChannelManager via ManyChannelMonitor::get_and_clear_pending_monitor_events().
512 #[must_use]
513 #[no_mangle]
514 pub extern "C" fn ChannelMonitor_get_and_clear_pending_monitor_events(this_arg: &mut ChannelMonitor) -> crate::c_types::derived::CVec_MonitorEventZ {
515         let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeChannelMonitor)) }.get_and_clear_pending_monitor_events();
516         let mut local_ret = Vec::new(); for item in ret.drain(..) { local_ret.push( { crate::ln::channelmonitor::MonitorEvent { inner: Box::into_raw(Box::new(item)), is_owned: true } }); };
517         local_ret.into()
518 }
519
520 /// Gets the list of pending events which were generated by previous actions, clearing the list
521 /// in the process.
522 ///
523 /// This is called by ManyChannelMonitor::get_and_clear_pending_events() and is equivalent to
524 /// EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
525 /// no internal locking in ChannelMonitors.
526 #[must_use]
527 #[no_mangle]
528 pub extern "C" fn ChannelMonitor_get_and_clear_pending_events(this_arg: &mut ChannelMonitor) -> crate::c_types::derived::CVec_EventZ {
529         let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeChannelMonitor)) }.get_and_clear_pending_events();
530         let mut local_ret = Vec::new(); for item in ret.drain(..) { local_ret.push( { crate::util::events::Event::native_into(item) }); };
531         local_ret.into()
532 }
533
534 /// Used by ChannelManager deserialization to broadcast the latest local state if its copy of
535 /// the Channel was out-of-date. You may use it to get a broadcastable local toxic tx in case of
536 /// fallen-behind, i.e when receiving a channel_reestablish with a proof that our remote side knows
537 /// a higher revocation secret than the local commitment number we are aware of. Broadcasting these
538 /// transactions are UNSAFE, as they allow remote side to punish you. Nevertheless you may want to
539 /// broadcast them if remote don't close channel with his higher commitment transaction after a
540 /// substantial amount of time (a month or even a year) to get back funds. Best may be to contact
541 /// out-of-band the other node operator to coordinate with him if option is available to you.
542 /// In any-case, choice is up to the user.
543 #[must_use]
544 #[no_mangle]
545 pub extern "C" fn ChannelMonitor_get_latest_local_commitment_txn(this_arg: &mut ChannelMonitor, logger: &crate::util::logger::Logger) -> crate::c_types::derived::CVec_TransactionZ {
546         let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeChannelMonitor)) }.get_latest_local_commitment_txn(logger);
547         let mut local_ret = Vec::new(); for item in ret.drain(..) { local_ret.push( { let mut local_ret_0 = ::bitcoin::consensus::encode::serialize(&item); local_ret_0.into() }); };
548         local_ret.into()
549 }
550