From 34e429fdf60a426f54bfbf5cce001cb6960874f8 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 13 Apr 2021 19:38:31 -0400 Subject: [PATCH] Use a trait to handle ChannelManager persistence instead of an Fn This avoids having to write new support for closures in the C bindings generation but, more importantly, we really need to also be handling Events in the same trait, so it makes sense to go ahead and convert it. For compatibility, the trait is implemented for any matching closure. --- background-processor/src/lib.rs | 56 ++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/background-processor/src/lib.rs b/background-processor/src/lib.rs index 120305446..de9aa286a 100644 --- a/background-processor/src/lib.rs +++ b/background-processor/src/lib.rs @@ -48,6 +48,38 @@ const FRESHNESS_TIMER: u64 = 60; #[cfg(test)] const FRESHNESS_TIMER: u64 = 1; +/// Trait which handles persisting a [`ChannelManager`] to disk. +/// +/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager +pub trait ChannelManagerPersister +where + M::Target: 'static + chain::Watch, + T::Target: 'static + BroadcasterInterface, + K::Target: 'static + KeysInterface, + F::Target: 'static + FeeEstimator, + L::Target: 'static + Logger, +{ + /// Persist the given [`ChannelManager`] to disk, returning an error if persistence failed + /// (which will cause the [`BackgroundProcessor`] which called this method to exit. + /// + /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager + fn persist_manager(&self, channel_manager: &ChannelManager) -> Result<(), std::io::Error>; +} + +impl +ChannelManagerPersister for Fun where + M::Target: 'static + chain::Watch, + T::Target: 'static + BroadcasterInterface, + K::Target: 'static + KeysInterface, + F::Target: 'static + FeeEstimator, + L::Target: 'static + Logger, + Fun: Fn(&ChannelManager) -> Result<(), std::io::Error>, +{ + fn persist_manager(&self, channel_manager: &ChannelManager) -> Result<(), std::io::Error> { + self(channel_manager) + } +} + impl BackgroundProcessor { /// Start a background thread that takes care of responsibilities enumerated in the top-level /// documentation. @@ -68,28 +100,28 @@ impl BackgroundProcessor { /// [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable /// [`FilesystemPersister::persist_manager`]: lightning_persister::FilesystemPersister::persist_manager pub fn start< - PM, Signer, + Signer: 'static + Sign, M: 'static + Deref + Send + Sync, T: 'static + Deref + Send + Sync, K: 'static + Deref + Send + Sync, F: 'static + Deref + Send + Sync, L: 'static + Deref + Send + Sync, Descriptor: 'static + SocketDescriptor + Send + Sync, - CM: 'static + Deref + Send + Sync, - RM: 'static + Deref + Send + Sync - >( - persist_channel_manager: PM, channel_manager: Arc>, - peer_manager: Arc>, logger: L, - ) -> Self where - Signer: 'static + Sign, + CMH: 'static + Deref + Send + Sync, + RMH: 'static + Deref + Send + Sync, + CMP: 'static + Send + ChannelManagerPersister, + CM: 'static + Deref> + Send + Sync, + PM: 'static + Deref> + Send + Sync, + > + (handler: CMP, channel_manager: CM, peer_manager: PM, logger: L) -> Self + where M::Target: 'static + chain::Watch, T::Target: 'static + BroadcasterInterface, K::Target: 'static + KeysInterface, F::Target: 'static + FeeEstimator, L::Target: 'static + Logger, - CM::Target: 'static + ChannelMessageHandler, - RM::Target: 'static + RoutingMessageHandler, - PM: 'static + Send + Fn(&ChannelManager) -> Result<(), std::io::Error>, + CMH::Target: 'static + ChannelMessageHandler, + RMH::Target: 'static + RoutingMessageHandler, { let stop_thread = Arc::new(AtomicBool::new(false)); let stop_thread_clone = stop_thread.clone(); @@ -100,7 +132,7 @@ impl BackgroundProcessor { let updates_available = channel_manager.await_persistable_update_timeout(Duration::from_millis(100)); if updates_available { - persist_channel_manager(&*channel_manager)?; + handler.persist_manager(&*channel_manager)?; } // Exit the loop if the background processor was requested to stop. if stop_thread.load(Ordering::Acquire) == true { -- 2.39.5