Update to LDK 0.0.121
[ldk-sample] / src / disk.rs
index 2c1834a6f0b7ac92caec7c712a659dd25786c718..9b9a72b229a90dbd2c4fed34a906537470ff7218 100644 (file)
@@ -1,16 +1,20 @@
-use crate::cli;
-use bitcoin::secp256k1::key::PublicKey;
-use bitcoin::BlockHash;
-use lightning::routing::network_graph::NetworkGraph;
+use crate::{cli, InboundPaymentInfoStorage, NetworkGraph, OutboundPaymentInfoStorage};
+use bitcoin::secp256k1::PublicKey;
+use bitcoin::Network;
+use chrono::Utc;
+use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringDecayParameters};
 use lightning::util::logger::{Logger, Record};
-use lightning::util::ser::{Readable, Writeable, Writer};
+use lightning::util::ser::{Readable, ReadableArgs, Writer};
 use std::collections::HashMap;
 use std::fs;
 use std::fs::File;
-use std::io::{BufRead, BufReader, BufWriter};
+use std::io::{BufRead, BufReader};
 use std::net::SocketAddr;
 use std::path::Path;
-use time::OffsetDateTime;
+use std::sync::Arc;
+
+pub(crate) const INBOUND_PAYMENTS_FNAME: &str = "inbound_payments";
+pub(crate) const OUTBOUND_PAYMENTS_FNAME: &str = "outbound_payments";
 
 pub(crate) struct FilesystemLogger {
        data_dir: String,
@@ -23,11 +27,14 @@ impl FilesystemLogger {
        }
 }
 impl Logger for FilesystemLogger {
-       fn log(&self, record: &Record) {
+       fn log(&self, record: Record) {
                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,
@@ -68,24 +75,44 @@ pub(crate) fn read_channel_peer_data(
        Ok(peer_data)
 }
 
-pub(crate) fn persist_network(path: &Path, network_graph: &NetworkGraph) -> 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 = network_graph.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_network(
+       path: &Path, network: Network, logger: Arc<FilesystemLogger>,
+) -> NetworkGraph {
+       if let Ok(file) = File::open(path) {
+               if let Ok(graph) = NetworkGraph::read(&mut BufReader::new(file), logger.clone()) {
+                       return graph;
+               }
        }
+       NetworkGraph::new(network, logger)
 }
 
-pub(crate) fn read_network(path: &Path, genesis_hash: BlockHash) -> NetworkGraph {
+pub(crate) fn read_inbound_payment_info(path: &Path) -> InboundPaymentInfoStorage {
        if let Ok(file) = File::open(path) {
-               if let Ok(graph) = NetworkGraph::read(&mut BufReader::new(file)) {
-                       return graph;
+               if let Ok(info) = InboundPaymentInfoStorage::read(&mut BufReader::new(file)) {
+                       return info;
+               }
+       }
+       InboundPaymentInfoStorage { payments: HashMap::new() }
+}
+
+pub(crate) fn read_outbound_payment_info(path: &Path) -> OutboundPaymentInfoStorage {
+       if let Ok(file) = File::open(path) {
+               if let Ok(info) = OutboundPaymentInfoStorage::read(&mut BufReader::new(file)) {
+                       return info;
+               }
+       }
+       OutboundPaymentInfoStorage { payments: HashMap::new() }
+}
+
+pub(crate) fn read_scorer(
+       path: &Path, graph: Arc<NetworkGraph>, logger: Arc<FilesystemLogger>,
+) -> ProbabilisticScorer<Arc<NetworkGraph>, Arc<FilesystemLogger>> {
+       let params = ProbabilisticScoringDecayParameters::default();
+       if let Ok(file) = File::open(path) {
+               let args = (params.clone(), Arc::clone(&graph), Arc::clone(&logger));
+               if let Ok(scorer) = ProbabilisticScorer::read(&mut BufReader::new(file), args) {
+                       return scorer;
                }
        }
-       NetworkGraph::new(genesis_hash)
+       ProbabilisticScorer::new(params, graph, logger)
 }