Skip to content

Commit db219c4

Browse files
committed
Migrate FilesystemPersister tests to FilesystemStore
1 parent c302763 commit db219c4

File tree

3 files changed

+213
-2
lines changed

3 files changed

+213
-2
lines changed

lightning-storage/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ lightning = { version = "0.0.116", path = "../lightning", default-features = fal
1818
rand = "0.8.5"
1919
libc = "0.2"
2020

21+
[dev-dependencies]
22+
bitcoin = { version = "0.29.0", default-features = false }
23+
lightning = { version = "0.0.116", path = "../lightning", features = ["_test_utils"] }
24+
2125
[target.'cfg(windows)'.dependencies]
2226
winapi = { version = "0.3", features = ["winbase"] }

lightning-storage/src/fs_store.rs

+139-1
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,150 @@ impl Read for FilesystemReader {
253253
#[cfg(test)]
254254
mod tests {
255255
use super::*;
256-
use crate::test_utils::do_read_write_remove_list_persist;
256+
use crate::test_utils::{do_read_write_remove_list_persist, do_test_store};
257+
258+
use bitcoin::hashes::hex::FromHex;
259+
use bitcoin::Txid;
260+
261+
use lightning::chain::ChannelMonitorUpdateStatus;
262+
use lightning::chain::chainmonitor::Persist;
263+
use lightning::chain::transaction::OutPoint;
264+
use lightning::check_closed_event;
265+
use lightning::events::{ClosureReason, MessageSendEventsProvider};
266+
use lightning::ln::functional_test_utils::*;
267+
use lightning::util::test_utils;
268+
use lightning::util::persist::read_channel_monitors;
269+
use std::fs;
270+
#[cfg(target_os = "windows")]
271+
use {
272+
lightning::get_event_msg,
273+
lightning::ln::msgs::ChannelMessageHandler,
274+
};
275+
276+
impl Drop for FilesystemStore{
277+
fn drop(&mut self) {
278+
// We test for invalid directory names, so it's OK if directory removal
279+
// fails.
280+
match fs::remove_dir_all(&self.data_dir) {
281+
Err(e) => println!("Failed to remove test persister directory: {}", e),
282+
_ => {}
283+
}
284+
}
285+
}
257286

258287
#[test]
259288
fn read_write_remove_list_persist() {
260289
let temp_path = std::env::temp_dir();
261290
let fs_store = FilesystemStore::new(temp_path);
262291
do_read_write_remove_list_persist(&fs_store);
263292
}
293+
294+
#[test]
295+
fn test_if_monitors_is_not_dir() {
296+
let store = FilesystemStore::new("test_monitors_is_not_dir".into());
297+
298+
fs::create_dir_all(&store.get_data_dir()).unwrap();
299+
let mut path = std::path::PathBuf::from(&store.get_data_dir());
300+
path.push("monitors");
301+
fs::File::create(path).unwrap();
302+
303+
let chanmon_cfgs = create_chanmon_cfgs(1);
304+
let mut node_cfgs = create_node_cfgs(1, &chanmon_cfgs);
305+
let chain_mon_0 = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[0].chain_source), &chanmon_cfgs[0].tx_broadcaster, &chanmon_cfgs[0].logger, &chanmon_cfgs[0].fee_estimator, &store, node_cfgs[0].keys_manager);
306+
node_cfgs[0].chain_monitor = chain_mon_0;
307+
let node_chanmgrs = create_node_chanmgrs(1, &node_cfgs, &[None]);
308+
let nodes = create_network(1, &node_cfgs, &node_chanmgrs);
309+
310+
// Check that read_channel_monitors() returns error if monitors/ is not a
311+
// directory.
312+
assert!(read_channel_monitors(&store, nodes[0].keys_manager, nodes[0].keys_manager).is_err());
313+
}
314+
315+
#[test]
316+
fn test_filesystem_store() {
317+
// Create the nodes, giving them FilesystemStores for data stores.
318+
let store_0 = FilesystemStore::new("test_filesystem_store_0".into());
319+
let store_1 = FilesystemStore::new("test_filesystem_store_1".into());
320+
do_test_store(&store_0, &store_1)
321+
}
322+
323+
// Test that if the store's path to channel data is read-only, writing a
324+
// monitor to it results in the store returning a PermanentFailure.
325+
// Windows ignores the read-only flag for folders, so this test is Unix-only.
326+
#[cfg(not(target_os = "windows"))]
327+
#[test]
328+
fn test_readonly_dir_perm_failure() {
329+
let store = FilesystemStore::new("test_readonly_dir_perm_failure".into());
330+
fs::create_dir_all(&store.get_data_dir()).unwrap();
331+
332+
// Set up a dummy channel and force close. This will produce a monitor
333+
// that we can then use to test persistence.
334+
let chanmon_cfgs = create_chanmon_cfgs(2);
335+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
336+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
337+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
338+
let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
339+
nodes[1].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[0].node.get_our_node_id()).unwrap();
340+
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
341+
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
342+
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
343+
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
344+
345+
// Set the store's directory to read-only, which should result in
346+
// returning a permanent failure when we then attempt to persist a
347+
// channel update.
348+
let path = &store.get_data_dir();
349+
let mut perms = fs::metadata(path).unwrap().permissions();
350+
perms.set_readonly(true);
351+
fs::set_permissions(path, perms).unwrap();
352+
353+
let test_txo = OutPoint {
354+
txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
355+
index: 0
356+
};
357+
match store.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
358+
ChannelMonitorUpdateStatus::PermanentFailure => {},
359+
_ => panic!("unexpected result from persisting new channel")
360+
}
361+
362+
nodes[1].node.get_and_clear_pending_msg_events();
363+
added_monitors.clear();
364+
}
365+
366+
// Test that if a store's directory name is invalid, monitor persistence
367+
// will fail.
368+
#[cfg(target_os = "windows")]
369+
#[test]
370+
fn test_fail_on_open() {
371+
// Set up a dummy channel and force close. This will produce a monitor
372+
// that we can then use to test persistence.
373+
let chanmon_cfgs = create_chanmon_cfgs(2);
374+
let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
375+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
376+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
377+
let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
378+
nodes[1].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[0].node.get_our_node_id()).unwrap();
379+
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
380+
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
381+
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
382+
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
383+
384+
// Create the store with an invalid directory name and test that the
385+
// channel fails to open because the directories fail to be created. There
386+
// don't seem to be invalid filename characters on Unix that Rust doesn't
387+
// handle, hence why the test is Windows-only.
388+
let store = FilesystemStore::new(":<>/".into());
389+
390+
let test_txo = OutPoint {
391+
txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
392+
index: 0
393+
};
394+
match store.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
395+
ChannelMonitorUpdateStatus::PermanentFailure => {},
396+
_ => panic!("unexpected result from persisting new channel")
397+
}
398+
399+
nodes[1].node.get_and_clear_pending_msg_events();
400+
added_monitors.clear();
401+
}
264402
}

