Skip to content

Commit f9f3548

Browse files
f jeff tweaks
1 parent 3d8ed22 commit f9f3548

File tree

2 files changed

+39
-52
lines changed

2 files changed

+39
-52
lines changed

background-processor/src/lib.rs

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ const CHAN_FRESHNESS_TIMER: u64 = 60;
4444
const CHAN_FRESHNESS_TIMER: u64 = 1;
4545

4646
impl BackgroundProcessor {
47-
/// Start the background thread that takes care of responsibilities (enumerated in the top-level
48-
/// documentation). Marked as `must_use` because otherwise the result is dropped immediately,
47+
/// Start a background thread that takes care of responsibilities enumerated in the top-level
48+
/// documentation. Marked as `must_use` because otherwise the result is dropped immediately,
4949
/// resulting in the thread being terminated.
5050
/// Important note: this thread will panic if invoking `persist_manager` results in an error (and
5151
/// `start()` will need to be called again to restart the `BackgroundProcessor`).
@@ -84,7 +84,7 @@ impl BackgroundProcessor {
8484
panic!("Errored persisting manager: {}", e);
8585
};
8686
}
87-
// If we see that the thread has been stopped, exit now.
87+
// Exit the loop if the background processor was requested to stop.
8888
if stop_thread.load(Ordering::Acquire) == true {
8989
log_trace!(logger, "Terminating background processor.");
9090
break;
@@ -214,70 +214,51 @@ mod tests {
214214
let nodes = create_nodes(2, "test_background_processor".to_string());
215215

216216
// Initiate the background processors to watch each node.
217-
let data_dir_0 = nodes[0].persister.get_data_dir();
218-
let data_dir_1 = nodes[1].persister.get_data_dir();
219-
let callback_0 = move |node| FilesystemPersister::persist_manager(data_dir_0.clone(), node);
220-
let callback_1 = move |node| FilesystemPersister::persist_manager(data_dir_1.clone(), node);
221-
let _processor_0 = BackgroundProcessor::start(callback_0, nodes[0].node.clone(), nodes[0].logger.clone());
222-
let _processor_1 = BackgroundProcessor::start(callback_1, nodes[1].node.clone(), nodes[1].logger.clone());
217+
let data_dir = nodes[0].persister.get_data_dir();
218+
let callback = move |node| FilesystemPersister::persist_manager(data_dir.clone(), node);
219+
let _processor = BackgroundProcessor::start(callback, nodes[0].node.clone(), nodes[0].logger.clone());
223220

224221
// Go through the channel creation process until each node should have something persisted.
225222
let tx = open_channel!(nodes[0], nodes[1], 100000);
226223

227-
let mut done_persisting = false;
228224
macro_rules! check_persisted_data {
229-
($node: expr, $filepath: expr) => {
230-
let bytes = loop {
231-
match std::fs::read($filepath) {
232-
Ok(bytes) => break bytes,
233-
Err(_) => continue
234-
}
235-
};
236-
let mut expected_bytes = Vec::new();
237-
assert!($node.write(&mut expected_bytes).is_ok());
238-
if bytes == expected_bytes {
239-
done_persisting = true;
225+
($node: expr, $filepath: expr, $expected_bytes: expr) => {
226+
match $node.write(&mut $expected_bytes) {
227+
Ok(()) => {
228+
loop {
229+
match std::fs::read($filepath) {
230+
Ok(bytes) => {
231+
if bytes == $expected_bytes {
232+
break
233+
} else {
234+
continue
235+
}
236+
},
237+
Err(_) => continue
238+
}
239+
}
240+
},
241+
Err(e) => panic!("Unexpected error: {}", e)
240242
}
241243
}
242244
}
243245

244246
// Check that the initial channel manager data is persisted as expected.
245-
let filepath_0 = get_full_filepath("test_background_processor_persister_0".to_string(), "manager".to_string());
246-
let filepath_1 = get_full_filepath("test_background_processor_persister_1".to_string(), "manager".to_string());
247+
let filepath = get_full_filepath("test_background_processor_persister_0".to_string(), "manager".to_string());
248+
let mut expected_bytes = Vec::new();
249+
check_persisted_data!(nodes[0].node, filepath.clone(), expected_bytes);
247250
loop {
248-
check_persisted_data!(nodes[0].node, filepath_0.clone());
249-
if done_persisting {
250-
// Check that eventually BackgroundProcessor resets the condvar when everything's done persisting.
251-
loop {
252-
if !nodes[0].node.get_persistence_condvar_value() { break }
253-
}
254-
break
255-
}
256-
}
257-
done_persisting = false;
258-
loop {
259-
check_persisted_data!(nodes[1].node, filepath_1.clone());
260-
if done_persisting {
261-
loop {
262-
if !nodes[1].node.get_persistence_condvar_value() { break }
263-
}
264-
break
265-
}
251+
if !nodes[0].node.get_persistence_condvar_value() { break }
266252
}
267253

268254
// Force-close the channel.
269-
nodes[0].node.force_close_channel(&OutPoint { txid: tx.txid(), index: 0 }.to_channel_id());
255+
nodes[0].node.force_close_channel(&OutPoint { txid: tx.txid(), index: 0 }.to_channel_id()).unwrap();
270256

271257
// Check that the force-close updates are persisted.
272-
done_persisting = false;
258+
let mut expected_bytes = Vec::new();
259+
check_persisted_data!(nodes[0].node, filepath.clone(), expected_bytes);
273260
loop {
274-
check_persisted_data!(nodes[0].node, filepath_0.clone());
275-
if done_persisting {
276-
loop {
277-
if !nodes[0].node.get_persistence_condvar_value() { break }
278-
}
279-
break
280-
}
261+
if !nodes[0].node.get_persistence_condvar_value() { break }
281262
}
282263
}
283264

@@ -314,6 +295,8 @@ mod tests {
314295
Err(std::io::Error::new(std::io::ErrorKind::Other, "test"))
315296
}
316297

298+
// We don't want the expected panic to print to the console during testing, so
299+
// swallow it here.
317300
std::panic::set_hook(Box::new(|panic_info| {
318301
if let Some(s) = panic_info.payload().downcast_ref::<String>() {
319302
assert_eq!(s, "Errored persisting manager: test");

lightning-persister/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub(crate) mod util;
1+
mod util;
22

33
extern crate lightning;
44
extern crate bitcoin;
@@ -78,7 +78,11 @@ impl FilesystemPersister {
7878

7979
/// Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
8080
/// initialization, within a file called "manager".
81-
pub fn persist_manager<ChanSigner, M, T, K, F, L>(data_dir: String, manager: Arc<ChannelManager<ChanSigner, Arc<M>, Arc<T>, Arc<K>, Arc<F>, Arc<L>>>) -> Result<(), std::io::Error>
81+
pub fn persist_manager<ChanSigner, M, T, K, F, L>(
82+
data_dir: String,
83+
manager: Arc<ChannelManager<ChanSigner, Arc<M>, Arc<T>, Arc<K>, Arc<F>,
84+
Arc<L>>>
85+
) -> Result<(), std::io::Error>
8286
where ChanSigner: ChannelKeys + Writeable,
8387
M: chain::Watch<Keys=ChanSigner>,
8488
T: BroadcasterInterface,

0 commit comments

Comments
 (0)