]> git.bitcoin.ninja Git - rapid-gossip-sync-server/commitdiff
Replicate Mutiny test scenarios.
authorArik Sosman <git@arik.io>
Mon, 16 Oct 2023 15:27:45 +0000 (08:27 -0700)
committerArik Sosman <git@arik.io>
Thu, 19 Oct 2023 19:24:38 +0000 (12:24 -0700)
Cargo.toml
src/tests/mod.rs

index c352d0b4dc75ef67345cd526de71267e61031f9c..f6d05157d9d1bd2c934b35d8b1408c344024569a 100644 (file)
@@ -13,6 +13,7 @@ tokio-postgres = { version = "=0.7.5" }
 futures = "0.3"
 
 [dev-dependencies]
+lightning = { version = "0.0.117", features = ["_test_utils"] }
 lightning-rapid-gossip-sync = { version = "0.0.117" }
 
 [profile.dev]
index f26cea8b44f180f3756b632e827c393fe857a57d..eabc7f494adb83df1c18368ddcb4c36fd030009f 100644 (file)
@@ -2,6 +2,7 @@
 
 use std::cell::RefCell;
 use std::sync::Arc;
+use std::thread;
 use std::time::{SystemTime, UNIX_EPOCH};
 use bitcoin::{BlockHash, Network};
 use bitcoin::secp256k1::ecdsa::Signature;
