]> git.bitcoin.ninja Git - rust-lightning/commitdiff
demo 2023-08-2472-demo
authorMatt Corallo <git@bluematt.me>
Wed, 23 Aug 2023 21:31:20 +0000 (21:31 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 23 Aug 2023 21:31:20 +0000 (21:31 +0000)
lightning/src/util/persist.rs
lightning/src/util/test_utils.rs

index 0f50f6bd8c595eae2d25f66b908a89240ecc42c1..0e52577a470e92124f0ef3925d386c6b6d2bc87a 100644 (file)
@@ -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<Vec<u8>>;
+       fn read<A, R: ReadableArgs<A>>(&self, namespace: &str, key: &str, args: A) -> Result<R, DecodeError>;
        /// 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<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore> Persist<ChannelSign
 /// Read previously persisted [`ChannelMonitor`]s from the store.
 pub fn read_channel_monitors<K: Deref, ES: Deref, SP: Deref>(
        kv_store: K, entropy_source: ES, signer_provider: SP,
-) -> io::Result<Vec<(BlockHash, ChannelMonitor<<SP::Target as SignerProvider>::Signer>)>>
+) -> Result<Vec<(BlockHash, ChannelMonitor<<SP::Target as SignerProvider>::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<<SP::Target as SignerProvider>::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<<SP::Target as SignerProvider>::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)
 }
index 40096ac7bf4a36372f4f5927094ee41f8532554a..bcce6057e276d44b7900c957f9c1a6f2f1f5fba4 100644 (file)
@@ -341,17 +341,16 @@ impl TestStore {
 }
 
 impl KVStore for TestStore {
-       fn read(&self, namespace: &str, key: &str) -> io::Result<Vec<u8>> {
+       fn read<A, R: ReadableArgs<A>>(&self, namespace: &str, key: &str, args: A) -> Result<R, msgs::DecodeError> {
                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))
                }
        }