From: Elias Rohrer Date: Fri, 4 Aug 2023 14:20:50 +0000 (+0200) Subject: Add `read_channel_monitors` utility X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=f0e2343b2b1f2a7a30c34cbf9a37a9f9036700ef;p=rust-lightning Add `read_channel_monitors` utility This replaces the `FilesystemPersister::read_channelmonitors` method, as we can now implement a single utility for all `KVStore`s. --- diff --git a/lightning/src/util/persist.rs b/lightning/src/util/persist.rs index 61c20b189..5f74c0543 100644 --- a/lightning/src/util/persist.rs +++ b/lightning/src/util/persist.rs @@ -9,7 +9,9 @@ //! and [`ChannelMonitor`] all in one place. use core::ops::Deref; -use bitcoin::hashes::hex::ToHex; +use bitcoin::hashes::hex::{FromHex, ToHex}; +use bitcoin::{BlockHash, Txid}; + use crate::io; use crate::prelude::{Vec, String}; use crate::routing::scoring::WriteableScore; @@ -24,7 +26,7 @@ use crate::ln::channelmanager::ChannelManager; use crate::routing::router::Router; use crate::routing::gossip::NetworkGraph; use crate::util::logger::Logger; -use crate::util::ser::Writeable; +use crate::util::ser::{ReadableArgs, Writeable}; /// The namespace under which the [`ChannelManager`] will be persisted. pub const CHANNEL_MANAGER_PERSISTENCE_NAMESPACE: &str = ""; @@ -150,3 +152,49 @@ impl Persist( + kv_store: K, entropy_source: ES, signer_provider: SP, +) -> io::Result::Signer>)>> +where + K::Target: KVStore, + ES::Target: EntropySource + Sized, + SP::Target: SignerProvider + Sized, +{ + let mut res = Vec::new(); + + for stored_key in kv_store.list(CHANNEL_MONITOR_PERSISTENCE_NAMESPACE)? { + let txid = Txid::from_hex(stored_key.split_at(64).0).map_err(|_| { + io::Error::new(io::ErrorKind::InvalidData, "Invalid tx ID in stored key") + })?; + + let index: u16 = stored_key.split_at(65).1.parse().map_err(|_| { + io::Error::new(io::ErrorKind::InvalidData, "Invalid tx index in stored key") + })?; + + match <(BlockHash, ChannelMonitor<::Signer>)>::read( + &mut io::Cursor::new(kv_store.read(CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, &stored_key)?), + (&*entropy_source, &*signer_provider), + ) { + Ok((block_hash, channel_monitor)) => { + if channel_monitor.get_funding_txo().0.txid != txid + || channel_monitor.get_funding_txo().0.index != index + { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "ChannelMonitor was stored under the wrong key", + )); + } + res.push((block_hash, channel_monitor)); + } + Err(_) => { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "Failed to deserialize ChannelMonitor" + )) + } + } + } + Ok(res) +}