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 std::str::FromStr;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
19 use std::str::FromStr;
21 use core::convert::Infallible;
22 use bitcoin::hashes::Hash;
23 use crate::c_types::*;
27 use lightning_persister::FilesystemPersister as nativeFilesystemPersisterImport;
28 type nativeFilesystemPersister = nativeFilesystemPersisterImport;
30 /// FilesystemPersister persists channel data on disk, where each channel's
31 /// data is stored in a file named after its funding outpoint.
33 /// Warning: this module does the best it can with calls to persist data, but it
34 /// can only guarantee that the data is passed to the drive. It is up to the
35 /// drive manufacturers to do the actual persistence properly, which they often
36 /// don't (especially on consumer-grade hardware). Therefore, it is up to the
37 /// user to validate their entire storage stack, to ensure the writes are
39 /// Corollary: especially when dealing with larger amounts of money, it is best
40 /// practice to have multiple channel data backups and not rely only on one
41 /// FilesystemPersister.
44 pub struct FilesystemPersister {
45 /// A pointer to the opaque Rust object.
47 /// Nearly everywhere, inner must be non-null, however in places where
48 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
49 pub inner: *mut nativeFilesystemPersister,
50 /// Indicates that this is the only struct which contains the same pointer.
52 /// Rust functions which take ownership of an object provided via an argument require
53 /// this to be true and invalidate the object pointed to by inner.
57 impl Drop for FilesystemPersister {
59 if self.is_owned && !<*mut nativeFilesystemPersister>::is_null(self.inner) {
60 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
64 /// Frees any resources used by the FilesystemPersister, if is_owned is set and inner is non-NULL.
66 pub extern "C" fn FilesystemPersister_free(this_obj: FilesystemPersister) { }
68 /// Used only if an object of this type is returned as a trait impl by a method
69 extern "C" fn FilesystemPersister_free_void(this_ptr: *mut c_void) {
70 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeFilesystemPersister); }
73 impl FilesystemPersister {
74 pub(crate) fn get_native_ref(&self) -> &'static nativeFilesystemPersister {
75 unsafe { &*ObjOps::untweak_ptr(self.inner) }
77 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFilesystemPersister {
78 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
80 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
81 pub(crate) fn take_inner(mut self) -> *mut nativeFilesystemPersister {
82 assert!(self.is_owned);
83 let ret = ObjOps::untweak_ptr(self.inner);
84 self.inner = std::ptr::null_mut();
88 /// Initialize a new FilesystemPersister and set the path to the individual channels'
92 pub extern "C" fn FilesystemPersister_new(mut path_to_channel_data: crate::c_types::Str) -> FilesystemPersister {
93 let mut ret = lightning_persister::FilesystemPersister::new(path_to_channel_data.into_string());
94 FilesystemPersister { inner: ObjOps::heap_alloc(ret), is_owned: true }
97 /// Get the directory which was provided when this persister was initialized.
100 pub extern "C" fn FilesystemPersister_get_data_dir(this_arg: &FilesystemPersister) -> crate::c_types::Str {
101 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_data_dir();
105 /// Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
106 /// initialization, within a file called \"manager\".
109 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 {
110 let mut ret = lightning_persister::FilesystemPersister::persist_manager(data_dir.into_string(), manager.get_native_ref());
111 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() };
115 /// Read `ChannelMonitor`s from disk.
118 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 {
119 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.read_channelmonitors(keys_manager);
120 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() };
124 impl From<nativeFilesystemPersister> for crate::lightning::chain::channelmonitor::Persist {
125 fn from(obj: nativeFilesystemPersister) -> Self {
126 let mut rust_obj = FilesystemPersister { inner: ObjOps::heap_alloc(obj), is_owned: true };
127 let mut ret = FilesystemPersister_as_Persist(&rust_obj);
128 // 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
129 rust_obj.inner = std::ptr::null_mut();
130 ret.free = Some(FilesystemPersister_free_void);
134 /// Constructs a new Persist which calls the relevant methods on this_arg.
135 /// This copies the `inner` pointer in this_arg and thus the returned Persist must be freed before this_arg is
137 pub extern "C" fn FilesystemPersister_as_Persist(this_arg: &FilesystemPersister) -> crate::lightning::chain::channelmonitor::Persist {
138 crate::lightning::chain::channelmonitor::Persist {
139 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
141 persist_new_channel: FilesystemPersister_Persist_persist_new_channel,
142 update_persisted_channel: FilesystemPersister_Persist_update_persisted_channel,
147 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 {
148 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());
149 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() };
153 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 {
154 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());
155 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() };