Version-aware serialization of node features and addresses in delta.
[rapid-gossip-sync-server] / src / lib.rs
1 #![deny(unsafe_code)]
2 #![deny(broken_intra_doc_links)]
3 #![deny(private_intra_doc_links)]
4 #![deny(non_upper_case_globals)]
5 #![deny(non_camel_case_types)]
6 #![deny(non_snake_case)]
7 #![deny(unused_variables)]
8 #![deny(unused_imports)]
9
10 extern crate core;
11
12 use std::collections::{HashMap, HashSet};
13 use std::fs::File;
14 use std::io::BufReader;
15 use std::ops::Deref;
16 use std::sync::Arc;
17 use bitcoin::blockdata::constants::ChainHash;
18 use lightning::log_info;
19
20 use lightning::routing::gossip::{NetworkGraph, NodeId};
21 use lightning::util::logger::Logger;
22 use lightning::util::ser::{ReadableArgs, Writeable};
23 use tokio::sync::mpsc;
24 use tokio_postgres::{Client, NoTls};
25 use crate::config::SYMLINK_GRANULARITY_INTERVAL;
26 use crate::lookup::DeltaSet;
27
28 use crate::persistence::GossipPersister;
29 use crate::serialization::{SerializationSet, UpdateSerialization};
30 use crate::snapshot::Snapshotter;
31 use crate::types::RGSSLogger;
32
33 mod downloader;
34 mod tracking;
35 mod lookup;
36 mod persistence;
37 mod serialization;
38 mod snapshot;
39 mod config;
40 mod hex_utils;
41 mod verifier;
42
43 pub mod types;
44
45 #[cfg(test)]
46 mod tests;
47
48 /// The purpose of this prefix is to identify the serialization format, should other rapid gossip
49 /// sync formats arise in the future.
50 ///
51 /// The fourth byte is the protocol version in case our format gets updated.
52 const GOSSIP_PREFIX: [u8; 3] = [76, 68, 75];
53
54 pub struct RapidSyncProcessor<L: Deref> where L::Target: Logger {
55         network_graph: Arc<NetworkGraph<L>>,
56         logger: L
57 }
58
59 pub struct SerializedResponse {
60         pub data: Vec<u8>,
61         pub message_count: u32,
62         pub node_announcement_count: u32,
63         /// Despite the name, the count of node announcements that have associated updates, be those
64         /// features, addresses, or both
65         pub node_update_count: u32,
66         pub node_feature_update_count: u32,
67         pub node_address_update_count: u32,
68         pub channel_announcement_count: u32,
69         pub update_count: u32,
70         pub update_count_full: u32,
71         pub update_count_incremental: u32,
72 }
73
74 impl<L: Deref + Clone + Send + Sync + 'static> RapidSyncProcessor<L> where L::Target: Logger {
75         pub fn new(logger: L) -> Self {
76                 let network = config::network();
77                 let network_graph = if let Ok(file) = File::open(&config::network_graph_cache_path()) {
78                         log_info!(logger, "Initializing from cached network graph…");
79                         let mut buffered_reader = BufReader::new(file);
80                         let network_graph_result = NetworkGraph::read(&mut buffered_reader, logger.clone());
81                         if let Ok(network_graph) = network_graph_result {
82                                 log_info!(logger, "Initialized from cached network graph!");
83                                 network_graph
84                         } else {
85                                 log_info!(logger, "Initialization from cached network graph failed: {}", network_graph_result.err().unwrap());
86                                 NetworkGraph::new(network, logger.clone())
87                         }
88                 } else {
89                         NetworkGraph::new(network, logger.clone())
90                 };
91                 let arc_network_graph = Arc::new(network_graph);
92                 Self {
93                         network_graph: arc_network_graph,
94                         logger
95                 }
96         }
97
98         pub async fn start_sync(&self) {
99                 log_info!(self.logger, "Starting Rapid Gossip Sync Server");
100                 log_info!(self.logger, "Snapshot interval: {} seconds", config::snapshot_generation_interval());
101
102                 // means to indicate sync completion status within this module
103                 let (sync_completion_sender, mut sync_completion_receiver) = mpsc::channel::<()>(1);
104
105                 if config::DOWNLOAD_NEW_GOSSIP {
106                         let (mut persister, persistence_sender) = GossipPersister::new(self.network_graph.clone(), self.logger.clone());
107
108                         log_info!(self.logger, "Starting gossip download");
109                         tokio::spawn(tracking::download_gossip(persistence_sender, sync_completion_sender,
110                                 Arc::clone(&self.network_graph), self.logger.clone()));
111                         log_info!(self.logger, "Starting gossip db persistence listener");
112                         tokio::spawn(async move { persister.persist_gossip().await; });
113                 } else {
114                         sync_completion_sender.send(()).await.unwrap();
115                 }
116
117                 let sync_completion = sync_completion_receiver.recv().await;
118                 if sync_completion.is_none() {
119                         panic!("Sync failed!");
120                 }
121                 log_info!(self.logger, "Initial sync complete!");
122
123                 // start the gossip snapshotting service
124                 Snapshotter::new(Arc::clone(&self.network_graph), self.logger.clone()).snapshot_gossip().await;
125         }
126 }
127
128 pub(crate) async fn connect_to_db() -> Client {
129         let connection_config = config::db_connection_config();
130         let (client, connection) = connection_config.connect(NoTls).await.unwrap();
131
132         tokio::spawn(async move {
133                 if let Err(e) = connection.await {
134                         panic!("connection error: {}", e);
135                 }
136         });
137
138         #[cfg(test)]
139         {
140                 let schema_name = tests::db_test_schema();
141                 let schema_creation_command = format!("CREATE SCHEMA IF NOT EXISTS {}", schema_name);
142                 client.execute(&schema_creation_command, &[]).await.unwrap();
143                 client.execute(&format!("SET search_path TO {}", schema_name), &[]).await.unwrap();
144         }
145
146         client.execute("set time zone UTC", &[]).await.unwrap();
147         client
148 }
149
150 /// This method generates a no-op blob that can be used as a delta where none exists.
151 ///
152 /// The primary purpose of this method is the scenario of a client retrieving and processing a
153 /// given snapshot, and then immediately retrieving the would-be next snapshot at the timestamp
154 /// indicated by the one that was just processed.
155 /// Previously, there would not be a new snapshot to be processed for that particular timestamp yet,
156 /// and the server would return a 404 error.
157 ///
158 /// In principle, this method could also be used to address another unfortunately all too common
159 /// pitfall: requesting snapshots from intermediate timestamps, i. e. those that are not multiples
160 /// of our granularity constant. Note that for that purpose, this method could be very dangerous,
161 /// because if consumed, the `timestamp` value calculated here will overwrite the timestamp that
162 /// the client previously had, which could result in duplicated or omitted gossip down the line.
163 fn serialize_empty_blob(current_timestamp: u64) -> Vec<u8> {
164         let mut blob = GOSSIP_PREFIX.to_vec();
165
166         let network = config::network();
167         let chain_hash = ChainHash::using_genesis_block(network);
168         chain_hash.write(&mut blob).unwrap();
169
170         let blob_timestamp = Snapshotter::<Arc<RGSSLogger>>::round_down_to_nearest_multiple(current_timestamp, SYMLINK_GRANULARITY_INTERVAL as u64) as u32;
171         blob_timestamp.write(&mut blob).unwrap();
172
173         0u32.write(&mut blob).unwrap(); // node count
174         0u32.write(&mut blob).unwrap(); // announcement count
175         0u32.write(&mut blob).unwrap(); // update count
176
177         blob
178 }
179
180 async fn calculate_delta<L: Deref + Clone>(network_graph: Arc<NetworkGraph<L>>, last_sync_timestamp: u32, snapshot_reference_timestamp: Option<u64>, logger: L) -> SerializationSet where L::Target: Logger {
181         let client = connect_to_db().await;
182
183         network_graph.remove_stale_channels_and_tracking();
184
185         // set a flag if the chain hash is prepended
186         // chain hash only necessary if either channel announcements or non-incremental updates are present
187         // for announcement-free incremental-only updates, chain hash can be skipped
188
189         let mut delta_set = DeltaSet::new();
190         lookup::fetch_channel_announcements(&mut delta_set, network_graph, &client, last_sync_timestamp, snapshot_reference_timestamp, logger.clone()).await;
191         log_info!(logger, "announcement channel count: {}", delta_set.len());
192         lookup::fetch_channel_updates(&mut delta_set, &client, last_sync_timestamp, logger.clone()).await;
193         log_info!(logger, "update-fetched channel count: {}", delta_set.len());
194         let node_delta_set = lookup::fetch_node_updates(&client, last_sync_timestamp, logger.clone()).await;
195         log_info!(logger, "update-fetched node count: {}", node_delta_set.len());
196         lookup::filter_delta_set(&mut delta_set, logger.clone());
197         log_info!(logger, "update-filtered channel count: {}", delta_set.len());
198         serialization::serialize_delta_set(delta_set, node_delta_set, last_sync_timestamp)
199 }
200
201 fn serialize_delta<L: Deref + Clone>(serialization_details: &SerializationSet, serialization_version: u8, logger: L) -> SerializedResponse where L::Target: Logger {
202         let mut output: Vec<u8> = vec![];
203         let snapshot_interval = config::snapshot_generation_interval();
204
205         let mut node_id_set: HashSet<NodeId> = HashSet::new();
206         let mut node_id_indices: HashMap<NodeId, usize> = HashMap::new();
207         let mut node_ids: Vec<NodeId> = Vec::new();
208         let mut duplicate_node_ids: i32 = 0;
209
210         let mut get_node_id_index = |node_id: NodeId| {
211                 if node_id_set.insert(node_id) {
212                         node_ids.push(node_id);
213                         let index = node_ids.len() - 1;
214                         node_id_indices.insert(node_id, index);
215                         return index;
216                 }
217                 duplicate_node_ids += 1;
218                 node_id_indices[&node_id]
219         };
220
221         // process announcements
222         // write the number of channel announcements to the output
223         let announcement_count = serialization_details.announcements.len() as u32;
224         announcement_count.write(&mut output).unwrap();
225         let mut previous_announcement_scid = 0;
226         for current_announcement in &serialization_details.announcements {
227                 let id_index_1 = get_node_id_index(current_announcement.node_id_1);
228                 let id_index_2 = get_node_id_index(current_announcement.node_id_2);
229                 let mut stripped_announcement = serialization::serialize_stripped_channel_announcement(&current_announcement, id_index_1, id_index_2, previous_announcement_scid);
230                 output.append(&mut stripped_announcement);
231
232                 previous_announcement_scid = current_announcement.short_channel_id;
233         }
234
235         // process updates
236         let mut previous_update_scid = 0;
237         let update_count = serialization_details.updates.len() as u32;
238         update_count.write(&mut output).unwrap();
239
240         let default_update_values = &serialization_details.full_update_defaults;
241         if update_count > 0 {
242                 default_update_values.cltv_expiry_delta.write(&mut output).unwrap();
243                 default_update_values.htlc_minimum_msat.write(&mut output).unwrap();
244                 default_update_values.fee_base_msat.write(&mut output).unwrap();
245                 default_update_values.fee_proportional_millionths.write(&mut output).unwrap();
246                 default_update_values.htlc_maximum_msat.write(&mut output).unwrap();
247         }
248
249         let mut update_count_full = 0;
250         let mut update_count_incremental = 0;
251         for current_update in &serialization_details.updates {
252                 match &current_update {
253                         UpdateSerialization::Full(_) => {
254                                 update_count_full += 1;
255                         }
256                         UpdateSerialization::Incremental(_, _) | UpdateSerialization::Reminder(_, _) => {
257                                 update_count_incremental += 1;
258                         }
259                 };
260
261                 let mut stripped_update = serialization::serialize_stripped_channel_update(&current_update, &default_update_values, previous_update_scid);
262                 output.append(&mut stripped_update);
263
264                 previous_update_scid = current_update.scid();
265         }
266
267         // some stats
268         let message_count = announcement_count + update_count;
269
270         let mut prefixed_output = GOSSIP_PREFIX.to_vec();
271         prefixed_output.push(serialization_version);
272
273         // always write the chain hash
274         serialization_details.chain_hash.write(&mut prefixed_output).unwrap();
275         // always write the latest seen timestamp
276         let latest_seen_timestamp = serialization_details.latest_seen;
277         let overflow_seconds = latest_seen_timestamp % snapshot_interval;
278         let serialized_seen_timestamp = latest_seen_timestamp.saturating_sub(overflow_seconds);
279         serialized_seen_timestamp.write(&mut prefixed_output).unwrap();
280
281         if serialization_version >= 2 { // serialize the most common node features
282                 for mutated_node_id in serialization_details.node_mutations.keys() {
283                         // consider mutated nodes outside channel announcements
284                         get_node_id_index(mutated_node_id.clone());
285                 }
286
287                 let default_feature_count = serialization_details.node_announcement_feature_defaults.len() as u8;
288                 debug_assert!(default_feature_count <= config::NODE_DEFAULT_FEATURE_COUNT, "Default feature count cannot exceed maximum");
289                 default_feature_count.write(&mut prefixed_output).unwrap();
290
291                 for current_feature in &serialization_details.node_announcement_feature_defaults {
292                         current_feature.write(&mut prefixed_output).unwrap();
293                 }
294         }
295
296         let node_id_count = node_ids.len() as u32;
297         node_id_count.write(&mut prefixed_output).unwrap();
298
299         let mut node_update_count = 0u32;
300         let mut node_feature_update_count = 0u32;
301         let mut node_address_update_count = 0u32;
302
303         for current_node_id in node_ids {
304                 let mut current_node_delta_serialization: Vec<u8> = Vec::new();
305                 current_node_id.write(&mut current_node_delta_serialization).unwrap();
306
307                 if serialization_version >= 2 {
308                         if let Some(node_delta) = serialization_details.node_mutations.get(&current_node_id) {
309                                 /*
310                                 Bitmap:
311                                 7: expect extra data after the pubkey (a u16 for the count, and then that number of bytes)
312                                 5-3: index of new features among default (1-6). If index is 7 (all 3 bits are set, it's
313                                 outside the present default range). 0 means no feature changes.
314                                 2: addresses have changed
315
316                                 1: used for all keys
317                                 0: used for odd keys
318                                 */
319
320                                 if node_delta.has_address_set_changed {
321                                         node_address_update_count += 1;
322
323                                         let address_set = &node_delta.latest_details_after_seen.as_ref().unwrap().addresses;
324                                         let mut address_serialization = Vec::new();
325
326                                         // we don't know a priori how many are <= 255 bytes
327                                         let mut total_address_count = 0u8;
328
329                                         for address in address_set.iter() {
330                                                 if total_address_count == u8::MAX {
331                                                         // don't serialize more than 255 addresses
332                                                         break;
333                                                 }
334                                                 if let Ok(serialized_length) = u8::try_from(address.serialized_length()) {
335                                                         total_address_count += 1;
336                                                         serialized_length.write(&mut address_serialization).unwrap();
337                                                         address.write(&mut address_serialization).unwrap();
338                                                 };
339                                         }
340
341                                         if total_address_count > 0 {
342                                                 // signal the presence of node addresses
343                                                 current_node_delta_serialization[0] |= 1 << 2;
344                                                 // serialize the actual addresses and count
345                                                 total_address_count.write(&mut current_node_delta_serialization).unwrap();
346                                                 current_node_delta_serialization.append(&mut address_serialization);
347                                         }
348                                 }
349
350                                 if node_delta.has_feature_set_changed {
351                                         node_feature_update_count += 1;
352
353                                         let latest_features = &node_delta.latest_details_after_seen.as_ref().unwrap().features;
354
355                                         // are these features among the most common ones?
356                                         if let Some(index) = serialization_details.node_announcement_feature_defaults.iter().position(|f| f == latest_features) {
357                                                 // this feature set is among the 6 defaults
358                                                 current_node_delta_serialization[0] |= ((index + 1) as u8) << 3;
359                                         } else {
360                                                 current_node_delta_serialization[0] |= 0b_0011_1000; // 7 << 3
361                                                 latest_features.write(&mut current_node_delta_serialization).unwrap();
362                                         }
363                                 }
364
365                                 if node_delta.has_address_set_changed || node_delta.has_feature_set_changed {
366                                         node_update_count += 1;
367                                 }
368                         }
369                 }
370
371                 prefixed_output.append(&mut current_node_delta_serialization);
372         }
373
374         prefixed_output.append(&mut output);
375
376         log_info!(logger, "duplicated node ids: {}", duplicate_node_ids);
377         log_info!(logger, "latest seen timestamp: {:?}", serialization_details.latest_seen);
378
379         SerializedResponse {
380                 data: prefixed_output,
381                 message_count,
382                 node_announcement_count: node_id_count,
383                 node_update_count,
384                 node_feature_update_count,
385                 node_address_update_count,
386                 channel_announcement_count: announcement_count,
387                 update_count,
388                 update_count_full,
389                 update_count_incremental,
390         }
391 }