Skip to content

Commit 9e52f3e

Browse files
committed
Rework unwrap() call in persistence
1 parent d8a20ed commit 9e52f3e

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

lightning-persister/src/lib.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,43 +72,41 @@ impl FilesystemPersister {
7272
let mut res = Vec::new();
7373
for file_option in fs::read_dir(path).unwrap() {
7474
let file = file_option.unwrap();
75-
let owned_file_name = file.file_name();
76-
let filename = owned_file_name.to_str();
77-
if !filename.is_some() || !filename.unwrap().is_ascii() || filename.unwrap().len() < 65 {
75+
let filename = file.file_name().to_str()
76+
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidData,
77+
"File name is not a valid utf8 string"))?;
78+
if !filename.is_ascii() || filename.len() < 65 {
7879
return Err(std::io::Error::new(
7980
std::io::ErrorKind::InvalidData,
8081
"Invalid ChannelMonitor file name",
8182
));
8283
}
83-
if filename.unwrap().ends_with(".tmp") {
84+
if filename.ends_with(".tmp") {
8485
// If we were in the middle of committing an new update and crashed, it should be
8586
// safe to ignore the update - we should never have returned to the caller and
8687
// irrevocably committed to the new state in any way.
8788
continue;
8889
}
8990

90-
let txid = Txid::from_hex(filename.unwrap().split_at(64).0);
91-
if txid.is_err() {
92-
return Err(std::io::Error::new(
91+
let txid = Txid::from_hex(filename.split_at(64).0)
92+
.map_err(|_| std::io::Error::new(
9393
std::io::ErrorKind::InvalidData,
9494
"Invalid tx ID in filename",
95-
));
96-
}
95+
))?;
9796

98-
let index = filename.unwrap().split_at(65).1.parse();
99-
if index.is_err() {
100-
return Err(std::io::Error::new(
97+
let index = filename.split_at(65).1.parse()
98+
.map_err(|_| std::io::Error::new(
10199
std::io::ErrorKind::InvalidData,
102100
"Invalid tx index in filename",
103-
));
104-
}
101+
))?;
105102

106103
let contents = fs::read(&file.path())?;
107104
let mut buffer = Cursor::new(&contents);
108105
match <(BlockHash, ChannelMonitor<<K::Target as SignerProvider>::Signer>)>::read(&mut buffer, &*keys_manager) {
109106
Ok((blockhash, channel_monitor)) => {
110-
if channel_monitor.get_funding_txo().0.txid != txid.unwrap() || channel_monitor.get_funding_txo().0.index != index.unwrap() {
111-
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "ChannelMonitor was stored in the wrong file"));
107+
if channel_monitor.get_funding_txo().0.txid != txid || channel_monitor.get_funding_txo().0.index != index {
108+
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData,
109+
"ChannelMonitor was stored in the wrong file"));
112110
}
113111
res.push((blockhash, channel_monitor));
114112
}

0 commit comments

Comments
 (0)