1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Utilities that handle persisting Rust-Lightning data to disk via standard filesystem APIs.
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};
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};
31 use lightning_persister::FilesystemPersister as nativeFilesystemPersisterImport;
32 pub(crate) type nativeFilesystemPersister = nativeFilesystemPersisterImport;
34 /// FilesystemPersister persists channel data on disk, where each channel's
35 /// data is stored in a file named after its funding outpoint.
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
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.
48 pub struct FilesystemPersister {
49 /// A pointer to the opaque Rust object.
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.
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.
61 impl Drop for FilesystemPersister {
63 if self.is_owned && !<*mut nativeFilesystemPersister>::is_null(self.inner) {
64 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
68 /// Frees any resources used by the FilesystemPersister, if is_owned is set and inner is non-NULL.
70 pub extern "C" fn FilesystemPersister_free(this_obj: FilesystemPersister) { }
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); }
77 impl FilesystemPersister {
78 pub(crate) fn get_native_ref(&self) -> &'static nativeFilesystemPersister {
79 unsafe { &*ObjOps::untweak_ptr(self.inner) }
81 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFilesystemPersister {
82 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
92 /// Initialize a new FilesystemPersister and set the path to the individual channels'
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 }
101 /// Get the directory which was provided when this persister was initialized.
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();
109 /// Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
110 /// initialization, within a file called \"manager\".
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() };
119 /// Read `ChannelMonitor`s from disk.
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() };
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);
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
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 },
145 persist_new_channel: FilesystemPersister_Persist_persist_new_channel,
146 update_persisted_channel: FilesystemPersister_Persist_update_persisted_channel,
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() };
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() };