Skip to content

Commit 8d2b1e0

Browse files
committed
Handle read_dir() errors in persister
1 parent 197a47a commit 8d2b1e0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lightning-persister/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl FilesystemPersister {
6161
/// Read `ChannelMonitor`s from disk.
6262
pub fn read_channelmonitors<K: Deref> (
6363
&self, keys_manager: K
64-
) -> Result<Vec<(BlockHash, ChannelMonitor<<K::Target as SignerProvider>::Signer>)>, std::io::Error>
64+
) -> std::io::Result<Vec<(BlockHash, ChannelMonitor<<K::Target as SignerProvider>::Signer>)>>
6565
where K::Target: KeysInterface + Sized,
6666
{
6767
let mut path = PathBuf::from(&self.path_to_channel_data);
@@ -70,7 +70,7 @@ impl FilesystemPersister {
7070
return Ok(Vec::new());
7171
}
7272
let mut res = Vec::new();
73-
for file_option in fs::read_dir(path).unwrap() {
73+
for file_option in fs::read_dir(path)? {
7474
let file = file_option.unwrap();
7575
let owned_file_name = file.file_name();
7676
let filename = owned_file_name.to_str()
@@ -164,6 +164,22 @@ mod tests {
164164
}
165165
}
166166

167+
#[test]
168+
fn test_if_monitors_is_not_dir() {
169+
let persister = FilesystemPersister::new("test_monitors_is_not_dir".to_string());
170+
171+
let chanmon_cfgs = create_chanmon_cfgs(1);
172+
let mut node_cfgs = create_node_cfgs(1, &chanmon_cfgs);
173+
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, &persister, &node_cfgs[0].keys_manager);
174+
node_cfgs[0].chain_monitor = chain_mon_0;
175+
let node_chanmgrs = create_node_chanmgrs(1, &node_cfgs, &[None]);
176+
let nodes = create_network(1, &node_cfgs, &node_chanmgrs);
177+
178+
// Check that read_channelmonitors() returns error if monitors/ is not a
179+
// directory.
180+
assert!(persister.read_channelmonitors(nodes[0].keys_manager).is_err());
181+
}
182+
167183
// Integration-test the FilesystemPersister. Test relaying a few payments
168184
// and check that the persisted data is updated the appropriate number of
169185
// times.

lightning-persister/test_monitors_is_not_dir/monitors

Whitespace-only changes.

0 commit comments

Comments
 (0)