From: Matt Corallo Date: Mon, 12 Apr 2021 17:48:29 +0000 (-0400) Subject: Return ChannelMonitors in a Vec, not HashMap when loading from disk X-Git-Tag: v0.0.14~29^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=6b2e179bc1fef2f91f84cdce883752162ec41466;p=rust-lightning Return ChannelMonitors in a Vec, not HashMap when loading from disk There's little reason for the HashMap - the ChannelMonitors are already unique (enforced by file names), and the eventual HashMap that users need when deserializing the `ChannelManager` is a slightly different form (it requires no BlockHash entry). --- diff --git a/lightning-persister/src/lib.rs b/lightning-persister/src/lib.rs index 8effedbce..74e05d4c6 100644 --- a/lightning-persister/src/lib.rs +++ b/lightning-persister/src/lib.rs @@ -12,7 +12,7 @@ extern crate lightning; extern crate bitcoin; extern crate libc; -use bitcoin::{BlockHash, Txid}; +use bitcoin::hash_types::{BlockHash, Txid}; use bitcoin::hashes::hex::{FromHex, ToHex}; use crate::util::DiskWriteable; use lightning::chain; @@ -24,7 +24,6 @@ use lightning::chain::transaction::OutPoint; use lightning::ln::channelmanager::ChannelManager; use lightning::util::logger::Logger; use lightning::util::ser::{ReadableArgs, Writeable}; -use std::collections::HashMap; use std::fs; use std::io::{Cursor, Error}; use std::ops::Deref; @@ -103,14 +102,14 @@ impl FilesystemPersister { /// Read `ChannelMonitor`s from disk. pub fn read_channelmonitors ( &self, keys_manager: K - ) -> Result)>, std::io::Error> + ) -> 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()); + return Ok(Vec::new()); } - let mut outpoint_to_channelmonitor = HashMap::new(); + let mut res = Vec::new(); for file_option in fs::read_dir(path).unwrap() { let file = file_option.unwrap(); let owned_file_name = file.file_name(); @@ -142,10 +141,10 @@ impl FilesystemPersister { 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), - ); + if channel_monitor.get_funding_txo().0.txid != txid.unwrap() || channel_monitor.get_funding_txo().0.index != index.unwrap() { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "ChannelMonitor was stored in the wrong file")); + } + res.push((blockhash, channel_monitor)); } Err(e) => return Err(std::io::Error::new( std::io::ErrorKind::InvalidData, @@ -153,7 +152,7 @@ impl FilesystemPersister { )) } } - Ok(outpoint_to_channelmonitor) + Ok(res) } } @@ -225,21 +224,21 @@ mod tests { // Check that the persisted channel data is empty before any channels are // open. let mut persisted_chan_data_0 = persister_0.read_channelmonitors(nodes[0].keys_manager).unwrap(); - assert_eq!(persisted_chan_data_0.keys().len(), 0); + assert_eq!(persisted_chan_data_0.len(), 0); let mut persisted_chan_data_1 = persister_1.read_channelmonitors(nodes[1].keys_manager).unwrap(); - assert_eq!(persisted_chan_data_1.keys().len(), 0); + assert_eq!(persisted_chan_data_1.len(), 0); // Helper to make sure the channel is on the expected update ID. macro_rules! check_persisted_data { ($expected_update_id: expr) => { persisted_chan_data_0 = persister_0.read_channelmonitors(nodes[0].keys_manager).unwrap(); - assert_eq!(persisted_chan_data_0.keys().len(), 1); - for (_, mon) in persisted_chan_data_0.values() { + assert_eq!(persisted_chan_data_0.len(), 1); + for (_, mon) in persisted_chan_data_0.iter() { assert_eq!(mon.get_latest_update_id(), $expected_update_id); } persisted_chan_data_1 = persister_1.read_channelmonitors(nodes[1].keys_manager).unwrap(); - assert_eq!(persisted_chan_data_1.keys().len(), 1); - for (_, mon) in persisted_chan_data_1.values() { + assert_eq!(persisted_chan_data_1.len(), 1); + for (_, mon) in persisted_chan_data_1.iter() { assert_eq!(mon.get_latest_update_id(), $expected_update_id); } }