X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fpersist.rs;h=ca0605c95983afd3b370cafddaf82de2868dadfb;hb=7a656719af1cf1d124821eb385a7d88f792660b4;hp=35d19eea46bed18b702af7952396e4d6a7152174;hpb=d1405f38bc6d51fcb1ca14fb4c506f8b27a2e70f;p=rust-lightning diff --git a/lightning/src/util/persist.rs b/lightning/src/util/persist.rs index 35d19eea..ca0605c9 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 alphabet of characters allowed for namespaces and keys. pub const KVSTORE_NAMESPACE_KEY_ALPHABET: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"; @@ -112,15 +114,6 @@ pub trait KVStore { fn list(&self, namespace: &str, sub_namespace: &str) -> io::Result>; } -/// Trait for a key-value store for persisting some writeable object at some key -/// Implementing `KVStorePersister` provides auto-implementations for [`Persister`] -/// and [`Persist`] traits. It uses "manager", "network_graph", -/// and "monitors/{funding_txo_id}_{funding_txo_index}" for keys. -pub trait KVStorePersister { - /// Persist the given writeable using the provided key - fn persist(&self, key: &str, object: &W) -> io::Result<()>; -} - /// Trait that handles persisting a [`ChannelManager`], [`NetworkGraph`], and [`WriteableScore`] to disk. pub trait Persister<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>> where M::Target: 'static + chain::Watch<::Signer>, @@ -142,7 +135,8 @@ pub trait Persister<'a, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error>; } -impl<'a, A: KVStorePersister, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, M, T, ES, NS, SP, F, R, L, S> for A + +impl<'a, A: KVStore, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref, S: WriteableScore<'a>> Persister<'a, M, T, ES, NS, SP, F, R, L, S> for A where M::Target: 'static + chain::Watch<::Signer>, T::Target: 'static + BroadcasterInterface, ES::Target: 'static + EntropySource, @@ -152,41 +146,107 @@ impl<'a, A: KVStorePersister, M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Dere R::Target: 'static + Router, L::Target: 'static + Logger, { - /// Persist the given ['ChannelManager'] to disk with the name "manager", returning an error if persistence failed. + /// Persist the given [`ChannelManager`] to disk, returning an error if persistence failed. fn persist_manager(&self, channel_manager: &ChannelManager) -> Result<(), io::Error> { - self.persist("manager", channel_manager) + self.write(CHANNEL_MANAGER_PERSISTENCE_NAMESPACE, + CHANNEL_MANAGER_PERSISTENCE_SUB_NAMESPACE, + CHANNEL_MANAGER_PERSISTENCE_KEY, + &channel_manager.encode()) } - /// Persist the given [`NetworkGraph`] to disk with the name "network_graph", returning an error if persistence failed. + /// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed. fn persist_graph(&self, network_graph: &NetworkGraph) -> Result<(), io::Error> { - self.persist("network_graph", network_graph) + self.write(NETWORK_GRAPH_PERSISTENCE_NAMESPACE, + NETWORK_GRAPH_PERSISTENCE_SUB_NAMESPACE, + NETWORK_GRAPH_PERSISTENCE_KEY, + &network_graph.encode()) } - /// Persist the given [`WriteableScore`] to disk with name "scorer", returning an error if persistence failed. + /// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed. fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error> { - self.persist("scorer", &scorer) + self.write(SCORER_PERSISTENCE_NAMESPACE, + SCORER_PERSISTENCE_SUB_NAMESPACE, + SCORER_PERSISTENCE_KEY, + &scorer.encode()) } } -impl Persist for K { +impl Persist for K { // TODO: We really need a way for the persister to inform the user that its time to crash/shut // down once these start returning failure. // A PermanentFailure implies we should probably just shut down the node since we're // force-closing channels without even broadcasting! fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus { - let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index); - match self.persist(&key, monitor) { + let key = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index); + match self.write( + CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, + CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE, + &key, &monitor.encode()) + { Ok(()) => chain::ChannelMonitorUpdateStatus::Completed, Err(_) => chain::ChannelMonitorUpdateStatus::PermanentFailure, } } fn update_persisted_channel(&self, funding_txo: OutPoint, _update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus { - let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index); - match self.persist(&key, monitor) { + let key = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index); + match self.write( + CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, + CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE, + &key, &monitor.encode()) + { Ok(()) => chain::ChannelMonitorUpdateStatus::Completed, Err(_) => chain::ChannelMonitorUpdateStatus::PermanentFailure, } } } + +/// Read previously persisted [`ChannelMonitor`]s from the store. +pub fn read_channel_monitors( + 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, CHANNEL_MONITOR_PERSISTENCE_SUB_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, CHANNEL_MONITOR_PERSISTENCE_SUB_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) +}