Skip to content

Commit 6b2e179

Browse files
committed
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).
1 parent 0a11eb1 commit 6b2e179

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

lightning-persister/src/lib.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern crate lightning;
1212
extern crate bitcoin;
1313
extern crate libc;
1414

15-
use bitcoin::{BlockHash, Txid};
15+
use bitcoin::hash_types::{BlockHash, Txid};
1616
use bitcoin::hashes::hex::{FromHex, ToHex};
1717
use crate::util::DiskWriteable;
1818
use lightning::chain;
@@ -24,7 +24,6 @@ use lightning::chain::transaction::OutPoint;
2424
use lightning::ln::channelmanager::ChannelManager;
2525
use lightning::util::logger::Logger;
2626
use lightning::util::ser::{ReadableArgs, Writeable};
27-
use std::collections::HashMap;
2827
use std::fs;
2928
use std::io::{Cursor, Error};
3029
use std::ops::Deref;
@@ -103,14 +102,14 @@ impl FilesystemPersister {
103102
/// Read `ChannelMonitor`s from disk.
104103
pub fn read_channelmonitors<Signer: Sign, K: Deref> (
105104
&self, keys_manager: K
106-
) -> Result<HashMap<OutPoint, (BlockHash, ChannelMonitor<Signer>)>, std::io::Error>
105+
) -> Result<Vec<(BlockHash, ChannelMonitor<Signer>)>, std::io::Error>
107106
where K::Target: KeysInterface<Signer=Signer> + Sized
108107
{
109108
let path = self.path_to_monitor_data();
110109
if !Path::new(&path).exists() {
111-
return Ok(HashMap::new());
110+
return Ok(Vec::new());
112111
}
113-
let mut outpoint_to_channelmonitor = HashMap::new();
112+
let mut res = Vec::new();
114113
for file_option in fs::read_dir(path).unwrap() {
115114
let file = file_option.unwrap();
116115
let owned_file_name = file.file_name();
@@ -142,18 +141,18 @@ impl FilesystemPersister {
142141
let mut buffer = Cursor::new(&contents);
143142
match <(BlockHash, ChannelMonitor<Signer>)>::read(&mut buffer, &*keys_manager) {
144143
Ok((blockhash, channel_monitor)) => {
145-
outpoint_to_channelmonitor.insert(
146-
OutPoint { txid: txid.unwrap(), index: index.unwrap() },
147-
(blockhash, channel_monitor),
148-
);
144+
if channel_monitor.get_funding_txo().0.txid != txid.unwrap() || channel_monitor.get_funding_txo().0.index != index.unwrap() {
145+
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "ChannelMonitor was stored in the wrong file"));
146+
}
147+
res.push((blockhash, channel_monitor));
149148
}
150149
Err(e) => return Err(std::io::Error::new(
151150
std::io::ErrorKind::InvalidData,
152151
format!("Failed to deserialize ChannelMonitor: {}", e),
153152
))
154153
}
155154
}
156-
Ok(outpoint_to_channelmonitor)
155+
Ok(res)
157156
}
158157
}
159158

@@ -225,21 +224,21 @@ mod tests {
225224
// Check that the persisted channel data is empty before any channels are
226225
// open.
227226
let mut persisted_chan_data_0 = persister_0.read_channelmonitors(nodes[0].keys_manager).unwrap();
228-
assert_eq!(persisted_chan_data_0.keys().len(), 0);
227+
assert_eq!(persisted_chan_data_0.len(), 0);
229228
let mut persisted_chan_data_1 = persister_1.read_channelmonitors(nodes[1].keys_manager).unwrap();
230-
assert_eq!(persisted_chan_data_1.keys().len(), 0);
229+
assert_eq!(persisted_chan_data_1.len(), 0);
231230

232231
// Helper to make sure the channel is on the expected update ID.
233232
macro_rules! check_persisted_data {
234233
($expected_update_id: expr) => {
235234
persisted_chan_data_0 = persister_0.read_channelmonitors(nodes[0].keys_manager).unwrap();
236-
assert_eq!(persisted_chan_data_0.keys().len(), 1);
237-
for (_, mon) in persisted_chan_data_0.values() {
235+
assert_eq!(persisted_chan_data_0.len(), 1);
236+
for (_, mon) in persisted_chan_data_0.iter() {
238237
assert_eq!(mon.get_latest_update_id(), $expected_update_id);
239238
}
240239
persisted_chan_data_1 = persister_1.read_channelmonitors(nodes[1].keys_manager).unwrap();
241-
assert_eq!(persisted_chan_data_1.keys().len(), 1);
242-
for (_, mon) in persisted_chan_data_1.values() {
240+
assert_eq!(persisted_chan_data_1.len(), 1);
241+
for (_, mon) in persisted_chan_data_1.iter() {
243242
assert_eq!(mon.get_latest_update_id(), $expected_update_id);
244243
}
245244
}

0 commit comments

Comments
 (0)