Remove println from lib.rs
[rapid-gossip-sync-server] / src / snapshot.rs
index 6894dbb6b24c10c5693d2b43ab469dacfa505eee..63975becd74204e732dbbd50c659cf45786a4cac 100644 (file)
@@ -1,20 +1,24 @@
 use std::collections::HashMap;
 use std::fs;
+use std::ops::Deref;
 use std::os::unix::fs::symlink;
 use std::sync::Arc;
 use std::time::{Duration, SystemTime, UNIX_EPOCH};
 
 use lightning::routing::gossip::NetworkGraph;
+use lightning::util::logger::Logger;
 
-use crate::{config, TestLogger};
+use crate::config;
+use crate::config::cache_path;
 
-pub(crate) struct Snapshotter {
-       network_graph: Arc<NetworkGraph<TestLogger>>,
+pub(crate) struct Snapshotter<L: Deref + Clone> where L::Target: Logger {
+       network_graph: Arc<NetworkGraph<L>>,
+       logger: L
 }
 
-impl Snapshotter {
-       pub fn new(network_graph: Arc<NetworkGraph<TestLogger>>) -> Self {
-               Self { network_graph }
+impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
+       pub fn new(network_graph: Arc<NetworkGraph<L>>, logger: L) -> Self {
+               Self { network_graph, logger }
        }
 
        pub(crate) async fn snapshot_gossip(&self) {
@@ -23,10 +27,10 @@ impl Snapshotter {
                let snapshot_sync_day_factors = [1, 2, 3, 4, 5, 6, 7, 14, 21, u64::MAX];
                let round_day_seconds = config::SNAPSHOT_CALCULATION_INTERVAL as u64;
 
-               let pending_snapshot_directory = "./res/snapshots_pending";
-               let pending_symlink_directory = "./res/symlinks_pending";
-               let finalized_snapshot_directory = "./res/snapshots";
-               let finalized_symlink_directory = "./res/symlinks";
+               let pending_snapshot_directory = format!("{}/snapshots_pending", cache_path());
+               let pending_symlink_directory = format!("{}/symlinks_pending", cache_path());
+               let finalized_snapshot_directory = format!("{}/snapshots", cache_path());
+               let finalized_symlink_directory = format!("{}/symlinks", cache_path());
                let relative_symlink_to_snapshot_path = "../snapshots";
 
                // this is gonna be a never-ending background job
@@ -76,7 +80,7 @@ impl Snapshotter {
                                {
                                        println!("Calculating {}-day snapshot", day_range);
                                        // calculate the snapshot
-                                       let snapshot = super::serialize_delta(network_graph_clone, current_last_sync_timestamp.clone() as u32, true).await;
+                                       let snapshot = super::serialize_delta(network_graph_clone, current_last_sync_timestamp.clone() as u32, self.logger.clone()).await;
 
                                        // persist the snapshot and update the symlink
                                        let snapshot_filename = format!("snapshot__calculated-at:{}__range:{}-days__previous-sync:{}.lngossip", reference_timestamp, day_range, current_last_sync_timestamp);
@@ -95,8 +99,9 @@ impl Snapshotter {
                                fs::write(&dummy_snapshot_path, dummy_snapshot).unwrap();
 
                                let dummy_symlink_path = format!("{}/{}.bin", pending_symlink_directory, reference_timestamp);
-                               println!("Symlinking dummy: {} -> {}", dummy_symlink_path, dummy_snapshot_path);
-                               symlink(&dummy_snapshot_path, &dummy_symlink_path).unwrap();
+                               let relative_dummy_snapshot_path = format!("{}/{}", relative_symlink_to_snapshot_path, dummy_filename);
+                               println!("Symlinking dummy: {} -> {}", dummy_symlink_path, relative_dummy_snapshot_path);
+                               symlink(&relative_dummy_snapshot_path, &dummy_symlink_path).unwrap();
                        }
 
                        for i in 0..10_001u64 {