Skip to content

Commit 2d9c91b

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 fc983f6 commit 2d9c91b

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

lightning/src/util/persist.rs

Lines changed: 50 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 namespace under which the [`ChannelManager`] will be persisted.
3032
pub const CHANNEL_MANAGER_PERSISTENCE_NAMESPACE: &str = "";
@@ -154,3 +156,49 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStorePersister> Persist<Ch
154156
}
155157
}
156158
}
159+
160+
/// Read previously persisted [`ChannelMonitor`]s from the store.
161+
pub fn read_channel_monitors<K: Deref, ES: Deref, SP: Deref>(
162+
kv_store: K, entropy_source: ES, signer_provider: SP,
163+
) -> crate::io::Result<Vec<(BlockHash, ChannelMonitor<<SP::Target as SignerProvider>::Signer>)>>
164+
where
165+
K::Target: KVStore,
166+
ES::Target: EntropySource + Sized,
167+
SP::Target: SignerProvider + Sized,
168+
{
169+
let mut res = Vec::new();
170+
171+
for stored_key in kv_store.list(CHANNEL_MONITOR_PERSISTENCE_NAMESPACE)? {
172+
let txid = Txid::from_hex(stored_key.split_at(64).0).map_err(|_| {
173+
crate::io::Error::new(crate::io::ErrorKind::InvalidData, "Invalid tx ID in stored key")
174+
})?;
175+
176+
let index: u16 = stored_key.split_at(65).1.parse().map_err(|_| {
177+
crate::io::Error::new(crate::io::ErrorKind::InvalidData, "Invalid tx index in stored key")
178+
})?;
179+
180+
match <(BlockHash, ChannelMonitor<<SP::Target as SignerProvider>::Signer>)>::read(
181+
&mut kv_store.read(CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, &stored_key)?,
182+
(&*entropy_source, &*signer_provider),
183+
) {
184+
Ok((block_hash, channel_monitor)) => {
185+
if channel_monitor.get_funding_txo().0.txid != txid
186+
|| channel_monitor.get_funding_txo().0.index != index
187+
{
188+
return Err(crate::io::Error::new(
189+
crate::io::ErrorKind::InvalidData,
190+
"ChannelMonitor was stored under the wrong key",
191+
));
192+
}
193+
res.push((block_hash, channel_monitor));
194+
}
195+
Err(_) => {
196+
return Err(crate::io::Error::new(
197+
crate::io::ErrorKind::InvalidData,
198+
"Failed to deserialize ChannelMonitor"
199+
))
200+
}
201+
}
202+
}
203+
Ok(res)
204+
}

0 commit comments

Comments
 (0)