1 use crate::{cli, NetworkGraph, PaymentInfoStorage};
2 use bitcoin::secp256k1::PublicKey;
5 use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringDecayParameters};
6 use lightning::util::logger::{Logger, Record};
7 use lightning::util::ser::{Readable, ReadableArgs, Writer};
8 use std::collections::HashMap;
11 use std::io::{BufRead, BufReader};
12 use std::net::SocketAddr;
16 pub(crate) const INBOUND_PAYMENTS_FNAME: &str = "inbound_payments";
17 pub(crate) const OUTBOUND_PAYMENTS_FNAME: &str = "outbound_payments";
19 pub(crate) struct FilesystemLogger {
22 impl FilesystemLogger {
23 pub(crate) fn new(data_dir: String) -> Self {
24 let logs_path = format!("{}/logs", data_dir);
25 fs::create_dir_all(logs_path.clone()).unwrap();
26 Self { data_dir: logs_path }
29 impl Logger for FilesystemLogger {
30 fn log(&self, record: &Record) {
31 let raw_log = record.args.to_string();
33 "{} {:<5} [{}:{}] {}\n",
34 // Note that a "real" lightning node almost certainly does *not* want subsecond
35 // precision for message-receipt information as it makes log entries a target for
36 // deanonymization attacks. For testing, however, its quite useful.
37 Utc::now().format("%Y-%m-%d %H:%M:%S%.3f"),
38 record.level.to_string(),
43 let logs_file_path = format!("{}/logs.txt", self.data_dir.clone());
44 fs::OpenOptions::new()
49 .write_all(log.as_bytes())
53 pub(crate) fn persist_channel_peer(path: &Path, peer_info: &str) -> std::io::Result<()> {
54 let mut file = fs::OpenOptions::new().create(true).append(true).open(path)?;
55 file.write_all(format!("{}\n", peer_info).as_bytes())
58 pub(crate) fn read_channel_peer_data(
60 ) -> Result<HashMap<PublicKey, SocketAddr>, std::io::Error> {
61 let mut peer_data = HashMap::new();
62 if !Path::new(&path).exists() {
63 return Ok(HashMap::new());
65 let file = File::open(path)?;
66 let reader = BufReader::new(file);
67 for line in reader.lines() {
68 match cli::parse_peer_info(line.unwrap()) {
69 Ok((pubkey, socket_addr)) => {
70 peer_data.insert(pubkey, socket_addr);
72 Err(e) => return Err(e),
78 pub(crate) fn read_network(
79 path: &Path, network: Network, logger: Arc<FilesystemLogger>,
81 if let Ok(file) = File::open(path) {
82 if let Ok(graph) = NetworkGraph::read(&mut BufReader::new(file), logger.clone()) {
86 NetworkGraph::new(network, logger)
89 pub(crate) fn read_payment_info(path: &Path) -> PaymentInfoStorage {
90 if let Ok(file) = File::open(path) {
91 if let Ok(info) = PaymentInfoStorage::read(&mut BufReader::new(file)) {
95 PaymentInfoStorage { payments: HashMap::new() }
98 pub(crate) fn read_scorer(
99 path: &Path, graph: Arc<NetworkGraph>, logger: Arc<FilesystemLogger>,
100 ) -> ProbabilisticScorer<Arc<NetworkGraph>, Arc<FilesystemLogger>> {
101 let params = ProbabilisticScoringDecayParameters::default();
102 if let Ok(file) = File::open(path) {
103 let args = (params.clone(), Arc::clone(&graph), Arc::clone(&logger));
104 if let Ok(scorer) = ProbabilisticScorer::read(&mut BufReader::new(file), args) {
108 ProbabilisticScorer::new(params, graph, logger)