use bitcoin::BlockHash;
use chrono::Utc;
use lightning::routing::network_graph::NetworkGraph;
+use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
use lightning::util::logger::{Logger, Record};
-use lightning::util::ser::{Readable, Writeable, Writer};
+use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter};
use std::net::SocketAddr;
use std::path::Path;
+use std::sync::Arc;
pub(crate) struct FilesystemLogger {
data_dir: String,
}
NetworkGraph::new(genesis_hash)
}
+
+pub(crate) fn persist_scorer(
+ path: &Path, scorer: &ProbabilisticScorer<Arc<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 = 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, graph: Arc<NetworkGraph>,
+) -> ProbabilisticScorer<Arc<NetworkGraph>> {
+ let params = ProbabilisticScoringParameters::default();
+ if let Ok(file) = File::open(path) {
+ if let Ok(scorer) =
+ ProbabilisticScorer::read(&mut BufReader::new(file), (params, Arc::clone(&graph)))
+ {
+ return scorer;
+ }
+ }
+ ProbabilisticScorer::new(params, graph)
+}