Skip to content

Commit 2213f24

Browse files
authored
Merge pull request #884 from TheBlueMatt/2021-04-bp-bindings
Prep background-processor for bindings inclusion
2 parents feeb893 + 452b720 commit 2213f24

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ members = [
66
"lightning-invoice",
77
"lightning-net-tokio",
88
"lightning-persister",
9-
"background-processor",
9+
"lightning-background-processor",
1010
]
1111

1212
# Our tests do actual crypo and lots of work, the tradeoff for -O1 is well worth it.

background-processor/src/lib.rs renamed to lightning-background-processor/src/lib.rs

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
2020
use std::thread;
2121
use std::thread::JoinHandle;
2222
use std::time::{Duration, Instant};
23+
use std::ops::Deref;
2324

2425
/// BackgroundProcessor takes care of tasks that (1) need to happen periodically to keep
2526
/// Rust-Lightning running properly, and (2) either can or should be run in the background. Its
@@ -47,6 +48,38 @@ const FRESHNESS_TIMER: u64 = 60;
4748
#[cfg(test)]
4849
const FRESHNESS_TIMER: u64 = 1;
4950

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+
5083
impl BackgroundProcessor {
5184
/// Start a background thread that takes care of responsibilities enumerated in the top-level
5285
/// documentation.
@@ -66,25 +99,29 @@ impl BackgroundProcessor {
6699
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
67100
/// [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
68101
/// [`FilesystemPersister::persist_manager`]: lightning_persister::FilesystemPersister::persist_manager
69-
pub fn start<PM, Signer, M, T, K, F, L, Descriptor: 'static + SocketDescriptor + Send, CM, RM>(
70-
persist_channel_manager: PM,
71-
channel_manager: Arc<ChannelManager<Signer, Arc<M>, Arc<T>, Arc<K>, Arc<F>, Arc<L>>>,
72-
peer_manager: Arc<PeerManager<Descriptor, Arc<CM>, Arc<RM>, Arc<L>>>, logger: Arc<L>,
73-
) -> Self
74-
where
102+
pub fn start<
75103
Signer: 'static + Sign,
76-
M: 'static + chain::Watch<Signer>,
77-
T: 'static + BroadcasterInterface,
78-
K: 'static + KeysInterface<Signer = Signer>,
79-
F: 'static + FeeEstimator,
80-
L: 'static + Logger,
81-
CM: 'static + ChannelMessageHandler,
82-
RM: 'static + RoutingMessageHandler,
83-
PM: 'static
84-
+ Send
85-
+ Fn(
86-
&ChannelManager<Signer, Arc<M>, Arc<T>, Arc<K>, Arc<F>, Arc<L>>,
87-
) -> Result<(), std::io::Error>,
104+
M: 'static + Deref + Send + Sync,
105+
T: 'static + Deref + Send + Sync,
106+
K: 'static + Deref + Send + Sync,
107+
F: 'static + Deref + Send + Sync,
108+
L: 'static + Deref + Send + Sync,
109+
Descriptor: 'static + SocketDescriptor + Send + Sync,
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
118+
M::Target: 'static + chain::Watch<Signer>,
119+
T::Target: 'static + BroadcasterInterface,
120+
K::Target: 'static + KeysInterface<Signer = Signer>,
121+
F::Target: 'static + FeeEstimator,
122+
L::Target: 'static + Logger,
123+
CMH::Target: 'static + ChannelMessageHandler,
124+
RMH::Target: 'static + RoutingMessageHandler,
88125
{
89126
let stop_thread = Arc::new(AtomicBool::new(false));
90127
let stop_thread_clone = stop_thread.clone();
@@ -95,7 +132,7 @@ impl BackgroundProcessor {
95132
let updates_available =
96133
channel_manager.await_persistable_update_timeout(Duration::from_millis(100));
97134
if updates_available {
98-
persist_channel_manager(&*channel_manager)?;
135+
handler.persist_manager(&*channel_manager)?;
99136
}
100137
// Exit the loop if the background processor was requested to stop.
101138
if stop_thread.load(Ordering::Acquire) == true {

0 commit comments

Comments
 (0)