Merge pull request #77 from arik-so/static-channel-reminders
[rapid-gossip-sync-server] / src / lib.rs
index f56aca260f218c0798df4b6ef2300c8534c4dcf5..64f0ff24616ec619cd95cbebe71b8808feb06f88 100644 (file)
@@ -14,6 +14,7 @@ use std::fs::File;
 use std::io::BufReader;
 use std::ops::Deref;
 use std::sync::Arc;
+use bitcoin::blockdata::constants::ChainHash;
 use lightning::log_info;
 
 use lightning::routing::gossip::{NetworkGraph, NodeId};
@@ -41,6 +42,9 @@ mod verifier;
 
 pub mod types;
 
+#[cfg(test)]
+mod tests;
+
 /// The purpose of this prefix is to identify the serialization format, should other rapid gossip
 /// sync formats arise in the future.
 ///
@@ -125,6 +129,14 @@ pub(crate) async fn connect_to_db() -> Client {
                }
        });
 
+       #[cfg(test)]
+       {
+               let schema_name = tests::db_test_schema();
+               let schema_creation_command = format!("CREATE SCHEMA IF NOT EXISTS {}", schema_name);
+               client.execute(&schema_creation_command, &[]).await.unwrap();
+               client.execute(&format!("SET search_path TO {}", schema_name), &[]).await.unwrap();
+       }
+
        client.execute("set time zone UTC", &[]).await.unwrap();
        client
 }
@@ -146,8 +158,7 @@ fn serialize_empty_blob(current_timestamp: u64) -> Vec<u8> {
        let mut blob = GOSSIP_PREFIX.to_vec();
 
        let network = config::network();
-       let genesis_block = bitcoin::blockdata::constants::genesis_block(network);
-       let chain_hash = genesis_block.block_hash();
+       let chain_hash = ChainHash::using_genesis_block(network);
        chain_hash.write(&mut blob).unwrap();
 
        let blob_timestamp = Snapshotter::<Arc<RGSSLogger>>::round_down_to_nearest_multiple(current_timestamp, SYMLINK_GRANULARITY_INTERVAL as u64) as u32;
@@ -160,7 +171,7 @@ fn serialize_empty_blob(current_timestamp: u64) -> Vec<u8> {
        blob
 }
 
-async fn serialize_delta<L: Deref + Clone>(network_graph: Arc<NetworkGraph<L>>, last_sync_timestamp: u32, logger: L) -> SerializedResponse where L::Target: Logger {
+async fn serialize_delta<L: Deref + Clone>(network_graph: Arc<NetworkGraph<L>>, last_sync_timestamp: u32, snapshot_reference_timestamp: Option<u64>, logger: L) -> SerializedResponse where L::Target: Logger {
        let client = connect_to_db().await;
 
        network_graph.remove_stale_channels_and_tracking();
@@ -189,7 +200,7 @@ async fn serialize_delta<L: Deref + Clone>(network_graph: Arc<NetworkGraph<L>>,
        };
 
        let mut delta_set = DeltaSet::new();
-       lookup::fetch_channel_announcements(&mut delta_set, network_graph, &client, last_sync_timestamp, logger.clone()).await;
+       lookup::fetch_channel_announcements(&mut delta_set, network_graph, &client, last_sync_timestamp, snapshot_reference_timestamp, logger.clone()).await;
        log_info!(logger, "announcement channel count: {}", delta_set.len());
        lookup::fetch_channel_updates(&mut delta_set, &client, last_sync_timestamp, logger.clone()).await;
        log_info!(logger, "update-fetched channel count: {}", delta_set.len());