Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning_persister.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 //! Utilities that handle persisting Rust-Lightning data to disk via standard filesystem APIs.
10
11 use alloc::str::FromStr;
12 use core::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16 #[cfg(feature="no-std")]
17 use alloc::{vec::Vec, boxed::Box};
18
19 mod util {
20
21 use alloc::str::FromStr;
22 use core::ffi::c_void;
23 use core::convert::Infallible;
24 use bitcoin::hashes::Hash;
25 use crate::c_types::*;
26 #[cfg(feature="no-std")]
27 use alloc::{vec::Vec, boxed::Box};
28
29 }
30
31 use lightning_persister::FilesystemPersister as nativeFilesystemPersisterImport;
32 pub(crate) type nativeFilesystemPersister = nativeFilesystemPersisterImport;
33
34 /// FilesystemPersister persists channel data on disk, where each channel's
35 /// data is stored in a file named after its funding outpoint.
36 ///
37 /// Warning: this module does the best it can with calls to persist data, but it
38 /// can only guarantee that the data is passed to the drive. It is up to the
39 /// drive manufacturers to do the actual persistence properly, which they often
40 /// don't (especially on consumer-grade hardware). Therefore, it is up to the
41 /// user to validate their entire storage stack, to ensure the writes are
42 /// persistent.
43 /// Corollary: especially when dealing with larger amounts of money, it is best
44 /// practice to have multiple channel data backups and not rely only on one
45 /// FilesystemPersister.
46 #[must_use]
47 #[repr(C)]
48 pub struct FilesystemPersister {
49         /// A pointer to the opaque Rust object.
50
51         /// Nearly everywhere, inner must be non-null, however in places where
52         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
53         pub inner: *mut nativeFilesystemPersister,
54         /// Indicates that this is the only struct which contains the same pointer.
55
56         /// Rust functions which take ownership of an object provided via an argument require
57         /// this to be true and invalidate the object pointed to by inner.
58         pub is_owned: bool,
59 }
60
61 impl Drop for FilesystemPersister {
62         fn drop(&mut self) {
63                 if self.is_owned && !<*mut nativeFilesystemPersister>::is_null(self.inner) {
64                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
65                 }
66         }
67 }
68 /// Frees any resources used by the FilesystemPersister, if is_owned is set and inner is non-NULL.
69 #[no_mangle]
70 pub extern "C" fn FilesystemPersister_free(this_obj: FilesystemPersister) { }
71 #[allow(unused)]
72 /// Used only if an object of this type is returned as a trait impl by a method
73 pub(crate) extern "C" fn FilesystemPersister_free_void(this_ptr: *mut c_void) {
74         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeFilesystemPersister); }
75 }
76 #[allow(unused)]
77 impl FilesystemPersister {
78         pub(crate) fn get_native_ref(&self) -> &'static nativeFilesystemPersister {
79                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
80         }
81         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFilesystemPersister {
82                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
83         }
84         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
85         pub(crate) fn take_inner(mut self) -> *mut nativeFilesystemPersister {
86                 assert!(self.is_owned);
87                 let ret = ObjOps::untweak_ptr(self.inner);
88                 self.inner = core::ptr::null_mut();
89                 ret
90         }
91 }
92 /// Initialize a new FilesystemPersister and set the path to the individual channels'
93 /// files.
94 #[must_use]
95 #[no_mangle]
96 pub extern "C" fn FilesystemPersister_new(mut path_to_channel_data: crate::c_types::Str) -> FilesystemPersister {
97         let mut ret = lightning_persister::FilesystemPersister::new(path_to_channel_data.into_string());
98         FilesystemPersister { inner: ObjOps::heap_alloc(ret), is_owned: true }
99 }
100
101 /// Get the directory which was provided when this persister was initialized.
102 #[must_use]
103 #[no_mangle]
104 pub extern "C" fn FilesystemPersister_get_data_dir(this_arg: &FilesystemPersister) -> crate::c_types::Str {
105         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_data_dir();
106         ret.into()
107 }
108
109 /// Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
110 /// initialization, within a file called \"manager\".
111 #[must_use]
112 #[no_mangle]
113 pub extern "C" fn FilesystemPersister_persist_manager(mut data_dir: crate::c_types::Str, manager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_NoneErrorZ {
114         let mut ret = lightning_persister::FilesystemPersister::persist_manager(data_dir.into_string(), manager.get_native_ref());
115         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::c_types::IOError::from_rust(e) }).into() };
116         local_ret
117 }
118
119 /// Read `ChannelMonitor`s from disk.
120 #[must_use]
121 #[no_mangle]
122 pub extern "C" fn FilesystemPersister_read_channelmonitors(this_arg: &FilesystemPersister, mut keys_manager: crate::lightning::chain::keysinterface::KeysInterface) -> crate::c_types::derived::CResult_CVec_C2Tuple_BlockHashChannelMonitorZZErrorZ {
123         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.read_channelmonitors(keys_manager);
124         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { let (mut orig_ret_0_0_0, mut orig_ret_0_0_1) = item; let mut local_ret_0_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_0.into_inner() }, crate::lightning::chain::channelmonitor::ChannelMonitor { inner: ObjOps::heap_alloc(orig_ret_0_0_1), is_owned: true }).into(); local_ret_0_0 }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::IOError::from_rust(e) }).into() };
125         local_ret
126 }
127
128 impl From<nativeFilesystemPersister> for crate::lightning::chain::chainmonitor::Persist {
129         fn from(obj: nativeFilesystemPersister) -> Self {
130                 let mut rust_obj = FilesystemPersister { inner: ObjOps::heap_alloc(obj), is_owned: true };
131                 let mut ret = FilesystemPersister_as_Persist(&rust_obj);
132                 // 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
133                 rust_obj.inner = core::ptr::null_mut();
134                 ret.free = Some(FilesystemPersister_free_void);
135                 ret
136         }
137 }
138 /// Constructs a new Persist which calls the relevant methods on this_arg.
139 /// This copies the `inner` pointer in this_arg and thus the returned Persist must be freed before this_arg is
140 #[no_mangle]
141 pub extern "C" fn FilesystemPersister_as_Persist(this_arg: &FilesystemPersister) -> crate::lightning::chain::chainmonitor::Persist {
142         crate::lightning::chain::chainmonitor::Persist {
143                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
144                 free: None,
145                 persist_new_channel: FilesystemPersister_Persist_persist_new_channel,
146                 update_persisted_channel: FilesystemPersister_Persist_update_persisted_channel,
147         }
148 }
149
150 #[must_use]
151 extern "C" fn FilesystemPersister_Persist_persist_new_channel(this_arg: *const c_void, mut funding_txo: crate::lightning::chain::transaction::OutPoint, monitor: &crate::lightning::chain::channelmonitor::ChannelMonitor, mut _update_id: crate::lightning::chain::chainmonitor::MonitorUpdateId) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ {
152         let mut ret = <nativeFilesystemPersister as lightning::chain::chainmonitor::Persist<_>>::persist_new_channel(unsafe { &mut *(this_arg as *mut nativeFilesystemPersister) }, *unsafe { Box::from_raw(funding_txo.take_inner()) }, monitor.get_native_ref(), *unsafe { Box::from_raw(_update_id.take_inner()) });
153         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() };
154         local_ret
155 }
156 #[must_use]
157 extern "C" fn FilesystemPersister_Persist_update_persisted_channel(this_arg: *const c_void, mut funding_txo: crate::lightning::chain::transaction::OutPoint, _update: &crate::lightning::chain::channelmonitor::ChannelMonitorUpdate, monitor: &crate::lightning::chain::channelmonitor::ChannelMonitor, mut _update_id: crate::lightning::chain::chainmonitor::MonitorUpdateId) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ {
158         let mut local__update = if _update.inner.is_null() { None } else { Some((* { _update.get_native_ref() }).clone()) };
159         let mut ret = <nativeFilesystemPersister as lightning::chain::chainmonitor::Persist<_>>::update_persisted_channel(unsafe { &mut *(this_arg as *mut nativeFilesystemPersister) }, *unsafe { Box::from_raw(funding_txo.take_inner()) }, &local__update, monitor.get_native_ref(), *unsafe { Box::from_raw(_update_id.take_inner()) });
160         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() };
161         local_ret
162 }
163