Persist channel scores
[ldk-sample] / src / disk.rs
index 2c1834a6f0b7ac92caec7c712a659dd25786c718..7be070c962f4d73e3c9445c8a5a8730fedd71fe3 100644 (file)
@@ -1,7 +1,9 @@
 use crate::cli;
 use bitcoin::secp256k1::key::PublicKey;
 use bitcoin::BlockHash;
+use chrono::Utc;
 use lightning::routing::network_graph::NetworkGraph;
+use lightning::routing::scorer::Scorer;
 use lightning::util::logger::{Logger, Record};
 use lightning::util::ser::{Readable, Writeable, Writer};
 use std::collections::HashMap;
@@ -10,7 +12,6 @@ use std::fs::File;
 use std::io::{BufRead, BufReader, BufWriter};
 use std::net::SocketAddr;
 use std::path::Path;
-use time::OffsetDateTime;
 
 pub(crate) struct FilesystemLogger {
        data_dir: String,
@@ -27,7 +28,10 @@ impl Logger for FilesystemLogger {
                let raw_log = record.args.to_string();
                let log = format!(
                        "{} {:<5} [{}:{}] {}\n",
-                       OffsetDateTime::now_utc().format("%F %T"),
+                       // Note that a "real" lightning node almost certainly does *not* want subsecond
+                       // precision for message-receipt information as it makes log entries a target for
+                       // deanonymization attacks. For testing, however, its quite useful.
+                       Utc::now().format("%Y-%m-%d %H:%M:%S%.3f"),
                        record.level.to_string(),
                        record.module_path,
                        record.line,
@@ -89,3 +93,25 @@ pub(crate) fn read_network(path: &Path, genesis_hash: BlockHash) -> NetworkGraph
        }
        NetworkGraph::new(genesis_hash)
 }
+
+pub(crate) fn persist_scorer(path: &Path, scorer: &Scorer) -> std::io::Result<()> {
+       let mut tmp_path = path.to_path_buf().into_os_string();
+       tmp_path.push(".tmp");
+       let file = fs::OpenOptions::new().write(true).create(true).open(&tmp_path)?;
+       let write_res = scorer.write(&mut BufWriter::new(file));
+       if let Err(e) = write_res.and_then(|_| fs::rename(&tmp_path, path)) {
+               let _ = fs::remove_file(&tmp_path);
+               Err(e)
+       } else {
+               Ok(())
+       }
+}
+
+pub(crate) fn read_scorer(path: &Path) -> Scorer {
+       if let Ok(file) = File::open(path) {
+               if let Ok(scorer) = Scorer::read(&mut BufReader::new(file)) {
+                       return scorer;
+               }
+       }
+       Scorer::default()
+}