X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Flookup.rs;h=a05bf50658e0312765b4d8b156f606ec74f7e728;hb=b8c59416210dbabb4a5b4629c657e4da13c98485;hp=b48b375921ef07df6b40a3a639eeee98e5237ce0;hpb=840194965a1465b2b2057413e7300782388fb373;p=rapid-gossip-sync-server diff --git a/src/lookup.rs b/src/lookup.rs index b48b375..a05bf50 100644 --- a/src/lookup.rs +++ b/src/lookup.rs @@ -31,17 +31,25 @@ pub(super) struct DirectedUpdateDelta { pub(super) last_update_before_seen: Option, pub(super) mutated_properties: MutatedProperties, pub(super) latest_update_after_seen: Option, + pub(super) serialization_update_flags: Option, } pub(super) struct ChannelDelta { pub(super) announcement: Option, pub(super) updates: (Option, Option), - pub(super) first_update_seen: Option, + pub(super) first_bidirectional_updates_seen: Option, + /// The seen timestamp of the older of the two latest directional updates + pub(super) requires_reminder: bool, } impl Default for ChannelDelta { fn default() -> Self { - Self { announcement: None, updates: (None, None), first_update_seen: None } + Self { + announcement: None, + updates: (None, None), + first_bidirectional_updates_seen: None, + requires_reminder: false, + } } } @@ -51,6 +59,7 @@ impl Default for DirectedUpdateDelta { last_update_before_seen: None, mutated_properties: MutatedProperties::default(), latest_update_after_seen: None, + serialization_update_flags: None, } } } @@ -63,14 +72,13 @@ pub(super) async fn connect_to_db() -> (Client, Connection) /// Fetch all the channel announcements that are presently in the network graph, regardless of /// whether they had been seen before. /// Also include all announcements for which the first update was announced -/// after `last_syc_timestamp` +/// after `last_sync_timestamp` pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, network_graph: Arc>, client: &Client, last_sync_timestamp: u32) { - let last_sync_timestamp_object = SystemTime::UNIX_EPOCH.add(Duration::from_secs(last_sync_timestamp as u64)); println!("Obtaining channel ids from network graph"); let channel_ids = { let read_only_graph = network_graph.read_only(); println!("Retrieved read-only network graph copy"); - let channel_iterator = read_only_graph.channels().into_iter(); + let channel_iterator = read_only_graph.channels().unordered_iter(); channel_iterator .filter(|c| c.1.announcement_message.is_some()) .map(|c| c.1.announcement_message.as_ref().unwrap().contents.short_channel_id as i64) @@ -90,29 +98,106 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ let current_seen_timestamp_object: SystemTime = current_announcement_row.get("seen"); let current_seen_timestamp: u32 = current_seen_timestamp_object.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32; - let mut current_channel_delta = delta_set.entry(scid).or_insert(ChannelDelta::default()); + let current_channel_delta = delta_set.entry(scid).or_insert(ChannelDelta::default()); (*current_channel_delta).announcement = Some(AnnouncementDelta { announcement: unsigned_announcement, seen: current_seen_timestamp, }); } - println!("Obtaining channel announcements whose first channel updates had not been seen yet"); + { + // THIS STEP IS USED TO DETERMINE IF A CHANNEL SHOULD BE OMITTED FROM THE DELTA + + println!("Annotating channel announcements whose oldest channel update in a given direction occurred after the last sync"); + // Steps: + // — Obtain all updates, distinct by (scid, direction), ordered by seen DESC // to find the oldest update in a given direction + // — From those updates, select distinct by (scid), ordered by seen DESC (to obtain the newer one per direction) + // This will allow us to mark the first time updates in both directions were seen + + // here is where the channels whose first update in either direction occurred after + // `last_seen_timestamp` are added to the selection + let newer_oldest_directional_updates = client.query(" + SELECT DISTINCT ON (short_channel_id) * + FROM ( + SELECT DISTINCT ON (short_channel_id, direction) short_channel_id, seen + FROM channel_updates + WHERE short_channel_id = any($1) + ORDER BY seen ASC, short_channel_id ASC, direction ASC + ) AS directional_last_seens + ORDER BY short_channel_id ASC, seen DESC + ", &[&channel_ids]).await.unwrap(); + + for current_row in newer_oldest_directional_updates { + let scid: i64 = current_row.get("short_channel_id"); + let current_seen_timestamp_object: SystemTime = current_row.get("seen"); + let current_seen_timestamp: u32 = current_seen_timestamp_object.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32; + + if current_seen_timestamp > last_sync_timestamp { + // the newer of the two oldest seen directional updates came after last sync timestamp + let current_channel_delta = delta_set.entry(scid as u64).or_insert(ChannelDelta::default()); + // first time a channel was seen in both directions + (*current_channel_delta).first_bidirectional_updates_seen = Some(current_seen_timestamp); + } + } + } - // here is where the channels whose first update in either direction occurred after - // `last_seen_timestamp` are added to the selection - let unannounced_rows = client.query("SELECT blob_signed, seen FROM (SELECT DISTINCT ON (short_channel_id) short_channel_id, blob_signed, seen FROM channel_updates ORDER BY short_channel_id ASC, seen ASC) AS first_seens WHERE first_seens.seen >= $1", &[&last_sync_timestamp_object]).await.unwrap(); - for current_row in unannounced_rows { + { + // THIS STEP IS USED TO DETERMINE IF A REMINDER UPDATE SHOULD BE SENT - let blob: Vec = current_row.get("blob_signed"); - let mut readable = Cursor::new(blob); - let unsigned_update = ChannelUpdate::read(&mut readable).unwrap().contents; - let scid = unsigned_update.short_channel_id; - let current_seen_timestamp_object: SystemTime = current_row.get("seen"); - let current_seen_timestamp: u32 = current_seen_timestamp_object.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32; + println!("Annotating channel announcements whose latest channel update in a given direction occurred more than six days ago"); + // Steps: + // — Obtain all updates, distinct by (scid, direction), ordered by seen DESC + // — From those updates, select distinct by (scid), ordered by seen ASC (to obtain the older one per direction) + let current_timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32; + let reminder_threshold_timestamp = current_timestamp.saturating_sub(config::CHANNEL_REMINDER_AGE); + let read_only_graph = network_graph.read_only(); - let mut current_channel_delta = delta_set.entry(scid).or_insert(ChannelDelta::default()); - (*current_channel_delta).first_update_seen = Some(current_seen_timestamp); + let older_latest_directional_updates = client.query(" + SELECT DISTINCT ON (short_channel_id) * + FROM ( + SELECT DISTINCT ON (short_channel_id, direction) short_channel_id, seen + FROM channel_updates + WHERE short_channel_id = any($1) + ORDER BY short_channel_id ASC, direction ASC, seen DESC + ) AS directional_last_seens + ORDER BY short_channel_id ASC, seen ASC + ", &[&channel_ids]).await.unwrap(); + + for current_row in older_latest_directional_updates { + let scid: i64 = current_row.get("short_channel_id"); + let current_seen_timestamp_object: SystemTime = current_row.get("seen"); + let current_seen_timestamp: u32 = current_seen_timestamp_object.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32; + + if current_seen_timestamp <= reminder_threshold_timestamp { + // annotate this channel as requiring that reminders be sent to the client + let current_channel_delta = delta_set.entry(scid as u64).or_insert(ChannelDelta::default()); + + // way might be able to get away with not using this + (*current_channel_delta).requires_reminder = true; + + if let Some(current_channel_info) = read_only_graph.channel(scid as u64) { + if current_channel_info.one_to_two.is_none() || current_channel_info.two_to_one.is_none() { + // we don't send reminders if we don't have bidirectional update data + continue; + } + + if let Some(info) = current_channel_info.one_to_two.as_ref() { + let flags: u8 = if info.enabled { 0 } else { 2 }; + let current_update = (*current_channel_delta).updates.0.get_or_insert(DirectedUpdateDelta::default()); + current_update.serialization_update_flags = Some(flags); + } + + if let Some(info) = current_channel_info.two_to_one.as_ref() { + let flags: u8 = if info.enabled { 1 } else { 3 }; + let current_update = (*current_channel_delta).updates.1.get_or_insert(DirectedUpdateDelta::default()); + current_update.serialization_update_flags = Some(flags); + } + } else { + // we don't send reminders if we don't have the channel + continue; + } + } + } } } @@ -142,7 +227,7 @@ pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Cli let scid = unsigned_channel_update.short_channel_id; let current_channel_delta = delta_set.entry(scid).or_insert(ChannelDelta::default()); - let mut update_delta = if !direction { + let update_delta = if !direction { (*current_channel_delta).updates.0.get_or_insert(DirectedUpdateDelta::default()) } else { (*current_channel_delta).updates.1.get_or_insert(DirectedUpdateDelta::default()) @@ -216,7 +301,7 @@ pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Cli } // determine mutations - if let Some(last_seen_update) = update_delta.last_update_before_seen.as_ref(){ + if let Some(last_seen_update) = update_delta.last_update_before_seen.as_ref() { if unsigned_channel_update.flags != last_seen_update.flags { update_delta.mutated_properties.flags = true; } @@ -236,7 +321,6 @@ pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Cli update_delta.mutated_properties.htlc_maximum_msat = true; } } - } println!("Processed intermediate rows ({}) (delta size: {}): {:?}", intermediate_update_count, delta_set.len(), start.elapsed()); } @@ -259,13 +343,14 @@ pub(super) fn filter_delta_set(delta_set: &mut DeltaSet) { let update_reference = update.as_ref().unwrap(); // update_reference.latest_update_after_seen.is_some() && !update_reference.intermediate_updates.is_empty() // if there has been an update after the channel was first seen - update_reference.latest_update_after_seen.is_some() + + v.requires_reminder || update_reference.latest_update_after_seen.is_some() }; let direction_a_meets_criteria = update_meets_criteria(&v.updates.0); let direction_b_meets_criteria = update_meets_criteria(&v.updates.1); - if !direction_a_meets_criteria && !direction_b_meets_criteria { + if !v.requires_reminder && !direction_a_meets_criteria && !direction_b_meets_criteria { delta_set.remove(&k); } }