Skip to content

Commit 197a47a

Browse files
authored
Merge pull request #1941 from andrei-21/feature/rework-unwrap
Rework `unwrap()` call in persistence
2 parents 8896f6e + a320ebc commit 197a47a

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

lightning-persister/src/lib.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,42 +73,41 @@ impl FilesystemPersister {
7373
for file_option in fs::read_dir(path).unwrap() {
7474
let file = file_option.unwrap();
7575
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 {
76+
let filename = owned_file_name.to_str()
77+
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidData,
78+
"File name is not a valid utf8 string"))?;
79+
if !filename.is_ascii() || filename.len() < 65 {
7880
return Err(std::io::Error::new(
7981
std::io::ErrorKind::InvalidData,
8082
"Invalid ChannelMonitor file name",
8183
));
8284
}
83-
if filename.unwrap().ends_with(".tmp") {
85+
if filename.ends_with(".tmp") {
8486
// If we were in the middle of committing an new update and crashed, it should be
8587
// safe to ignore the update - we should never have returned to the caller and
8688
// irrevocably committed to the new state in any way.
8789
continue;
8890
}
8991

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

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

106104
let contents = fs::read(&file.path())?;
107105
let mut buffer = Cursor::new(&contents);
108106
match <(BlockHash, ChannelMonitor<<K::Target as SignerProvider>::Signer>)>::read(&mut buffer, &*keys_manager) {
109107
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"));
108+
if channel_monitor.get_funding_txo().0.txid != txid || channel_monitor.get_funding_txo().0.index != index {
109+
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData,
110+
"ChannelMonitor was stored in the wrong file"));
112111
}
113112
res.push((blockhash, channel_monitor));
114113
}

0 commit comments

Comments
 (0)