use crate::cli;
-use bitcoin::hashes::hex::FromHex;
use bitcoin::secp256k1::key::PublicKey;
-use bitcoin::{BlockHash, Txid};
-use lightning::chain::channelmonitor::ChannelMonitor;
-use lightning::chain::keysinterface::{InMemorySigner, KeysManager};
-use lightning::chain::transaction::OutPoint;
+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::{ReadableArgs, 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, Cursor, Write};
-use std::io::{BufRead, BufReader, Cursor};
+use std::io::{BufRead, BufReader, BufWriter};
use std::net::SocketAddr;
use std::path::Path;
use std::sync::Arc;
-use time::OffsetDateTime;
pub(crate) struct FilesystemLogger {
data_dir: String,
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,
Ok(peer_data)
}
-pub(crate) fn read_channelmonitors(
- path: String, keys_manager: Arc<KeysManager>,
-) -> Result<HashMap<OutPoint, (BlockHash, ChannelMonitor<InMemorySigner>)>, std::io::Error> {
- if !Path::new(&path).exists() {
- return Ok(HashMap::new());
- }
- let mut outpoint_to_channelmonitor = HashMap::new();
- for file_option in fs::read_dir(path).unwrap() {
- let file = file_option.unwrap();
- let owned_file_name = file.file_name();
- let filename = owned_file_name.to_str();
- if !filename.is_some() || !filename.unwrap().is_ascii() || filename.unwrap().len() < 65 {
- return Err(std::io::Error::new(
- std::io::ErrorKind::Other,
- "Invalid ChannelMonitor file name",
- ));
- }
-
- let txid = Txid::from_hex(filename.unwrap().split_at(64).0);
- if txid.is_err() {
- return Err(std::io::Error::new(
- std::io::ErrorKind::Other,
- "Invalid tx ID in filename",
- ));
- }
-
- let index = filename.unwrap().split_at(65).1.split('.').next().unwrap().parse();
- if index.is_err() {
- return Err(std::io::Error::new(
- std::io::ErrorKind::Other,
- "Invalid tx index in filename",
- ));
+pub(crate) fn read_network(path: &Path, genesis_hash: BlockHash) -> NetworkGraph {
+ if let Ok(file) = File::open(path) {
+ if let Ok(graph) = NetworkGraph::read(&mut BufReader::new(file)) {
+ return graph;
}
+ }
+ NetworkGraph::new(genesis_hash)
+}
- let contents = fs::read(&file.path())?;
+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(())
+ }
+}
- if let Ok((blockhash, channel_monitor)) =
- <(BlockHash, ChannelMonitor<InMemorySigner>)>::read(
- &mut Cursor::new(&contents),
- &*keys_manager,
- ) {
- outpoint_to_channelmonitor.insert(
- OutPoint { txid: txid.unwrap(), index: index.unwrap() },
- (blockhash, channel_monitor),
- );
- } else {
- return Err(std::io::Error::new(
- std::io::ErrorKind::Other,
- "Failed to deserialize ChannelMonitor",
- ));
+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;
}
}
- Ok(outpoint_to_channelmonitor)
+ ProbabilisticScorer::new(params, graph)
}