X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning_persister.rs;h=cd112e9c488cf3ad608750bcd897125d9a204eef;hb=c4fa20597f2d4e41dad125ef009283e1a8e66d68;hp=a93e915c80047080572143589b080486ee0113a1;hpb=4ed52804d3cd73f05bba6a705528d0d43495aa88;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning_persister.rs b/lightning-c-bindings/src/lightning_persister.rs index a93e915..cd112e9 100644 --- a/lightning-c-bindings/src/lightning_persister.rs +++ b/lightning-c-bindings/src/lightning_persister.rs @@ -8,24 +8,28 @@ //! Utilities that handle persisting Rust-Lightning data to disk via standard filesystem APIs. -use std::str::FromStr; -use std::ffi::c_void; +use alloc::str::FromStr; +use core::ffi::c_void; use core::convert::Infallible; use bitcoin::hashes::Hash; use crate::c_types::*; +#[cfg(feature="no-std")] +use alloc::{vec::Vec, boxed::Box}; mod util { -use std::str::FromStr; -use std::ffi::c_void; +use alloc::str::FromStr; +use core::ffi::c_void; use core::convert::Infallible; use bitcoin::hashes::Hash; use crate::c_types::*; +#[cfg(feature="no-std")] +use alloc::{vec::Vec, boxed::Box}; } use lightning_persister::FilesystemPersister as nativeFilesystemPersisterImport; -type nativeFilesystemPersister = nativeFilesystemPersisterImport; +pub(crate) type nativeFilesystemPersister = nativeFilesystemPersisterImport; /// FilesystemPersister persists channel data on disk, where each channel's /// data is stored in a file named after its funding outpoint. @@ -66,7 +70,7 @@ impl Drop for FilesystemPersister { pub extern "C" fn FilesystemPersister_free(this_obj: FilesystemPersister) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn FilesystemPersister_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn FilesystemPersister_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeFilesystemPersister); } } #[allow(unused)] @@ -81,7 +85,7 @@ impl FilesystemPersister { pub(crate) fn take_inner(mut self) -> *mut nativeFilesystemPersister { assert!(self.is_owned); let ret = ObjOps::untweak_ptr(self.inner); - self.inner = std::ptr::null_mut(); + self.inner = core::ptr::null_mut(); ret } } @@ -112,6 +116,16 @@ pub extern "C" fn FilesystemPersister_persist_manager(mut data_dir: crate::c_typ local_ret } +/// Write the provided `NetworkGraph` to the path provided at `FilesystemPersister` +/// initialization, within a file called \"network_graph\" +#[must_use] +#[no_mangle] +pub extern "C" fn FilesystemPersister_persist_network_graph(mut data_dir: crate::c_types::Str, network_graph: &crate::lightning::routing::network_graph::NetworkGraph) -> crate::c_types::derived::CResult_NoneErrorZ { + let mut ret = lightning_persister::FilesystemPersister::persist_network_graph(data_dir.into_string(), network_graph.get_native_ref()); + 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() }; + local_ret +} + /// Read `ChannelMonitor`s from disk. #[must_use] #[no_mangle] @@ -121,12 +135,12 @@ pub extern "C" fn FilesystemPersister_read_channelmonitors(this_arg: &Filesystem local_ret } -impl From for crate::lightning::chain::channelmonitor::Persist { +impl From for crate::lightning::chain::chainmonitor::Persist { fn from(obj: nativeFilesystemPersister) -> Self { let mut rust_obj = FilesystemPersister { inner: ObjOps::heap_alloc(obj), is_owned: true }; let mut ret = FilesystemPersister_as_Persist(&rust_obj); // 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 - rust_obj.inner = std::ptr::null_mut(); + rust_obj.inner = core::ptr::null_mut(); ret.free = Some(FilesystemPersister_free_void); ret } @@ -134,8 +148,8 @@ impl From for crate::lightning::chain::channelmonitor /// Constructs a new Persist which calls the relevant methods on this_arg. /// This copies the `inner` pointer in this_arg and thus the returned Persist must be freed before this_arg is #[no_mangle] -pub extern "C" fn FilesystemPersister_as_Persist(this_arg: &FilesystemPersister) -> crate::lightning::chain::channelmonitor::Persist { - crate::lightning::chain::channelmonitor::Persist { +pub extern "C" fn FilesystemPersister_as_Persist(this_arg: &FilesystemPersister) -> crate::lightning::chain::chainmonitor::Persist { + crate::lightning::chain::chainmonitor::Persist { this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }, free: None, persist_new_channel: FilesystemPersister_Persist_persist_new_channel, @@ -144,15 +158,16 @@ pub extern "C" fn FilesystemPersister_as_Persist(this_arg: &FilesystemPersister) } #[must_use] -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 { - let mut ret = >::persist_new_channel(unsafe { &mut *(this_arg as *mut nativeFilesystemPersister) }, *unsafe { Box::from_raw(funding_txo.take_inner()) }, monitor.get_native_ref()); - 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() }; +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 { + let mut ret = >::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()) }); + 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() }; local_ret } #[must_use] -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 { - let mut ret = >::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()); - 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() }; +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 { + let mut local__update = if _update.inner.is_null() { None } else { Some((* { _update.get_native_ref() }).clone()) }; + let mut ret = >::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()) }); + 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() }; local_ret }