Skip to content

Commit 34e429f

Browse files
committed
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.
1 parent f223d2a commit 34e429f

File tree

1 file changed

+44
-12
lines changed
  • background-processor/src

1 file changed

+44
-12
lines changed

background-processor/src/lib.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ const FRESHNESS_TIMER: u64 = 60;
4848
#[cfg(test)]
4949
const FRESHNESS_TIMER: u64 = 1;
5050

51+
/// Trait which handles persisting a [`ChannelManager`] to disk.
52+
///
53+
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
54+
pub trait ChannelManagerPersister<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
55+
where
56+
M::Target: 'static + chain::Watch<Signer>,
57+
T::Target: 'static + BroadcasterInterface,
58+
K::Target: 'static + KeysInterface<Signer = Signer>,
59+
F::Target: 'static + FeeEstimator,
60+
L::Target: 'static + Logger,
61+
{
62+
/// Persist the given [`ChannelManager`] to disk, returning an error if persistence failed
63+
/// (which will cause the [`BackgroundProcessor`] which called this method to exit.
64+
///
65+
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
66+
fn persist_manager(&self, channel_manager: &ChannelManager<Signer, M, T, K, F, L>) -> Result<(), std::io::Error>;
67+
}
68+
69+
impl<Fun, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
70+
ChannelManagerPersister<Signer, M, T, K, F, L> for Fun where
71+
M::Target: 'static + chain::Watch<Signer>,
72+
T::Target: 'static + BroadcasterInterface,
73+
K::Target: 'static + KeysInterface<Signer = Signer>,
74+
F::Target: 'static + FeeEstimator,
75+
L::Target: 'static + Logger,
76+
Fun: Fn(&ChannelManager<Signer, M, T, K, F, L>) -> Result<(), std::io::Error>,
77+
{
78+
fn persist_manager(&self, channel_manager: &ChannelManager<Signer, M, T, K, F, L>) -> Result<(), std::io::Error> {
79+
self(channel_manager)
80+
}
81+
}
82+
5183
impl BackgroundProcessor {
5284
/// Start a background thread that takes care of responsibilities enumerated in the top-level
5385
/// documentation.
@@ -68,28 +100,28 @@ impl BackgroundProcessor {
68100
/// [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
69101
/// [`FilesystemPersister::persist_manager`]: lightning_persister::FilesystemPersister::persist_manager
70102
pub fn start<
71-
PM, Signer,
103+
Signer: 'static + Sign,
72104
M: 'static + Deref + Send + Sync,
73105
T: 'static + Deref + Send + Sync,
74106
K: 'static + Deref + Send + Sync,
75107
F: 'static + Deref + Send + Sync,
76108
L: 'static + Deref + Send + Sync,
77109
Descriptor: 'static + SocketDescriptor + Send + Sync,
78-
CM: 'static + Deref + Send + Sync,
79-
RM: 'static + Deref + Send + Sync
80-
>(
81-
persist_channel_manager: PM, channel_manager: Arc<ChannelManager<Signer, M, T, K, F, L>>,
82-
peer_manager: Arc<PeerManager<Descriptor, CM, RM, L>>, logger: L,
83-
) -> Self where
84-
Signer: 'static + Sign,
110+
CMH: 'static + Deref + Send + Sync,
111+
RMH: 'static + Deref + Send + Sync,
112+
CMP: 'static + Send + ChannelManagerPersister<Signer, M, T, K, F, L>,
113+
CM: 'static + Deref<Target = ChannelManager<Signer, M, T, K, F, L>> + Send + Sync,
114+
PM: 'static + Deref<Target = PeerManager<Descriptor, CMH, RMH, L>> + Send + Sync,
115+
>
116+
(handler: CMP, channel_manager: CM, peer_manager: PM, logger: L) -> Self
117+
where
85118
M::Target: 'static + chain::Watch<Signer>,
86119
T::Target: 'static + BroadcasterInterface,
87120
K::Target: 'static + KeysInterface<Signer = Signer>,
88121
F::Target: 'static + FeeEstimator,
89122
L::Target: 'static + Logger,
90-
CM::Target: 'static + ChannelMessageHandler,
91-
RM::Target: 'static + RoutingMessageHandler,
92-
PM: 'static + Send + Fn(&ChannelManager<Signer, M, T, K, F, L>) -> Result<(), std::io::Error>,
123+
CMH::Target: 'static + ChannelMessageHandler,
124+
RMH::Target: 'static + RoutingMessageHandler,
93125
{
94126
let stop_thread = Arc::new(AtomicBool::new(false));
95127
let stop_thread_clone = stop_thread.clone();
@@ -100,7 +132,7 @@ impl BackgroundProcessor {
100132
let updates_available =
101133
channel_manager.await_persistable_update_timeout(Duration::from_millis(100));
102134
if updates_available {
103-
persist_channel_manager(&*channel_manager)?;
135+
handler.persist_manager(&*channel_manager)?;
104136
}
105137
// Exit the loop if the background processor was requested to stop.
106138
if stop_thread.load(Ordering::Acquire) == true {

0 commit comments

Comments
 (0)