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;
}
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()
+}
));
};
- // Step 16: Create InvoicePayer
+ // Step 16: Initialize routing Scorer
+ let scorer_path = format!("{}/scorer", ldk_data_dir.clone());
+ let scorer = Arc::new(Mutex::new(disk::read_scorer(Path::new(&scorer_path))));
+ let scorer_persist = Arc::clone(&scorer);
+ tokio::spawn(async move {
+ let mut interval = tokio::time::interval(Duration::from_secs(600));
+ loop {
+ interval.tick().await;
+ if disk::persist_scorer(Path::new(&scorer_path), &scorer_persist.lock().unwrap())
+ .is_err()
+ {
+ // Persistence errors here are non-fatal as channels will be re-scored as payments
+ // fail, but they may indicate a disk error which could be fatal elsewhere.
+ eprintln!("Warning: Failed to persist scorer, check your disk and permissions");
+ }
+ }
+ });
+
+ // Step 17: Create InvoicePayer
let router = DefaultRouter::new(network_graph.clone(), logger.clone());
- let scorer = Arc::new(Mutex::new(Scorer::default()));
let invoice_payer = Arc::new(InvoicePayer::new(
channel_manager.clone(),
router,
payment::RetryAttempts(5),
));
- // Step 17: Persist ChannelManager
+ // Step 18: Persist ChannelManager
let data_dir = ldk_data_dir.clone();
let persist_channel_manager_callback =
move |node: &ChannelManager| FilesystemPersister::persist_manager(data_dir.clone(), &*node);
- // Step 18: Background Processing
+ // Step 19: Background Processing
let background_processor = BackgroundProcessor::start(
persist_channel_manager_callback,
invoice_payer.clone(),