lightning-storage/src/test_utils.rs

+70-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use lightning::util::persist::KVStore;
1+
use lightning::util::persist::{KVStore, read_channel_monitors};
2+
use lightning::ln::functional_test_utils::*;
3+
use lightning::chain::channelmonitor::CLOSED_CHANNEL_UPDATE_ID;
4+
use lightning::util::test_utils;
5+
use lightning::{check_closed_broadcast, check_closed_event, check_added_monitors};
6+
use lightning::events::ClosureReason;
27

38
use rand::{thread_rng, RngCore};
49

@@ -32,3 +37,67 @@ pub(crate) fn do_read_write_remove_list_persist<K: KVStore>(kv_store: &K) {
3237
let listed_keys = kv_store.list(namespace).unwrap();
3338
assert_eq!(listed_keys.len(), 0);
3439
}
40+
41+
// Integration-test the given KVStore implementation. Test relaying a few payments and check that
42+
// the persisted data is updated the appropriate number of times.
43+
pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {
44+
let chanmon_cfgs = create_chanmon_cfgs(2);
45+
let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
46+
let chain_mon_0 = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[0].chain_source), &chanmon_cfgs[0].tx_broadcaster, &chanmon_cfgs[0].logger, &chanmon_cfgs[0].fee_estimator, store_0, node_cfgs[0].keys_manager);
47+
let chain_mon_1 = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[1].chain_source), &chanmon_cfgs[1].tx_broadcaster, &chanmon_cfgs[1].logger, &chanmon_cfgs[1].fee_estimator, store_1, node_cfgs[1].keys_manager);
48+
node_cfgs[0].chain_monitor = chain_mon_0;
49+
node_cfgs[1].chain_monitor = chain_mon_1;
50+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
51+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
52+
53+
// Check that the persisted channel data is empty before any channels are
54+
// open.
55+
let mut persisted_chan_data_0 = read_channel_monitors(store_0, nodes[0].keys_manager, nodes[0].keys_manager).unwrap();
56+
assert_eq!(persisted_chan_data_0.len(), 0);
57+
let mut persisted_chan_data_1 = read_channel_monitors(store_1, nodes[1].keys_manager, nodes[1].keys_manager).unwrap();
58+
assert_eq!(persisted_chan_data_1.len(), 0);
59+
60+
// Helper to make sure the channel is on the expected update ID.
61+
macro_rules! check_persisted_data {
62+
($expected_update_id: expr) => {
63+
persisted_chan_data_0 = read_channel_monitors(store_0, nodes[0].keys_manager, nodes[0].keys_manager).unwrap();
64+
assert_eq!(persisted_chan_data_0.len(), 1);
65+
for (_, mon) in persisted_chan_data_0.iter() {
66+
assert_eq!(mon.get_latest_update_id(), $expected_update_id);
67+
}
68+
persisted_chan_data_1 = read_channel_monitors(store_1, nodes[1].keys_manager, nodes[1].keys_manager).unwrap();
69+
assert_eq!(persisted_chan_data_1.len(), 1);
70+
for (_, mon) in persisted_chan_data_1.iter() {
71+
assert_eq!(mon.get_latest_update_id(), $expected_update_id);
72+
}
73+
}
74+
}
75+
76+
// Create some initial channel and check that a channel was persisted.
77+
let _ = create_announced_chan_between_nodes(&nodes, 0, 1);
78+
check_persisted_data!(0);
79+
80+
// Send a few payments and make sure the monitors are updated to the latest.
81+
send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
82+
check_persisted_data!(5);
83+
send_payment(&nodes[1], &vec!(&nodes[0])[..], 4000000);
84+
check_persisted_data!(10);
85+
86+
// Force close because cooperative close doesn't result in any persisted
87+
// updates.
88+
nodes[0].node.force_close_broadcasting_latest_txn(&nodes[0].node.list_channels()[0].channel_id, &nodes[1].node.get_our_node_id()).unwrap();
89+
check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed);
90+
check_closed_broadcast!(nodes[0], true);
91+
check_added_monitors!(nodes[0], 1);
92+
93+
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
94+
assert_eq!(node_txn.len(), 1);
95+
96+
connect_block(&nodes[1], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![node_txn[0].clone(), node_txn[0].clone()]));
97+
check_closed_broadcast!(nodes[1], true);
98+
check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
99+
check_added_monitors!(nodes[1], 1);
100+
101+
// Make sure everything is persisted as expected after close.
102+
check_persisted_data!(CLOSED_CHANNEL_UPDATE_ID);
103+
}

0 commit comments

Comments
 (0)