X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-persister%2Fsrc%2Flib.rs;h=4500621276dbef797ac42f94008cd72f2cfb8686;hb=83595dbfdc9669e52b7095a6c2bc087a4398cc1d;hp=b853b5796b6e7e226fb2efae1d00495ef5de389d;hpb=09714e6fe286b8cd5863b616748362b76c2fbdf0;p=rust-lightning diff --git a/lightning-persister/src/lib.rs b/lightning-persister/src/lib.rs index b853b579..45006212 100644 --- a/lightning-persister/src/lib.rs +++ b/lightning-persister/src/lib.rs @@ -3,8 +3,10 @@ #![deny(broken_intra_doc_links)] #![deny(missing_docs)] -#![cfg_attr(all(test, feature = "unstable"), feature(test))] -#[cfg(all(test, feature = "unstable"))] extern crate test; +#![cfg_attr(docsrs, feature(doc_auto_cfg))] + +#![cfg_attr(all(test, feature = "_bench_unstable"), feature(test))] +#[cfg(all(test, feature = "_bench_unstable"))] extern crate test; mod util; @@ -14,6 +16,7 @@ extern crate libc; use bitcoin::hash_types::{BlockHash, Txid}; use bitcoin::hashes::hex::{FromHex, ToHex}; +use lightning::routing::network_graph::NetworkGraph; use crate::util::DiskWriteable; use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; @@ -25,7 +28,7 @@ use lightning::ln::channelmanager::ChannelManager; use lightning::util::logger::Logger; use lightning::util::ser::{ReadableArgs, Writeable}; use std::fs; -use std::io::{Cursor, Error}; +use std::io::{Cursor, Error, Write}; use std::ops::Deref; use std::path::{Path, PathBuf}; @@ -46,7 +49,7 @@ pub struct FilesystemPersister { } impl DiskWriteable for ChannelMonitor { - fn write_to_file(&self, writer: &mut fs::File) -> Result<(), Error> { + fn write_to_file(&self, writer: &mut W) -> Result<(), Error> { self.write(writer) } } @@ -59,7 +62,13 @@ where F::Target: FeeEstimator, L::Target: Logger, { - fn write_to_file(&self, writer: &mut fs::File) -> Result<(), std::io::Error> { + fn write_to_file(&self, writer: &mut W) -> Result<(), std::io::Error> { + self.write(writer) + } +} + +impl DiskWriteable for NetworkGraph { + fn write_to_file(&self, writer: &mut W) -> Result<(), std::io::Error> { self.write(writer) } } @@ -101,6 +110,13 @@ impl FilesystemPersister { util::write_to_file(path, "manager".to_string(), manager) } + /// Write the provided `NetworkGraph` to the path provided at `FilesystemPersister` + /// initialization, within a file called "network_graph" + pub fn persist_network_graph(data_dir: String, network_graph: &NetworkGraph) -> Result<(), std::io::Error> { + let path = PathBuf::from(data_dir); + util::write_to_file(path, "network_graph".to_string(), network_graph) + } + /// Read `ChannelMonitor`s from disk. pub fn read_channelmonitors ( &self, keys_manager: K @@ -122,6 +138,12 @@ impl FilesystemPersister { "Invalid ChannelMonitor file name", )); } + if filename.unwrap().ends_with(".tmp") { + // If we were in the middle of committing an new update and crashed, it should be + // safe to ignore the update - we should never have returned to the caller and + // irrevocably committed to the new state in any way. + continue; + } let txid = Txid::from_hex(filename.unwrap().split_at(64).0); if txid.is_err() { @@ -362,7 +384,7 @@ mod tests { } } -#[cfg(all(test, feature = "unstable"))] +#[cfg(all(test, feature = "_bench_unstable"))] pub mod bench { use test::Bencher;