@@ -39,7 +40,7 @@ fn current_time() -> u32 {
 
 pub(crate) fn db_test_schema() -> String {
        DB_TEST_SCHEMA.with(|suffix_reference| {
-               let mut suffix_option = suffix_reference.borrow();
+               let suffix_option = suffix_reference.borrow();
                suffix_option.as_ref().unwrap().clone()
        })
 }
@@ -114,7 +115,10 @@ impl SchemaSanitizer {
                        let unix_time = current_time.duration_since(UNIX_EPOCH).expect("Time went backwards");
                        let timestamp_seconds = unix_time.as_secs();
                        let timestamp_nanos = unix_time.as_nanos();
-                       let preimage = format!("{}", timestamp_nanos);
+                       // sometimes Rust thinks two tests start at the same nanosecond, causing a schema conflict
+                       let thread_id = thread::current().id();
+                       let preimage = format!("{:?}-{}", thread_id, timestamp_nanos);
+                       println!("test schema preimage: {}", preimage);
                        let suffix = Sha256dHash::hash(preimage.as_bytes()).into_inner().to_hex();
                        // the schema must start with a letter
                        let schema = format!("test_{}_{}", timestamp_seconds, suffix);
@@ -215,3 +219,363 @@ async fn test_trivial_setup() {
        assert_eq!(last_update_seen_a, update_result - CLIENT_BACKDATE_INTERVAL);
        assert_eq!(last_update_seen_b, update_result - CLIENT_BACKDATE_INTERVAL);
 }
+
+#[tokio::test]
+async fn test_full_snapshot_recency() {
+       let _sanitizer = SchemaSanitizer::new();
+       let logger = Arc::new(TestLogger::new());
+       let network_graph = NetworkGraph::new(Network::Bitcoin, logger.clone());
+       let network_graph_arc = Arc::new(network_graph);
+
+       let short_channel_id = 1;
+       let timestamp = current_time();
+       println!("timestamp: {}", timestamp);
+
+       { // seed the db
+               let (mut persister, receiver) = GossipPersister::new(network_graph_arc.clone(), logger.clone());
+               let announcement = generate_announcement(short_channel_id);
+               network_graph_arc.update_channel_from_announcement_no_lookup(&announcement).unwrap();
+               receiver.send(GossipMessage::ChannelAnnouncement(announcement, None)).await.unwrap();
+
+               { // direction false
+                       { // first update
+                               let update = generate_update(short_channel_id, false, timestamp - 1, 0, 0, 0, 0, 38);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       { // second update
+                               let update = generate_update(short_channel_id, false, timestamp, 0, 0, 0, 0, 39);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+               }
+               { // direction true
+                       { // first and only update
+                               let update = generate_update(short_channel_id, true, timestamp, 0, 0, 0, 0, 10);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+               }
+
+               drop(receiver);
+               persister.persist_gossip().await;
+       }
+
+       let client_graph = NetworkGraph::new(Network::Bitcoin, logger.clone());
+       let client_graph_arc = Arc::new(client_graph);
+
+       { // sync after initial seed
+               let serialization = serialize_delta(network_graph_arc.clone(), 0, logger.clone()).await;
+               logger.assert_log_contains("rapid_gossip_sync_server", "announcement channel count: 1", 1);
+
+               let channel_count = network_graph_arc.read_only().channels().len();
+
+               assert_eq!(channel_count, 1);
+               assert_eq!(serialization.message_count, 3);
+               assert_eq!(serialization.announcement_count, 1);
+               assert_eq!(serialization.update_count, 2);
+
+               let rgs = RapidGossipSync::new(client_graph_arc.clone(), logger.clone());
+               let update_result = rgs.update_network_graph(&serialization.data).unwrap();
+               // the update result must be a multiple of our snapshot granularity
+               assert_eq!(update_result % config::snapshot_generation_interval(), 0);
+               assert!(update_result < timestamp);
+
+               let readonly_graph = client_graph_arc.read_only();
+               let channels = readonly_graph.channels();
+               let client_channel_count = channels.len();
+               assert_eq!(client_channel_count, 1);
+
+               let first_channel = channels.get(&short_channel_id).unwrap();
+               assert!(&first_channel.announcement_message.is_none());
+               // ensure the update in one direction shows the latest fee
+               assert_eq!(first_channel.one_to_two.as_ref().unwrap().fees.proportional_millionths, 39);
+               assert_eq!(first_channel.two_to_one.as_ref().unwrap().fees.proportional_millionths, 10);
+       }
+
+       clean_test_db().await;
+}
+
+#[tokio::test]
+async fn test_full_snapshot_recency_with_wrong_seen_order() {
+       let _sanitizer = SchemaSanitizer::new();
+       let logger = Arc::new(TestLogger::new());
+       let network_graph = NetworkGraph::new(Network::Bitcoin, logger.clone());
+       let network_graph_arc = Arc::new(network_graph);
+
+       let short_channel_id = 1;
+       let timestamp = current_time();
+       println!("timestamp: {}", timestamp);
+
+       { // seed the db
+               let (mut persister, receiver) = GossipPersister::new(network_graph_arc.clone(), logger.clone());
+               let announcement = generate_announcement(short_channel_id);
+               network_graph_arc.update_channel_from_announcement_no_lookup(&announcement).unwrap();
+               receiver.send(GossipMessage::ChannelAnnouncement(announcement, None)).await.unwrap();
+
+               { // direction false
+                       { // first update, seen latest
+                               let update = generate_update(short_channel_id, false, timestamp - 1, 0, 0, 0, 0, 38);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, Some(timestamp))).await.unwrap();
+                       }
+                       { // second update, seen first
+                               let update = generate_update(short_channel_id, false, timestamp, 0, 0, 0, 0, 39);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, Some(timestamp - 1))).await.unwrap();
+                       }
+               }
+               { // direction true
+                       { // first and only update
+                               let update = generate_update(short_channel_id, true, timestamp, 0, 0, 0, 0, 10);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+               }
+
+               drop(receiver);
+               persister.persist_gossip().await;
+       }
+
+       let client_graph = NetworkGraph::new(Network::Bitcoin, logger.clone());
+       let client_graph_arc = Arc::new(client_graph);
+
+       { // sync after initial seed
+               let serialization = serialize_delta(network_graph_arc.clone(), 0, logger.clone()).await;
+               logger.assert_log_contains("rapid_gossip_sync_server", "announcement channel count: 1", 1);
+
+               let channel_count = network_graph_arc.read_only().channels().len();
+
+               assert_eq!(channel_count, 1);
+               assert_eq!(serialization.message_count, 3);
+               assert_eq!(serialization.announcement_count, 1);
+               assert_eq!(serialization.update_count, 2);
+
+               let rgs = RapidGossipSync::new(client_graph_arc.clone(), logger.clone());
+               let update_result = rgs.update_network_graph(&serialization.data).unwrap();
+               // the update result must be a multiple of our snapshot granularity
+               assert_eq!(update_result % config::snapshot_generation_interval(), 0);
+               assert!(update_result < timestamp);
+
+               let readonly_graph = client_graph_arc.read_only();
+               let channels = readonly_graph.channels();
+               let client_channel_count = channels.len();
+               assert_eq!(client_channel_count, 1);
+
+               let first_channel = channels.get(&short_channel_id).unwrap();
+               assert!(&first_channel.announcement_message.is_none());
+               // ensure the update in one direction shows the latest fee
+               assert_eq!(first_channel.one_to_two.as_ref().unwrap().fees.proportional_millionths, 39);
+               assert_eq!(first_channel.two_to_one.as_ref().unwrap().fees.proportional_millionths, 10);
+       }
+
+       clean_test_db().await;
+}
+
+#[tokio::test]
+async fn test_full_snapshot_recency_with_wrong_propagation_order() {
+       let _sanitizer = SchemaSanitizer::new();
+       let logger = Arc::new(TestLogger::new());
+       let network_graph = NetworkGraph::new(Network::Bitcoin, logger.clone());
+       let network_graph_arc = Arc::new(network_graph);
+
+       let short_channel_id = 1;
+       let timestamp = current_time();
+       println!("timestamp: {}", timestamp);
+
+       { // seed the db
+               let (mut persister, receiver) = GossipPersister::new(network_graph_arc.clone(), logger.clone());
+               let announcement = generate_announcement(short_channel_id);
+               network_graph_arc.update_channel_from_announcement_no_lookup(&announcement).unwrap();
+               receiver.send(GossipMessage::ChannelAnnouncement(announcement, None)).await.unwrap();
+
+               { // direction false
+                       // apply updates in their timestamp order
+                       let update_1 = generate_update(short_channel_id, false, timestamp - 1, 0, 0, 0, 0, 38);
+                       let update_2 = generate_update(short_channel_id, false, timestamp, 0, 0, 0, 0, 39);
+                       network_graph_arc.update_channel_unsigned(&update_1.contents).unwrap();
+                       network_graph_arc.update_channel_unsigned(&update_2.contents).unwrap();
+
+                       // propagate updates in their seen order
+                       receiver.send(GossipMessage::ChannelUpdate(update_2, Some(timestamp - 1))).await.unwrap();
+                       receiver.send(GossipMessage::ChannelUpdate(update_1, Some(timestamp))).await.unwrap();
+               }
+               { // direction true
+                       { // first and only update
+                               let update = generate_update(short_channel_id, true, timestamp, 0, 0, 0, 0, 10);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+               }
+
+               drop(receiver);
+               persister.persist_gossip().await;
+       }
+
+       let client_graph = NetworkGraph::new(Network::Bitcoin, logger.clone());
+       let client_graph_arc = Arc::new(client_graph);
+
+       { // sync after initial seed
+               let serialization = serialize_delta(network_graph_arc.clone(), 0, logger.clone()).await;
+               logger.assert_log_contains("rapid_gossip_sync_server", "announcement channel count: 1", 1);
+
+               let channel_count = network_graph_arc.read_only().channels().len();
+
+               assert_eq!(channel_count, 1);
+               assert_eq!(serialization.message_count, 3);
+               assert_eq!(serialization.announcement_count, 1);
+               assert_eq!(serialization.update_count, 2);
+
+               let rgs = RapidGossipSync::new(client_graph_arc.clone(), logger.clone());
+               let update_result = rgs.update_network_graph(&serialization.data).unwrap();
+               // the update result must be a multiple of our snapshot granularity
+               assert_eq!(update_result % config::snapshot_generation_interval(), 0);
+               assert!(update_result < timestamp);
+
+               let readonly_graph = client_graph_arc.read_only();
+               let channels = readonly_graph.channels();
+               let client_channel_count = channels.len();
+               assert_eq!(client_channel_count, 1);
+
+               let first_channel = channels.get(&short_channel_id).unwrap();
+               assert!(&first_channel.announcement_message.is_none());
+               // ensure the update in one direction shows the latest fee
+               assert_eq!(first_channel.one_to_two.as_ref().unwrap().fees.proportional_millionths, 39);
+               assert_eq!(first_channel.two_to_one.as_ref().unwrap().fees.proportional_millionths, 10);
+       }
+
+       clean_test_db().await;
+}
+
+#[tokio::test]
+async fn test_full_snapshot_mutiny_scenario() {
+       let _sanitizer = SchemaSanitizer::new();
+       let logger = Arc::new(TestLogger::new());
+       let network_graph = NetworkGraph::new(Network::Bitcoin, logger.clone());
+       let network_graph_arc = Arc::new(network_graph);
+
+       let short_channel_id = 873706024403271681;
+       let timestamp = current_time();
+       // let oldest_simulation_timestamp = 1693300588;
+       let latest_simulation_timestamp = 1695909301;
+       let timestamp_offset = timestamp - latest_simulation_timestamp;
+       println!("timestamp: {}", timestamp);
+
+       { // seed the db
+               let (mut persister, receiver) = GossipPersister::new(network_graph_arc.clone(), logger.clone());
+               let announcement = generate_announcement(short_channel_id);
+               network_graph_arc.update_channel_from_announcement_no_lookup(&announcement).unwrap();
+               receiver.send(GossipMessage::ChannelAnnouncement(announcement, None)).await.unwrap();
+
+               { // direction false
+                       {
+                               let update = generate_update(short_channel_id, false, 1693507369 + timestamp_offset, 0, 0, 0, 0, 38);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1693680390 + timestamp_offset, 0, 0, 0, 0, 38);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1693749109 + timestamp_offset, 0, 0, 0, 0, 200);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1693925190 + timestamp_offset, 0, 0, 0, 0, 200);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1694008323 + timestamp_offset, 0, 0, 0, 0, 209);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1694219924 + timestamp_offset, 0, 0, 0, 0, 209);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1694267536 + timestamp_offset, 0, 0, 0, 0, 210);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1694458808 + timestamp_offset, 0, 0, 0, 0, 210);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1694526734 + timestamp_offset, 0, 0, 0, 0, 200);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1694794765 + timestamp_offset, 0, 0, 0, 0, 200);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, Some(1695909301 + 2 * config::SYMLINK_GRANULARITY_INTERVAL + timestamp_offset))).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, false, 1695909301 + timestamp_offset, 0, 0, 0, 0, 130);
+                               // network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+               }
+               { // direction true
+                       {
+                               let update = generate_update(short_channel_id, true, 1693300588 + timestamp_offset, 0, 0, 0, 0, 10);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+                       {
+                               let update = generate_update(short_channel_id, true, 1695003621 + timestamp_offset, 0, 0, 0, 0, 10);
+                               network_graph_arc.update_channel_unsigned(&update.contents).unwrap();
+                               receiver.send(GossipMessage::ChannelUpdate(update, None)).await.unwrap();
+                       }
+               }
+
+               drop(receiver);
+               persister.persist_gossip().await;
+       }
+
+       let client_graph = NetworkGraph::new(Network::Bitcoin, logger.clone());
+       let client_graph_arc = Arc::new(client_graph);
+
+       { // sync after initial seed
+               let serialization = serialize_delta(network_graph_arc.clone(), 0, logger.clone()).await;
+               logger.assert_log_contains("rapid_gossip_sync_server", "announcement channel count: 1", 1);
+
+               let channel_count = network_graph_arc.read_only().channels().len();
+
+               assert_eq!(channel_count, 1);
+               assert_eq!(serialization.message_count, 3);
+               assert_eq!(serialization.announcement_count, 1);
+               assert_eq!(serialization.update_count, 2);
+
+               let rgs = RapidGossipSync::new(client_graph_arc.clone(), logger.clone());
+               let update_result = rgs.update_network_graph(&serialization.data).unwrap();
+               println!("update result: {}", update_result);
+               // the update result must be a multiple of our snapshot granularity
+               assert_eq!(update_result % config::snapshot_generation_interval(), 0);
+               assert!(update_result < timestamp);
+
+               let timestamp_delta = timestamp - update_result;
+               println!("timestamp delta: {}", timestamp_delta);
+               assert!(timestamp_delta < config::snapshot_generation_interval());
+
+               let readonly_graph = client_graph_arc.read_only();
+               let channels = readonly_graph.channels();
+               let client_channel_count = channels.len();
+               assert_eq!(client_channel_count, 1);
+
+               let first_channel = channels.get(&short_channel_id).unwrap();
+               assert!(&first_channel.announcement_message.is_none());
+               assert_eq!(first_channel.one_to_two.as_ref().unwrap().fees.proportional_millionths, 130);
+               assert_eq!(first_channel.two_to_one.as_ref().unwrap().fees.proportional_millionths, 10);
+       }
+
+       clean_test_db().await;
+}