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