Skip to content

Commit 7afa230

Browse files
committed
Handle read_dir() errors in persister
1 parent 197a47a commit 7afa230

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

lightning-persister/src/lib.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl FilesystemPersister {
4848
/// Initialize a new FilesystemPersister and set the path to the individual channels'
4949
/// files.
5050
pub fn new(path_to_channel_data: String) -> Self {
51-
return Self {
51+
Self {
5252
path_to_channel_data,
5353
}
5454
}
@@ -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,27 @@ 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+
fs::create_dir_all(&persister.path_to_channel_data).unwrap();
172+
let mut path = std::path::PathBuf::from(&persister.path_to_channel_data);
173+
path.push("monitors");
174+
fs::File::create(path).unwrap();
175+
176+
let chanmon_cfgs = create_chanmon_cfgs(1);
177+
let mut node_cfgs = create_node_cfgs(1, &chanmon_cfgs);
178+
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);
179+
node_cfgs[0].chain_monitor = chain_mon_0;
180+
let node_chanmgrs = create_node_chanmgrs(1, &node_cfgs, &[None]);
181+
let nodes = create_network(1, &node_cfgs, &node_chanmgrs);
182+
183+
// Check that read_channelmonitors() returns error if monitors/ is not a
184+
// directory.
185+
assert!(persister.read_channelmonitors(nodes[0].keys_manager).is_err());
186+
}
187+
167188
// Integration-test the FilesystemPersister. Test relaying a few payments
168189
// and check that the persisted data is updated the appropriate number of
169190
// times.

0 commit comments

Comments
 (0)