Change Persist's Sign from an associated type to a generic param
[rust-lightning] / lightning-persister / src / lib.rs
index da5cba95e673d72ecfdb7ed90d1e5340f045195c..cb7223a426a0b9a0cde2b6aa8a128ee7af5cbf92 100644 (file)
@@ -6,17 +6,21 @@ extern crate libc;
 
 use bitcoin::hashes::hex::ToHex;
 use crate::util::DiskWriteable;
+use lightning::chain;
+use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
 use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr};
 use lightning::chain::channelmonitor;
-use lightning::chain::keysinterface::ChannelKeys;
+use lightning::chain::keysinterface::{Sign, KeysInterface};
 use lightning::chain::transaction::OutPoint;
+use lightning::ln::channelmanager::ChannelManager;
+use lightning::util::logger::Logger;
 use lightning::util::ser::Writeable;
 use std::fs;
 use std::io::Error;
+use std::sync::Arc;
 
 #[cfg(test)]
 use {
-       lightning::chain::keysinterface::KeysInterface,
        lightning::util::ser::ReadableArgs,
        bitcoin::{BlockHash, Txid},
        bitcoin::hashes::hex::FromHex,
@@ -40,12 +44,24 @@ pub struct FilesystemPersister {
        path_to_channel_data: String,
 }
 
-impl<ChanSigner: ChannelKeys> DiskWriteable for ChannelMonitor<ChanSigner> {
+impl<Signer: Sign> DiskWriteable for ChannelMonitor<Signer> {
        fn write_to_file(&self, writer: &mut fs::File) -> Result<(), Error> {
                self.write(writer)
        }
 }
 
+impl<Signer: Sign, M, T, K, F, L> DiskWriteable for ChannelManager<Signer, Arc<M>, Arc<T>, Arc<K>, Arc<F>, Arc<L>>
+where M: chain::Watch<Signer>,
+      T: BroadcasterInterface,
+      K: KeysInterface<Signer=Signer>,
+      F: FeeEstimator,
+      L: Logger,
+{
+       fn write_to_file(&self, writer: &mut fs::File) -> Result<(), std::io::Error> {
+               self.write(writer)
+       }
+}
+
 impl FilesystemPersister {
        /// Initialize a new FilesystemPersister and set the path to the individual channels'
        /// files.
@@ -55,9 +71,29 @@ impl FilesystemPersister {
                }
        }
 
+       pub fn get_data_dir(&self) -> String {
+               self.path_to_channel_data.clone()
+       }
+
+       /// Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
+       /// initialization, within a file called "manager".
+       pub fn persist_manager<Signer, M, T, K, F, L>(
+               data_dir: String,
+               manager: &ChannelManager<Signer, Arc<M>, Arc<T>, Arc<K>, Arc<F>, Arc<L>>
+       ) -> Result<(), std::io::Error>
+       where Signer: Sign,
+             M: chain::Watch<Signer>,
+             T: BroadcasterInterface,
+             K: KeysInterface<Signer=Signer>,
+             F: FeeEstimator,
+             L: Logger
+       {
+               util::write_to_file(data_dir, "manager".to_string(), manager)
+       }
+
        #[cfg(test)]
        fn load_channel_data<Keys: KeysInterface>(&self, keys: &Keys) ->
-               Result<HashMap<OutPoint, ChannelMonitor<Keys::ChanKeySigner>>, ChannelMonitorUpdateErr> {
+               Result<HashMap<OutPoint, ChannelMonitor<Keys::Signer>>, ChannelMonitorUpdateErr> {
                if let Err(_) = fs::create_dir_all(&self.path_to_channel_data) {
                        return Err(ChannelMonitorUpdateErr::PermanentFailure);
                }
@@ -80,7 +116,7 @@ impl FilesystemPersister {
                        if contents.is_err() { return Err(ChannelMonitorUpdateErr::PermanentFailure); }
 
                        if let Ok((_, loaded_monitor)) =
-                               <(BlockHash, ChannelMonitor<Keys::ChanKeySigner>)>::read(&mut Cursor::new(&contents.unwrap()), keys) {
+                               <(BlockHash, ChannelMonitor<Keys::Signer>)>::read(&mut Cursor::new(&contents.unwrap()), keys) {
                                res.insert(OutPoint { txid: txid.unwrap(), index: index.unwrap() }, loaded_monitor);
                        } else {
                                return Err(ChannelMonitorUpdateErr::PermanentFailure);
@@ -90,7 +126,7 @@ impl FilesystemPersister {
        }
 }
 
-impl<ChanSigner: ChannelKeys + Send + Sync> channelmonitor::Persist<ChanSigner> for FilesystemPersister {
+impl<ChanSigner: Sign + Send + Sync> channelmonitor::Persist<ChanSigner> for FilesystemPersister {
        fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr> {
                let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
                util::write_to_file(self.path_to_channel_data.clone(), filename, monitor)