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;
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()
})
}
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);
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;
+}