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;
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;
/// 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();
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,
))
}
}
- Ok(outpoint_to_channelmonitor)
+ Ok(res)
}
}
// 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);
}
}