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).
This commit is contained in:
Matt Corallo 2021-04-12 13:48:29 -04:00
parent 0a11eb19ab
commit 6b2e179bc1

View file

@ -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<Signer: Sign, K: Deref> (
&self, keys_manager: K
) -> Result<HashMap<OutPoint, (BlockHash, ChannelMonitor<Signer>)>, std::io::Error>
) -> Result<Vec<(BlockHash, ChannelMonitor<Signer>)>, std::io::Error>
where K::Target: KeysInterface<Signer=Signer> + 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<Signer>)>::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);
}
}