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;
/// 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.
/// 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,
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)
}
}
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))
}
}