Skip to content

Commit 485f99b

Browse files
committed
Add read_channel_monitors utility
This replaces the `FilesystemPersister::read_channelmonitors` method, as we can now implement a single utility for all `KVStore`s.
1 parent eeb619f commit 485f99b

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

lightning/src/util/persist.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
//! and [`ChannelMonitor`] all in one place.
1010
1111
use core::ops::Deref;
12-
use bitcoin::hashes::hex::ToHex;
12+
use bitcoin::hashes::hex::{FromHex, ToHex};
13+
use bitcoin::{BlockHash, Txid};
14+
1315
use crate::io;
1416
use crate::prelude::{Vec, String};
1517
use crate::routing::scoring::WriteableScore;
@@ -24,7 +26,7 @@ use crate::ln::channelmanager::ChannelManager;
2426
use crate::routing::router::Router;
2527
use crate::routing::gossip::NetworkGraph;
2628
use crate::util::logger::Logger;
27-
use crate::util::ser::Writeable;
29+
use crate::util::ser::{ReadableArgs, Writeable};
2830

2931
/// The alphabet of characters allowed for namespaces and keys.
3032
pub const KVSTORE_NAMESPACE_KEY_ALPHABET: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
@@ -187,3 +189,52 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStorePersister> Persist<Ch
187189
}
188190
}
189191
}
192+
193+
/// Read previously persisted [`ChannelMonitor`]s from the store.
194+
pub fn read_channel_monitors<K: Deref, ES: Deref, SP: Deref>(
195+
kv_store: K, entropy_source: ES, signer_provider: SP,
196+
) -> io::Result<Vec<(BlockHash, ChannelMonitor<<SP::Target as SignerProvider>::Signer>)>>
197+
where
198+
K::Target: KVStore,
199+
ES::Target: EntropySource + Sized,
200+
SP::Target: SignerProvider + Sized,
201+
{
202+
let mut res = Vec::new();
203+
204+
for stored_key in kv_store.list(
205+
CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE)?
206+
{
207+
let txid = Txid::from_hex(stored_key.split_at(64).0).map_err(|_| {
208+
io::Error::new(io::ErrorKind::InvalidData, "Invalid tx ID in stored key")
209+
})?;
210+
211+
let index: u16 = stored_key.split_at(65).1.parse().map_err(|_| {
212+
io::Error::new(io::ErrorKind::InvalidData, "Invalid tx index in stored key")
213+
})?;
214+
215+
match <(BlockHash, ChannelMonitor<<SP::Target as SignerProvider>::Signer>)>::read(
216+
&mut io::Cursor::new(
217+
kv_store.read(CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE, &stored_key)?),
218+
(&*entropy_source, &*signer_provider),
219+
) {
220+
Ok((block_hash, channel_monitor)) => {
221+
if channel_monitor.get_funding_txo().0.txid != txid
222+
|| channel_monitor.get_funding_txo().0.index != index
223+
{
224+
return Err(io::Error::new(
225+
io::ErrorKind::InvalidData,
226+
"ChannelMonitor was stored under the wrong key",
227+
));
228+
}
229+
res.push((block_hash, channel_monitor));
230+
}
231+
Err(_) => {
232+
return Err(io::Error::new(
233+
io::ErrorKind::InvalidData,
234+
"Failed to deserialize ChannelMonitor"
235+
))
236+
}
237+
}
238+
}
239+
Ok(res)
240+
}

0 commit comments

Comments
 (0)