From: Matt Corallo <649246+TheBlueMatt@users.noreply.github.com> Date: Fri, 9 Apr 2021 23:57:20 +0000 (+0000) Subject: Merge pull request #861 from lightning-signer/degenerify X-Git-Tag: v0.0.14~33 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=dba0709b084cd3c1f50f95afed5111e5f8afda39;p=rust-lightning Merge pull request #861 from lightning-signer/degenerify De-generify Sign methods --- dba0709b084cd3c1f50f95afed5111e5f8afda39 diff --cc lightning-persister/src/lib.rs index 313808799,47789f2ef..368945d1c --- a/lightning-persister/src/lib.rs +++ b/lightning-persister/src/lib.rs @@@ -102,64 -108,42 +102,64 @@@ impl FilesystemPersister util::write_to_file(path, "manager".to_string(), manager) } - #[cfg(test)] - fn load_channel_data(&self, keys: &Keys) -> - Result>, ChannelMonitorUpdateErr> { - if let Err(_) = fs::create_dir_all(self.path_to_monitor_data()) { - return Err(ChannelMonitorUpdateErr::PermanentFailure); + /// Read `ChannelMonitor`s from disk. + pub fn read_channelmonitors ( + &self, keys_manager: K + ) -> Result)>, std::io::Error> + where K::Target: KeysInterface + Sized + { + let path = self.path_to_monitor_data(); + if !Path::new(&path).exists() { + return Ok(HashMap::new()); + } + let mut outpoint_to_channelmonitor = HashMap::new(); + for file_option in fs::read_dir(path).unwrap() { + let file = file_option.unwrap(); + let owned_file_name = file.file_name(); + let filename = owned_file_name.to_str(); + if !filename.is_some() || !filename.unwrap().is_ascii() || filename.unwrap().len() < 65 { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "Invalid ChannelMonitor file name", + )); } - let mut res = HashMap::new(); - for file_option in fs::read_dir(self.path_to_monitor_data()).unwrap() { - let file = file_option.unwrap(); - let owned_file_name = file.file_name(); - let filename = owned_file_name.to_str(); - if !filename.is_some() || !filename.unwrap().is_ascii() || filename.unwrap().len() < 65 { - return Err(ChannelMonitorUpdateErr::PermanentFailure); - } - let txid = Txid::from_hex(filename.unwrap().split_at(64).0); - if txid.is_err() { return Err(ChannelMonitorUpdateErr::PermanentFailure); } - - let index = filename.unwrap().split_at(65).1.split('.').next().unwrap().parse(); - if index.is_err() { return Err(ChannelMonitorUpdateErr::PermanentFailure); } + let txid = Txid::from_hex(filename.unwrap().split_at(64).0); + if txid.is_err() { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "Invalid tx ID in filename", + )); + } - let contents = fs::read(&file.path()); - if contents.is_err() { return Err(ChannelMonitorUpdateErr::PermanentFailure); } + let index = filename.unwrap().split_at(65).1.parse(); + if index.is_err() { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "Invalid tx index in filename", + )); + } - if let Ok((_, loaded_monitor)) = - <(BlockHash, ChannelMonitor)>::read(&mut Cursor::new(&contents.unwrap()), keys) { - res.insert(OutPoint { txid: txid.unwrap(), index: index.unwrap() }, loaded_monitor); - } else { - return Err(ChannelMonitorUpdateErr::PermanentFailure); - } + let contents = fs::read(&file.path())?; + let mut buffer = Cursor::new(&contents); + match <(BlockHash, ChannelMonitor)>::read(&mut buffer, &*keys_manager) { + Ok((blockhash, channel_monitor)) => { + outpoint_to_channelmonitor.insert( + OutPoint { txid: txid.unwrap(), index: index.unwrap() }, + (blockhash, channel_monitor), + ); + } + Err(e) => return Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!("Failed to deserialize ChannelMonitor: {}", e), + )) } - Ok(res) } + Ok(outpoint_to_channelmonitor) + } } - impl channelmonitor::Persist for FilesystemPersister { + impl channelmonitor::Persist for FilesystemPersister { fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> { let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index); util::write_to_file(self.path_to_monitor_data(), filename, monitor)