diff --git a/Cargo.toml b/Cargo.toml
index 99aeed7..c352d0b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,13 +5,16 @@ edition = "2021"
[dependencies]
bitcoin = "0.29"
-lightning = { version = "0.0.116" }
-lightning-block-sync = { version = "0.0.116", features=["rest-client"] }
-lightning-net-tokio = { version = "0.0.116" }
+lightning = { version = "0.0.117" }
+lightning-block-sync = { version = "0.0.117", features=["rest-client"] }
+lightning-net-tokio = { version = "0.0.117" }
tokio = { version = "1.25", features = ["full"] }
-tokio-postgres = { version="=0.7.5" }
+tokio-postgres = { version = "=0.7.5" }
futures = "0.3"
+[dev-dependencies]
+lightning-rapid-gossip-sync = { version = "0.0.117" }
+
[profile.dev]
panic = "abort"
diff --git a/src/config.rs b/src/config.rs
index 1de2482..0655aa7 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -74,13 +74,19 @@ pub(crate) fn cache_path() -> String {
pub(crate) fn db_connection_config() -> Config {
let mut config = Config::new();
- let host = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_HOST").unwrap_or("localhost".to_string());
- let user = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_USER").unwrap_or("alice".to_string());
- let db = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_NAME").unwrap_or("ln_graph_sync".to_string());
+ let env_name_prefix = if cfg!(test) {
+ "RAPID_GOSSIP_TEST_DB"
+ } else {
+ "RAPID_GOSSIP_SYNC_SERVER_DB"
+ };
+
+ let host = env::var(format!("{}{}", env_name_prefix, "_HOST")).unwrap_or("localhost".to_string());
+ let user = env::var(format!("{}{}", env_name_prefix, "_USER")).unwrap_or("alice".to_string());
+ let db = env::var(format!("{}{}", env_name_prefix, "_NAME")).unwrap_or("ln_graph_sync".to_string());
config.host(&host);
config.user(&user);
config.dbname(&db);
- if let Ok(password) = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_PASSWORD") {
+ if let Ok(password) = env::var(format!("{}{}", env_name_prefix, "_PASSWORD")) {
config.password(&password);
}
config
diff --git a/src/lib.rs b/src/lib.rs
index f56aca2..1852911 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -41,6 +41,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 +128,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
}
diff --git a/src/tests/mod.rs b/src/tests/mod.rs
new file mode 100644
index 0000000..b62406d
--- /dev/null
+++ b/src/tests/mod.rs
@@ -0,0 +1,216 @@
+//! Multi-module tests that use database fixtures
+
+use std::cell::RefCell;
+use std::sync::Arc;
+use std::time::{SystemTime, UNIX_EPOCH};
+use bitcoin::{BlockHash, Network};
+use bitcoin::secp256k1::ecdsa::Signature;
+use bitcoin::secp256k1::{Secp256k1, SecretKey};
+use bitcoin::hashes::Hash;
+use bitcoin::hashes::hex::ToHex;
+use bitcoin::hashes::sha256d::Hash as Sha256dHash;
+use lightning::ln::features::ChannelFeatures;
+use lightning::ln::msgs::{ChannelAnnouncement, ChannelUpdate, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
+use lightning::routing::gossip::{NetworkGraph, NodeId};
+use lightning::util::ser::Writeable;
+use lightning_rapid_gossip_sync::RapidGossipSync;
+use crate::{config, serialize_delta};
+use crate::persistence::GossipPersister;
+use crate::types::{GossipMessage, tests::TestLogger};
+
+const CLIENT_BACKDATE_INTERVAL: u32 = 3600 * 24 * 7; // client backdates RGS by a week
+
+thread_local! {
+ static DB_TEST_SCHEMA: RefCell