Skip to content

Commit 8184092

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 da06ce2 commit 8184092

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

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

0 commit comments

Comments
 (0)