From: Matt Corallo Date: Wed, 23 Aug 2023 21:31:20 +0000 (+0000) Subject: demo X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=f5dd8870ec3310f8ed95869a1e2f15c134e6ec7d;p=rust-lightning demo --- diff --git a/lightning/src/util/persist.rs b/lightning/src/util/persist.rs index 0f50f6bd8..0e52577a4 100644 --- a/lightning/src/util/persist.rs +++ b/lightning/src/util/persist.rs @@ -23,6 +23,7 @@ use crate::sign::{EntropySource, NodeSigner, WriteableEcdsaChannelSigner, Signer use crate::chain::transaction::OutPoint; use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate}; use crate::ln::channelmanager::ChannelManager; +use crate::ln::msgs::DecodeError; use crate::routing::router::Router; use crate::routing::gossip::NetworkGraph; use crate::util::logger::Logger; @@ -61,7 +62,7 @@ pub trait KVStore { /// Returns an [`ErrorKind::NotFound`] if the given `key` could not be found in the given `namespace`. /// /// [`ErrorKind::NotFound`]: io::ErrorKind::NotFound - fn read(&self, namespace: &str, key: &str) -> io::Result>; + fn read>(&self, namespace: &str, key: &str, args: A) -> Result; /// Persists the given data under the given `key`. /// /// Will create the given `namespace` if not already present in the store. @@ -148,7 +149,7 @@ impl Persist( kv_store: K, entropy_source: ES, signer_provider: SP, -) -> io::Result::Signer>)>> +) -> Result::Signer>)>, DecodeError> where K::Target: KVStore, ES::Target: EntropySource + Sized, @@ -158,35 +159,22 @@ where 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") + DecodeError::InvalidValue })?; let index: u16 = stored_key.split_at(65).1.parse().map_err(|_| { - io::Error::new(io::ErrorKind::InvalidData, "Invalid tx index in stored key") + DecodeError::InvalidValue })?; - 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" - )) - } + let (block_hash, channel_monitor): (BlockHash, ChannelMonitor<::Signer>) = + kv_store.read(CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, &stored_key, (&*entropy_source, &*signer_provider))?; + + if channel_monitor.get_funding_txo().0.txid != txid + || channel_monitor.get_funding_txo().0.index != index + { + return Err(DecodeError::InvalidValue); } + res.push((block_hash, channel_monitor)); } Ok(res) } diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 40096ac7b..bcce6057e 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -341,17 +341,16 @@ impl TestStore { } impl KVStore for TestStore { - fn read(&self, namespace: &str, key: &str) -> io::Result> { + fn read>(&self, namespace: &str, key: &str, args: A) -> Result { let persisted_lock = self.persisted_bytes.lock().unwrap(); if let Some(outer_ref) = persisted_lock.get(namespace) { if let Some(inner_ref) = outer_ref.get(key) { - let bytes = inner_ref.clone(); - Ok(bytes) + R::read(&mut std::io::Cursor::new(inner_ref), args) } else { - Err(io::Error::new(io::ErrorKind::NotFound, "Key not found")) + Err(msgs::DecodeError::Io(io::ErrorKind::NotFound)) } } else { - Err(io::Error::new(io::ErrorKind::NotFound, "Namespace not found")) + Err(msgs::DecodeError::Io(io::ErrorKind::NotFound)) } }